1 / 24

Prims Algorithm | What Is Prims Algorithm? | Introduction To Prims Algorithm |

This presentation on Prims Algorithm will acquaint you with the theoretical explanation and mathematical interpretation of the minimum spanning tree for a given graph. In this data structures tutorial, you will understand What Is Prims Algorithm is and how you can develop a minimum spanning tree by making use of it. After that, you will learn how you can implement a Prim's algorithm program Using the C language.<br><br>So, let's get started!<br><br>The topics covered in this tutorial are:<br>1. Introduction to Primu2019s Algorithm<br>2. Finding MST with Primu2019s Algorithm<br>3. Coding Implementation of Primu2019s Algorithm<br>

Simplilearn
Télécharger la présentation

Prims Algorithm | What Is Prims Algorithm? | Introduction To Prims Algorithm |

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 Prim’s Algorithm Finding MST with Prim’s Algorithm Coding Implementation of Prim’s Algorithm

  2. Introduction to Prim’s Algorithm

  3. Click here to watch the video

  4. Introduction to Prim’s Algorithm • Prim’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. • Prim’s algorithm is a Greedy algorithm.

  5. Finding MST with Prim’s Algorithm

  6. Finding MST with Prim’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. B D 7 H 3 3 1 5 F I A 9 4 2 4 6 C E G 1 3

  7. Finding MST with Prim’s Algorithm Step: 1 - Choose an arbitrary starting Vertex. Here, we will begin with vertex A B D H 7 3 1 3 5 F I A A 9 4 2 6 4 1 C E G 3

  8. Finding MST with Prim’s Algorithm Step: 2 – Keep including connected edges with minimal cost. We will choose B as edge A->B has minimum edge weight. B B D H 7 3 1 3 1 5 F I A A 9 4 2 6 4 2 C E G 3

  9. Finding MST with Prim’s Algorithm Now, from vertex B we will choose vertex D as it has minimum cost compared to edge BF. B B D D 3 H 7 3 1 3 1 5 F I A A 9 4 2 6 4 2 C E G 3

  10. Finding MST with Prim’s Algorithm From vertex D, there is only one edge, so we will add it in our spanning tree. B B D D H H 7 3 3 7 1 3 1 5 F I A A 9 4 2 6 4 2 C E G 3

  11. Finding MST with Prim’s Algorithm From vertex H, there are two possible paths. We will choose the one with minimal value. B B D D H H 7 3 3 7 1 3 1 3 5 F I I A A 9 4 2 6 4 2 C E G 3

  12. Finding MST with Prim’s Algorithm From vertex I, there is only one possible path. B B D D H H 7 3 3 7 1 3 1 3 5 F I I A A 9 4 2 6 4 4 2 C E G G 3

  13. Finding MST with Prim’s Algorithm From vertex G, we will choose vertex E. B B D D H H 7 3 3 7 1 3 1 3 5 F I I A A 9 8 3 6 4 4 2 2 C E E G G 2

  14. Finding MST with Prim’s Algorithm From vertex E, we will choose vertex C. B B D D H H 7 3 3 7 1 3 1 3 5 F I I A A 9 8 3 6 4 4 2 2 2 C C E E G G 2

  15. Finding MST with Prim’s Algorithm Step: 3 – Make sure that there is no loop in tree structure. To avoid loop we will move to the F vertex. B B D D H H 7 3 3 7 1 3 1 3 5 F F I I A A 9 8 8 3 6 4 4 2 2 2 C C E E G G 2

  16. Finding MST with Prim’s Algorithm Step: 3 – Make sure that there is no loop in tree structure. To avoid loop we will move to the F vertex. B B D D H H 7 3 3 7 At each vertex, the Prim’s algorithm considers all possible edges, and picks the minimum weight edge from those edges. That is why it is considered as Greedy Algorithm. The cost for this MST G’(V’, E’) is 30. 1 3 1 3 5 F F I I A A 9 8 8 3 6 4 4 2 2 2 C C E E G G 2

  17. Coding Implementation of Prim’s Algorithm

  18. Implementation Strategy For the representation of graph we will use an adjacency matrix. This adjacency matrix will store weights and edges between different nodes. 0 0 1 2 We will be implementing graph using a 2-D matrix in C programming language. The indices of this 2-D matrix will be the vertices of the graph and the key values inserted will be the weights for edges. 2 4 0 1 2 1 2 3 For edge 1 to 2 we have weight value 3. For index 0 to 1 the edge is having weight 2. For vertices 0,1, and 2 there is no loop, so primarily let’s generate all vertices. For edge between vertex 0 and 2 we have weight value 4

  19. Implementation Strategy • We will maintain set for keeping the track of vertices included in MST. • Primarily, we will set all key values (edge weights) to infinity and during run-time we will set the first vertex to 0 for choosing it as first vertex in a tree. • While Set doesn’t include all vertices: • Pick a vertex u with minimum edge weight. • Include u to Set.  • Update key value of all adjacent vertices of u.

  20. Let’s create a program to generate Minimum Spanning Tree using Prim’s Algorithm!

  21. Graph for Constructing an MST For graphG(V, E) given below, construct a Minimum Spanning Tree G’(V’, E’) such that the edge weight is minimized. 3 4 2 1 0 7 5 6 8 3 4 11

More Related