# 1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.

## Presentation on theme: "1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm."— Presentation transcript:

1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm

2 Definition of MST Let G=(V,E) be a connected, undirected graph. For each edge (u,v) in E, we have a weight w(u,v) specifying the cost (length of edge) to connect u and v. We wish to find a (acyclic) subset T of E that connects all of the vertices in V and whose total weight is minimized. Since the total weight is minimized, the subset T must be acyclic (no circuit). Thus, T is a tree. We call it a spanning tree. The problem of determining the tree T is called the minimum-spanning-tree problem.

3 Application of MST: an example In the design of electronic circuitry, it is often necessary to make a set of pins electrically equivalent by wiring them together. To interconnect n pins, we can use n-1 wires, each connecting two pins. We want to minimize the total length of the wires. Minimum Spanning Trees can be used to model this problem.

4 Here is an example of a connected graph and its minimum spanning tree: a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 Notice that the tree is not unique: replacing (b,c) with (a,h) yields another spanning tree with the same minimum weight.

5 Growing a MST(Generic Algorithm) Set A is always a subset of some minimum spanning tree. This property is called the invariant Property. An edge (u,v) is a safe edge for A if adding the edge to A does not destroy the invariant. A safe edge is just the CORRECT edge to choose to add to T. GENERIC_MST(G,w) 1A:={} 2while A does not form a spanning tree do 3find an edge (u,v) that is safe for A 4A:=A ∪ {(u,v)} 5return A

6 How to find a safe edge We need some definitions and a theorem. A cut (S,V-S) of an undirected graph G=(V,E) is a partition of V. An edge crosses the cut (S,V-S) if one of its endpoints is in S and the other is in V-S. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.

7 V-S↓ a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 S↑S↑ ↑ S ↓ V-S This figure shows a cut (S,V-S) of the graph. The edge (d,c) is the unique light edge crossing the cut.

