SlideShare a Scribd company logo
For any help regarding Computer Science Assignment Help
Visit: https://www.programminghomeworkhelp.com/ ,
Email : support@programminghomeworkhelp.com or
call us at - +1 678 648 4277 programminghomeworkhelp.com
Introduction To Algorithms Fall
Problem
(a) T F Computing √ b ac for an n-bit positive integer a can be done in O(lg n) iterations of
Newton’s method.
(b) T F Suppose we want to solve a polynomial equation f(x) = 0. While our choice of initial
approximation x0 will affect how quickly Newton’s method converges, it will always
converge eventually.
(c) T F Karatsuba’s integer multiplication algorithm always runs faster than the grade-school
integer multiplication algorithm.
(d) T F If we convert an n-digit base-256 number into base 2, the resulting number of digits
is .
(e) T F In a weighted undirected graph G = (V, E, w), breadth-first search from a vertex s
finds single-source shortest paths from s (via parent pointers) in O(V + E) time.
(f) T F In a weighted undirected tree G = (V, E, w), breadth-first search from a vertex s
finds single-source shortest paths from s (via parent pointers) in O(V + E) time.
(g) T F In a weighted undirected tree G = (V, E, w), depth-first search from a vertex s finds
single-source shortest paths from s (via parent pointers) in O(V + E) time.
(h) T F If a graph represents tasks and their interdependencies (i.e., an edge (u, v)
indicates that u must happen before v happens), then the breadth-first search
order of vertices is a valid order in which to tackle the tasks.
programminghomeworkhelp.com
(i) T F Dijkstra’s shortest-path algorithm may relax an edge more than once in a graph with a
cycle.
(j) T F Given a weighted directed graph G = (V, E, w) and a source s ∈ V , if G has a negative-
weight cycle somewhere, then the Bellman-Ford algorithm will necessarily compute
an incorrect result for some δ(s, v).
(k) T F In a weighted directed graph G = (V, E, w) containing no zero- or positive-weight
cycles, Bellman-Ford can find a longest (maximum-weight) path from vertex s to
vertex t.
(l) T F In a weighted directed graph G = (V, E, w) containing a negativeweight cycle,
running the Bellman-Ford algorithm from s will find a shortest acyclic path from s to
a given destination vertex t.
(m) T F The bidirectional Dijkstra algorithm runs asymptotically faster than the Dijkstra
algorithm.
(n) T F Given a weighted directed graph G = (V, E, w) and a shortest path p from s to t, if
we doubled the weight of every edge to produce G0 = (V, E, w0 ), then p is also a
shortest path in G0 .
programminghomeworkhelp.com
Problem 3. MazeCraft
You are playing SnowStorm’s new video game, MazeCraft. Realizing that you can
convert a maze into a graph with vertices representing cells and edges representing
passages, you want to use your newly learned graph-search algorithms to navigate
themaze. Consider thefollowing converted graph.
For the following questions, assume that the graph is represented using adjacency
lists, and that all adjacency lists are sorted, i.e., the vertices in an adjacency list
are always sorted alphabetically.
(a) Suppose thatyou wantto find a path from A to H. If you use breadth-first
search, write down the resulting path as a sequence of vertices.
(b)If you use depth-first search to find a path from A to H, write down
the resulting path as a sequence of vertices.
programminghomeworkhelp.com
(c) To determine whether the maze has cycles or multiple paths to the same destination, you
decide to use the edge classification of depth-first search. Run depth- first search on the
graph reproduced below, starting from vertex A, and label every edge with T if it’s a tree
edge, B if it’s a back edge, F if it’s a forward edge, and C if it’s a cross edge.
As a reminder, recall that an edge (u, v) is
• a tree edge (T) if v was first discovered by exploring edge (u, v) (tree edges form the
depth-first forest);
• a back edge (B) if v is u’s ancestor in a depth-first tree;
• a forward edge (F) if v is u’s descendant in a depth-first tree; and
• a cross edge (C) if none of the above apply.
programminghomeworkhelp.com
A
B
C
(d) [Now suppose that the passages in the maze are directional. Rerun depth- first search
in the directed graph below and label the edges accordingly.
D
F
E G H
(e) [ Suppose each passage in the maze causes a different amount of damage to you in
game. You change the graph to use weights to represent the damage caused by each
edge. You then use Dijkstra’s algorithm to find the path from A to H with the lowest
possible damage. Write down the order in which vertices get removed from the
priority queue when running Dijkstra’s algorithm.
programminghomeworkhelp.com
Problem 4. Malfunctioning Calculator
Former 6.006 student Mallory Funke is at a math competition, but her calculator isn’t working.
It seems to work fine for whole numbers, but the numbers after the decimal point always
seem to be the sequence repeated over and over again, making those digits useless. For
one of the problems, she has been asked to compute [A3/4♩ for a few different integer values of
A. Mal knows that Newton’s Method can be used to compute the square root or the cube root
of an integer A. So as a first step in computing [A3/4♩, Mal wants to use Newton’s Method to
compute [A1/4♩. She then plans to use that information to compute [A3/4♩.
(a) Mal decides to use the function f (x) = x4 − A, because it has a root at x = A1/4.
Use Newton’s Method on f (x) to generate a formula for computing increasingly
accurate estimates of [A1/4♩. In other words, give a formula for the more accurate
estimate xi+1 in terms of a less accurate estimate xi. The formula you construct must
use only addition, subtraction, multiplication, and division. (You do not need to
simplify the formula.)
(b) Mal decides to use the technique from part (a) to compute the value B = [A1/4♩.
She then plans to compute [A3/4♩ by calculating the value C = B3 = B·B·B. Provide
an explanation of why this technique does not work.
Hint: Define α to be the fractional part of A1/4, so that B = A1/4 − α. What happens
when you compute C = B3?
programminghomeworkhelp.com
(c) Mal clearly needs a way to check her answer for [A3/4♩, using only integers. Given a pair of
positive integers A and C, explain how to check whether C ≤ A3/4 using O(1) additions,
subtractions, multiplications, and comparisons.
(d) Explain how to check whether C = [A3/4♩, again using only O(1) addi- tions, subtractions,
multiplications, and comparisons.
Hint: Recall how the floor function is defined.
(e) Give a brief description of an algorithm that takes as input a d-digit positive integer A and
computes [A3/4♩. The only arithmetic operations you can use are inte- ger addition, integer
subtraction, integer multiplication, integer division, and integer comparison. Your
algorithm should use Θ(lg d) arithmetic operations in total, but par- tial credit will be
awarded for using Θ(d) arithmetic operations. For this question, you may assume that
Newton’s Method has a quadratic rateof convergencefor whatever function you devise.
programminghomeworkhelp.com
Problem 5. The Tourist
Your new startup, Bird Tours, brings people around Boston in a new aviation car that can
both drive and fly. You’ve constructed a weighted directed graph G = (V, E, w)
representing the best time to drive or fly between various city sites (represented by
vertices in V ). You’ve also written a history of Boston, which would be best described by
visiting sites v0, v1, . . . , vk in that order.
Your goal is to find the shortest path in G that visits v0, v1, . . . , vk in order, possibly
visiting other vertices in between. (The path must have v0, v1, . . . , vk as a subsequence;
the path is allowed to visit a vertex more than once. For example, v0, v2, v1, v2, . . . , vk is
legal.) To do the computation, you’ve found an online service, Paths ’ Us, that will
compute the shortest path from a given source s to a given target t in a given weighted
graph, for the bargain price of $1. You see how to solve the problem by paying $k, calling
Paths ’ Us with (v0, v1, G), (v1, v2, G), . . . , (vk—1, vk, G) and piecing together the paths.
Describe how to solve the problem with only $1 by calling Paths ’ Us with (s, t, Gr) for
a newly constructed graph Gr = (V r, Er, wr), and converting the resulting path into a
path in G.
programminghomeworkhelp.com
Problem 6. Fill ’Er Up
You are traveling by car from one city to another city. Unfortunately, you have a hole in your
gas tank, and you have to refill your gas tank to travel across more than two roads. In addition,
there is a toll booth on every road that charges you for using that road. Your goal is to find the
least-expensive path from your start to your destination.
You represent the city network using a directed graph G = (V, E, w) with weights wdefined on
both edges and vertices. The vertices V represent the cities and the edges E represent the roads.
The weight w(e) of an edge e represents the toll amount on that road. The weight w(v) of a
vertex v is the price of filling your gas tank in that city (which is a fixed price independent of
how much gas you have left, or ∞ if there is no gas available to purchase). You are allowed (but
not obligated) to end your journey with an empty tank, and you may assume that you always
start your journey with a full tank.
Below is an example graph that we will use to answer part (a). One seemingly cheap path from
s to t is (s, u1, u2, t) at a cost of $8. Unfortunately, this path is not valid, because our leaky gas
tank won’t permit moving across three edges without refilling our gas tank.
One valid path is (s, u3, u2, t) at a cost of $22. (This is a valid path: we begin with a full tank,
travel across one edge to a gas station where we refill our tank, and then travel two edges to the
destination, arriving with an empty gas tank. Notice that we are unable to continue the journey
to u5 if we wanted to, because even though there is a gas station there, we have to traverse a
third edge to get to it.)
programminghomeworkhelp.com
Gas:
$5
Gas:
$3
NO
GAS
NO
GAS
T
oll: $5
Toll: $7
T
oll: $2 T
oll: $1
T
oll: $2
T
oll: $10
T
oll: $3
s Gas:
Free
t
NO
GAS
T
oll: $1
u1 u2
T
oll: $4
Gas:
$1
u5
T
oll: $9
T
oll: $1
There are some extra copies of this graph at the end of the exam (for your
convenience).
(a) The valid path given in the description above is not the best path. Find a least-expensive path
from s to t in the graph above.
(b) Find the least-expensive path from s to u5 in the graph above.
(c) Give an O(V log V +E) algorithm to find, in a given graph G = (V, E, w), a least-expensive valid
path from a city s to a city t, or report that no such path exists.
programminghomeworkhelp.com
Gas:
Free
Gas:
$5
Gas:
$3
NO
GAS
NO
GAS
T
oll: $5
Toll: $7
T
oll: $2 T
oll: $1
T
oll: $2
T
oll: $10
T
oll: $3
s
t
NO
GAS
T
oll: $1
u1 u2
Here are some extra copies of the graph from Problem 6 (for your convenience).
u3 u4
T
oll: $4
Gas:
$1
u5
T
oll: $9
T
oll: $1
Gas:
Free
Gas:
$5
Gas:
$3
NO
GAS
NO
GAS
T
oll: $5
Toll: $7
T
oll: $2 T
oll: $1
T
oll: $2
T
oll: $10
T
oll: $3
s
t
NO
GAS
T
oll: $1
u1 u2
u3 u4
T
oll: $4
Gas:
$1
u5
T
oll: $9
T
oll: $1
programminghomeworkhelp.com
(a) T F
Solutions
Circle (T)rue or (F)alse. You don’t need to justify your choice.
Computing √ b ac for an n-bit positive integer a can be done in O(lg n) iterations of
Newton’s method.
Solution: True. This is the bound obtained by Newton’s Method’s quadratic
convergence.
(b) T F Suppose we want to solve a polynomial equation f(x) = 0. While our choice of initial
approximation x0 will affect how quickly Newton’s method converges, it will always
converge eventually.
(c) T F Karatsuba’s integer multiplication algorithm always runs faster than the grade-
school integer multiplication algorithm.
Solution: False. Problem Set 5 has shown that the O(N2 ) algorithm runs faster
for small numbers.
(d) T F
If we convert an n-digit base-256 number into base 2, the resulting number of
digits is Θ(n 2 ).
programminghomeworkhelp.com
(e) T F In a weighted undirected graph G = (V, E, w), breadth-first search from a vertex s finds
single-source shortest paths from s (via parent pointers) in O(V + E) time.
Solution: False. Only in unweighted graphs.
(f) T F In a weighted undirected tree G = (V, E, w), breadth-first search from a vertex s finds
single-source shortest paths from s (via parent pointers) in O(V + E) time.
Solution: True. In a tree, there is only one path between two vertices, and breadth-first
search finds it.
(g) T F In a weighted undirected tree G = (V, E, w), depth-first search from a vertex s finds
single-source shortest paths from s (via parent pointers) in O(V + E) time.
Solution: True. In a tree, there is only one path between two vertices, and depth-first
search finds it.
(h) T F If a graph represents tasks and their interdependencies (i.e., an edge (u, v) indicates
that u must happen before v happens), then the breadth-first search order of
vertices is a valid order in which to tackle the tasks.
Solution: No, you’d prefer depth-first search, which can easily be used to produce a
topological sort of the graph, which would correspond to a valid task order. BFS can
produce incorrect results.
(i) T F Dijkstra’s shortest-path algorithm may relax an edge more than once in a graph with
a cycle.
Solution: False. Dijkstra’s algorithm always visits each node at most once; this is why
it produces an incorrect result in the presence of negative-weight edges.
programminghomeworkhelp.com
(j) T F Given a weighted directed graph G = (V, E, w) and a source s ∈ V , if G has a
negative-weight cycle somewhere, then the Bellman-Ford algorithm will necessarily
compute an incorrect result for some δ(s, v).
Solution: False. The negative-weight cycle has to be reachable from s.
(k) T F In a weighted directed graph G = (V, E, w) containing no zero- or positive-weight
cycles, Bellman-Ford can find a longest (maximum-weight) path from vertex s to vertex t.
Solution: True. Negate the weights.
(l) T F In a weighted directed graph G = (V, E, w) containing a negative- weight cycle,
running the Bellman-Ford algorithm from s will find a shortest acyclic path from s to a
given destination vertex t.
Solution: False. Bellman-Ford will terminate, and can detect the presence of that
negative-weight cycle, but it can’t “route around it.” (You could always remove an edge
to break the cycle and try again, though.)
(m)T F The bidirectional Dijkstra algorithm runs asymptotically faster than the Dijkstra
algorithm.
Solution: False. The constant factor behind bidirectional Dijkstra is better, but the worst-case
running time is the same.
programminghomeworkhelp.com
[ Given a weighted directed graph G = (V, E, w) and a shortest path p from s to
t, if we doubled the weight of every edge to produce Gr = (V, E, wr), then p is
also a shortest path in Gr.
(n) T F
Solution: True. Multiplying edge weights by any positive constant factor pre-
serves their relative order, as well as the relative order of any linear combination
of the weights. All path weights are linear combinations of edge weights, so the
relative order of path weights is preserved. This means that a shortest path in G
will still be a shortest path in Gr.
programminghomeworkhelp.com
Problem 3. MazeCraft [
You are playing SnowStorm’s new video game, MazeCraft. Realizing that you can convert a maze
into a graph with vertices representing cells and edges representing passages, you want to use your
newly learned graph-search algorithms to navigate the maze. Consider the following converted
graph.
For the following questions, assume that the graph is represented using adjacency lists, and that all
adjacency lists are sorted, i.e., the vertices in an adjacency list are always sorted alphabetically.
(a) [4 points] Suppose thatyou wanttofind a pathfrom A toH. If you use breadth-first search,
write down the resulting path as a sequence of vertices.
Solution: A, B, D, F, H
P
·L
♩,
programminghomeworkhelp.com
(b) If you use depth-first search to find a path from A to H, write down the resulting path as a
sequence of vertices.
Solution: A, B, C, D, E, G, F, H if you use recursion, or A, C, D, F, H if you use stack-based
implemention. Essentially, the recursion-based implementation will visit the neighbors in
alphabetical order, while the stack-based implementation will visit the neighbors in reverse
alphabetical order.
(c) To determine whether the maze has cycles or multiple paths to the same destination, you
decide to use the edge classification of depth-first search. Run depth- first search on the graph
reproduced below, starting from vertex A, and label every edge with T if it’s a tree edge, B if
it’s a back edge, F if it’s a forward edge, and C if it’s a cross edge.
As a reminder, recall that an edge (u, v) is
• a tree edge (T) if v was first discovered by exploring edge (u, v) (tree edges form the
depth-first forest);
• a back edge (B) if v is u’s ancestor in a depth-first tree;
• a forward edge (F) if v is u’s descendant in a depth-first tree; and
• a cross edge (C) if none of the above apply.
Solution: Recursion-based implementation:
programminghomeworkhelp.com
A
B
C
F
E G H
T
B
T
T
T
B
T
T
T
B
B
Stack-based implementation:
D
T T
B
B F
T
B B T
B
T
A C E G H
T T
1
w
2
Scoring: For Problem 3(c) and 3(d), we computed the score as [P − N ♩, where
P is the total points of the problem and Nw is the number of wrong labels.
programminghomeworkhelp.com
A
B
C
(d) Now suppose that the passages in the maze are directional. Rerun depth-
first search in the directed graph below and label the edges accordingly.
Solution: Recursion-based implementation:
D
F
E G H
T
B
T
T
T
F
C
T
T
C
T
Stack-based implementation:
D
T T
C T
B F
T T T
F
C
A C E G H
B T
(e) [ Suppose each passage in the maze causes a different amount of damage to you in game. You
change the graph to use weights to represent the damage caused by each edge. You then use
Dijkstra’s algorithm to find the path from A to H with the lowest possible damage. Write
down the order in which vertices get removed from the priority queue when running
Dijkstra’s algorithm.
D
programminghomeworkhelp.com
Problem 4. Malfunctioning Calculator
a.
programminghomeworkhelp.com
(c) Mal clearly needs a way to check her answer for [A3/4♩, using only integers. Given a pair of
positive integers A and C, explain how to check whether C ≤ A3/4 using O(1) additions,
subtractions, multiplications, and comparisons.
Solution: The equation C ≤ A3/4 is equivalent to the equation C4 ≤ A3, which is
equivalent to C · C · C · C ≤ A · A · A.
(d) Explain howtocheck whetherC = [A3/4♩, again using only O(1) addi- tions, subtractions,
multiplications, and comparisons.
Hint: Recall how the floor function is defined.
Solution: If C = [A3/4♩, then by the definition of the floor function, we have C ≤ A3/4 and C
+ 1 > A3/4. We can check bothof these using thetechniquefrom part (c).
(b) Mal decides to use the technique from part (a) to compute the value B = [A1/4♩. She then
plans to compute [A3/4♩ by calculating the value C = B3 = B·B·B. Provide an explanation
of why this technique does not work.
Hint: Define α to be the fractional part of A1/4, so that B = A1/4 − α. What happens when
you compute C = B3?
Solution: When you expand out the formula for C, you get
C = B3
= (A1/4
− α)3
= A3/4
− 3A2/4
α + 3A1/4
α2
− α3
.
If A is large, then γ = 3A2/4α − 3A1/4α2 + α3 will be significantly greater than 1, and so
we’ll have C = A3/4 − γ with γ > 1. Hence, C will not be [A3/4♩.
programminghomeworkhelp.com
(e) Give a brief description of an algorithm that takes as input a d-digit positive integer A and
computes [A3/4♩. The only arithmetic operations you can use are inte- ger addition, integer
subtraction, integer multiplication, integer division, and integer comparison. Your
algorithm should use Θ(lg d) arithmetic operations in total, but par- tial credit will be
awarded for using Θ(d) arithmetic operations. For this question, you may assume that
Newton’s Method has a quadratic rateof convergencefor whatever function you devise.
Solution:
Θ(lg d) Solution: The technique from part (b) didn’t work because we threw away
information (taking the floor) before performing another operation. Instead, we first
calculate B = A3, then compute C = [B1/4♩ using the formula from part (a) to continually
improve our estimate. Because we have a quadratic rate of convergence, we will improve
our estimate Θ(log d) times, and each time we will perform a constant number of
arithmetic operations.
Note: This is equivalent to applying Newton’s Method to the function f (x) = x4 −A3, but
does not require you to rederive the equation for xi+1 in terms of xi.
Θ(d) Solution: Use the comparison method from part (c) to do binary search. This
requires us to do Θ(1) operations for each of the bits in the result, for a total of Θ(d)
operations.
programminghomeworkhelp.com
Problem 5. The Tourist
Your new startup, Bird Tours, brings people around Boston in a new aviation car that can both
drive and fly. You’ve constructed a weighted directed graph G = (V, E, w) representing the best
time to drive or fly between various city sites (represented by vertices in V ). You’ve also
written a history of Boston, which would be best described by visiting sites v0, v1, . . . , vk in that
order.
Your goal is to find the shortest path in G that visits v0, v1, . . . , vk in order, possibly visiting
other vertices in between. (The path must have v0, v1, . . . , vk as a subsequence; the path is
allowed to visit a vertex more than once. For example, v0, v2, v1, v2, . . . , vk is legal.) To do the
computation, you’ve found an online service, Paths ’ Us, that will compute the shortest path
from a given source s to a given target t in a given weighted graph, for the bargain price of $1.
You see how to solve the problem by paying $k, calling Paths ’ Us with (v0, v1, G), (v1, v2, G), .
. . , (vk—1, vk, G) and piecing together the paths. Describe how to solve the problem with only
$1 by calling Paths ’ Us with (s, t, Gr) for a newly constructed graph Gr = (V r, Er, wr), and
convertingtheresulting path into a path in G.
Solution: To form the graph Gr, we start from the disjoint union of k copies of the given graph G,
using superscripts to denote the different copies G1, G2, . . . , Gk . That is, for each vertex v ∈
V , we make k copies v1, v2, . . . , vk in V r; and for each edge (u, v) ∈E, we form the edges
programminghomeworkhelp.com
programminghomeworkhelp.com
Problem 6. Fill ’Er Up!
You are traveling by car from one city to another city. Unfortunately, you have a hole in your
gas tank, and you have to refill your gas tank to travel across more than two roads. In addition,
there is a toll booth on every road that charges you for using that road. Your goal is to find the
least-expensive path from your start to your destination.
You represent the city network using a directed graph G = (V, E, w) with weights wdefined on
both edges and vertices. The vertices V represent the cities and the edges E represent the roads.
The weight w(e) of an edge e represents the toll amount on that road. The weight w(v) of a
vertex v is the price of filling your gas tank in that city (which is a fixed price independent of how
much gas you have left, or ∞ if there is no gas available to purchase). You are allowed (but not
obligated) to end your journey with an empty tank, and you may assume that you always start
your journey with a full tank.
Below is an example graph that we will use to answer part (a). One seemingly cheap path from s
to t is (s, u1, u2, t) at a cost of $8. Unfortunately, this path is not valid, because our leaky gas tank
won’t permit moving across three edges without refilling our gas tank.
One valid path is (s, u3, u2, t) at a cost of $22. (This is a valid path: we begin with a full tank,
travel across one edge to a gas station where we refill our tank, and then travel two edges to the
destination, arriving with an empty gas tank. Notice that we are unable to continue the journey
to u5 if we wanted to, because even though there is a gas station there, we have to traverse a
third edge to get to it.)
programminghomeworkhelp.com
Gas:
$5
Gas:
$3
NO
GAS
NO
GAS
T
oll: $5
Toll: $7
T
oll: $2 T
oll: $1
T
oll: $2
T
oll: $10
T
oll: $3
s Gas:
Free
t
NO
GAS
T
oll: $1
u1 u2
T
oll: $4
Gas:
$1
u5
T
oll: $9
T
oll: $1
There are some extra copies of this graph at the end of the exam (for your
convenience).
programminghomeworkhelp.com
(a) The valid path given in the description above is not the best path. Find a least-expensive
path from s to t in the graph above.
Solution: (s, u1, u3, u2, t)
(b) Find the least-expensive path from s to u5 in the graph above.
Solution: (s, u1, u3, u2, u4, t, u5)
(c) Give an O(V log V +E) algorithm to find, in a given graph G = (V, E, w), a least-
expensive valid path from a city s to a city t, or report that no such path exists.
Solution: We will solve this problem by transforming G = (V, E, w), the graph of cities,
roads, and toll/gas costs, into a new graph Gr = (V r, Er, wr). We will then use Dijkstra’s
algorithm to find the desired path.
First construct the vertex set V r. For each vertex v ∈ V , create three vertices in V r: vf
, vh, and ve. These stand for a full tank, a half-full tank, and an empty tank, respectively.
This operation takes O(V ) time, since we iterate |V | times and create three new vertices
each time.
Next we will discuss the construction of Er and wr. For each edge (u, v) = e ∈ E, create
edges in Er as follows. Create edges (uf , vh) and (uh, ve) to represent the emptying of half
the tank during the move from u to v. The weights on both of these edges will be the
original weight w(u, v). This operation takes O(E) time, since we iterate |E| times and
insert two new edges into Er each time.
programminghomeworkhelp.com
We can now run Dijkstra’s algorithm on this graph to find paths from sf to tf , th, and te,
and we return the shortest of the three. The graph transformation took O(V + E) time
and, as the number of vertices and the number of edges increased by a constant factor,
running Dijkstra on this graph takes O(V log V + E) time. Therefore, the total running
time is O(V log V + E).
Finally, we will represent the cost of fueling up by creating additional edges as follows. For
every vertex v ∈ V , we check whether there is a gas station there. If there is a gas station
at this vertex, then we add the edges (ve, vf ) and (vh, vf ) to Er. Since the cost of fueling up
is not dependent on the amount of gas that is already in the tank, we assign both of these
edges the cost of fueling up at that city. This procedure takes O(V ) time, since we iterate
over all of thevertices andcreate a constant number of new edges.
programminghomeworkhelp.com

More Related Content

PPTX
Algorithm Assignment Help
PPTX
Chemistry Assignment Help
PPTX
Digital Signal Processing Assignment Help
PPTX
Signal Processing Assignment Help
PPTX
Signal Processing Assignment Help
PPTX
Signals Processing Homework Help
PDF
Solution 3.
PPTX
Machnical Engineering Assignment Help
Algorithm Assignment Help
Chemistry Assignment Help
Digital Signal Processing Assignment Help
Signal Processing Assignment Help
Signal Processing Assignment Help
Signals Processing Homework Help
Solution 3.
Machnical Engineering Assignment Help

What's hot (20)

PPTX
Stochastic Assignment Help
PPTX
Mechanical Engineering Assignment Help
PDF
Answers withexplanations
PPTX
DSP System Homework Help
PPTX
Signal Processing Homework Help
PDF
Assignment 2 daa
PPTX
Matlab Assignment Help
PDF
Ee693 questionshomework
PPTX
Logistics Management Assignment Help
PPTX
Algorithm Assignment Help
PPTX
Signals Processing Assignment Help
PPTX
Signal Processing Assignment Help
PPTX
Diffusion Homework Help
PPTX
Mathematical Statistics Homework Help
PPTX
Data Analysis Homework Help
PPTX
Matlab Assignment Help
PPT
5.1 greedy 03
PDF
Daa chapter11
PPTX
Statistics Assignment Help
Stochastic Assignment Help
Mechanical Engineering Assignment Help
Answers withexplanations
DSP System Homework Help
Signal Processing Homework Help
Assignment 2 daa
Matlab Assignment Help
Ee693 questionshomework
Logistics Management Assignment Help
Algorithm Assignment Help
Signals Processing Assignment Help
Signal Processing Assignment Help
Diffusion Homework Help
Mathematical Statistics Homework Help
Data Analysis Homework Help
Matlab Assignment Help
5.1 greedy 03
Daa chapter11
Statistics Assignment Help
Ad

Similar to Computer Science Assignment Help (20)

PPTX
Design and Analysis of Algorithms Assignment Help
PPTX
Computer Network Homework Help
PPTX
Algorithms Design Exam Help
PPTX
Algorithms Design Assignment Help
PPT
rachit.ppt for academic purposes for college students
PPTX
Design and Analysis of Algorithms Exam Help
PPTX
Algorithm Homework Help
PPTX
Programming Exam Help
PPTX
Algorithm Exam Help
PDF
Graph applications chapter
PPTX
Temporal graph
PPT
Graphs in Data Structure
PPT
Lecture_10_Parallel_Algorithms_Part_II.ppt
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
PDF
Topological Sort
PPTX
DOCX
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
PDF
Lecture13
Design and Analysis of Algorithms Assignment Help
Computer Network Homework Help
Algorithms Design Exam Help
Algorithms Design Assignment Help
rachit.ppt for academic purposes for college students
Design and Analysis of Algorithms Exam Help
Algorithm Homework Help
Programming Exam Help
Algorithm Exam Help
Graph applications chapter
Temporal graph
Graphs in Data Structure
Lecture_10_Parallel_Algorithms_Part_II.ppt
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
Data structure Graph PPT ( BFS & DFS ) NOTES
Topological Sort
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
Lecture13
Ad

More from Programming Homework Help (20)

PPTX
Data Structures and Algorithm: Sample Problems with Solution
PPTX
Seasonal Decomposition of Time Series Data
PPTX
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
PPTX
Exploring Control Flow: Harnessing While Loops in Python
PPTX
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
PPTX
C Assignment Help
PPTX
Python Question - Python Assignment Help
PPTX
Best Algorithms Assignment Help
PPTX
Algorithm Homework Help
PPTX
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
PPTX
Algorithm Homework Help
PPTX
Algorithms Design Homework Help
PPTX
Algorithm Assignment Help
PPTX
Algorithm Homework Help
PPTX
C Homework Help
PPTX
C Homework Help
PPTX
Computer Science Assignment Help
PPTX
Software Construction Assignment Help
PPTX
C Assignment Help
PPTX
Programming Homework Help
Data Structures and Algorithm: Sample Problems with Solution
Seasonal Decomposition of Time Series Data
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
Exploring Control Flow: Harnessing While Loops in Python
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
C Assignment Help
Python Question - Python Assignment Help
Best Algorithms Assignment Help
Algorithm Homework Help
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
Algorithm Homework Help
Algorithms Design Homework Help
Algorithm Assignment Help
Algorithm Homework Help
C Homework Help
C Homework Help
Computer Science Assignment Help
Software Construction Assignment Help
C Assignment Help
Programming Homework Help

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Pre independence Education in Inndia.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
Cell Structure & Organelles in detailed.
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pre independence Education in Inndia.pdf
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPH.pptx obstetrics and gynecology in nursing
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharma ospi slides which help in ospi learning
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cell Types and Its function , kingdom of life
Pharmacology of Heart Failure /Pharmacotherapy of CHF

Computer Science Assignment Help

  • 1. For any help regarding Computer Science Assignment Help Visit: https://www.programminghomeworkhelp.com/ , Email : support@programminghomeworkhelp.com or call us at - +1 678 648 4277 programminghomeworkhelp.com
  • 2. Introduction To Algorithms Fall Problem (a) T F Computing √ b ac for an n-bit positive integer a can be done in O(lg n) iterations of Newton’s method. (b) T F Suppose we want to solve a polynomial equation f(x) = 0. While our choice of initial approximation x0 will affect how quickly Newton’s method converges, it will always converge eventually. (c) T F Karatsuba’s integer multiplication algorithm always runs faster than the grade-school integer multiplication algorithm. (d) T F If we convert an n-digit base-256 number into base 2, the resulting number of digits is . (e) T F In a weighted undirected graph G = (V, E, w), breadth-first search from a vertex s finds single-source shortest paths from s (via parent pointers) in O(V + E) time. (f) T F In a weighted undirected tree G = (V, E, w), breadth-first search from a vertex s finds single-source shortest paths from s (via parent pointers) in O(V + E) time. (g) T F In a weighted undirected tree G = (V, E, w), depth-first search from a vertex s finds single-source shortest paths from s (via parent pointers) in O(V + E) time. (h) T F If a graph represents tasks and their interdependencies (i.e., an edge (u, v) indicates that u must happen before v happens), then the breadth-first search order of vertices is a valid order in which to tackle the tasks. programminghomeworkhelp.com
  • 3. (i) T F Dijkstra’s shortest-path algorithm may relax an edge more than once in a graph with a cycle. (j) T F Given a weighted directed graph G = (V, E, w) and a source s ∈ V , if G has a negative- weight cycle somewhere, then the Bellman-Ford algorithm will necessarily compute an incorrect result for some δ(s, v). (k) T F In a weighted directed graph G = (V, E, w) containing no zero- or positive-weight cycles, Bellman-Ford can find a longest (maximum-weight) path from vertex s to vertex t. (l) T F In a weighted directed graph G = (V, E, w) containing a negativeweight cycle, running the Bellman-Ford algorithm from s will find a shortest acyclic path from s to a given destination vertex t. (m) T F The bidirectional Dijkstra algorithm runs asymptotically faster than the Dijkstra algorithm. (n) T F Given a weighted directed graph G = (V, E, w) and a shortest path p from s to t, if we doubled the weight of every edge to produce G0 = (V, E, w0 ), then p is also a shortest path in G0 . programminghomeworkhelp.com
  • 4. Problem 3. MazeCraft You are playing SnowStorm’s new video game, MazeCraft. Realizing that you can convert a maze into a graph with vertices representing cells and edges representing passages, you want to use your newly learned graph-search algorithms to navigate themaze. Consider thefollowing converted graph. For the following questions, assume that the graph is represented using adjacency lists, and that all adjacency lists are sorted, i.e., the vertices in an adjacency list are always sorted alphabetically. (a) Suppose thatyou wantto find a path from A to H. If you use breadth-first search, write down the resulting path as a sequence of vertices. (b)If you use depth-first search to find a path from A to H, write down the resulting path as a sequence of vertices. programminghomeworkhelp.com
  • 5. (c) To determine whether the maze has cycles or multiple paths to the same destination, you decide to use the edge classification of depth-first search. Run depth- first search on the graph reproduced below, starting from vertex A, and label every edge with T if it’s a tree edge, B if it’s a back edge, F if it’s a forward edge, and C if it’s a cross edge. As a reminder, recall that an edge (u, v) is • a tree edge (T) if v was first discovered by exploring edge (u, v) (tree edges form the depth-first forest); • a back edge (B) if v is u’s ancestor in a depth-first tree; • a forward edge (F) if v is u’s descendant in a depth-first tree; and • a cross edge (C) if none of the above apply. programminghomeworkhelp.com
  • 6. A B C (d) [Now suppose that the passages in the maze are directional. Rerun depth- first search in the directed graph below and label the edges accordingly. D F E G H (e) [ Suppose each passage in the maze causes a different amount of damage to you in game. You change the graph to use weights to represent the damage caused by each edge. You then use Dijkstra’s algorithm to find the path from A to H with the lowest possible damage. Write down the order in which vertices get removed from the priority queue when running Dijkstra’s algorithm. programminghomeworkhelp.com
  • 7. Problem 4. Malfunctioning Calculator Former 6.006 student Mallory Funke is at a math competition, but her calculator isn’t working. It seems to work fine for whole numbers, but the numbers after the decimal point always seem to be the sequence repeated over and over again, making those digits useless. For one of the problems, she has been asked to compute [A3/4♩ for a few different integer values of A. Mal knows that Newton’s Method can be used to compute the square root or the cube root of an integer A. So as a first step in computing [A3/4♩, Mal wants to use Newton’s Method to compute [A1/4♩. She then plans to use that information to compute [A3/4♩. (a) Mal decides to use the function f (x) = x4 − A, because it has a root at x = A1/4. Use Newton’s Method on f (x) to generate a formula for computing increasingly accurate estimates of [A1/4♩. In other words, give a formula for the more accurate estimate xi+1 in terms of a less accurate estimate xi. The formula you construct must use only addition, subtraction, multiplication, and division. (You do not need to simplify the formula.) (b) Mal decides to use the technique from part (a) to compute the value B = [A1/4♩. She then plans to compute [A3/4♩ by calculating the value C = B3 = B·B·B. Provide an explanation of why this technique does not work. Hint: Define α to be the fractional part of A1/4, so that B = A1/4 − α. What happens when you compute C = B3? programminghomeworkhelp.com
  • 8. (c) Mal clearly needs a way to check her answer for [A3/4♩, using only integers. Given a pair of positive integers A and C, explain how to check whether C ≤ A3/4 using O(1) additions, subtractions, multiplications, and comparisons. (d) Explain how to check whether C = [A3/4♩, again using only O(1) addi- tions, subtractions, multiplications, and comparisons. Hint: Recall how the floor function is defined. (e) Give a brief description of an algorithm that takes as input a d-digit positive integer A and computes [A3/4♩. The only arithmetic operations you can use are inte- ger addition, integer subtraction, integer multiplication, integer division, and integer comparison. Your algorithm should use Θ(lg d) arithmetic operations in total, but par- tial credit will be awarded for using Θ(d) arithmetic operations. For this question, you may assume that Newton’s Method has a quadratic rateof convergencefor whatever function you devise. programminghomeworkhelp.com
  • 9. Problem 5. The Tourist Your new startup, Bird Tours, brings people around Boston in a new aviation car that can both drive and fly. You’ve constructed a weighted directed graph G = (V, E, w) representing the best time to drive or fly between various city sites (represented by vertices in V ). You’ve also written a history of Boston, which would be best described by visiting sites v0, v1, . . . , vk in that order. Your goal is to find the shortest path in G that visits v0, v1, . . . , vk in order, possibly visiting other vertices in between. (The path must have v0, v1, . . . , vk as a subsequence; the path is allowed to visit a vertex more than once. For example, v0, v2, v1, v2, . . . , vk is legal.) To do the computation, you’ve found an online service, Paths ’ Us, that will compute the shortest path from a given source s to a given target t in a given weighted graph, for the bargain price of $1. You see how to solve the problem by paying $k, calling Paths ’ Us with (v0, v1, G), (v1, v2, G), . . . , (vk—1, vk, G) and piecing together the paths. Describe how to solve the problem with only $1 by calling Paths ’ Us with (s, t, Gr) for a newly constructed graph Gr = (V r, Er, wr), and converting the resulting path into a path in G. programminghomeworkhelp.com
  • 10. Problem 6. Fill ’Er Up You are traveling by car from one city to another city. Unfortunately, you have a hole in your gas tank, and you have to refill your gas tank to travel across more than two roads. In addition, there is a toll booth on every road that charges you for using that road. Your goal is to find the least-expensive path from your start to your destination. You represent the city network using a directed graph G = (V, E, w) with weights wdefined on both edges and vertices. The vertices V represent the cities and the edges E represent the roads. The weight w(e) of an edge e represents the toll amount on that road. The weight w(v) of a vertex v is the price of filling your gas tank in that city (which is a fixed price independent of how much gas you have left, or ∞ if there is no gas available to purchase). You are allowed (but not obligated) to end your journey with an empty tank, and you may assume that you always start your journey with a full tank. Below is an example graph that we will use to answer part (a). One seemingly cheap path from s to t is (s, u1, u2, t) at a cost of $8. Unfortunately, this path is not valid, because our leaky gas tank won’t permit moving across three edges without refilling our gas tank. One valid path is (s, u3, u2, t) at a cost of $22. (This is a valid path: we begin with a full tank, travel across one edge to a gas station where we refill our tank, and then travel two edges to the destination, arriving with an empty gas tank. Notice that we are unable to continue the journey to u5 if we wanted to, because even though there is a gas station there, we have to traverse a third edge to get to it.) programminghomeworkhelp.com
  • 11. Gas: $5 Gas: $3 NO GAS NO GAS T oll: $5 Toll: $7 T oll: $2 T oll: $1 T oll: $2 T oll: $10 T oll: $3 s Gas: Free t NO GAS T oll: $1 u1 u2 T oll: $4 Gas: $1 u5 T oll: $9 T oll: $1 There are some extra copies of this graph at the end of the exam (for your convenience). (a) The valid path given in the description above is not the best path. Find a least-expensive path from s to t in the graph above. (b) Find the least-expensive path from s to u5 in the graph above. (c) Give an O(V log V +E) algorithm to find, in a given graph G = (V, E, w), a least-expensive valid path from a city s to a city t, or report that no such path exists. programminghomeworkhelp.com
  • 12. Gas: Free Gas: $5 Gas: $3 NO GAS NO GAS T oll: $5 Toll: $7 T oll: $2 T oll: $1 T oll: $2 T oll: $10 T oll: $3 s t NO GAS T oll: $1 u1 u2 Here are some extra copies of the graph from Problem 6 (for your convenience). u3 u4 T oll: $4 Gas: $1 u5 T oll: $9 T oll: $1 Gas: Free Gas: $5 Gas: $3 NO GAS NO GAS T oll: $5 Toll: $7 T oll: $2 T oll: $1 T oll: $2 T oll: $10 T oll: $3 s t NO GAS T oll: $1 u1 u2 u3 u4 T oll: $4 Gas: $1 u5 T oll: $9 T oll: $1 programminghomeworkhelp.com
  • 13. (a) T F Solutions Circle (T)rue or (F)alse. You don’t need to justify your choice. Computing √ b ac for an n-bit positive integer a can be done in O(lg n) iterations of Newton’s method. Solution: True. This is the bound obtained by Newton’s Method’s quadratic convergence. (b) T F Suppose we want to solve a polynomial equation f(x) = 0. While our choice of initial approximation x0 will affect how quickly Newton’s method converges, it will always converge eventually. (c) T F Karatsuba’s integer multiplication algorithm always runs faster than the grade- school integer multiplication algorithm. Solution: False. Problem Set 5 has shown that the O(N2 ) algorithm runs faster for small numbers. (d) T F If we convert an n-digit base-256 number into base 2, the resulting number of digits is Θ(n 2 ). programminghomeworkhelp.com
  • 14. (e) T F In a weighted undirected graph G = (V, E, w), breadth-first search from a vertex s finds single-source shortest paths from s (via parent pointers) in O(V + E) time. Solution: False. Only in unweighted graphs. (f) T F In a weighted undirected tree G = (V, E, w), breadth-first search from a vertex s finds single-source shortest paths from s (via parent pointers) in O(V + E) time. Solution: True. In a tree, there is only one path between two vertices, and breadth-first search finds it. (g) T F In a weighted undirected tree G = (V, E, w), depth-first search from a vertex s finds single-source shortest paths from s (via parent pointers) in O(V + E) time. Solution: True. In a tree, there is only one path between two vertices, and depth-first search finds it. (h) T F If a graph represents tasks and their interdependencies (i.e., an edge (u, v) indicates that u must happen before v happens), then the breadth-first search order of vertices is a valid order in which to tackle the tasks. Solution: No, you’d prefer depth-first search, which can easily be used to produce a topological sort of the graph, which would correspond to a valid task order. BFS can produce incorrect results. (i) T F Dijkstra’s shortest-path algorithm may relax an edge more than once in a graph with a cycle. Solution: False. Dijkstra’s algorithm always visits each node at most once; this is why it produces an incorrect result in the presence of negative-weight edges. programminghomeworkhelp.com
  • 15. (j) T F Given a weighted directed graph G = (V, E, w) and a source s ∈ V , if G has a negative-weight cycle somewhere, then the Bellman-Ford algorithm will necessarily compute an incorrect result for some δ(s, v). Solution: False. The negative-weight cycle has to be reachable from s. (k) T F In a weighted directed graph G = (V, E, w) containing no zero- or positive-weight cycles, Bellman-Ford can find a longest (maximum-weight) path from vertex s to vertex t. Solution: True. Negate the weights. (l) T F In a weighted directed graph G = (V, E, w) containing a negative- weight cycle, running the Bellman-Ford algorithm from s will find a shortest acyclic path from s to a given destination vertex t. Solution: False. Bellman-Ford will terminate, and can detect the presence of that negative-weight cycle, but it can’t “route around it.” (You could always remove an edge to break the cycle and try again, though.) (m)T F The bidirectional Dijkstra algorithm runs asymptotically faster than the Dijkstra algorithm. Solution: False. The constant factor behind bidirectional Dijkstra is better, but the worst-case running time is the same. programminghomeworkhelp.com
  • 16. [ Given a weighted directed graph G = (V, E, w) and a shortest path p from s to t, if we doubled the weight of every edge to produce Gr = (V, E, wr), then p is also a shortest path in Gr. (n) T F Solution: True. Multiplying edge weights by any positive constant factor pre- serves their relative order, as well as the relative order of any linear combination of the weights. All path weights are linear combinations of edge weights, so the relative order of path weights is preserved. This means that a shortest path in G will still be a shortest path in Gr. programminghomeworkhelp.com
  • 17. Problem 3. MazeCraft [ You are playing SnowStorm’s new video game, MazeCraft. Realizing that you can convert a maze into a graph with vertices representing cells and edges representing passages, you want to use your newly learned graph-search algorithms to navigate the maze. Consider the following converted graph. For the following questions, assume that the graph is represented using adjacency lists, and that all adjacency lists are sorted, i.e., the vertices in an adjacency list are always sorted alphabetically. (a) [4 points] Suppose thatyou wanttofind a pathfrom A toH. If you use breadth-first search, write down the resulting path as a sequence of vertices. Solution: A, B, D, F, H P ·L ♩, programminghomeworkhelp.com
  • 18. (b) If you use depth-first search to find a path from A to H, write down the resulting path as a sequence of vertices. Solution: A, B, C, D, E, G, F, H if you use recursion, or A, C, D, F, H if you use stack-based implemention. Essentially, the recursion-based implementation will visit the neighbors in alphabetical order, while the stack-based implementation will visit the neighbors in reverse alphabetical order. (c) To determine whether the maze has cycles or multiple paths to the same destination, you decide to use the edge classification of depth-first search. Run depth- first search on the graph reproduced below, starting from vertex A, and label every edge with T if it’s a tree edge, B if it’s a back edge, F if it’s a forward edge, and C if it’s a cross edge. As a reminder, recall that an edge (u, v) is • a tree edge (T) if v was first discovered by exploring edge (u, v) (tree edges form the depth-first forest); • a back edge (B) if v is u’s ancestor in a depth-first tree; • a forward edge (F) if v is u’s descendant in a depth-first tree; and • a cross edge (C) if none of the above apply. Solution: Recursion-based implementation: programminghomeworkhelp.com
  • 19. A B C F E G H T B T T T B T T T B B Stack-based implementation: D T T B B F T B B T B T A C E G H T T 1 w 2 Scoring: For Problem 3(c) and 3(d), we computed the score as [P − N ♩, where P is the total points of the problem and Nw is the number of wrong labels. programminghomeworkhelp.com A B C (d) Now suppose that the passages in the maze are directional. Rerun depth- first search in the directed graph below and label the edges accordingly. Solution: Recursion-based implementation: D F E G H T B T T T F C T T C T Stack-based implementation: D T T C T B F T T T F C A C E G H B T
  • 20. (e) [ Suppose each passage in the maze causes a different amount of damage to you in game. You change the graph to use weights to represent the damage caused by each edge. You then use Dijkstra’s algorithm to find the path from A to H with the lowest possible damage. Write down the order in which vertices get removed from the priority queue when running Dijkstra’s algorithm. D programminghomeworkhelp.com
  • 21. Problem 4. Malfunctioning Calculator a. programminghomeworkhelp.com
  • 22. (c) Mal clearly needs a way to check her answer for [A3/4♩, using only integers. Given a pair of positive integers A and C, explain how to check whether C ≤ A3/4 using O(1) additions, subtractions, multiplications, and comparisons. Solution: The equation C ≤ A3/4 is equivalent to the equation C4 ≤ A3, which is equivalent to C · C · C · C ≤ A · A · A. (d) Explain howtocheck whetherC = [A3/4♩, again using only O(1) addi- tions, subtractions, multiplications, and comparisons. Hint: Recall how the floor function is defined. Solution: If C = [A3/4♩, then by the definition of the floor function, we have C ≤ A3/4 and C + 1 > A3/4. We can check bothof these using thetechniquefrom part (c). (b) Mal decides to use the technique from part (a) to compute the value B = [A1/4♩. She then plans to compute [A3/4♩ by calculating the value C = B3 = B·B·B. Provide an explanation of why this technique does not work. Hint: Define α to be the fractional part of A1/4, so that B = A1/4 − α. What happens when you compute C = B3? Solution: When you expand out the formula for C, you get C = B3 = (A1/4 − α)3 = A3/4 − 3A2/4 α + 3A1/4 α2 − α3 . If A is large, then γ = 3A2/4α − 3A1/4α2 + α3 will be significantly greater than 1, and so we’ll have C = A3/4 − γ with γ > 1. Hence, C will not be [A3/4♩. programminghomeworkhelp.com
  • 23. (e) Give a brief description of an algorithm that takes as input a d-digit positive integer A and computes [A3/4♩. The only arithmetic operations you can use are inte- ger addition, integer subtraction, integer multiplication, integer division, and integer comparison. Your algorithm should use Θ(lg d) arithmetic operations in total, but par- tial credit will be awarded for using Θ(d) arithmetic operations. For this question, you may assume that Newton’s Method has a quadratic rateof convergencefor whatever function you devise. Solution: Θ(lg d) Solution: The technique from part (b) didn’t work because we threw away information (taking the floor) before performing another operation. Instead, we first calculate B = A3, then compute C = [B1/4♩ using the formula from part (a) to continually improve our estimate. Because we have a quadratic rate of convergence, we will improve our estimate Θ(log d) times, and each time we will perform a constant number of arithmetic operations. Note: This is equivalent to applying Newton’s Method to the function f (x) = x4 −A3, but does not require you to rederive the equation for xi+1 in terms of xi. Θ(d) Solution: Use the comparison method from part (c) to do binary search. This requires us to do Θ(1) operations for each of the bits in the result, for a total of Θ(d) operations. programminghomeworkhelp.com
  • 24. Problem 5. The Tourist Your new startup, Bird Tours, brings people around Boston in a new aviation car that can both drive and fly. You’ve constructed a weighted directed graph G = (V, E, w) representing the best time to drive or fly between various city sites (represented by vertices in V ). You’ve also written a history of Boston, which would be best described by visiting sites v0, v1, . . . , vk in that order. Your goal is to find the shortest path in G that visits v0, v1, . . . , vk in order, possibly visiting other vertices in between. (The path must have v0, v1, . . . , vk as a subsequence; the path is allowed to visit a vertex more than once. For example, v0, v2, v1, v2, . . . , vk is legal.) To do the computation, you’ve found an online service, Paths ’ Us, that will compute the shortest path from a given source s to a given target t in a given weighted graph, for the bargain price of $1. You see how to solve the problem by paying $k, calling Paths ’ Us with (v0, v1, G), (v1, v2, G), . . . , (vk—1, vk, G) and piecing together the paths. Describe how to solve the problem with only $1 by calling Paths ’ Us with (s, t, Gr) for a newly constructed graph Gr = (V r, Er, wr), and convertingtheresulting path into a path in G. Solution: To form the graph Gr, we start from the disjoint union of k copies of the given graph G, using superscripts to denote the different copies G1, G2, . . . , Gk . That is, for each vertex v ∈ V , we make k copies v1, v2, . . . , vk in V r; and for each edge (u, v) ∈E, we form the edges programminghomeworkhelp.com
  • 26. Problem 6. Fill ’Er Up! You are traveling by car from one city to another city. Unfortunately, you have a hole in your gas tank, and you have to refill your gas tank to travel across more than two roads. In addition, there is a toll booth on every road that charges you for using that road. Your goal is to find the least-expensive path from your start to your destination. You represent the city network using a directed graph G = (V, E, w) with weights wdefined on both edges and vertices. The vertices V represent the cities and the edges E represent the roads. The weight w(e) of an edge e represents the toll amount on that road. The weight w(v) of a vertex v is the price of filling your gas tank in that city (which is a fixed price independent of how much gas you have left, or ∞ if there is no gas available to purchase). You are allowed (but not obligated) to end your journey with an empty tank, and you may assume that you always start your journey with a full tank. Below is an example graph that we will use to answer part (a). One seemingly cheap path from s to t is (s, u1, u2, t) at a cost of $8. Unfortunately, this path is not valid, because our leaky gas tank won’t permit moving across three edges without refilling our gas tank. One valid path is (s, u3, u2, t) at a cost of $22. (This is a valid path: we begin with a full tank, travel across one edge to a gas station where we refill our tank, and then travel two edges to the destination, arriving with an empty gas tank. Notice that we are unable to continue the journey to u5 if we wanted to, because even though there is a gas station there, we have to traverse a third edge to get to it.) programminghomeworkhelp.com
  • 27. Gas: $5 Gas: $3 NO GAS NO GAS T oll: $5 Toll: $7 T oll: $2 T oll: $1 T oll: $2 T oll: $10 T oll: $3 s Gas: Free t NO GAS T oll: $1 u1 u2 T oll: $4 Gas: $1 u5 T oll: $9 T oll: $1 There are some extra copies of this graph at the end of the exam (for your convenience). programminghomeworkhelp.com
  • 28. (a) The valid path given in the description above is not the best path. Find a least-expensive path from s to t in the graph above. Solution: (s, u1, u3, u2, t) (b) Find the least-expensive path from s to u5 in the graph above. Solution: (s, u1, u3, u2, u4, t, u5) (c) Give an O(V log V +E) algorithm to find, in a given graph G = (V, E, w), a least- expensive valid path from a city s to a city t, or report that no such path exists. Solution: We will solve this problem by transforming G = (V, E, w), the graph of cities, roads, and toll/gas costs, into a new graph Gr = (V r, Er, wr). We will then use Dijkstra’s algorithm to find the desired path. First construct the vertex set V r. For each vertex v ∈ V , create three vertices in V r: vf , vh, and ve. These stand for a full tank, a half-full tank, and an empty tank, respectively. This operation takes O(V ) time, since we iterate |V | times and create three new vertices each time. Next we will discuss the construction of Er and wr. For each edge (u, v) = e ∈ E, create edges in Er as follows. Create edges (uf , vh) and (uh, ve) to represent the emptying of half the tank during the move from u to v. The weights on both of these edges will be the original weight w(u, v). This operation takes O(E) time, since we iterate |E| times and insert two new edges into Er each time. programminghomeworkhelp.com
  • 29. We can now run Dijkstra’s algorithm on this graph to find paths from sf to tf , th, and te, and we return the shortest of the three. The graph transformation took O(V + E) time and, as the number of vertices and the number of edges increased by a constant factor, running Dijkstra on this graph takes O(V log V + E) time. Therefore, the total running time is O(V log V + E). Finally, we will represent the cost of fueling up by creating additional edges as follows. For every vertex v ∈ V , we check whether there is a gas station there. If there is a gas station at this vertex, then we add the edges (ve, vf ) and (vh, vf ) to Er. Since the cost of fueling up is not dependent on the amount of gas that is already in the tank, we assign both of these edges the cost of fueling up at that city. This procedure takes O(V ) time, since we iterate over all of thevertices andcreate a constant number of new edges. programminghomeworkhelp.com

Editor's Notes