Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What is Kruskal’s Algorithm? Steps, Examples, Overview

Updated on 12 June, 2023

7.27K+ views
9 min read

Introduction to Kruskal Algorithm

Kruskal’s Algorithm is a popular algorithm used in graph theory to find the Minimum Spanning Tree (MST) of a weighted graph. The MST represents the subset of edges that form the most efficient way to connect all the vertices while minimizing the total weight. By employing Kruskal’s Algorithm, we can solve complex optimization problems in various domains, such as network design, transportation planning, and circuit layout. This algorithm follows a step-by-step approach, carefully selecting edges based on their weights to construct the MST. To learn more about these algorithms, you can check out the MS in Full Stack AI and ML program by upGrad.

How does Kruskal’s Algorithm work?

Kruskal’s Algorithm follows a simple and intuitive approach to finding the MST of a connected weighted graph. Let’s explore the steps and process involved:

  • Sort the Edges: The first step in Kruskal’s Algorithm is to sort all the edges in the graph in the non-decreasing order of their weights. This step ensures that we consider edges with lower weights first.
  • Initialize an Empty MST: Create an empty set to represent the MST.
  • Iterate through the Edges: Starting from the edge with the lowest weight, iterate through the sorted edges.
  • Check for Cycle: For each edge, check if including it in the MST would create a cycle. A cycle is formed when adding an edge connects two vertices that are already connected through a different path.
  • Add to the MST: If including the current edge does not create a cycle, add it to the MST set.
  • Repeat Until MST is Complete: Continue steps 4 and 5 until there are V-1 edges in the MST, where V is the number of vertices in the graph. The MST will always have V-1 edges, ensuring that all vertices are connected without forming cycles.

By following these steps, Kruskal’s Algorithm effectively constructs the Minimum Spanning Tree, connecting all the vertices with the least total weight.

Learn Machine learning courses from the world’s top universities.

Example of Kruskal’s Algorithm

Let’s consider a graph with five vertices: A, B, C, D, and E. The edges and their corresponding weights are as follows:

AB: 3

AC: 1

BC: 4

BD: 2

CD: 5

CE: 6

By applying Kruskal’s Algorithm to this graph, we can find the Minimum Spanning Tree. Here’s a step-by-step breakdown of the process:

  • Sort the edges in non-decreasing order of their weights: AC, BD, AB, BC, CD, CE.
  • Initialize an empty MST.
  • Take the edge AC with weight 1 and add it to the MST.
  • Move to the next edge, BD with weight 2, and add it to the MST.
  • Proceed to edge AB with weight 3 and include it in the MST.
  • Add the edge BC with weight 4 to the MST.
  • Skip the edge CD as it creates a cycle in the current MST.
  • Finally, include the edge CE with weight 6 to the MST.
  • The resulting Minimum Spanning Tree for this graph includes the edges AC, BD, AB, and BC.

What is a Spanning Tree?

A spanning tree of a graph is a subgraph that includes all the vertices of the original graph but contains only a subset of the edges. It is a connected and acyclic graph, which means there are no cycles in the spanning tree. In other words, it is a tree that spans all the vertices of the graph, providing a way to reach any vertex from any other vertex.

What is a Minimum Spanning Tree (MST)?

A Minimum Spanning Tree (MST) is a spanning tree of a graph that has the minimum possible total weight among all the spanning trees. It represents the subset of edges that form the most efficient and cost-effective way to connect all the vertices in the graph. The primary objective of finding an MST is to minimize the overall cost or weight while ensuring that all vertices are connected.

How many edges does a Minimum Spanning Tree have?

A Minimum Spanning Tree (MST) of a graph with V vertices always has V-1 edges. This property holds for any connected graph. The MST includes the optimal subset of edges that connects all the vertices while minimizing the total weight.

Creating Minimum Spanning Tree using Kruskal’s Algorithm

Explanation of the process of creating an MST using Kruskal’s Algorithm

To create a Minimum Spanning Tree (MST) using Kruskal’s Algorithm, follow these steps:

  1. Sort the Edges: Start by sorting all the edges of the graph in the non-decreasing order of their weights. This ensures that we consider edges with lower weights first.
  2. Initialize the MST: Create an empty set to represent the MST.
  3. Iterate through the Edges: Begin iterating through the sorted edges.
  4. Check for Cycle: For each edge, check if including it in the MST would create a cycle. This can be done using the Union-Find algorithm, which efficiently detects cycles and maintains the connectivity of the graph.
  5. Add to the MST: If including the current edge does not create a cycle, add it to the MST set.
  6. Repeat Until MST is Complete: Continue steps 4 and 5 until there are V-1 edges in the MST, where V is the number of vertices in the graph. The MST will have V-1 edges since a spanning tree includes all vertices without forming cycles.

By following these steps, Kruskal’s Algorithm constructs the Minimum Spanning Tree of the given graph. It selects edges with the lowest weights that do not form cycles, ensuring that the resulting MST is the most cost-effective way to connect all the vertices. In order to get a hand over these algorithms, you can choose the Advanced Certificate Programme in Machine Learning & NLP from IIITB. 

Union Find Algorithm

