Title: Minimum Spanning Tree
1Minimum Spanning Tree
- What is a Minimum Spanning Tree.
- Constructing a Minimum Spanning Tree.
- What is a Minimum-Cost Spanning Tree.
- Prims Algorithm.
- Animated Example.
- Implementation.
- Kruskals algorithm.
- Animated Example.
- Implementation.
- Review Questions.
2What is a Minimum Spanning Tree.
- Minimum Spanning Tree is applicable only to
connected undirected graphs. - The idea is to find another graph with the same
number of vertices, but with minimum number of
edges. - Let G (V,E) be connected undirected graph. A
minimum spanning tree for G is a graph, T (V,
E) with the following properties - V V
- T is connected
- T is acyclic.
3Example of Minimum Spanning Tree
- The following figure shows a graph G1 together
with its three possible minimum spanning trees.
(a)
(b)
(c)
(d)
- The example shows that the minimal spanning tree
for a graph is generally not unique.
4Constructing Minimum Spanning Tree
- A spanning tree can be constructed using any of
the traversal algorithms discussed, and taking
note of the edges that are actually followed
during the traversals. - The spanning tree obtained using breadth-first
traversal is called breadth-first spanning tree,
while that obtained with depth-first traversal is
called depth-first spanning tree. - For example, for the graph in figure 1, figure
(c) is a depth-first spanning tree, while figure
(d) is a breadth-first spanning tree.
5What is a Minimum-Cost Spanning Tree
- Minimum-Cost spanning tree is applicable only to
edge-weighted connected undirected graphs. - For an edge-weighted , connected, undirected
graph, G, the total cost of G is the sum of the
weights on all its edges. - A minimum-cost spanning tree for G is a minimum
spanning tree of G that has the least total cost.
6Prims Algorithm
- Prims algorithm finds a minimum cost spanning
tree by selecting edges from the graph one-by-one
as follows - It starts with a tree, T, consisting of the
starting vertex, x. - Then, it finds and adds the shortest edge
connecting T to the rest of the graph. - The process is repeated until all vertices are
added to T.
Let T be a tree consisting of only the starting
vertex x While (T has fewer than n vertices)
find the smallest edge connecting T to G-T
add it to T
7Animated Example for Prims Algorithm.
8Implementation of Prims Algorithm.
- Prims algorithn can be implememted similar to the
Dijskras algorithm as shown below
1 public static Graph primsAlgorithm(Graph g,
int start) 2 int n g.getNumberOfVertices()
3 Entry table new Entryn 4 for(int
v 0 v lt n v) 5 tablev new
Entry() 6 tablestart.distance 0 7
PriorityQueue queuenew BinaryHeap(g.getNumberOfEd
ges()) 8 queue.enqueue(new Association(new
Int(0), 9
g.getVertex(start))) 10 while(!queue.isEmpty()
) 11 Association association(Association)que
ue.dequeueMin() 12 Vertex v0
(Vertex)association.getValue() 13 int n0
v0.getNumber() 14 if(!tablen0.known) 15
tablen0.known true 16
Enumeration p v0.getEmanatingEdges()
9Implementation of Prims Algorithm Cont'd
17 while (p.hasMoreElements()) 18
Edge edge (Edge) p.nextElement() 19
Vertex v1 edge.getMate(v0) 20 int n1
v1.getNumber() 21 Int weight
(Int)edge.getWeight() 22 int d
weight.intValue() 23 if(!tablen1.known
tablen1.distancegtd) 24
tablen1.distance d 25
tablen1.predecessor n0 26
queue.enqueue(new Association(new Int(d),
v1)) 27 28 29 30
31 Graph result new GraphAsLists(n) 32
for(int v 0 v lt n v) 33
result.addVertex(v) 34 for(int v 0 v lt n
v) 35 if( v ! start) 36
result.addEdge(v, tablev.predecessor, 37
new Int(tablev.distance))
38 return result
10Kruskal's Algorithm.
- Kruskals algorithm also finds the minimum cost
spanning tree of a graph by adding edges
one-by-one. - However, Kruskals algorithm forms a forest of
trees, which are joined together incrementally to
form the minimum cost spanning tree.
sort the edges of G in increasing order of
weights form a forest by taking each vertex of G
to be a tree for each edge e in sorted list
if the endpoints of e are in separate trees
join the two tree to form a bigger tree by
adding e return the resulting single tree
11Animated Example for Kruskals Algorithm.
- Demonstrating Kruskal's algorithm.
12Implementation of Kruskals Algorithm.
- To implement Kruskal's algorithm, we need a data
structure that maintains a partition of sets. It
should have the following methods - find(item) returns the set containing the
item. - join(s1, s2) replace the sets, s1 and s2 with
their union. - We shall implement the partition data structure
as a forest. That is, each set in the partition
is represented as a tree.
13Implementation of Kruskals Algorithm Cont'd.
- The tree we shall use to represent a set in a
partition is unlike any of the trees we have seen
so far. - The nodes have arbitrary degrees.
- There is no restriction in the ordering of the
keys. - Instead of pointing to children, each node of the
tree points to its parent. - This tree is represented as an array of objects,
where - Each object i is stored at index I
- Each object points to its parent or (null if
root)
14Implementation of Kruskals Algorithm Cont'd.
- The ith array element holds the node that
contains item i. - Thus, to implement the find method, we start at
index i of the array and follow the parent
pointers until we reach a node whose parent is
null and return it. - To implemented the join method, we attach the
smaller tree (the one with fewer keys) to the
root of the larger one
15Implementation of Kruskals Algorithm Cont'd.
- The following code shows the implementation of
the Partition class
1 public class Partition 2
protected PartitionTree forest 3 protected
int count 4 public class PartitionTree
5 protected int item, count 1 6
protected PartitionTree parent 7 public
PartitionTree(int item) 8 this.item
item 9 parent null 10 11
12 public Partition(int size) 13
forest new PartitionTreesize 14 for
(int item 0 item ltsize item) 15
forestitem new PartitionTree(item) 16
count size 17 18 //...
16Implementation of Kruskals Algorithm Cont'd
18 public PartitionTree find(int item)
19 PartitionTree ptr forestitem 20
while (ptr.parent ! null) 21 ptr
ptr.parent 22 23 return ptr 24
25 public void join (PartitionTree t1,
PartitionTree t2) 26 if (!isPartitionTree(t
1) !isPartitionTree(t2)) 27 throw new
IllegalArgumentException("bad argument") 28
else if (t1.count gt t2.count) 29
t2.parent t1 30 t1.countt2.count 31
32 else 33 t1.parent
t2 34 t2.count t1.count 35
36 count -- 37 38 //...
17Implementation of Kruskals Algorithm Cont'd.
- We can use the Partition data structure to
implement the Kruskals algorithm as follows
1 public static Graph kruskalsAlgorithm(Graph g)
2 int n g.getNumberOfVertices() 3
Graph result new GraphAsLists(n) 4 for(int
v 0 v lt n v) 5 result.addVertex(v) 6
PriorityQueue queuenew BinaryHeap(g.getNumber
OfEdges()) 7 Enumeration p g.getEdges() 8
while(p.hasMoreElements()) 9 Edge edge
(Edge) p.nextElement() 10 Int weight
(Int)edge.getWeight() 11 queue.enqueue(new
Association(weight, edge)) 12 13 //...
18Implementation of Kruskals Algorithm Cont'd
13 Partition partition new Partition(n) 14
while (!queue.isEmpty() partition.getCount()
gt 1) 15 Association association 16
(Association) queue.dequeueMin()
17 Edge edge (Edge)association.getValue(
) 18 Int weight (Int)
edge.getWeight() 19 int n0
edge.getV0().getNumber() 20 int n1
edge.getV1().getNumber() 21
Partition.PartitionTree t1 partition.find(n0) 2
2 Partition.PartitionTree t2
partition.find(n1) 23 if(t1 ! t2) 24
partition.join(t1, t2) 25
result.addEdge(n0, n1, weight) 26 27
28 return result 29
19Review Questions
GB
- Find the breadth-first spanning tree and
depth-first spanning tree of the graph GA shown
above. - For the graph GB shown above, trace the
execution of Prim's algorithm as it finds the
minimum-cost spanning tree of the graph starting
from vertex a. - Repeat question 2 above using Kruskal's
algorithm. - Complete the implementation of the Partition
class by writing a method isPartitionTree(Partitio
nTree tree) that returns true if tree is a valid
partition tree in the partition forest.