0% found this document useful (0 votes)
55 views

Topics: Minimum Spanning Trees

The document discusses minimum spanning trees and algorithms for finding them, including Kruskal's and Prim's algorithms. Kruskal's algorithm works by sorting the edges by weight and building the spanning tree by adding the shortest edges that do not create cycles. Prim's algorithm starts at one vertex and builds out the minimum spanning tree by always adding the closest edge from the growing tree. Both algorithms are illustrated through examples on weighted graphs.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Topics: Minimum Spanning Trees

The document discusses minimum spanning trees and algorithms for finding them, including Kruskal's and Prim's algorithms. Kruskal's algorithm works by sorting the edges by weight and building the spanning tree by adding the shortest edges that do not create cycles. Prim's algorithm starts at one vertex and builds out the minimum spanning tree by always adding the closest edge from the growing tree. Both algorithms are illustrated through examples on weighted graphs.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture 13

Topics
Minimum Spanning Trees

Reference: Introduction to Algorithm by Cormen


Chapter 13: Minimum Spanning Trees

Data Structure and Algorithm

1.1

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.

Data Structure and Algorithm

1.2

Spanning Tree Example


2
4
3

5
6

A connected,
undirected Graph

2
4

4
1

1
Many possible Spanning tree

Data Structure and Algorithm

1.3

Minimum Spanning Tree Example


2
4
3

5
6
1

2
3
1
Minimum Spanning tree
Data Structure and Algorithm

1.4

A weighted Graph

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
Data Structure and Algorithm

1.5

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

Data Structure and Algorithm

1.6

Kruskals 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

Data Structure and Algorithm

1.7

Kruskals Algorithm Example(1)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.8

Kruskals Algorithm Example(2)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.9

Kruskals Algorithm Example(3)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.10

Kruskals Algorithm Example(4)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.11

Kruskals Algorithm Example(5)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.12

Kruskals Algorithm Example(6)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.13

Kruskals Algorithm Example(7)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.14

Kruskals Algorithm Example(8)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.15

Kruskals Algorithm Example(9)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.16

Kruskals Algorithm Example(10)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.17

Kruskals Algorithm Example(11)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.18

Kruskals Algorithm Example(12)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.19

Kruskals Algorithm Example(13)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.20

Kruskals Algorithm Example(14)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.21

Kruskals 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
Data Structure and Algorithm

1.22

Prims 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

Data Structure and Algorithm

1.23

Prims Algorithm Example(1)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.24

Prims Algorithm Example(2)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.25

Prims Algorithm Example(3)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.26

Prims Algorithm Example(4)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.27

Prims Algorithm Example(5)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.28

Prims Algorithm Example(6)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.29

Prims Algorithm Example(7)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.30

Prims Algorithm Example(8)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.31

Prims Algorithm Example(9)

8
4

2
11

10
2

Data Structure and Algorithm

14

1.32

Prims Algorithm
MST_PRIM (G,w,r)
Q = V[G]

// r = starting node
//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)
Data Structure and Algorithm

1.33

You might also like