The Union Find Algorithm, also known as the Disjoint Set Data Structure, is an efficient way to keep track of elements that are partitioned into disjoint sets. It provides operations to determine which set an element belongs to and to merge two sets. In the context of Kruskal’s Algorithm, the Union Find algorithm is used to detect cycles when adding edges to the MST. It helps maintain the connectivity of the graph and ensures that no cycles are formed in the MST construction process.

Implementation of Kruskal’s Algorithm

A detailed explanation of how to implement Kruskal’s Algorithm

To implement Kruskal’s Algorithm, you can follow the steps outlined earlier. Here’s a detailed explanation of the implementation process:

  1. Sort the Edges: Begin by sorting all the edges of the graph in the non-decreasing order of their weights.
  2. Initialize the MST and Union Find Data Structure: Create an empty set to represent the MST and initialize the Union Find data structure with each vertex as a separate set.
  3. Iterate through the Edges: Start iterating through the sorted edges.
  4. Check for Cycle: For each edge, check if including it in the MST would create a cycle. This can be done by checking if the endpoints of the edge belong to the same set in the Union Find data structure. If they do not belong to the same set, including the edge does not create a cycle.
  5. Add to the MST and Union Find Data Structure: If including the current edge does not create a cycle, add it to the MST set and merge the sets of its endpoints in the Union Find data structure.
  6. Repeat Until MST is Complete: Continue steps 4 and 5 until there are V-1 edges in the MST, where V is the number of vertices in the graph.

By implementing these steps, you can effectively construct the Minimum Spanning Tree using Kruskal’s Algorithm. The Union Find data structure plays a crucial role in detecting cycles and maintaining the connectivity of the graph throughout the process.

Kruskal’s Algorithm vs Prim’s Algorithm

Kruskal’s Algorithm and Prim’s Algorithm are both widely used for finding the Minimum Spanning Tree (MST) of a graph. However, they differ in their approach:

Kruskal’s Algorithm

  • Kruskal’s Algorithm follows a greedy approach.
  • It starts by sorting all the edges of the graph in non-decreasing order of weights.
  • It iteratively selects the edges with the smallest weights and adds them to the MST, as long as they do not create a cycle.
  • Kruskal’s Algorithm does not necessarily start from a specific vertex.

Prim’s Algorithm

  • Prim’s Algorithm also follows a greedy approach.
  • It starts by selecting an arbitrary vertex as the starting point.
  • It iteratively adds the shortest edge that connects the MST to a new vertex, until all vertices are included.
  • Prim’s Algorithm always starts from a specific vertex.

The choice between Kruskal’s Algorithm and Prim’s Algorithm depends on the specific requirements and characteristics of the graph. Kruskal’s Algorithm is typically preferred when the graph is dense, while Prim’s Algorithm is more efficient for sparse graphs.

Applications of Kruskal’s Algorithm

Kruskal’s Algorithm has several applications in various domains. Here are three common examples:

  1. Network Design: Kruskal’s Algorithm can be used to design cost-effective networks, such as telephone or internet networks, by connecting cities or routers with the minimum cost of laying cables or establishing connections.
  2. Circuit Design: In electronic circuit design, Kruskal’s Algorithm can be used to determine the minimum cost of connecting components or nodes in a circuit, minimizing the overall wiring or connection cost.
  3. DNA Sequencing: Kruskal’s Algorithm can be applied to determine the evolutionary relationships between different species based on their DNA sequences. Constructing a minimum cost-spanning tree helps identify common ancestors and genetic similarities.

Conclusion

In conclusion, Kruskal’s Algorithm is a popular and efficient algorithm for finding the Minimum Spanning Tree of a graph. By iteratively selecting edges with the smallest weights, it constructs a tree that connects all vertices with the minimum total weight. The algorithm works by avoiding cycles and is commonly used in network design, circuit design, and DNA sequencing applications. Kruskal’s Algorithm can be implemented in various programming languages like Python, Java, and C/C, and its time complexity is primarily determined by the sorting step and the Union-Find operations. Understanding Kruskal’s Algorithm and its applications provides valuable insights into graph theory and optimization problems. Learn more about these complex algorithms via Advanced Certificate Programme in Machine Learning & Deep Learning from IITB which may aid you to become a professional Machine Learning Engineer.

Frequently Asked Questions (FAQs)

1. What is the difference between Kruskal's Algorithm and Prim's Algorithm?

Kruskal's Algorithm follows a greedy approach and starts by sorting all the edges, while Prim's Algorithm starts from a specific vertex and adds the shortest edges iteratively.

2. What is a Minimum Spanning Tree?

A Minimum Spanning Tree is a tree that connects all vertices of a graph with the minimum total weight.

3. How does Kruskal's Algorithm avoid cycles?

Kruskal's Algorithm uses the Union-Find algorithm to determine if adding an edge creates a cycle. It maintains a disjoint set data structure to track the connected components of the graph.

4. Can Kruskal's Algorithm handle disconnected graphs?

Yes, Kruskal's Algorithm can handle disconnected graphs. It will construct a minimum-spanning forest, which is a collection of Minimum Spanning Trees for each connected component.

5. Is Kruskal's Algorithm efficient for large graphs?

Kruskal's Algorithm has a time complexity of O(E log E) and is generally efficient for large graphs. However, the choice of algorithm depends on the characteristics of the graph and specific requirements.