Transcript

1

Minimum Spanning TreesMinimum Spanning Trees

Minimum- Spanning TreesMinimum- Spanning Trees

1. Concrete example: computer connection

2. Definition of a Minimum- Spanning Tree

Imagine: You wish to connect all the computers in an office building using the least amount of cable

- Each vertex in a graph G represents a computer- Each edge represents the amount of cable needed to connect all computers

Concrete exampleConcrete example

Spanning TreeSpanning Tree

A A spanning tree spanning tree of of GG is a subgraph whichis a subgraph which is tree (acyclic) and connect all the vertices in V.

Spanning tree has only |V| - 1 edges.

Problem: Laying Telephone WireProblem: Laying Telephone Wire

Central office

Wiring: Naive ApproachWiring: Naive Approach

Central office

Expensive!

Wiring: Better ApproachWiring: Better Approach

Central office

Minimize the total length of wire connecting the customers

A spanning tree of a graph is just a subgraph that contains all the

vertices and is a tree.

A graph may have many spanning trees.

or

or

or

Some Spanning Trees from Graph A

Graph A

Spanning Trees

All 16 of its Spanning Trees

Complete Graph

10

Total Number of Spanning TreesTotal Number of Spanning Trees A A completecomplete graph with graph with nn vertices has vertices has

nn ((nn-2)-2) spanning trees. spanning trees.((Cayley's formulaCayley's formula)) 5533 is 125 8 is 125 86 6 is 262144is 262144

101088 is 100 million 100 is 100 million 10098 98 is 10 is 10196196

Compare: there areCompare: there are31.5576*1031.5576*106 6 seconds in a year.seconds in a year.

A nanosecond is one billionth (10A nanosecond is one billionth (10-9-9) of a ) of a second. second. (An electrical signal can travel about 30cm in a (An electrical signal can travel about 30cm in a nanosecond.) There arenanosecond.) There are

31.5576*1031.5576*1015 15 nanoseconds in a year.nanoseconds in a year. We are not going to be able to find all spanning We are not going to be able to find all spanning

trees for large graphs even on the fastest computers, trees for large graphs even on the fastest computers, at least not in our lifetimes. We have to get smart at least not in our lifetimes. We have to get smart about trees.about trees.

11

Minimum Spanning TreeMinimum Spanning Tree

Input: Undirected connected graph G = (V, E) and weight

function w : E→R,

Output: A Minimum spanning tree T : tree that connects all

the vertices and minimizes

Greedy Algorithms Generic MST algorithm Kruskal’s algorithm Prim’s algorithm

( , )

( ) ( , )u v T

w T w u v

12 12

Hallmark for “greedy”Hallmark for “greedy”algorithmsalgorithms

Theorem. Theorem. Let Let T T be the MST of be the MST of G G = (= (VV, , EE)),,and let and let A A VV. Suppose that . Suppose that ((uu, , vv) ) ∈ ∈ E E is theis theleast-weight edge connecting least-weight edge connecting A A to to V V – – AA..Then, Then, ((uu, , vv) ) ∈ ∈ TT..

Greedy-choice propertyA locally optimal choice

is globally optimal.

13 13

Growing a Minimum Spanning Tree Growing a Minimum Spanning Tree (MST)(MST)

Generic algorithmGeneric algorithm Grow MST one edge at a time Manage a set of edges A, maintaining the

following loop invariant: Prior to each iteration, A is a subset of some MST

At each iteration, we determine an edge (u, v) that can be added to A without violate this invariant A {(u, v)} is also a subset of a MST (u, v) is called a safe edge for A

14 14

GENERIC-MSTGENERIC-MST

–Loop in lines 2-4 is executed |V| - 1 times

•Any MST tree contains |V| - 1 edges•The execution time depends on how

to find a safe edge

How to Find A Safe Edge?How to Find A Safe Edge?

