1 / 37

Kruskal Algorithm | Kruskal Algorithm For Minimum Spanning Trees | Data Structur

This presentation on Kruskal Algorithm will acquaint you with the theoretical explanation and complete drive-through example for constructing a minimum spanning tree for given graph. This data structure tutorial will acquaint you with clear understanding of Kruskal Algorithm For Minimum Spanning Trees. After that, you will learn the implementation strategy to develop a C program for Kruskal's algorithm.

Simplilearn
Download Presentation

Kruskal Algorithm | Kruskal Algorithm For Minimum Spanning Trees | Data Structur

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What’s in It For You? Introduction to Kruskal’s Algorithm Creating Minimum Spanning Tree Using Kruskal’s Algorithm Implementation of Kruskal’s Algorithm

  2. Introduction to Kruskal’s Algorithm

  3. Click here to watch the video

  4. Introduction to Kruskal’s Algorithm • Kruskal’s algorithm is used to find the Minimum Spanning Tree for a given graph. • This algorithm finds the subset of edges for graph G(V, E) such that the summation of edge weights is minimum. • Kruskal’s algorithm is a Greedy algorithm. • The data structure used to implement Kruskal’s algorithm is known as Union Find.

  5. What Is Union Find? • Union Find is data structure that keeps track of elements which are split into one or more than one disjoint sets. • It has two primary operations Find and Union. • Find operation returns the set of element to which given element (argument) belongs. • Union operation merges two disjoint sets.

  6. Real- Life Example of Union Find J L Singing Magnets placed on table: A B C D E F G H I J K L B E K Set PURPLE C A D Set RED I F G H Magnetic balls are close to each other. Thus, they will form a connection. Set GREEN

  7. We divide the provided graph G(V, E) into three different sets. And we use Union Find algorithm to create an MST by adding edges (from maintained sets) with minimum edge weight.

  8. Steps In Kruskal’s Algorithm Steps to Perform Sort all edges in increasing order of their weight. Pick the smallest edge. Check if the new edge is creating cycle or loop in spanning tree. If cycle is not formed, then include the edge in MST. Otherwise, Discard the edge. Repeat from step- 2 until |V| - 1 edges are included in the MST.

  9. Creating Minimum Spanning Tree Using Kruskal’s Algorithm

  10. Finding MST with Kruskal’s Algorithm We need to find Minimum Spanning Tree G’(V’, E’) for graphG(V, E) such that the sum of edge weights is minimized. 6 D B 5 7 5 A E 0 1 3 2 4 8 2 C F 3

  11. Finding MST with Kruskal’s Algorithm Step: 1 – Remove all loops and parallel edges from the given graph G(V, E). 6 D B 5 7 5 A E 0 1 3 2 4 8 2 C F 3

  12. 6 D B Step: 2 – Arrange all edges in increasing order of their edge weights. 5 7 5 E A 2 3 4 2 8 F C 3 D B E A 2 F C

  13. 6 D B Step 3 – Keep inserting edges into the spanning tree. 5 7 5 E A 2 3 4 2 8 F C 3 D B E 2 A 2 F C

  14. 6 D B Step 3 – Keep inserting edges into the spanning tree. 5 7 5 E A 2 3 4 2 8 F C 3 D B E A 2 3 2 F C

  15. 6 D B Step 3 – Keep inserting edges into the spanning tree E = |V| - 1. 5 7 5 E A 2 3 4 2 8 F C 3 D B E A 2 3 2 F C 3

  16. 6 D B Step 3 – Make Sure that inserted edge does not form any cycle in spanning tree. 5 7 5 E A 2 3 4 2 8 F C 3 D B E A 2 3 2 4 F C 3

  17. 6 D B Step 3 – Keep inserting edges into the spanning tree E = |V| - 1. 5 7 5 E A 2 3 4 2 8 F C 3 D B 5 E A 2 3 2 F C 3

  18. 6 D B Step 3 – Keep inserting edges into the spanning tree until E = |V| - 1. 5 7 5 E A 2 3 4 2 8 F C 3 6 D B E A 2 3 2 F C 3

  19. 6 D B Step 3 – Keep inserting edges into the spanning tree until E = |V| - 1. 5 7 5 E A 2 3 4 2 8 F C 3 D B 7 E A 2 3 2 F C 3

  20. 6 D B Step 3 – Keep inserting edges into the spanning tree until E = |V| - 1. 5 7 5 E A 2 3 4 2 8 F C 3 Now, Number of edges E = |V|- 1, Thus we will stop inserting edges into our spanning tree structure. The Edge weight for this minimum spanning tree is 17. D B 7 E A 2 3 2 F C 3

  21. Find MST for Given Graph For given graph G(V, E) find G’(V’, E’) such that its edge weight is minimized. A 5 B 1 4 4 2 2 E D C 4 2 2 H 1 11 5 J 1 6 1 0 I 7 G F 4

  22. MST Using Kruskal’s Algorithm The Minimum Spanning Tree using Kruskal’s Algorithm. A 5 B 1 4 4 2 2 E D C 4 2 2 H 1 11 5 J 1 6 1 0 I 7 G F 4

  23. Implementation of Kruskal’s Algorithm

  24. Implementation Strategy • Convert edge into structure for keeping track of source and destination node. • Arrange all edges in increasing order of their edge weights. • Create three distinct sets for maintaining all the nodes, rank of the nodes and parent node for each node. • Add edges to the tree which do not make cycle.

  25. Implementation Strategy 2 B A B A 4 3 4 2 C D D C 1 0 1 2 3 NODES PARENT -1 -1 -1 -1 RANK 0 0 0 0

  26. Implementation Strategy 2 B A B A 4 3 4 2 C D D C 1 A B C D NODES C PARENT -1 -1 -1 -1 D is pointing to C, thus absolute parent rank of C will become 1. RANK 0 0 1 0 0

  27. Implementation Strategy 2 B A B A 4 3 4 2 2 C D D C 1 1 A B C D NODES PARENT C -1 C -1 -1 As rank of C is higher thus, B will also point to C. RANK 0 1 0 0

  28. Implementation Strategy 2 2 B A B A 4 3 4 2 2 C D D C 1 1 A B C D NODES PARENT C -1 C -1 C Both A and B have distinct parents thus, connection can be made. 1 + 1 = 2 RANK 0 1 0 0

  29. Implementation Strategy 2 2 B A B A 4 3 4 2 2 C D D C 1 1 A B C D NODES PARENT C -1 C C Both B and D have same parent node, thus new connection will create a cycle. RANK 0 2 0 0

  30. Implementation Strategy 2 2 B A B A 4 3 4 2 2 C D D C 1 1 A B C D NODES PARENT C -1 C C Both A and C also have same parent node. Hence, connection is not possible. RANK 0 2 0 0

  31. Implementation Strategy 2 2 B A B A 4 3 4 2 2 C D D C 1 1 A B C D NODES PARENT C -1 C C Finally, A and D also have same parent. So we won’t add edge AD. RANK 0 2 0 0

  32. Implementation Strategy 2 2 B A B A 4 3 4 2 2 C D D C 1 1 To avoid the traversal of all edges we can add one constraint to our traversal. We can limit it using loop from E = 0 to E = |V| -1. A B C D NODES PARENT C -1 C C Finally, A and D also have same parent. So we won’t add edge AD. RANK 0 2 0 0

  33. Time Complexity Time Complexity = O(ElogE + ElogV) Picking the edge out of edge list Sorting all edges Time Kruskal’s Algorithm N

  34. Let’s create a C program to implement Kruskal’s Algorithm using Union-Find.

More Related