




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





(c) In what order are the edges added in Kruskal’s algorithm?
(d) Show the union-find trees at the end of Kruskal’s algorithm (in case of a tie in rank, the lexicograpfically first node becomes root).
I Union(B,D) puts B as the parent (by the lexicographical constraint). Union(D,E) links E to B since Find(D) returns B. Union(A,B) and Union(B,C) also link A and C to B. This yields:
I True. Every operation in the union-find data structure takes time at most a constant factor times the running time for the find operation. The find operation’s running time is O(h), where h is the height of the tree it searches, since find(x) begins at element x and follows the path to x’s parent, and then to its grandparent, and so on, until it reaches the root. Since we make unions carefully, by always making the root node of lesser rank point to the root node of greater rank, the height of any tree is bounded by log n.
I True. The argument in part (8) did not rely on path compression. Path compression only makes the path that the find operation follows shorter.
I False. This is the amortized cost, not an upper bound on the cost of any operation. A single operation, even with path compression, could take as long as O(log n).
changekey(v,dist(v)) The runtime will be the same as dijkstra’s O(V log V + E) since in either case, we are only making a constant change per iteration of the inner loop. We may argue correctness by considering, for any destination t, the shortest path that has the minimum num of hops. We want to say that when we pop a node along this path from the heap, that it has the same dist(v) and hops(v) values as it does on this path. We show this inductively: it is certainly true for s. Now assume that we pick a point on the path. We know by the inductive hypothesis, that the node before it on this path, u, had the correct dist(u) and hops(u) values when popped from the heap. This means that dist(v) must be at most dist(u) + l(u, v) and hops(v) at most hops(u) + 1. They also are at least these values, or else there would be a better path than the one we were considering. Now we must show that v has not been popped from the heap already. But this follows for the same reason that it was true in the original dijkstra’s algorithm (see lecture notes for the argument). Note: many students didn’t change the condition in the ’if’ statement, and just put hops(v) = hops(u) + 1. This will end up returning the length of the shortest path that you would find in the original algorithm which may not be the shortest one.
(b) This was a tough one. No one got this completely right. Many people got the basic gist: add a very small value to each edge to bias toward shorter paths. Yes, this means that if two paths were of equal weight before, then the shorter one ill be chosen in the new graph. However, you have to show that the increments are small enough so that you don’t get a new shortest path that just happened to have a very small amount of extra weight from the old one, but was dramatically shorter. Take the following example:
maxsuff[i + 1] =
I max{maxsuff[i] + ai+1, 0 }
(c) What other data structure do you need in order to recover in the end the beginning and end of the maximum contiguous sum?
I The most common approach was to use two pointers to keep track of the beginning and the end of the best sum seen so far. However, one also needs a pointer to keep track of the beginning of the best suffix in order to be able to update the two pointers properly. It is possible to find the beginning and the end without keeping track of any new information. Once we are done, we can search the maxsuff array for the element that equals maxsum[n]. This marks the end of the desired sum. Then we can start at this point in the original sequence and proceed backwards, adding terms until we reach the desired sum.