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

Minimum Spanning Trees and Data Structures, Exams of Data Structures and Algorithms

The general greedy method for finding minimum spanning trees, focusing on kruskal's algorithm as a special case. It also discusses the properties that must be maintained in the d-heap data structure and compares the running time of prim's algorithm using d-heaps and fibonacci heaps. Lastly, it explains how to modify a minimum spanning tree when an edge weight is changed.

Typology: Exams

2012/2013

Uploaded on 03/23/2013

sardai
sardai 🇮🇳

4

(10)

117 documents

1 / 7

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
Kruskal’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.
Kruskal’s algorithm goes through the edges in non-decreasing order of their weight. On finding an
edge that joins two separate blue trees, it colors it blue. This is a proper application of the blue rule,
since the cut defined by placing one of the blue trees on one side of the cut and all other vertices on
the other side, constitutes a cut that we can apply the blue rule to. Since the edge under consideration
is a minimum weight uncolored edge, it is also minimum weight among edges crossing the cut.
When Kruskal’s algorithm finds an edge joining two vertices in the same blue tree, it colors it red.
This is a proper application of the red rule, since the edge and the tree path joining its endpoints form
a cycle that the red rule can be applied to. Since the edge being considered, is the only uncolored edge,
in the cycle, it must be a maximum weight uncolored edge in the cycle.
2. (10 points) The correctness of any data structure operation depends on its maintaining
certain essential properties of the data structure. The data portion of the class declaration
for the C++ implementation of the d-heap data structure is shown below. What properties
of the data must be maintained by programs that operate on it?
class dheap {
int N; // max number of items in heap
int n; // number of items in heap
int d; // base of heap
item *h; // {h[1],...,h[n]} is set of items
int *pos; // pos[i] gives position of i in h
keytyp *kvec; // kvec[i] is key of item i
...
};
d>1, 0
n
N, for 2
i
n, kvec
[
(i-1)/d
]
kvec[i], for 1
i
n, pos[h[i]] = i, 1
h[i]
n,
1
pos[i]
n.
CS 541 – Algorithms and Programs
Exam 1 Solutions
J
onathan Turner
10/4/01
pf3
pf4
pf5

Partial preview of the text

Download Minimum Spanning Trees and Data Structures 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 Kruskal’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.

