Best Algorithms Design Assignment Help
Best Algorithms Design Assignment Help
Solution: False. If the heaviest edge in the graph is the only edge
connecting some vertex to the rest of the graph, then it must be in
every minimum spanning tree.
Solution: False. It’s possible to do this in linear time using SELECT: first
find the median of A in Θ(n) time, and then partition A around its median.
Then we can take n/4 elements from either side to get a total of n/2
elements in A whose median is also the median of A.
(g) T F There is a density 0 < ρ < 1 such that the asymptotic running time
of the Floyd2
Warshall algorithm on graphs G = (V, E) where |E| = ρ |V | is better
than that of
Johnson’s algorithm.
(h) T F Consider the all pairs shortest paths problem where there are
also weights on the
vertices, and the weight of a path is the sum of the weights
on the edges and
vertices on the path. Then, the following algorithm finds the
weights of the
shortest paths between all pairs in the graph:
APSP-WITH-WEIGHTED-VERTICES(G, w):
1 for (u, v) ∈ E
2 Set w ′ (u, v) = (w(u) + w(v))/2 + w(u, v)
3 Run Johnson’s algorithm on G, w′ to compute the distances δ ′ (u,
v) for all u, v ∈ V .
4 for u, v ∈ V
5 Set duv = δ ′ (u, v) + ½ (w(u) + w(v))
Problem 2. Translation
1. For each edge (u, v), if there is some cluster Ci such that
u, v ∈ Ci , then add (u, v) to the set Ei . Otherwise, add (u, v)
to the set Eglobal.
The third step also requires care. We can store the graph to allow
us to efficiently lookup w(·, ·) and to tell whether an edge (i, j)
has been added to the graph. We can also store source(·, ·) to
allow for Θ(1) lookups. So the runtime here is bounded by Θ(m).
The fourth step is Kruskal’s again, only once, on a graph with ≤ n
vertices and ≤ m edges, so the total runtime is Θ(m lg n). The
final step involves a lookup for each edge (i, j) ∈ T, for a total
runtime of Θ(k) ≤ Θ(n). Hence, the runtime is dominated by the
two steps involving Kruskal. So the total worst-case runtime is
Θ(m lg n).
FLOYD-WARSHALL(W):
1 n = W.rows
2 D(0) = W
3 for k = 1 to n (k)
4 let D(k) = (d(k)ij ) be a new n × n matrix
5 for i = 1 to n
6 for j = 1 to n
7
8 return D(n)
While the second and third changes above are unnecessary for
this part, they lay the groundwork for future parts below.
(b) How would you modify your algorithm from part (a) to keep
track not only of shortest paths with only blue edges, but also
those with exactly one red edge, and to output the lengths of the
shortest red/blue paths for all pairs of vertices in this graph?
1 n = Wr.rows
2 D(0) b = Wb
3 D(0) r = Wr
4 for k = 1 to n
5
be new n × n matrices
6 for i = 1 to n
7 for j = 1 to n
8 d(k)bij = min (d(k-1)b,ij , d(k-1)b,ik + d(k-1) b,kj
10 return D(n)r
(c) Prove the correctness of your algorithm using a loop
invariant.
For the paths including exactly one red edge, for a given pair (i,
j), there are two cases: either the shortest red/blue path from i to j
using intermediate vertices through k does not go through k, or it
does. If it does not go through k, then the length of this path is
equal to d(k-1)r,ij. If it does go through k, then the red edge on this
path is either between i and k or between k and j. Because of the
optimal substructure of shortest paths, we can therefore break this
case down into two subcases: the shortest path length is equal to
min
(d(k-1)r,ik + d(k-1)b,kj, d(k-1)b.ik+ d(k-1)r,kj).
Upon realizing that it was 8:30 PM on Wednesday and he had not yet
started his 6.046 pset, Ben Bitdiddle found n − 1 other students (for a
total of n students) in the same situation, and they decided to do the
pset, which coincidentally had n problems in total, together.
Their brilliant plan for finishing the pset in time was to sit in a circle
and assign one problem to each student, so that for i = 0, 1, . . . , n−1,
student i did problem number i, and wrote up a solution for problem i
meriting p(i) points. Then, they each copied the solutions to all the
other problems from the student next to them, so that student i was
copying from student i − 1 (and student 0 was copying from student
n − 1).
(a) Write a formula that describes the total pset score S(x) for
student x, where the total pset score is the sum of the scores that
student x got on each of the n problems.
Solution: