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)
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Implementation of BFS using adjacency matrix
- Implementation of DFS using adjacency matrix
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Bellman Ford Algorithm (Simple Implementation)
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- Push Relabel Algorithm | Set 2 (Implementation)
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
- Freivald’s Algorithm to check if a matrix is product of two
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
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.