Kruskal’s algorithm goes through the edges in non-decreasing order of their weight. On finding an edge that joins two separate blue trees, it colors it blue. This is a proper application of the blue rule, since the cut defined by placing one of the blue trees on one side of the cut and all other vertices on the other side, constitutes a cut that we can apply the blue rule to. Since the edge under consideration is a minimum weight uncolored edge, it is also minimum weight among edges crossing the cut. When Kruskal’s algorithm finds an edge joining two vertices in the same blue tree, it colors it red. This is a proper application of the red rule, since the edge and the tree path joining its endpoints form a cycle that the red rule can be applied to. Since the edge being considered, is the only uncolored edge, in the cycle, it must be a maximum weight uncolored edge in the cycle.

  1. (10 points) The correctness of any data structure operation depends on its maintaining certain essential properties of the data structure. The data portion of the class declaration for the C++ implementation of the d -heap data structure is shown below. What properties of the data must be maintained by programs that operate on it? class dheap { int N; // max number of items in heap int n; // number of items in heap int d; // base of heap item *h; // {h[1],...,h[n]} is set of items int *pos; // pos[i] gives position of i in h keytyp *kvec; // kvec[i] is key of item i ... };

d>1, 0 ≤ n ≤ N, for 2 ≤ i ≤ n, kvec [ (i-1)/d  ] ≤ kvec[i], for 1 ≤ i ≤ n, pos[h[i]] = i, 1 ≤ h[i] ≤ n,

1 ≤ pos[i] ≤ n.

CS 541 – Algorithms and Programs

Exam 1 Solutions

Jonathan Turner 10/4/

  1. (12 points) Let Pd ( n ) denote the running time of Prim’s algorithm using d -heaps, where the value of d is chosen dynamically to give the best overall running time. Let PF (n) denote the running time of Prim’s algorithm, using Fibonacci heaps. Which of the following statements is true? Justify your answers.

Pd is O ( PF ) when m = 3 n.

This is true, since P (^) d = O ( m (log n ) / log(2+ m/n )) = O ( n log n ) and PF = Ω( m + n log n ) = Ω( n log n ).

Pd is O ( PF ) when m = n^2 /4.

This is true, since P (^) d = O ( m (log n ) / log(2+ m/n )) = O ( n^2 ) and PF = Ω( m + n log n ) = Ω( n^2 ).

Pd is O ( PF ) when m = n (log n ) 2.

This is false, since P (^) d = Ω( m (log n ) / log(2+ m/n )) = Ω( n (log n ) 3 /log log n ) and PF = O ( m + n log n ) = O ( n (log n ) 2 ) and n (log n ) 3 /log log n grows more quickly than n (log n ) 2 does.

Pd is O( PF ) when m = n3/^2.

This is true, since P (^) d = O ( m (log n ) / log(2+ m/n )) = O ( n 3/2^ ) and PF = Ω( m + n log n ) = Ω( n 3/2^ ).

  1. (10 points) The figure below shows a Fibonacci heap. Show the state of the data structure after performing a deletemin operation.

c 7,0 q 8,

a 4,3 p 6,

n 7,0 e 5,0 g 9,0 h 8,

f 5,

key , rank

r 7,0 i 5,2 o 6,

m 7,1 k 5,

!! !!

!!!!

!!!!

!!!!

s 6,

!!!! denotes a set mark bit

d 2,3 l^ 4,2^ b 3,

cc 7,07,0 qq 8,08,

aa 4,34,3 pp 6,06,

nn 7,07,0 ee 5,05,0 gg 9,09,0 hh 8,28,

ff 5,15,

key , rank

rr 7,07,0 ii 5,25,2 oo 6,06,

mm 7,17,1 kk 5,05,

!! !!

!!!!

!!!!

!!!!

ss 6,06,

!!!! denotes a set mark bit

dd 2,32,3 ll^ 4,24,2^ bb 3,13,

c 7,0 q 8,

a 4,

n 7,0 e 5,0 g 9,0 h 8,

f 5,

r 7,0 i 5,

o 6,

m 7,1 k 5,

!!!!

!!!! !!!!

!!!! p 6,

s 6,

l 4,

b 3,

cc 7,07,0 qq 8,08,

aa 4,34,

nn 7,07,0 ee 5,05,0 gg 9,09,0 hh 8,28,

ff 5,15,

rr 7,07,0 ii 5,25,

oo 6,06,

mm 7,17,1 kk 5,05,

!!!!

!!!! !!!!

!!!!!!!! (^) ppp 6,06,06,

ss 6,06,

ll 4,24,

bb 3,13,

  1. (15 points) Suppose that in the amortized analysis of the partition data structure with path compression, we defined level ( x ) = ( rank ( p ( x )) − rank ( x )) 1/2. Give an upper bound on the final value of fcredit for this case, by modifying the original analysis. Show the details of your modified analysis. Also, give a lower bound on rank ( p ( x )) in terms of level ( x ) and rank ( x ).

Since rank(x) ≤ lg n, it follows the level(x) ≤ (lg n) 1/2^. Since it is also non-negative, there are

≤ 1+(lg n) 1/2^ distinct values that level(x) can have. Since fcredit is incremented only for vertices x

that have no proper ancestor y with level(y)=level(x), the number of different vertices for which fcredit is incremented during a top level find is at most equal to the number of distinct values of the

level function. Thus, after m operations on the partition data structure, fcredit ≤ m(1+(lg n) 1/2^ ).

From the definition of level(x), we have level(x)1+ ( rank ( p ( x )) − rank ( x )) 1/2^. So,

rank(p(x)) ≥ rank(x) + (level(x) – 1) 2.