Theorem 23.1. Let A be a subset of E that is Theorem 23.1. Let A be a subset of E that is included in some MST, let (S, V-S) be any included in some MST, let (S, V-S) be any cutcut of of G that G that respectsrespects A, and let (u, v) be a A, and let (u, v) be a light edgelight edge crossing (S, V-S). Then edge (u, v) is safe for Acrossing (S, V-S). Then edge (u, v) is safe for A Cut (S, V-S): a partition of V Crossing edge: one endpoint in S and the

other in V-S A cut respects a set of A of edges if no edges in A crosses the

cut A light edge crossing a cut if its weight is the minimum of any

edge crossing the cut

16 16

17

• A={(a,b}, (c, i}, (h, g}, {g, f}}

• S={a, b, c, i, e}; V-S = {h, g, f, d} many kinds of cuts satisfying

the requirements of Theorem 23.1

• (c, f) is the light edges crossing S and V-S and will be a safe edge

Illustration of Theorem 23.1Illustration of Theorem 23.1

18

Example: MSTExample: MST

19

Example: MSTExample: MST

20

Kruskal's AlgorithmKruskal's Algorithm

Edge based algorithm

Greedy strategy:

From the remaining edges, select a least-cost edge that does not result in a cycle when added to the set of already selected edges

Repeat |V|-1 times

21

Kruskal's AlgorithmKruskal's Algorithm

INPUT: edge-weighted graph G = (V, E), with |V| = n

OUTPUT: a spanning tree A of G

touches all vertices, has n-1 edges of minimum cost ( = total edge weight)

Algorithm: Start with A empty, Add the edges one at a time, in increasing weight order An edge is accepted it if connects vertices of distinct

trees (if the edge does not form a cycle in A) until A contains n-1 edges

22

Kruskal's AlgorithmKruskal's Algorithm

MST-Kruskal(G,w)1 A 2 for each vertex v V[G] do3 Make-Set(v) //creates set containing v (for initialization)4 sort the edges of E 5 for each (u,v)E do6 if Find-Set(u) Find-Set(v) then // different

component7 A A {(u,v)}8 Union(Set(u),Set(v)) // merge9 return A

23

Data Structures For Kruskal’s Algorithm

Does the addition of an edge (u, v) to T result in a cycle?

Each component of T is a tree. When u and v are in the

same component, the addition of the edge (u, v) creates a cycle.

different components, the addition of the edge (u, v) does not create a cycle.

1 3

2 4

2 47

57

68

36

24

Data Structures For Kruskal’s Data Structures For Kruskal’s Algorithm

Each component of T is defined by the vertices in the component.

Represent each component as a set of vertices. {1, 2, 3, 4}, {5, 6}, {7, 8}

Two vertices are in the same component iff they are in the same set of vertices.

1 35 7

2 46 8

234 67

25

Data Structures For Kruskal’s Data Structures For Kruskal’s Algorithm

When an edge (u, v) is added to T, the two components that have vertices u and v combine to become a single component

In our set representation of components, the set that has vertex u and the set that has vertex v are united. {1, 2, 3, 4} + {5, 6} {1, 2, 3, 4, 5, 6}

1 3 5 7

2 4 6 82

34 67

26

Kruskal’s AlgorithmKruskal’s Algorithm2 19

9

1

5

13

1725

148

21

2 19

9

1

5

13

1725

148

21

2 19

9

1

5

13

1725

148

21

2 19

9

1?

5

13

1725

148

21

2 19

9

1

5

13

1725

148

21

2? 19

9

1

5

13

1725

148

21

27

Kruskal’s AlgorithmKruskal’s Algorithm2 19

9

1

5

13

1725

148

21

2 19

9

1

5?

13

1725

148

21

2 19

9

1

5

13

1725

148

21

2 19

9

1

5

13

1725

148?

21

2 19

9

1

5

13

1725

14

21

2 19

9?

1

5

13

1725

148

21

28

Kruskal’s AlgorithmKruskal’s Algorithm2 19

9

1

5

13

1725

148

21

2 19

9

1

5

13?

1725

148

21

2 19

