1.1 data structure and algorithm lecture 13 minimum spanning trees topics reference: introduction to...
TRANSCRIPT
1.1Data Structure and Algorithm
Lecture 13
Minimum Spanning Trees
Topics
Reference: Introduction to Algorithm by Cormen
Chapter 13: Minimum Spanning Trees
1.2Data 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.
1.3Data 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
1.4Data Structure and Algorithm
Minimum Spanning Tree Example
2
5
1
3
4
6
2
1
3
A weighted Graph
Minimum Spanning tree
1.5Data 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
1.6Data 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
1.7Data 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
1.22Data 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
1.23Data 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
1.33Data 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)