Advanced Algorithms Lab 4 – Solutions of assignment

02/01/14&
Advanced Algorithms
Floriano Zini
Free University of Bozen-Bolzano
Faculty of Computer Science
Academic Year 2013-2014
Lab 4 – Solutions of
assignment
1&
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani
161
Exercises
02/01/14&
5.1. Consider the following graph.
6
A
1
2
2
E
5
B
F
1
4
5
Assignment 03
6
C
7
5
G
3
D
H
3
(a) What is the cost of its minimum spanning tree?
(b) How many minimum spanning trees does it have?
Exercise 5.2.b page 148 DPV
(c) Suppose Kruskal’s algorithm is run on this graph. In what order are the edges added to the
MST?
For each edge
this sequence,
give athe
cut that
justifies its addition.
Suppose
weinwant
to find
minimum
5.2. Suppose
we want to find
the minimum
tree of the
following graph.
spanning
tree
of thespanning
following
graph
A
1
8
4
E
B
5
2
6
F
C
2
6
1
G
3
D
1
4
1
H
(a) Run Prim’s algorithm; whenever there is a choice of nodes, always use alphabetic ordering
(e.g.,
start
from node A). algorithm
Draw a table showing
the intermediate
of thehow
cost array.
Run
Kruskal’s
on the
graph.values
Show
the
(b) Run
Kruskal’s algorithm
on the
same graph. Show
howat
theevery
disjoint-sets data structure
disjoint-sets
data
structure
looks
looks at every intermediate stage (including the structure of the directed trees), assuming
intermediate
stage (including the structure of the
path
compression is used.
directed trees), assuming path compression is used
5.3. Design a linear-time algorithm for the following task.
Input: A connected, undirected graph G.
Question: Is there an edge you can remove from G while still leaving G connected?
Can you reduce the running time of your algorithm to O(|V |)?
5.4. Show that if an undirected graph with n vertices has k connected components, then it has at
least n − k edges.
5.5. Consider an undirected graph G = (V, E) with nonnegative edge weights we ≥ 0. Suppose that
you have computed a minimum spanning tree of G, and that you have also computed shortest
paths to all nodes from a particular node s ∈ V .
Algorithms
Now suppose each edge weight is increased by 1: the144
new weights are we! = we + 1.
Assignment 03
Figure 5.4 Kruskal’s minimum spanning tree algorithm.
(a) Does the minimum spanning tree change? Give
an example
procedure
kruskal(G, w) where it changes or prove it
Input:
A connected undirected graph G = (V, E) with edge weights w
cannot
change.5.2.b page
Output:
A minimum spanning tree defined by the edges X
Exercise
e
for where
all u ∈ V : they change or prove they cannot
(b) Do
the shortest
148
DPV paths change? Give an example
makeset(u)
change.
Solution
Let’s run the Kruskal’s algorithm
1. 
X = {}
Sort the edges E by weight
for all edges {u, v} ∈ E, in increasing order of weight:
if find(u) "= find(v):
add edge {u, v} to X
union(u, v)
And whenever we add an edge, we are merging two components.
Call makeset(u) for all u ∈ V !
union(x, y): merge the sets containing x and y.
The final algorithm is shown in Figure 5.4. It uses |V | makeset, 2|E| find, and |V | − 1
union operations.
0 F0 G0 H0
data structure: A0 B0 C0 D0 E5.1.4
A data structure for disjoint sets
2. 
Initialize the solution !
X = {}
3. 
Union by rank
One way to store a set is as a directed tree (Figure 5.5). Nodes of the tree are elements of the
set, arranged in no particular order, and each has parent pointers that eventually lead up to
the root of the tree. This root element is a convenient representative, or name, for the set. It
is distinguished from the other elements by the fact that its parent pointer is a self-loop.
Sort edges in E by increasing weight !
Figure 5.5 A directed-tree representation of two sets {B, E} and {A, C, D, F, G, H}.
E
H
{(A,B):1,(D,G):1,(F,G):1,(H,G):1,(B,C):2,(C,G):2,
F
D
B
C
(C,D):3,(A,E):4,(D,H):4,(E,F):5,(B,F):6,(B,G):6,(A,F):8}
NB: (x,y):w means that the edge (x,y) has weight w
G
A
2&
02/01/14&
Assignment 03
Exercise 5.2.b page 148 DPV
Solution (cont.)
Loop over ordered edges
4. 
! 
(A,B)
find(A)≠find(B); no compression; X={(A,B)};
union(A,B)=({A0,B1},{A!B})
data structure: ({A0,B1},{A!B})
! 
(D,G)
find(D)≠find(G); no compression; X={(A,B),(D,G)};
union(D,G)=({D0,G1},{D!G})
data structure: ({A0,B1,D0,G1},{A!B,D!G})
! 
(F,G)
find(F)≠find(G); no compression; X={(A,B),(D,G),(F,G)};
union(F,G)=({D0,F0,G1}, {D!G,F!G})
data structure: ({A0,B1,D0,F0,G1},{A!B,D!G,F!G})
! 
(H,G)
find(H)≠find(G); no compression; X={(A,B),(D,G),(F,G),(H,G)};
union(H,G)=({D0,F0,H0,G1}, {D!G,F!G,H!G})
data structure: ({A0,B1,D0,F0,H0,G1},{A!B,D!G,F!G,H!G})
! 
(B,C)
find(B) ≠find(C); no compression; X={(A,B),(D,G),(F,G),(H,G),(B,C)}
union(B,C)=({A0,C0,B1}, {A!B,C!B})
data structure: ({A0,C0,B1,D0,F0,H0,G1},{A!B,C!B,D!G,F!G,H!G})
Assignment 03
Exercise 5.2.b page 148 DPV
Solution (cont.)
Loop over ordered edges
4. 
5. 
! 
(C,G)
find(C)≠find(G); no compression;
X={(A,B),(D,G),(F,G),(H,G),(B,C),(C,G)};
union(C,G)=({A0,C0,D0,F0,H0,B1,G2},{A!B,C!B,D!G,F!G,H!G,B!G})
data structure: ({A0,C0,B1,D0,F0,H0,G2},{A!B,C!B,D!G,F!G,H!G,B!G})
! 
(C,D)
find(C)=find(D); compression:({A0,C0,D0,F0,H0,B1,G2},{A!B,C!G,D!G,F!G,H!G,B!G})
X={(A,B),(D,G),(F,G),(H,G),(B,C),(C,G)};
data structure: ({A0,C0,D0,F0,H0,B1,G2},{A!B,C!G,D!G,F!G,H!G,B!G})
! 
(A,E)
find(A)≠find(E); compression:({A0,C0,D0,F0,H0,B1,G2},{A!G,C!G,D!G,F!G,H!G,B!G})
X={(A,B),(D,G),(F,G),(H,G),(B,C),(C,G),(A,E)}
union(A,E) = ({A0,C0,D0,F0,H0,E0,B1,G2},{A!G,C!G,D!G,F!G,H!G,B!G,E!G})
data structure: ({A0,C0,D0,F0,H0,E0,B1,G2},{A!B,C!G,D!G,F!G,H!G,B!G,E!G})
! 
(D,H),(E,F),(B,F),(B,G),(A,F)
find(x)=find(y); no compression;
X={(A,B),(D,G),(F,G),(H,G),(B,C),(C,G),(A,E)}
data structure: ({A0,C0,D0,F0,H0,E0,B1,G2},{A!G,C!G,D!G,F!G,H!G,B!G,E!G})
MST = ({A,B,C,D,E,F,G,H},{(A,B),(D,G),(F,G),(H,G),(B,C),(C,G),(A,E)})
cost(MST)=1+1+1+1+2+2+4=12
3&
02/01/14&
Assignment 03
Exercise 5.9 page 149 DPV (partially)
The following statements may or may not be correct. In each case,
either prove it (if it is correct) or give a counterexample (if it isn’t correct).
Always assume that the graph G = (V, E) is undirected. Do not assume that
edge weights are distinct unless this is specifically stated.
1. 
If graph G has more than |V|−1 edges, and there is a unique heaviest edge,
then this edge cannot be part of a minimum spanning tree
2. 
If G has a cycle with a unique heaviest edge e, then e cannot be part of any
MST
3. 
Let e be any edge of minimum weight in G. Then e must be part of some
MST
4. 
If the lightest edge in a graph is unique, then it must be part of every MST.
5. 
If e is part of some MST of G, then it must be a lightest edge across some
cut of G.
Assignment 03
Exercise 5.9 page 149 DPV (partially)
Solution
1. 
False, consider the case where the heaviest edge is a bridge (is the only
edge connecting two connected components of G).
2. 
True, consider removing e from the MST and adding another edge
belonging to the same cycle. Then we get e new tree with less total weight.
3. 
True, e will belong to the MST produced by Kruskal’s algorithm.
4. 
True, if not there exists a cycle connecting the two endpoints of e, so
adding e and removing another edge of the cycle, produces a lightest tree
5. 
True, consider the cut that has u in one side and v in the other,
where e=(u, v).
4&