



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Main points of this past exam are: Advanced Data Structures, Algorithms, Incomplete Instance, Wgraph Data Structure, Firstedge, Edges, Prim's Algorithm, Changekey, Invoked, Weighted Graph
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Be neat and concise, but complete.
The figure below shows a complete graph on 5 vertices. If we start Prim’s algorithm from vertex u 1 , all the other vertices will be inserted into the heap during the initialization phase, and they will be removed from the heap in the order of their subscripts. On each iteration, changekey will be called on every edge incident to the current vertex connecting to a vertex with higher subscript.
The example generalizes directly to any n. The edge weight on edges of the form {ui ,u (^) i+1 } is 1. The weight of all other edges {u (^) i ,u (^) j } with i<j is n–i. For the general case, changkey is called
u 1 u 2 u 3 u 4 u 5 1 1 1
4 3 2
4
(^43)
1
u 1 u 2 u 3 u 4 u 5 1 1 1
4 3 2
4
(^43)
1
procedure minspantree( graph G , set tree_edges ); vertex u , v ; set tree_vertices ; heap S ; tree_vertices := {1}; tree_edges := {}; for {1, v } ∈ edges(1) ⇒ insert({1, v }, cost (1, v ), S ); rof ; do S ≠ {} ⇒ { u , v } := deletemin( S ); // assume u ∈ tree_vertices tree_vertices := tree_vertices ∪ { v }; tree_edges := tree_edges ∪ { u,v }; for { v,w } ∈ edges ( v ) ⇒ if w ∈ tree_vertices ⇒ delete({ v,w }, S ); | w ∉ tree_vertices ⇒ insert({ v,w }, cost ( v,w ), S ); fi; rof ; od ; end ;
Explain why this algorithm is an instance of the general greedy method.
The algorithm maintains the invariant that the heap contains the edges that cross the cut defined by the set of tree_vertices and its complement. The minimum cost edge crossed by this cut is the one selected by the blue rule of the greedy algorithm. Edges not included in the tree are implicitly colored red. For each of these, the edge, together with the tree path joining its endpoints is a cycle to which the red rule can be applied.
Give an expression for the time required by all the deletemin operations, in terms of the number of vertices ( n ), the number of edges ( m ) and the heap parameter ( d ). Similarly, give an expression for the time required by all the insert operations and all the delete operations.
total deletemin time: O(nd log d n) total insert time: O(m log d n) total delete time: O(md log d n)
What choice of d gives the best overall running time? Why?
The overall running time is O(md log d n). This is minimized by selecting a small value for d (like 2).
An alternative way implement a min-max heap is to store the items in two separate d -heaps, one of which is organized to support the findmin operation and the other organized to implement the findmax operation. How does the space used in these two implementations compare? Assume that the min-max heap is not required to provide a general delete operation or a changkey operatiion, but is required to implement both deletemin and deletemax.
The original min-max heap implementation requires 2n words of memory for a heap of size n. It uses n words to store the heap array and n words for the key values. The alternate implementation requires 5n words. This is because when we do a deletemin, we need to delete the item from both heaps and in the “max” heap, it will not be at the root of the tree. Hence, we need an array to give us the position of each node in the max heap. For the same reason, we need an array to give us the position of each node in the min heap. The key values are the same in both heaps, so it’s not necessary to store them twice.
Give an asymptotic upper bound on the running time of the changekey operation on this alternate implementation of the min-max heap, in the case when the key increases in value. Also, give an upper bound on its running time when the key decreases in value. How is the running time affected by the value of d used in the underlying d -heaps? What is the best choice for d. Justify your answers.
The running time is O(d log (^) d n) regardless of whether changkey increases or decreases. This is because in both cases, we must do a siftup in one heap and a siftdown in the other. So the running time increases with d, meaning that a small value (like 2) is best.
Give an asymptotic upper bound on the running time of the insert operation. How is the running time affected by the value of d used in the underlying d -heaps? What is the best choice for d from the perspective of the insert operation? Justify your answers.
The running time is O(log (^) d n), since inserting the item into both heaps requires a siftup operation. The best choice of d, from the perspective of the insert operation is n–1 where n is the maximum heap size, since in this case, the insert takes constant time. Of course, this is a poor choice for deletemin.
lg n
Assuming that the credit and debit variables used in the original analysis are defined and assigned values in the same way as in the original analysis, give a tight upper bound on the value for fcredit after m operations have been done. Justify your answer.
m (1+lg n ). During a top level find, fcredit is implemented at most once for each different value of the level function. There are at most 1+lg n values that the level function can take on, so fcredit can increase by at most 1+lg n for each top level find. Suppose that debit (x) is incremented during a find operation and that level (x)= i before the find. If p ( x ) =w before the find and p ( x )= z after the find , how does the value of r ( z ) compare to r ( w )? Explain.
r(z) ≥ r(w)+2 i^ –1. Because debit(x) was incremented, it must have a proper ancestor y with level(y)=i. By the definition of level, r(p(y))–r(y) ≥ 2 i^ –1. Since r increases as you go up the tree, it follows that r(z)–r(w) ≥ 2 i^ –.
How many times can debit ( x ) be incremented while level ( x )= i? Why?
At most twice. Each time debit(x) is incremented while level(x)=i, r(p(x)) grows by 2 i^ –1. After this happens two times, level(x) must increase by at least 1. Also note that because r(p(x)) never decreases, neither does level(x).
Give an upper bound on the final value of debit ( x ). Explain.
2(1+lg n ). Since level can take on at most (1+lg n) distinct values, and debit(x) can be incremented at most twice for each of these values it can never exceed 2(1+ lg n ).