Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS Exam Solutions: Minimum Spanning Trees, Fibonacci Heaps, Dynamic Trees, and Algorithms, Exams of Data Structures and Algorithms

Solutions to various computer science exam questions. Topics covered include minimum spanning trees using the general greedy method and prim's algorithm, fibonacci heaps, dynamic trees, and dijkstra's algorithm. Students can use this document as a study resource for understanding these concepts and preparing for exams.

Typology: Exams

2012/2013

Uploaded on 03/23/2013

sardai
sardai 🇮🇳

4

(10)

117 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
- 1 -
Be neat and concise, but complete.
1. (15 points) State the general greedy method for finding minimum spanning trees. Show that
Prim’s algorithm is a special case of the general greedy method.
The general greedy method applies one of the following two coloring steps until all edges are colored.
On completion, the blue edges form a minimum spanning tree.
Blue rule. Find a cut crossed by no blue edges and at least one uncolored edge. Select a minimum
weight uncolored edge crossing the cut and color it blue.
Red rule. Find a cycle with no red edges and at least one uncolored edge. Select an uncolored edge
of maximum weight from the cycle and color it red.
Prim’s algorithm builds a spanning tree incrementally. At each step, it selects a least-cost edge from
the set of edges that has one endpoint in the tree. These edges are in the cut that separates the vertices
that have been added to the tree from those that have not yet been added. Since the cut has no blue
edges and at least one uncolored edge, each step is a correct application of the blue rule. The edges that
are not in the tree when the algorithm ends are implicitly colored red. Since for each uncolored edge,
the cycle consisting of the edge and the tree path joining its endpoints is a cycle that the red rule
applies to, this implicit coloring is consistent with the general greedy method.
CS 541 – Algorithms and Programs
Final Exam Solutions
J
onathan Turne
r
12/13/01
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS Exam Solutions: Minimum Spanning Trees, Fibonacci Heaps, Dynamic Trees, and Algorithms and more Exams Data Structures and Algorithms in PDF only on Docsity!

Be neat and concise, but complete.

  1. (15 points) State the general greedy method for finding minimum spanning trees. Show that

Prim’s algorithm is a special case of the general greedy method.

The general greedy method applies one of the following two coloring steps until all edges are colored. On completion, the blue edges form a minimum spanning tree.

Blue rule. Find a cut crossed by no blue edges and at least one uncolored edge. Select a minimum weight uncolored edge crossing the cut and color it blue.

Red rule. Find a cycle with no red edges and at least one uncolored edge. Select an uncolored edge of maximum weight from the cycle and color it red.

Prim’s algorithm builds a spanning tree incrementally. At each step, it selects a least-cost edge from the set of edges that has one endpoint in the tree. These edges are in the cut that separates the vertices that have been added to the tree from those that have not yet been added. Since the cut has no blue edges and at least one uncolored edge, each step is a correct application of the blue rule. The edges that are not in the tree when the algorithm ends are implicitly colored red. Since for each uncolored edge, the cycle consisting of the edge and the tree path joining its endpoints is a cycle that the red rule applies to, this implicit coloring is consistent with the general greedy method.

CS 541 – Algorithms and Programs

Final Exam Solutions

Jonathan Turner 12/13/

  1. (15 points) A printout of a Fibonacci heap is shown below. Each set of values represents the

(item number, key and rank). Mark bits that are set are shown as an asterisk. 5 1 0 7 2 2 3 6 1 12 19 0 11 20 0 8 3 3 14 4 2* 2 4 1 4 9 0 1 5 0 15 8 1 6 10 0 10 5 1* 9 13 0

Which items are siblings of item 10? 14 and 15

What are the values of the four pointers associated with item 3 (identify each of the pointers)? parent is 7, left sibling is 11, right sibling is 11 and child is 12.

How many children did item 3 have when it first became a child of item 7? 1

How many children did item 10 have when it first became a child of item 8? 2

Suppose the item 2 is deleted from the heap. Modify the printout to show the effect of this.

The modified printout appears below.

  1. (15 points) How many changekey operations are performed by Dijkstra’s algorithm when

executed on the graph shown below with vertex v 1 as the source vertex?

3 changekeys are performed when v 2 is removed from the heap, 2 are performed when v 3 is removed and 1 when v 4 is. So the total is 6.

Generalize this example to obain an n vertex graph on which Dijkstra’s algorithm performs Ω( n^2 ) changekey operations. Draw a diagram of this graph and explain why Dijkstra’s algorithm does require Ω( n^2 ) changekey operations.

The generalized graph has vertices v 1 to v (^) n and edges from each vi to all vertices v (^) j with j>i. For edges of the form (v (^) i ,v (^) i+1 ), the weight is 1. For all other edges leaving v (^) i , the edge weight is 2(n-i-1). The graph is illustrated below.

When Dijkstra’s algorithm is run on this graph with v 1 as the source, the vertices are processed in numerical order. After vertex v (^) i is processed, the distance to vertex v (^) i+1 is i and the distance to vertices v (^) j with j>i+1 is (i-1)+2(n-i-1)=2n-i-3. (This is easily proved by induction.) When vertex vi+ is processed, the distance to vertex v (^) i+2 changes from 2n-i-3 to i+1 and the distance to vertices vj with j>i+2 changes to i+2(n-i-2)=2n-i-4. These changes in the distance result in changekey operations. The total number of changekey operations is (n-2)+(n-1)+... +1=(1/2)(n-2)(n-1)= Ω( n^2 ).

