Download presentation
Presentation is loading. Please wait.
Published byOpal Walsh Modified over 9 years ago
1
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees
2
1.2 Data Structure and Algorithm Minimum Spanning Tree(MST) In circuit design, sometimes we need to make short some of the pins of the circuit by connecting them with wire. To interconnect a set of n pins, we can use an arrangement of n-1 wires, each connecting 2 pins. Of all arrangements, the one that uses the least amount of wire is usually the most desirable. We can model this wiring problem with a connected, undirected weighted graph G(V,E). We will find an acyclic subset T from E that connects all the vertices and whose total weight is minimized. Since T is acyclic and connects all vertices, it is called a spanning tree. The spanning tree which has minimum weight is called minimum spanning tree.
3
1.3 Data Structure and Algorithm Spanning Tree Example 2 5 1 3 4 6 2 1 4 2 1 3 5 1 4 A connected, undirected Graph Many possible Spanning tree
4
1.4 Data Structure and Algorithm Minimum Spanning Tree Example 2 5 1 3 4 6 2 1 3 A weighted Graph Minimum Spanning tree
5
1.5 Data Structure and Algorithm Some Definition A cut ( S, V-S ) of an undirected graph G =(V,E) is a partition of V. An edge (u,v) crosses the cut ( S, V-S ) if one of its endpoints is in S and the other is in V-S A cut respects the set A of edges if no edge in A crosses the cut. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut S V-S
6
1.6 Data Structure and Algorithm Generic MST Generic_MST(G,w) A = 0 //empty set while A does not form a spanning tree do find an edge (u,v) that is safe for A A = A U {(u,v)} return A
7
1.7 Data Structure and Algorithm Kruskal’s Algorithm This is a greedy algorithm. A greedy algorithm chooses some local optimum (ie. picking an edge with the least weight in a MST). Kruskal's algorithm works as follows: Take a graph with 'n' vertices, keep adding the shortest (least cost) edge, while avoiding the creation of cycles, until (n - 1) edges have been added. NOTE: Sometimes two or more edges may have the same cost. The order in which the edges are chosen, in this case, does not matter. Different MSTs may result, but they will all have the same total cost, which will always be the minimum cost
8
1.8 Data Structure and Algorithm Kruskal’s Algorithm Example(1) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
9
1.9 Data Structure and Algorithm Kruskal’s Algorithm Example(2) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
10
1.10 Data Structure and Algorithm Kruskal’s Algorithm Example(3) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
11
1.11 Data Structure and Algorithm Kruskal’s Algorithm Example(4) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
12
1.12 Data Structure and Algorithm Kruskal’s Algorithm Example(5) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
13
1.13 Data Structure and Algorithm Kruskal’s Algorithm Example(6) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
14
1.14 Data Structure and Algorithm Kruskal’s Algorithm Example(7) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
15
1.15 Data Structure and Algorithm Kruskal’s Algorithm Example(8) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
16
1.16 Data Structure and Algorithm Kruskal’s Algorithm Example(9) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
17
1.17 Data Structure and Algorithm Kruskal’s Algorithm Example(10) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
18
1.18 Data Structure and Algorithm Kruskal’s Algorithm Example(11) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
19
1.19 Data Structure and Algorithm Kruskal’s Algorithm Example(12) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
20
1.20 Data Structure and Algorithm Kruskal’s Algorithm Example(13) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
21
1.21 Data Structure and Algorithm Kruskal’s Algorithm Example(14) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
22
1.22 Data Structure and Algorithm Kruskal’s Algorithm MST_KRUSKAL ( G, w ) A = 0 //empty set for each vertex of V[G] do Make_Set(v) Sort the edges of E by nondecreasing weight w for each edge (u,v) of E, in order by nondecreasing weight do if Find_Set(u) <> Find_Set(v) then A = A U {(u,v)} Union (u,v) return A
23
1.23 Data Structure and Algorithm Prim’s Algorithm This algorithm builds the MST one vertex at a time. It starts at any vertex in a graph (vertex A, for example), and finds the least cost vertex (vertex B, for example) connected to the start vertex. Now, from either 'A' or 'B', it will find the next least costly vertex connection, without creating a cycle (vertex C, for example). Now, from either 'A', 'B', or 'C', it will find the next least costly vertex connection, without creating a cycle, and so on it goes. Eventually, all the vertices will be connected, without any cycles, and an MST will be the result. NOTE: Two or more edges may have the same cost, so when there is a choice by two or more vertices that is exactly the same, then one will be chosen, and an MST will still result
24
1.24 Data Structure and Algorithm Prim’s Algorithm Example(1) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
25
1.25 Data Structure and Algorithm Prim’s Algorithm Example(2) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
26
1.26 Data Structure and Algorithm Prim’s Algorithm Example(3) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
27
1.27 Data Structure and Algorithm Prim’s Algorithm Example(4) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
28
1.28 Data Structure and Algorithm Prim’s Algorithm Example(5) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
29
1.29 Data Structure and Algorithm Prim’s Algorithm Example(6) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
30
1.30 Data Structure and Algorithm Prim’s Algorithm Example(7) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
31
1.31 Data Structure and Algorithm Prim’s Algorithm Example(8) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
32
1.32 Data Structure and Algorithm Prim’s Algorithm Example(9) 8 14 10 9 8 4 1 7 2 11 6 4 2 7
33
1.33 Data Structure and Algorithm Prim’s Algorithm MST_PRIM (G,w,r) // r = starting node Q = V[G] //priority queue containing vertex for each u of Q do key[u] = infinite key[r] = 0 pre[r] = NIL //pre = parent while Q is not empty do u = ExtractMin(Q) for each v of Adj[u] do if v in Q and w(u,v) <key[v] then pre[v] = u key[v] = w(u,v)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.