9

1

5

13

1725

148

21

2 19

9

1

5

13

1725

14?8

21

2 19

9

1

5

13

1725

148

21

2 19

9

1

5

13

17?25

148

21

29

Kruskal’s AlgorithmKruskal’s Algorithm2 19?

9

1

5

13

1725

148

21

2 19

9

1

5

13

1725

148

21?

2 19

9

1

5

13

1725?

148

21

2 19

9

1

5

13

1725

148

21

2 19

9

1

5

13

1725

148

21

30

C

FE

A B

D

5

64

3

4

2

1 2

3

2

31

C

FE

A B

D

5

64

3

4

2

1 2

3

2

32

C

FE

A B

D

5

64

3

4

2

1 2

3

2

33

C

FE

A B

D

5

64

3

4

2

1 2

3

2

34

C

FE

A B

D

5

64

3

4

2

1 2

3

2

35

C

FE

A B

D

5

64

3

4

2

1 2

3

2

cycle!!

36

C

FE

A B

D

5

64

3

4

2

1 2

3

2

37

C

FE

A B

D

5

64

3

4

2

1 2

3

2

38

C

FE

A B

D

3

2

1 2

2

minimum- spanning tree

39

Kruskal's AlgorithmKruskal's Algorithm

MST-Kruskal(G,w)1 A

2 for each vertex v V[G] do // takestakes O(O(VV)) 3 Make-Set(v)

4 sort the edges of E // takestakes OO((EE lg lg EE))

//// takestakes OO((EE)) 5 for each (u,v)E, in nondecreasing of weight do6 if Find-Set(u) Find-Set(v) then7 A A {(u,v)}8 Union(Set(u),Set(v))9 return A

40

Running Time of Kruskal’s Algorithm

Kruskal’s Algorithm consists of two stages. Initializing the set A in line 1 takes O(1) time. Sorting the edges by weight in line 4.

takes O(E lg E)

Performing |V| MakeSet() operations for loop in lines 2-3. |E| FindSet(), for loop in lines 5-8. |V| - 1 Union(), for loop in lines 5-8. which takes O(V + E)

The total running time is O(E lg E) We have lg │E│ = O(lg V) because # of E = V-1 So total running time becomes O(E lg V).

41

Prim’s AlgorithmPrim’s Algorithm

The tree starts from an arbitrary root vertex r and grows until the tree spans all the vertices in V.

At each step, Adds only edges that are safe for A. When algorithm terminates, edges in A form

MST.

Vertex based algorithm.

Grows one tree T, one vertex at a time

42

MST-PrimMST-Prim(G,w,r)(G,w,r) //G: graph with weight w and a root vertex r//G: graph with weight w and a root vertex r

