e 10 6 Exam 1 Solutions edges l lnext rnext firstedge a b c d e 0 f Jonathan Turner 9/29/05 CS 542 Advanced Data Structures and Algorithms

Similar documents
Exam 1 Solutions. Jonathan Turner 10/4/01 9/27/01. CS 541 Algorithms and Programs. Be neat and concise, but complete.

Final Exam. Jonathan Turner 5/12/2010. CS 542 Advanced Data Structures and Algorithms

CSE 542 Advanced Data Structures and Algorithms. Lab 4 Solution

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

6.1 Minimum Spanning Trees

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Lab 1 Solution. Jon Turner. CSE 542 Advanced Data Structures and Algorithms

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Exam Data structures DIT960/DAT036

CS 310 Advanced Data Structures and Algorithms

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Minimum Spanning Trees

CSE 373 Sample Midterm #2 (closed book, closed notes, calculators o.k.)

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Course Review for Finals. Cpt S 223 Fall 2008

Rank-Pairing Heaps. Bernard Haeupler Siddhartha Sen Robert E. Tarjan. SIAM JOURNAL ON COMPUTING Vol. 40, No. 6 (2011), pp.

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

1. The following graph is not Eulerian. Make it into an Eulerian graph by adding as few edges as possible.

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

22 Elementary Graph Algorithms. There are two standard ways to represent a

Design and Analysis of Algorithms - - Assessment

Assignment 5: Solutions

Minimum Spanning Trees

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

What is a minimal spanning tree (MST) and how to find one

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum Spanning Trees My T. UF

23.2 Minimum Spanning Trees

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

CMSC 341 Leftist Heaps

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CSci 231 Final Review

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CISC 320 Midterm Exam

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CMSC 341 Lecture 15 Leftist Heaps

Practice Exam #3, Math 100, Professor Wilson. MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Algorithms for Minimum Spanning Trees

CSE 332, Spring 2010, Midterm Examination 30 April 2010

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

CSE 100: GRAPH ALGORITHMS

Final Exam in Algorithms and Data Structures 1 (1DL210)

CMSC 341 Lecture 15 Leftist Heaps

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Minimum Spanning Trees

CSE 373 Autumn 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

CSE 5311 Notes 4a: Priority Queues

CS521 \ Notes for the Final Exam

13 th Annual Johns Hopkins Math Tournament Saturday, February 19, 2011 Automata Theory EUR solutions

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

MC302 GRAPH THEORY SOLUTIONS TO HOMEWORK #1 9/19/13 68 points + 6 extra credit points

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Chapter 22 Splay Trees

Depth-first Search (DFS)

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

) $ f ( n) " %( g( n)

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Course Review. Cpt S 223 Fall 2009

Minimum Spanning Trees

Autumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Matching Theory. Figure 1: Is this graph bipartite?

Optimization I : Brute force and Greedy strategy

Lecture Notes for Chapter 23: Minimum Spanning Trees

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Recall: Properties of B-Trees

( ) n 3. n 2 ( ) D. Ο

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Soft Heaps And Minimum Spanning Trees

SKEW HEAPS: Self-Adjusting Heaps

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Minimum spanning trees

CSE 100 Advanced Data Structures

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Priority Queues. Meld(Q 1,Q 2 ) merge two sets

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Lecture 13: Minimum Spanning Trees Steven Skiena

Solution to Problem 1 of HW 2. Finding the L1 and L2 edges of the graph used in the UD problem, using a suffix array instead of a suffix tree.

