Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
• There can be several spanning trees for a graph. Figure shows some of the
trees for the graph with vertices v1,v2,v3,v4
5 Lecture #12
Adapted from slides by Dr A. Sattar
Minimum Spanning Trees
• A weighted undirected graph can have several spanning trees
• One of the spanning trees has smallest sum of all the weights
associated with the edges. This tree is called minimum spanning tree
(MST).
6 Lecture #12
Adapted from slides by Dr A. Sattar
Why do we need MST?
• Minimum spanning trees have many practical applications. Some typical
applications are:
• A telephone network can be configured, using minimum spanning tree, to have
minimum cable length.
• The air travel routes can be selected so that the travel time or travel cost is least.
• Linking a group of island with bridges so that total bridge span length is minimum
• However, MST is not necessary the shortest path and it does not apply to cycle
MST don’t solve TSP
• Travel salesman problem (TSP) can not be solved by
MST :
• salesman needs to go home (what’s the cost going home?)
• TSP is a cycle
• use MST to approximate
• solve TSP by exhaustive approach try every permutation on cyclic
graph
Two important algorithms for creating a minimum spanning tree for a graph,
named after their inventors, are Kruskal’s algorithm and Prim’s algorithm.
Kruskal’s Algorithm
• The Kruskal’s algorithm works as follows:
2 12 3
1 14 6 0 0 0
28
1 16 2 10 1 1 10 1
14 16
3 18 6 5 6 2 5 6 2 5 6 2
24
25
3 22 4 18 12
4 4
4
4 24 6 22
3 3 3
4 25 5
28
0 10 5
2 12 3
1 14 6
0 0 0
1 16 2
10 1 10 1 10 1
14 14 16
3 18 6
5 6 2 5 6 2 5 6 2
3 22 4
12 12 12
4 4 4
4 24 6
3 3 3
4 25 5
+ 3 6
0 28 1
cycle
0 10 5
12
2 3
1 14 6 0 0
1 16
2 10 1 10 1
14 16 14 16
3 18 6 5 6 2 5 6 2
25
3 22 4 12 12
4 4
22 22
4 24 6 3 + 4 6 3
cycle
cost = 10 +25+22+12+16+14
4 25 5
28
0 1
Kruskal’s Algorithm
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 {(u, v)}
8. UNION(u, v)
9.Return A
13 Lecture #12
Adapted from slides by Dr A. Sattar
Kruskal’s Algorithm
• MAKE-SET(x) creates a new set whose only member
(and thus representative) is x. Since sets are disjoint, x
should not be in some other set