1 1 forfor each u each u V[G]{ V[G]{2 key[u] 2 key[u] 33 p[u] p[u] NULL // parent of u NULL // parent of u }}4 key[r] 4 key[r] 0 05 5 Q = BuildMinHeap(V,key); // Q – vertices out of T// Q – vertices out of T6 6 whilewhile Q Q dodo7 u 7 u ExtractMin(Q) // making u part of T ExtractMin(Q) // making u part of T8 8 forfor each v each v Adj[u] Adj[u] dodo9 9 ifif v v Q and w(u,v) Q and w(u,v) key[v] key[v] thenthen10 p[v]10 p[v] u u11 key[v]11 key[v] w(u,v) w(u,v)

Prim’s AlgorithmPrim’s Algorithm

updating keys

• For each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree.

• key [v]= key [v]= ∞∞ if there is no edge and p [v] names parent of v in tree. if there is no edge and p [v] names parent of v in tree.• When algorithm terminates the min-priority queue Q is empty.When algorithm terminates the min-priority queue Q is empty.

43

Prim’s AlgorithmPrim’s Algorithm

Lines 1-5Lines 1-5 set the key of each vertex to set the key of each vertex to ∞ (except root r, whose ∞ (except root r, whose key is set to 0 first vertex processed). Also, set parent of each key is set to 0 first vertex processed). Also, set parent of each vertex to NULL, and initialize min-priority queue Q to contain vertex to NULL, and initialize min-priority queue Q to contain all vertices.all vertices.

Line 7Line 7 identifies a vertex u identifies a vertex u єє Q Q

Removing u from set Q adds it to set Q-V of vertices in tree, Removing u from set Q adds it to set Q-V of vertices in tree, thus adding (u, p[ u]) to A.thus adding (u, p[ u]) to A.

The The forfor loop of loop of lines 8-11lines 8-11 update update keykey and and pp fields of every fields of every vertex v adjacent to u but not in tree.vertex v adjacent to u but not in tree.

44

Run on example graph

1410

3

6 45

2

9

15

8

45

Run on example graph

1410

3

6 45

2

9

15

8

key[u] =

46

Run on example graph

0

1410

3

6 45

2

9

15

8

Pick a start vertex r

r

47

Run on example graph

0

1410

3

6 45

2

9

15

8

Red vertices have been removed from Q

u

48

Run on example graph

0

3

1410

3

6 45

2

9

15

8

Red arrows indicate parent pointers

u

49

Run on example graph

14

0

3

1410

3

6 45

2

9

15

8

u

50

Run on example graph

14

0

3

1410

3

6 45

2

9

15

8u

Extract_min from Q

51

Run on example graph

14

0 8

3

1410

3

6 45

2

9

15

8u

52

Run on example graph

10

0 8

3

1410

3

6 45

2

9

15

8u

53

Run on example graph

10

0 8

3

1410

3

6 45

2

9

15

8u

54

Run on example graph

10 2

0 8

3

1410

3

6 45

2

9

15

8u

55

Run on example graph

10 2

0 8 15

3

1410

3

6 45

2

9

15

8u

56

Run on example graph

10 2

0 8 15

3

1410

3

6 45

2

9

15

8

u

57

Run on example graph

10 2 9

0 8 15

3

1410

3

6 45

2

9

15

8

u

58

Run on example graph

10 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

59

Run on example graph

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

60

Run on example graph

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

61

Run on example graph

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

62

Run on example graph

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

63

Run on example graph

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

64

• What is the hidden cost in this code?

MST-Prim(G,w,r)1 for each u V[Q]2 key[u] 3 p[u] NULL 4 key[r] 05 Q = BuildHeap(V,key); ////Q – vertices out of T6 while Q do7 u ExtractMin(Q) // making u part of T8 for each v Adj[u] do9 if v Q and w(u,v) < key[v] then10 p[v] u11 key[v] w(u,v)

DecreaseKey(v, w(u,v));

Prim’s Running TimePrim’s Running Time

updating keys

while loop is executed |V| times

Decrease-Key is executed O(|E|) times

Extract-Min is executed |V| times

65

Prim’s Running TimePrim’s Running Time

Time complexity depends on data structure QBinary heap: O(E lg V):

BuildHeap takes O(log V) timenumber of “while” iterations: V

ExtractMin takes O(lg V) timetotal number of “for” iterations: E

DecreaseKey takes O(lg V) timeHence,

Time = log V + V.T(ExtractMin) + E.T(DecreaseKey)

Time = O(V lg V + E lg V) = O(E lg V)Since E V – 1 (because G is connected)

66

Minimum bottleneck spanning treeMinimum bottleneck spanning tree

A bottleneck edge is the highest weighted edge in a A bottleneck edge is the highest weighted edge in a spanning tree.spanning tree.

A spanning tree is a A spanning tree is a minimum bottleneck minimum bottleneck spanning treespanning tree (or  (or MBSTMBST) if the graph does not ) if the graph does not contain a spanning tree with a smaller bottleneck contain a spanning tree with a smaller bottleneck edge weight.edge weight.

A MST is necessarily a MBST, but a MBST is not A MST is necessarily a MBST, but a MBST is not necessarily a MST.necessarily a MST.


Top Related