( D. Θ n. ( ) f n ( ) D. Ο%

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Principles of Algorithm Design

Part II. Graph Theory. Year

Transcription:

CS Advanced Data Structures and Algorithms Exam Solutions Jonathan Turner //0 Be neat and concise, but complete.. ( points) An incomplete instance of the wgraph data structure is shown below. Fill in all the missing entries. firstedge a b c d e f 8 0 6 7 8 0 edges l lnext wt rnext r a 7 e d 0 7 f b 6 8 a c 7 0 b a d c 0 0 e c 6 8 0 f a 0 c e 0 6 d f 6 e - -

. (0 points) In the worst-case analysis of Prim s algorithm, we saw that the number of calls to the changekey procedure was O(m). Show that it is also Ω(m). More specifically, show that it is Ω(n ). Do this by showing that for each value of n, there is a weighted graph with Ω(n ) edges and that changekey is invoked when most of these edges are examined. Be sure to specify the edge weights. Draw a picture of the graph for the case of n=. The figure below shows a complete graph on vertices. If we start Prim s algorithm from vertex u, 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. u u u u u The example generalizes directly to any n. The edge weight on edges of the form {u i,u i+ } is. The weight of all other edges {u i,u j } with i<j is n i. For the general case, changkey is called (n ) + (n ) +... + times, which is Ω (n ). - -

. (0 points) The figure below shows an incomplete representation of an intermediate state in the execution of the round-robin algorithm. In particular, the state of the partition data structure is not shown. Show the complete state of the algorithm after one more iteration. You should include the state of the partition data structure (show the sets in the partition data structure as trees). Also, circle all the nodes in the leftist heaps that should be considered deleted. a b g h c e 8 f d 6 queue: f c g d partition: g c d 0 f 0 a 0 b 0 h 0 e 0 h(g) - -, - -, ag, bg, ab ac, ab, ag, eg, leftist heaps h(f ) ef, h(d) - -, de, bg, cf, df 6, dh 8, df 6, de, bh, h(c) - -, bc, ch, ch, eg,, ef, eh, ac, cf, bh, eh, bc, dh 6, - -

. ( points) The minimum spanning tree algorithm shown below is similar to Prim s algorithm, but in this case the heap stores all the edges with exactly one endpoint in the set of tree vertices. procedure minspantree(graph G, set tree_edges); vertex u,v; set tree_vertices; heap S; tree_vertices := {}; tree_edges := {}; for {,v} edges() insert({,v},cost(,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(ndlog d n) total insert time: O(mlog d n) total delete time: O(mdlog d n) What choice of d gives the best overall running time? Why? The overall running time is O(mdlog d n). This is minimized by selecting a small value for d (like ). - -

. (0 points) Problem set discusses a min-max heap, a data structure that supports both a findmin operation and a findmax operation. In the implementation discussed in the problem set, nodes at even distances from the root have the smallest key value in their subtree, while nodes at odd distances from the root have the largest key value in their subtree. 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 n 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 n 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 ) 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 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. - -

6. (0 points) Let n = n+ n + + nr and assume all n i are positive. Give an upper bound on r ni in terms of n and r. Explain your answer. i= The upper bound is r n/ r = nr. This follows from the general upper bound discussed in problem set. Since the square root function grows more slowly than any linear function, we can get an upper bound by replacing each value in the sum by the average of the n i values. Give an upper bound on r i= n / i. Explain your answer. / The upper bound is n. This follows from a complementary argument to the one discussed in problem set. Since the function is grows faster than linearly, we can get an upper bound by making all but one of the n i values equal to zero. - 6 -

7. ( points) Consider a version of the partition data structure that uses path compression, but does not use link-by-rank. In such a version, we can reduce the space consumed by the algorithm, by eliminating the rank variables. The link subroutine in this version would simply make the parent pointer of the first argument equal to the second argument. To analyze the performance of this version, it s useful to define a function r which takes the role of the rank in the analysis. For each node x, r(x) is initialized to zero. Then, during the link operation that makes node x the child of node y, r(y) is assigned the value max{r(y),+r(x)}. Define level( x) = lg( + r( p( x)) r( x)). What is the largest value that level(x) can have? 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 (+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 +lg n values that the level function can take on, so fcredit can increase by at most +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)+ i. 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) i. Since r increases as you go up the tree, it follows that r(z) r(w) 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 i. After this happens two times, level(x) must increase by at least. 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. (+lg n). Since level can take on at most (+lg n) distinct values, and debit(x) can be incremented at most twice for each of these values it can never exceed (+ lg n). - 7 -