We have discussed Prim’s algorithm and its implementation for adjacency matrix representation of graphs.
As discussed in the previous post, in Prim’s algorithm, two sets are maintained, one set contains list of vertices already included in MST, other set contains vertices not yet included. In every iteration, we consider the minimum weight edge among the edges that connect the two sets.
The implementation discussed in previous post uses two arrays to find minimum weight edge that connects the two sets. Here we use one inMST[V]. The value of MST[i] is going to be true if vertex i is included in the MST. In every pass, we consider only those edges such that one vertex of the edge is included in MST and other is not. After we pick an edge, we mark both vertices as included in MST.
Edge 0:(0, 1) cost: 2 Edge 1:(1, 2) cost: 3 Edge 2:(1, 4) cost: 5 Edge 3:(0, 3) cost: 6 Minimum cost= 16
Time Complexity : O(V3)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Comparison between Adjacency List and Adjacency Matrix representation of Graph
- Convert Adjacency Matrix to Adjacency List representation of Graph
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Implementation of DFS using adjacency matrix
- Implementation of BFS using adjacency matrix
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Bellman Ford Algorithm (Simple Implementation)
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Add and Remove vertex in Adjacency List representation of Graph
- Add and Remove Edge in Adjacency List representation of a Graph
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- C program to implement Adjacency Matrix of a given Graph
- Push Relabel Algorithm | Set 2 (Implementation)
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Implementation of Non-Restoring Division Algorithm for Unsigned Integer
- Implementation of Restoring Division Algorithm for unsigned integer
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.