Minimum Spanning Trees 1 Minimum Spanning Trees 1

  • Slides: 66
Download presentation
Minimum Spanning Trees 1

Minimum Spanning Trees 1

Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning

Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree

Concrete example Imagine: You wish to connect all the computers in an office building

Concrete example 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

Spanning Tree m A spanning tree of G is a subgraph which q is

Spanning Tree m A spanning tree of G is a subgraph which q is tree (acyclic) q and connect all the vertices in V. q Spanning tree has only |V| - 1 edges.

Problem: Laying Telephone Wire Central office

Problem: Laying Telephone Wire Central office

Wiring: Naive Approach Central office Expensiv e!

Wiring: Naive Approach Central office Expensiv e!

Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

Spanning Trees A spanning tree of a graph is just a subgraph that contains

Spanning Trees 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. Graph A Some Spanning Trees from Graph A o r o r

Complete Graph All 16 of its Spanning Trees

Complete Graph All 16 of its Spanning Trees

Total Number of Spanning Trees m m m A complete graph with n vertices

Total Number of Spanning Trees m m m A complete graph with n vertices has n (n-2) spanning trees. (Cayley's formula) 53 is 125 86 is 262144 108 is 100 million 10098 is 10196 Compare: there are 31. 5576*106 seconds in a year. A nanosecond is one billionth (10 -9) of a second. (An electrical signal can travel about 30 cm in a nanosecond. ) There are 31. 5576*1015 nanoseconds in a year. We are not going to be able to find all spanning trees for large graphs even on the fastest computers, at least not in our lifetimes. We have to get smart about trees. 10

Minimum Spanning Tree m Input: q Undirected connected graph G = (V, E) and

Minimum Spanning Tree m Input: q Undirected connected graph G = (V, E) and weight function w : E→R, m Output: q A Minimum spanning tree T : tree that connects all the vertices and minimizes m Greedy Algorithms q Generic MST algorithm q Kruskal’s algorithm q Prim’s algorithm 11

Hallmark for “greedy” algorithms Greedy-choice property A locally optimal choice is globally optimal. Theorem.

Hallmark for “greedy” algorithms Greedy-choice property A locally optimal choice is globally optimal. Theorem. Let T be the MST of G = (V, E), and let A V. Suppose that (u, v) ∈ E is the least-weight edge connecting A to V – A. Then, (u, v) ∈ T. 12 12

Growing a Minimum Spanning Tree (MST) m Generic algorithm q Grow MST one edge

Growing a Minimum Spanning Tree (MST) m Generic algorithm q Grow MST one edge at a time q Manage a set of edges A, maintaining the following loop invariant: Ø Prior to each iteration, A is a subset of some MST q 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 13 13

GENERIC-MST –Loop in lines 2 -4 is executed |V| - 1 times • Any

GENERIC-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 14 14

How to Find A Safe Edge? m Theorem 23. 1. Let A be a

How to Find A Safe Edge? m Theorem 23. 1. Let A be a subset of E that is included in some MST, let (S, V-S) be any cut of G that respects A, and let (u, v) be a light edge crossing (S, V-S). Then edge (u, v) is safe for A q Cut (S, V-S): a partition of V q Crossing edge: one endpoint in S and the other in V-S q q 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

16 16

Illustration of Theorem 23. 1 • A={(a, b}, (c, i}, (h, g}, {g, f}}

Illustration of Theorem 23. 1 • 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 17

Example: MST 18

Example: MST 18

Example: MST 19

Example: MST 19

Kruskal's Algorithm m Edge based algorithm m Greedy strategy: q From the remaining edges,

Kruskal's Algorithm m Edge based algorithm m Greedy strategy: q 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 q Repeat |V|-1 times 20

Kruskal's Algorithm m m INPUT: q edge-weighted graph G = (V, E), with |V|

Kruskal's Algorithm m m INPUT: q edge-weighted graph G = (V, E), with |V| = n OUTPUT: q a spanning tree A of G Ø touches all vertices, Ø has n-1 edges Ø of minimum cost ( = total edge weight) m Algorithm: q Start with A empty, q Add the edges one at a time, in increasing weight order q An edge is accepted it if connects vertices of distinct trees (if the edge does not form a cycle in A) q until A contains n-1 edges 21

Kruskal's Algorithm MST-Kruskal(G, w) 1 A ¬ Æ 2 for each vertex v Î

Kruskal's Algorithm MST-Kruskal(G, w) 1 A ¬ Æ 2 for each vertex v Î V[G] do 3 Make-Set(v) //creates set containing v (for initialization) 4 sort the edges of E 5 for each (u, v)ÎE do 6 if Find-Set(u) ¹ Find-Set(v) then // different component 7 A ¬ A {(u, v)} 8 Union(Set(u), Set(v)) // merge 9 return A 22

Data Structures For Kruskal’s Algorithm Does the addition of an edge (u, v) to

Data Structures For Kruskal’s Algorithm Does the addition of an edge (u, v) to T result in a cycle? m Each component of T is a tree. q When u and v are in the m Øsame component, the addition of the edge (u, v) creates a cycle. 1 3 7 2 4 Ødifferent components, the addition of the edge (u, v) does not create a cycle. 5 7 6 6 23 3 8

Data Structures For Kruskal’s Algorithm m Each component of T is defined by the

Data Structures For Kruskal’s Algorithm m Each component of T is defined by the vertices in the component. Represent each component as a set of vertices. q {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 2 2 7 3 5 6 7 3 4 6 8 4 24

Data Structures For Kruskal’s Algorithm m m When an edge (u, v) is added

Data Structures For Kruskal’s Algorithm m m 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. q {1, 2, 3, 4} + {5, 6} {1, 2, 3, 4, 5, 6} 1 2 2 7 3 5 6 7 3 4 6 8 4 25

Kruskal’s Algorithm 26

Kruskal’s Algorithm 26

Kruskal’s Algorithm 27

Kruskal’s Algorithm 27

Kruskal’s Algorithm 28

Kruskal’s Algorithm 28

Kruskal’s Algorithm 29

Kruskal’s Algorithm 29

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 30 F

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 31 F

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 32 F

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 33 F

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 34 F

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 35 F cycle!!

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 36 F

5 A 4 6 2 C B 2 1 3 3 D E 2

5 A 4 6 2 C B 2 1 3 3 D E 2 4 37 F

minimum- spanning tree A B 2 2 C D 1 3 E 2 F

minimum- spanning tree A B 2 2 C D 1 3 E 2 F 38

Kruskal's Algorithm MST-Kruskal(G, w) 1 A ¬ Æ 2 for each vertex v Î

Kruskal's Algorithm MST-Kruskal(G, w) 1 A ¬ Æ 2 for each vertex v Î V[G] do // 3 Make-Set(v) 4 sort the edges of E // takes O(V) O(E lg E) // takes O(E) 5 for each (u, v)ÎE, in nondecreasing of weight do 6 if Find-Set(u) ¹ Find-Set(v) then 7 A ¬ A {(u, v)} 8 Union(Set(u), Set(v)) 9 return A 39

Running Time of Kruskal’s Algorithm m Kruskal’s Algorithm consists of two stages. q Initializing

Running Time of Kruskal’s Algorithm m Kruskal’s Algorithm consists of two stages. q Initializing the set A in line 1 takes O(1) time. q Sorting the edges by weight in line 4. Ø takes O(E lg E) q Performing Ø |V| Make. Set() operations Ø |E| Find. Set(), Ø |V| - 1 Union(), Ø which takes O(V + E) m for loop in lines 2 -3. for loop in lines 5 -8. The total running time is q O(E lg E) q We have lg │E│ = O(lg V) because # of E = V-1 q So total running time becomes O(E lg V). 40

Prim’s Algorithm m The tree starts from an arbitrary root vertex r and grows

Prim’s Algorithm m The tree starts from an arbitrary root vertex r and grows until the tree spans all the vertices in V. m At each step, q Adds only edges that are safe for A. q When algorithm terminates, edges in A form MST. m Vertex based algorithm. m Grows one tree T, one vertex at a time 41

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

Prim’s Algorithm MST-Prim(G, w, r) //G: graph with weight w and a root vertex r 1 for each u Î V[G]{ 2 key[u] ¬ ¥ 3 p[u] ¬ NULL // parent of u } 4 key[r] ¬ 0 5 Q = Build. Min. Heap(V, key); // Q – vertices out of T 6 while Q ¹ Æ do 7 u ¬ Extract. Min(Q) // making u part of T 8 for each v Î Adj[u] do updating 9 if v Î Q and w(u, v) key[v] then keys 10 p[v] ¬ u 11 key[v] ¬ w(u, v) • For each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree. • key [v]= ∞ if there is no edge and p [v] names parent of v in tree. • When algorithm terminates the min-priority queue Q is empty. 42

Prim’s Algorithm m Lines 1 -5 set the key of each vertex to ∞

Prim’s Algorithm m Lines 1 -5 set the key of each vertex to ∞ (except root r, whose key is set to 0 first vertex processed). Also, set parent of each vertex to NULL, and initialize min-priority queue Q to contain all vertices. m Line 7 identifies a vertex u є Q m Removing u from set Q adds it to set Q-V of vertices in tree, thus adding (u, p[ u]) to A. m The for loop of lines 8 -11 update key and p fields of every vertex v adjacent to u but not in tree. 43

Run on example graph 44

Run on example graph 44

Run on example graph 45

Run on example graph 45

Run on example graph 46

Run on example graph 46

Run on example graph 47

Run on example graph 47

Run on example graph 48

Run on example graph 48

Run on example graph 49

Run on example graph 49

Run on example graph Extract_min from Q 50

Run on example graph Extract_min from Q 50

Run on example graph 51

Run on example graph 51

Run on example graph 52

Run on example graph 52

Run on example graph 53

Run on example graph 53

Run on example graph 54

Run on example graph 54

Run on example graph 55

Run on example graph 55

Run on example graph 56

Run on example graph 56

Run on example graph 57

Run on example graph 57

Run on example graph 58

Run on example graph 58

Run on example graph 59

Run on example graph 59

Run on example graph 60

Run on example graph 60

Run on example graph 61

Run on example graph 61

Run on example graph 62

Run on example graph 62

Run on example graph 63

Run on example graph 63

Prim’s Running Time What is the hidden cost in this code? Extract-Min is executed

Prim’s Running Time What is the hidden cost in this code? Extract-Min is executed |V| times MST-Prim(G, w, r) Decrease-Key is executed O(|E|) times 1 for each u Î V[Q] 2 key[u] ¬ ¥ 3 p[u] ¬ NULL while loop is executed |V| times 4 key[r] ¬ 0 5 Q = Build. Heap(V, key); //Q // – vertices out of T 6 while Q ¹ Æ do 7 u ¬ Extract. Min(Q) // making u part of T 8 for each v Î Adj[u] do 9 if v Î Q and w(u, v) < key[v] then updating 10 p[v] ¬ u keys 11 key[v] ¬ w(u, v) • Decrease. Key(v, w(u, v)); 64

Prim’s Running Time m Time complexity depends m Binary heap: O(E lg V): on

Prim’s Running Time m Time complexity depends m Binary heap: O(E lg V): on data structure Q q Build. Heap takes O(log V) time q number of “while” iterations: V ØExtract. Min takes O(lg V) time q total number of “for” iterations: E ØDecrease. Key takes O(lg V) time m Hence, q Time = log V + V. T(Extract. Min) + E. T(Decrease. Key) q Time = O(V lg V + E lg V) = O(E lg V) ØSince E V – 1 (because G is connected) 65

Minimum bottleneck spanning tree m m m A bottleneck edge is the highest weighted

Minimum bottleneck spanning tree m m m A bottleneck edge is the highest weighted edge in a spanning tree. A spanning tree is a minimum bottleneck spanning tree (or MBST) if the graph does not contain a spanning tree with a smaller bottleneck edge weight. A MST is necessarily a MBST, but a MBST is not necessarily a MST. 66