3.5 Minimum Cost Spanning Trees Kruskal and Prim's Algorithms
3.5 Minimum Cost Spanning Trees Kruskal and Prim's Algorithms
5 Minimum
Spanning Tree
Pradnya Bhangale
[email protected]
Tree and Graph
• A tree data structure is a hierarchical data structure that
consists of nodes connected by edges.
• Each node can have multiple child nodes, but only one
parent node.
• The topmost node in the tree is called the root node.
Spanning Tree
Conditions:
• V’=V
• E’ C E
• E’ = IVI-1
• Spanning tree no
G(V’,E’) cycle and is
connected
G(V,E)
Example
E
Analysis
Example 1
Solution
OR
Example 2: Solution
(6) If an edge <0, 1> is added in step (5) then the next minimum
weighted edge is <1, 2> to be added to connect nodes 1 and 2. If
an edge <1, 2> is added in step (5) then the next minimum
weighted edge is <0, 1> to be added to connected nodes 0 and
1.
Example 2: Solution
(7) Include the next minimum weighed edge is <4, 5> to connect
two subtrees
(i) a subtree with nodes 0, 1, 2, 4 and
(ii) a subtree with nodes 3, 5
Example 2: Solution
(8) The addition of any of the remaining edges <1, 4>,<3, 4>, <0,
3>, <2, 4>, <2, 5> to the forest obtained in step (7) forms a cycle,
so we cannot add any of them to the forest. A tree formed in the
above step (7) is a spanning tree of a given graph with the cost =
1 + 2 + 3 + 3 + 4 = 13 units
Total Time Complexity = O(V) + O(ElogE) + O(E) + O(log V) = O(ElogE)
Example: Kruskal’s Algorithm
Solution