lecture 16

19
Lecture 12 Minimum Spanning Tree

Transcript of lecture 16

Page 1: lecture 16

Lecture 12

Minimum Spanning Tree

Page 2: lecture 16

Motivating Example: Point to Multipoint Communication

• Single source, Multiple Destinations• Broadcast

– All nodes in the network are destinations

• Multicast– Some nodes in the network are destinations

• Only one copy of the information travels along common edges

Message replication along forking points only.

1

2

Page 3: lecture 16

Spanning Tree

• We consider undirected graphs here.

• A tree is a connected graph without a cycle

• A spanning tree is a tree which has all vertices of the graph

• There may be multiple spanning trees

• We need to choose the minimum weight tree for broadcast.

Page 4: lecture 16

54

45 Blue edge spanning tree is the minimum weight spanning tree

Page 5: lecture 16

Properties of a Tree

• A tree of V vertices has V-1 edges

• There exists a unique path between any two vertices of a tree.

• Adding any edge to a tree creates a unique cycle. Breaking any edge on this cycle restores a tree.

Page 6: lecture 16

Minimum Spanning Tree Construction

• We maintain a set of edges A, which is initially empty.

• Edges are added to A one at a time such that finally A becomes a minimum spanning tree.

• Edges are never removed from A.• So ``safe’’ edges must be added to A, i.e., at any

stage A must be a part of a spanning tree.

Page 7: lecture 16

Safe Edge Addition

• Consider a cut in a graph (a cut consists of 2 sets which partition the vertex set).

•A cut respects a set of edges I if no edge in the set crosses the cut.

•A minimum weight edge crossing a cut is denoted a light weight edge in the cut

{A,B,C} {D,E,F} constitute a cut.

Let I = {edge AB, edge EF}.Cut {A,B,C}, {D,E,F} respects set I

A B

C D

E F2

1

3 1

Edge CD is a light weight edge in the example cut.

Page 8: lecture 16

• Let A be a subset of a MST (minimum weight spanning tree).

• Let (S, V – S) be any cut that respects A

• Let edge (u, v) be a light edge crossing the cut

• Then A (u, v) is subset of a MST

Page 9: lecture 16

Assume that all the edge weights are distinct.

Let no MST containing A contain edge (u, v).

Add the edge (u, v) to T

Consider a MST T containing A

Since (u, v) is not in T, T (u, v) contains a cycle, and (u, v) is in the cycle.

Edge (u, v) are in the opposite sides of the cut. Since any cycle must cross the cut even number of times, there exists at least one other edge (x, y) crossing the cut. Clearly, w(x, y) > w(u, v).

Page 10: lecture 16

The edge (x, y) is not in A because (x, y) crosses the cut, and the cut respects A.

Removing (x, y) from the cycle, breaks the cycle and hence

creates a spanning tree, T’, s.t. T’ = T (u, v) – (x, y)

w(T’) = w(T) + w(u, v) – w(x, y)

w(T) (as w(u, v) < w(x, y))

This contradicts the fact that T is a MST.

Page 11: lecture 16

• So we always find a cut that respects A,

• And add a light edge across the cut to A.

• Kruskals and Prims algorithms find the cut differently.

Page 12: lecture 16

Kruskals Algorithm

• A = 1. For each vertex u in V, Create_Set(u)2. Sort E in increasing order by weight w3. For each edge (u,v) in the sorted list

– If Set(u)= Set(v)• Add (u,v) to A• Union Set(u) and Set(v)

• Return A;

Page 13: lecture 16

Complexity Analysis

• The operations create set, testing whether set(u) == set(v), union operations can be done in log V operations, using Union find data structure.

• Step 1 can be done in Vlog V• Step 2 can be done in Elog E• Step 3 can be done in Elog V• Overall complexity is O(Vlog V + Elog E + Elog

V) or O((V + E)log V)

Page 14: lecture 16

5

1

0 2

28

5

75

1

0 2

28

5

7

5

1

0 2

28

5

7

5

1

0 2

28

5

7

5

1

0 2

28

5

7

Page 15: lecture 16

Prims Algorithm

• Maintains a set of vertices S already in the spanning tree.

• Initially, S consists of one vertex r, selected arbitrarily.

• For every vertex u in V – S, maintain the weight of the lightest edge between u and any vertex in S.

• If there is no edge between u and S, then this weight associated with u is infinity.

Page 16: lecture 16

• Add the vertex with the least weight to S.

• This is in effect adding the light weight edge crossing the cut S and V-S.

• Whenever a vertex is added to S, the weights of all its neighbors are reduced, if necessary.

Page 17: lecture 16

Pseudo-Code

For each u in V, key[u] =

S =

Pred[r] = NULL

Key[r] = 0

While V = S

u = Extract_Min(V-S)

For each (v in Adj(u))

if (v not in S) key(v) = min(w(u, v), key(v))

and pred(v) = u

Add u in S

Page 18: lecture 16

For each v in Adj[u]…. can be done in E complexity

Rest of the loop can be done in V2 complexity

So, overall O(V2)

Using heaps we can solve in O((V + E)logV)

Page 19: lecture 16

Example

0

s

7

5

7

2

1

8

5

3

2

53

1

s

0 2

8

5

7

2

85s

0

73

1

52

3

5

8

0

3

2 5

s7

2

1

85

5

3 1

85

s

0

7

2

3

1

52

3

5

1

3 85

s

0

7

2

1

52

3

5

1