| Take for example this graph. Its got 7 nodes, most of which with 3 or 4 edges connecting them to other nodes. Clearly, this is not a minimum spanning tree. However, if we remove all the edges labeld with red, we will have a min spanning tree. | ![]() |
| Take, for example, this simple graph. First, note that the shortest path from any one node to any other node is 1. For a minimum spanning tree, we aren't interrested in shortest pathes. Our concern is having each node connected to the minumum spanning tree with the cheapest edge possible. | ![]() |
![]() |
This is one possiblity of a minimum spanning tree. There are two other correct solutions. One with edges { (A,B), (A,C) } and { (A,C), (B,C) } The solution set is dependent on how a minimum path is chosen. |
T = {} // T is the set of pairs (edges) that make up the min span tree
while ( T has < n-1 edges and E not empty ) {
pick (u,v) from E such that its weight is minimal
delete (u,v) from E
if adding (u,v) doesn't create a cycle, add it to T
}
|
Trace through Kruskal's algorithm:
|
TV = { 0 }
T = { }
while( T has < n-1 edges ) {
find (u,v) with least cost, such that
u is in TV and v isn't in TV
if no such edge exists, break
add v to TV
add (u,v) to T
}
On termiantion, if the graph is connected, TV will contain all the nodes
in the graph, and T will contain the set edges comprising the minimum
spanning tree. | TV | T |
| { 0 } | { } |
| { 0, 2 } | { (0,2) } |
| { 0, 2, 4 } | { (0,2), (2,4) } |
| { 0, 2, 4, 5 } | { (0,2), (2,4), (4,5) } |
| { 0, 2, 4, 5, 3 } | { (0,2), (2,4), (4,5), (0,3) } |
| { 0, 2, 4, 5, 3, 1 } | { (0,2), (2,4), (4,5), (0,3), (2,1) } |
| { 0, 2, 4, 5, 3, 1, 7 } | { (0,2), (2,4), (4,5), (0,3), (2,1), (1,7) } |
| { 0, 2, 4, 5, 3, 1, 7, 6 } | { (0,2), (2,4), (4,5), (0,3), (2,1), (1,7), (4,6) } |