Professional Documents
Culture Documents
Prims
Prims
#include <stdio.h>
#include <limits.h>
#define MAX_VERTICES 20
key[0] = 0;
parent[0] = -1;
int main() {
int graph[MAX_VERTICES][MAX_VERTICES];
int vertices;
primMST(graph, vertices);
return 0;
}
1. Start by defining the maximum number of vertices (MAX_VERTICES) the graph can have.
2. Define a function getMinimumKey that takes the key array, mstSet array, and the number of vertices as input. This function finds the vertex
with the minimum key value among the vertices not yet included in the MST.
3. Define a function printMST that takes the parent array, the graph array, and the number of vertices as input. This function prints the edges
and their weights in the minimum spanning tree.
4. Define the main primMST function that takes the graph array and the number of vertices as input. This function performs the Prim's
algorithm to find the minimum spanning tree.
5. Inside the primMST function, declare and initialize the parent, key, and mstSet arrays, each of size MAX_VERTICES.
6. Set the key values of all vertices to INT_MAX and mark all vertices as not yet included in the MST by setting mstSet values to 0.
7. Set the key value of the first vertex (0th vertex) as 0, as it will be the first vertex of the MST.
8. Set the parent of the first vertex as -1 to indicate that it is the root of the MST.
9. For count from 0 to vertices - 1, repeat steps 10-14.
10. Find the vertex u with the minimum key value among the vertices not yet included in the MST. Use the getMinimumKey function for this.
11. Mark vertex u as included in the MST by setting mstSet[u] to 1.
12. For each vertex v in the graph, if there is an edge between u and v, and v is not yet included in the MST (mstSet[v] is 0), and the weight of
the edge between u and v is less than the current key[v], update key[v] with the weight of the edge and set parent[v] as u.
13. After the loop in step 9 completes, the MST has been constructed. Call the printMST function to print the edges and their weights in the
MST.
14. Exit the primMST function.
15. In the main function, declare the graph array and the number of vertices.
16. Prompt the user to enter the number of vertices and read the input.
17. Prompt the user to enter the adjacency matrix values and read them into the graph array.
18. Call the primMST function with the graph array and the number of vertices.
19. End the program.