v 1 1 v 2 v 3 v 4 v 5

6

(^111)

6

6

4

2

4

v 1 1 v 2 v 3 v 4 v 5

6

(^111)

6

6

4

2

4

v 1 1 v 2 v 3 v (^) n -1 v (^) n

2( n -2)

(^11)

2

(^1 1) v (^) n -2 1

2( n -2) 2( n -2)

2( n -3)

2( n -3)

2( n -4)

v^... 1 1 v 2 v 3 v^ n -1 v^ n

2( n -2)

(^11)

2

(^1 1) v (^) n -2 1

2( n -2) 2( n -2)

2( n -3)

2( n -3)

2( n -4)

...

  1. (15 points) Let G be a directed graph and t be a designated destination vertex. Let T be a

reverse shortest path tree with destination t. That is, T is a spanning tree of G directed toward t in which all paths are shortest paths. Define p ( u ) be the first vertex (after u ) on the tree path from u to t. Let secondBest ( u ) be the first vertex (after u ) on a second-best path from u to t. More precisely, if v is secondBest ( u ), then vp ( u ), ( u , v ) is an edge and there is no other such vertex wp ( u ), for which the shortest path from u to t that starts with w is shorter than the shortest path from u to t that starts with v. Fill in the body of the C++ program shown below to compute secondBest ( u ) for all vertices ut. Your program should run in O ( m ) time, where m is the number or edges in G. void secondBest(wdigraph G, vertex p[], int d[], vertex sb[]) { // Return the second best next hop vertex of u in sb[u] for // all vertices u. p[u] is the parent of u in the shortest // path tree and d[u] is the length of the shortest path // from u to the destination. vertex u, v; edge e; int sblen for (u = 1; u != G.n; u++) { if (p[u] == Null) continue; sb[u] = Null; for (e = G.firstout(u); e!=Null; e = G.nextout(u,e)){ v = G.head(e); if (v != p[u] && (sb[u] == Null || G.w(e) + d[v] < sblen)) { sb[u] = v; sblen = G.w(e) + d[v]; } } } }

  1. (10 points). The figure below represents an instance of the minimum cost, maximum flow

problem. What is the cost of the flow that is shown? 46

Apply two steps of the minimum cost augmenting path algorithm. Show the changes resulting from the first step by modifying the first figure. Show the changes resulting from the second step by modifying the second figure.

The solution is shown in the second and third figures below, with changes highlighted.

3,__,

5,__,

4,__,

3,__,

1,__,

2,__,

2,__,

1,__,

4,__,

s

c f

t

h g

b

a

d

e

5,__,

4,__,

3,__,

1,__,

2,__,

2,__,

4,__,

s

c f

t

h g

b

a

d

e

3,__,

5,__,

2,__,

6,,4 3,,

4,__,

3,__,

1,__,

2,__,

2,__,

1,__,

4,__,

s

c f

t

h g

b

a

d

e

cap,flow,cost

3,__,

5,__,

4,__,

3,__,

1,__,

2,__,

2,__,

1,__,

4,__,

s

c f

t

h g

b

a

d

e

5,__,

4,__,

3,__,

1,__,

2,__,

2,__,

4,__,

s

c f

t

h g

b

a

d

e

3,__,

5,__,

2,__,

6,,4 3,,

4,__,

3,__,

1,__,

2,__,

2,__,

1,__,

4,__,

s

c f

t

h g

b

a

d

e

3,__,

5,__,

2,__,

6,,4 3,,

4,__,

3,__,

1,__,

2,__,

2,__,

1,__,

4,__,

s

c f

t

h g

b

a

d

e

cap,flow,cost

  1. (15 points) The figure below shows an intermediate state in the computation of the

Edmonds-Karp algorithm for finding a maximum size matching in a general graph., with blossoms indicated by the shaded areas.

Draw a picture of the partition data structure associated with this state of the computation (there is no single right answer here, but your answer should be consistent with the figure).

For each blossom, identify its bridge , by marking it with the letter B.

What is the augmenting path found if edge { g , l } is examined next? rbjpdqlgcehkfi

List the edges in the matching, after the corresponding augmentation is performed.

{r,b}, {j,p}, {d,q}, {g,l}, {c,e}, {h,k}, {i,f}, {m,n}

j c

d g i

k

b e h

f

l m n

−^ +

q

p

r

  • (^) + −

j c

d g i

k

b e h

f

l m n

−^ +

q

p

r

  • (^) + −

e c f k g h

n (^) m p

d b j

l

q

i r

ee cc ff kk gg hh

n (^) m p

d b j

l

q

i r

j c

d g i

k

b e h

f

l m n

−^ +

q

p

r

  • (^) + −

B

B B

B

j c

d g i

k

b e h

f

l m n

−^ +

q

p

r

  • (^) + −

j c

d g i

k

b e h

f

l m n

−^ +

q

p

r

  • (^) + −

B

B B

B