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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Convert Adjacency Matrix to Adjacency List representation of Graph
- Convert Adjacency List to Adjacency Matrix representation of a Graph
- Comparison between Adjacency List and Adjacency Matrix 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 Edge in Adjacency Matrix representation of a Graph
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Bellman Ford Algorithm (Simple Implementation)
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Add and Remove Edge in Adjacency List representation of a Graph
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- Add and Remove vertex in Adjacency List representation of Graph
- 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 Restoring Division Algorithm for unsigned integer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.