8 Theorem 25.1 Let G=(V,E) be a connected, undirected graph with a real- valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S,V-S) be any cut of G such that for any edge (u, v) in A, {u, v}  S or {u, v}  (V-S). Let (u,v) be a light edge crossing (S,V-S). Then, edge (u,v) is safe for A. Proof: Let T opt be an minimum spanning tree.(Blue) A --a subset of T opt and (u,v)-- a light edge crossing (S, V-S) If (u,v) is NOT safe, then (u,v) is not in T. (See the red edge in Figure) There MUST be another edge (u’, v’) in T opt crossing (S, V-S).(Green) We can replace (u’,v’) in T opt with (u, v) and get another treeT’ opt Since (u, v) is light (the shortest edge connect crossing (S, V-S), T’ opt is also optimal. 1 2 1 11 11 1

9 Corollary 25.2 Let G=(V,E) be a connected, undirected graph with a real- valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let C be a connected component (tree) in the forest G A =(V,A). Let (u,v) be a light edge (shortest among all edges connecting C with others components) connecting C to some other component in G A. Then, edge (u,v) is safe for A. (For Kruskal’s algorithm)

10 The algorithms of Kruskal and Prim The two algorithms are elaborations of the generic algorithm. They each use a specific rule to determine a safe edge in line 3 of GENERIC_MST. In Kruskal's algorithm, –The set A is a forest. –The safe edge added to A is always a least-weight edge in the graph that connects two distinct components. In Prim's algorithm, –The set A forms a single tree. –The safe edge added to A is always a least-weight edge connecting the tree to a vertex not in the tree.

11 Kruskal's algorithm (basic part) 1(Sort the edges in an increasing order) 2A:={} 3while E is not empty do { 3 take an edge (u, v) that is shortest in E and delete it from E 4 if u and v are in different components then add (u, v) to A } Note: each time a shortest edge in E is considered.

12 Kruskal's algorithm (Fun Part, not required) MST_KRUSKAL(G,w) 1A:={} 2for each vertex v in V[G] 3do MAKE_SET(v) 4sort the edges of E by nondecreasing weight w 5for each edge (u,v) in E, in order by nondecreasing weight 6do if FIND_SET(u) != FIND_SET(v) 7thenA:=A ∪ {(u,v)} 8UNION(u,v) 9return A (Disjoint set is discussed in Chapter 21, Page 498)

13 Disjoint-Set (Chapter 21, Page 498) Keep a collection of sets S 1, S 2,.., S k, –Each S i is a set, e,g, S 1 ={v 1, v 2, v 8 }. Three operations –Make-Set(x)-creates a new set whose only member is x. –Union(x, y) –unites the sets that contain x and y, say, S x and S y, into a new set that is the union of the two sets. –Find-Set(x)-returns a pointer to the representative of the set containing x.

14 Our implementation uses a disjoint-set data structure to maintain several disjoint sets of elements. Each set contains the vertices in a tree of the current forest. The operation FIND_SET(u) returns a representative element from the set that contains u. Thus, we can determine whether two vertices u and v belong to the same tree by testing whether FIND_SET(u) equals FIND_SET(v). The combining of trees is accomplished by the UNION procedure. Running time O(|E| lg (|E|)). (The analysis is not required.)

15 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 The execution of Kruskal's algorithm (Moderate part) The edges are considered by the algorithm in sorted order by weight. The edge under consideration at each step is shown with a red weight number.

16 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

17 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

18 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

19 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

20 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

21 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

22 Prim's algorithm (basic part) MST_PRIM(G,w,r) 1.A={} 2.S:={r} (r is an arbitrary node in V) 3. Q=V-{r}; 4. while Q is not empty do { 5 take an edge (u, v) such that (1) u  S and v  Q (v  S ) and (u, v) is the shortest edge satisfying (1) 6 add (u, v) to A, add v to S and delete v from Q }

23 Prim's algorithm (Fun part, not required) MST_PRIM(G,w,r) 1for each u in Q do 2key[u]:=∞ 3parent[u]:=NIL 4 key[r]:=0 5Q  V[Q] 6while Q!={} do 7u:=EXTRACT_MIN(Q) 8for each v in Adj[u] do 9if v in Q and w(u,v)<key[v] 10thenparent[v]:=u 11key[v]:=w(u,v)

24 Grow the minimum spanning tree from the root vertex r. Q is a priority queue, holding all vertices that are not in the tree now. key[v] is the minimum weight of any edge connecting v to a vertex in the tree. parent[v] names the parent of v in the tree. When the algorithm terminates, Q is empty; the minimum spanning tree A for G is thus A={(v,parent[v]):v ∈ V-{r}}. Running time: O(|E|+|V|lg |V|). (Analysis is not required)

25 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 The execution of Prim's algorithm (moderate part) the root vertex

26 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

27 a b h cd e fg i 4 879 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

28 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8

29 a b h cd e fg i 4 87 9 10 14 4 2 2 6 1 7 11 8 Bottleneck spanning tree: A spanning tree of G whose largest edge weight is minimum over all spanning trees of G. The value of the bottleneck spanning tree is the weight of the maximum-weight edge in T. Theorem: A minimum spanning tree is also a bottleneck spanning tree.

30 2-optimal Euler path problem

31 Euler circuit in a directed graph: An Euler circuit in a directed graph is a directed circuit that visits every edge in G exactly once. v1 v2 v3 v6 v5 v4 Figure 1 a b c d e f g

32 Theorem 2: If all the vertices of a connected graph have equal in-degrees and out-degrees, then the graph has an Euler circuit. in-degree-- number of edges pointing to the node out-degree-- number of edges pointing out from the node.

33 2-path: A 2-path is a directed subgraph consisting of two consecutive edges. For example: v is called the midpoint. Every 2-path is given a cost (positive number) A Euler circuit of m edges contains m 2-paths,and the cost of the Euler circuit is the total weight of the m 2-paths. uvw

34 An Example: a b c d There are four 2-paths: (a, b), (b, c), (c, d) and (d, a). If c(a,b)=1, c(b, c)=2, c(c,d)=3, and c(d, a)=3, then the total cost of the four 2-paths is 1+2+3+3=9.

35 2-optimal Euler circuit problem: Instance: A directed graph, each of its vertices has in- degree at most 2 and out-degree at most 2, and 2-path has a cost. Question: Find an Euler circuit with the smallest cost among all possible Euler circuits. Back to the example of the last slide. –If c(a,b)=1, c(b, c)=2, c(c,d)=3, and c(d, a)=3, the total cost of the four 2-paths is 1+2+3+3=9.

36 An Euler circuit: An Euler circuit in the directed graph is: a, b, g, f, e, d, c, a. v1 v2 v3 v6 v5 v4 Figure 1 a b c d e f g The 2-paths are: (a, b), (b, g), (g, f), (f, e), (e,d), (d, c) and (c, a)

37 Line graph: If we view each edge in the above graph G(Figure 1) as a vertex, and each 2-path in the above graph as an edge, we get another graph L(G), called line graph. a c b g d f e

38 Continue: Observation: An Euler circuit in graph G corresponds to a Hamilton circuit in graph L(G). 2-optimal Euler path problem becomes a TSP problem in line graph. Theorem: TSP in line graph L(G), where G has in-degree at most 2 and out-degree at most 2, can be solved in polynomial time. (TSP in general is NP-complete)

39 Line graph: In the line graph, the corresponding Hamilton circuit is: a, b, g, f, e, d, c, a. Some edges in the line graph are not used, e.g., (b, c), and (d, g). a c b g d f e

Download ppt "1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm."

Similar presentations