Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Lecture On Programming

Lecture of college programming

Uploaded by

Amir Freer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture On Programming

Lecture of college programming

Uploaded by

Amir Freer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming III

Minimum Spanning Tree(Kruskal’s Algorithm)


Overview of Kruskal's Algorithm
• Minimum spanning trees (MSTs) are fundamental in graph theory and
have numerous applications in various fields, including network design,
clustering, and optimization.
• MSTs are trees that connect all the vertices in a graph with the minimum
possible total edge weight, ensuring the most efficient connectivity.
• Efficient algorithms are required to find MSTs in large graphs with
complex edge structures.
• Kruskal's algorithm is one such algorithm that efficiently finds the MST
of a graph by iteratively selecting edges of minimum weight that do not
form cycles.
Key Features of Kruskal's Algorithm
Greedy Strategy: Kruskal's algorithm follows a greedy approach, where it selects edges
in increasing order of their weights and adds them to the minimum spanning tree (MST) if
they do not form a cycle.
Edge Sorting: Initially, the algorithm sorts all the edges of the graph based on their
weights in non-decreasing order.
Union-Find Data Structure: Kruskal's algorithm typically employs a union-find data
structure (also known as disjoint-set data structure) to efficiently determine whether
adding an edge will create a cycle in the MST.
Edge Selection: Starting with an empty MST, Kruskal's algorithm iterates through the
sorted edges and adds each edge to the MST if it does not create a cycle.
Minimum Spanning Tree (MST) Construction: By selecting edges with the minimum
weight and avoiding cycles, Kruskal's algorithm constructs a minimum spanning tree that
spans all vertices of the graph with the minimum possible total edge weight.
Graph Search Process
Edge Selection: The algorithm selects edges from the sorted list of edges
in increasing order of their weights.
Cycle Detection: Before adding an edge to the MST, Kruskal's algorithm
checks whether adding the edge will create a cycle. This is typically done
using a union-find data structure to efficiently detect cycles.
Edge Addition: If adding the edge does not create a cycle in the current
MST, it is added to the MST. Otherwise, the edge is skipped.
Iterative Process: The search process continues iteratively until all
vertices are connected in the MST or until all edges have been considered.
Minimum Spanning Tree
A minimum spanning tree (MST) of a connected, undirected graph is a
subset of its edges that forms a tree and spans all the vertices of the
graph while minimizing the total edge weight.
• Spanning Tree: A spanning tree of a graph is a subgraph that is a tree
(i.e., it is acyclic and connected) and includes all the vertices of the
original graph.
• Minimum Total Weight: Among all possible spanning trees of a
graph, the minimum spanning tree is the one with the smallest total
sum of edge weights.
Minimum Spanning Tree
Some important properties of minimum spanning trees include:
• Each vertex in the graph is included exactly once in the minimum
spanning tree.
• The minimum spanning tree contains n−1 edges, where n is the
number of vertices in the graph.
• The minimum spanning tree does not contain any cycles.
Example
G
9

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

Pick the smallest Edge. In this case it is 1, for vertices E and 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

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 log⁡E), 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)

You might also like