Sei sulla pagina 1di 21

2

Spanning trees
Suppose you have a connected undirected graph
Connected: every node is reachable from every other node
Undirected: edges do not have an associated direction
...then a spanning tree of the graph is a connected
subgraph in which there are no cycles
A connected,
undirected graph
Four of the spanning trees of the graph
A
B
C
D E
F
2
3
3
2
5
5
6
6
8
4
A connected undirected graph of some vertices
which contains cycles with weight on each
edges.
3
A
B
C
D E
F
3
3
2
5
6
A spanning tree is a connected graph of all
vertices which contains no cycles
4
Length of this tree is 19
A
B
C
D E
F
2
3
3
2
5
The minimum spanning tree is the spanning tree
with the least weight.
The least weight is 15
5
Solution for Minimum Spanning Tree Problem
Prims Algorithm is a quick way of finding the
Minimum Spanning Tree (or minimum connector).

Kruskals Algorithm may also be used to find a
minimum spanning tree, but this considers the
weights themselves rather than the connecting
points.
6
Kruskals Algorithm
Step 1: Choose the edge of least weight.

Step 2: Choose from those remaining edges the edge of
least weight which does not form a cycle with
already chosen edges. (If there are several
such edges, choose one arbitrarily.)

Step 3: Repeat Step 2 until n-1 edges have been chosen.
7
Aim: To find a minimum spanning tree
for a connected graph with n nodes:
MST-KRUSKAL (G, w)
1 A
2 for each vertex v V[G]
3 do MAKE-SET(v)
4 sort the edges of E into non-decreasing order by weight w
5 for each edge (u, v) E, taken in non-decreasing order by
weight
6 do if FIND-SET(u) = FIND-SET(v)
7 then A A{(u, v)}
8 U NION(u, v)
9 return A
8
Kruskals Algorithm
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
9
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
10
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
11
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
12
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
13
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
14
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
15
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
16
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
17
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
18
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
19
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
90
50
110
50
120
110
70
100
20
Bangalore
Patna
Hyderabad
Chennai
Kanyakumari
Cochin
Surat
Jaipur
Mumbai
Bhopal
Noida
50
40
40
30
80
70
80
50
50
70
21
MST Found
22
THANK
YOU

Potrebbero piacerti anche