Lecture On Programming
Lecture On Programming
3 7
F
A D
3 5
2 8
C
1
4
B E
3
Steps
Initialisation
Iterate Through Sorted Edges
Edge Selection
Cycle Detection
Termination
Finalisation
Initialisation
Node Connected Distance 9 G
Node
3 7 F
E C 1 A D
A B 2 3 5
C A 3 2 8
C 1
E B 3
4
A D 3 B E
3
C B 4
D C 5 Initialisation:
Create an empty list to store the edges of the minimum
D F 7
spanning tree (MST).
F E 8 Sort all the edges of the graph in non-decreasing order of their
F G 9 weights.
Initialisation of Data Structures (Optional)
Create a disjoint-set data structure (also known as union-find) to keep
track of connected components.
Iterate Through Sorted Edges:
Node Connected
Node
A
B
C
D
E
F
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 2, for vertices A
and B, Check to see if that connection would create a cycle, No, then it is
good.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 3, for vertices A
and D, A and C and B and E, In this case I have chosen edge A and D.
Check to see if that connection would create a cycle, No, then it is good.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 3, for vertices A
and C and B and E, In this case I have chosen edge A and C. Check to
see if that connection would create a cycle, No, then it is good.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 3, for vertices B
and E, In this case I have chosen edge A and C. Check to see if that
connection would create a cycle, Yes, then it is not good, so we move on
to the next iteration.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 4, for vertices B
and C, Check to see if that connection would create a cycle, Yes, then it
is not good, so we move on to the next iteration.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 5, for vertices D
and C, Check to see if that connection would create a cycle, Yes, then it
is not good, so we move on to the next iteration.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 7, for vertices D
and F, Check to see if that connection would create a cycle, No, then it is
good, we can add this to the MST.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 8, for vertices F
and E, Check to see if that connection would create a cycle, Yes, then it
is not good, so we move on to the next iteration.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 5 A D 3
2 8 C B 4
C 1 D C 5
4 D F 7
B E
3 F E 8
F G 9
Next Iteration, pick the smallest Edge. In this case it is 7, for vertices D
and F, Check to see if that connection would create a cycle, No, then it is
good, we can add this to the MST.
Walkthrough
Node Connected Distance
Node
E C 1
9 G A B 2
3 7 F
C A 3
A D E B 3
3 A D 3
2 C B 4
C 1 D C 5
D F 7
B E
F E 8
F G 9
Now, we have our MST. The Minimum Total Weight of the MST is
1+2+3+3+7+9 = 25.
Time Complexity
• Time Complexity:
• Sorting: The algorithm requires sorting of all the edges based on their
weights, which typically takes O(E logE), where E is the number of
edges.
• The time complexity of Kruskal's algorithm is dominated by the
sorting step.
Space Complexity
The space complexity of Kruskal's algorithm primarily depends on the
data structures used to store the edges and the disjoint-set data structure.
The space required to store the edges of the graph is O(E).
The space required for the disjoint-set data structure is O(V), where V is
the number of vertices.
Thus, the overall space complexity of Kruskal's algorithm is O(E+V).
Summary
Complexity Description
Time Complexity O(E log E)
Space Complexity O(V + E)