Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Prims

#include <stdio.h>
#include <limits.h>

#define MAX_VERTICES 20

int getMinimumKey(int key[], int mstSet[], int vertices) {


int min = INT_MAX, minIndex;
for (int v = 0; v < vertices; v++) {
if (mstSet[v] == 0 && key[v] < min) {
min = key[v];
minIndex = v;
}
}
return minIndex;
}

void printMST(int parent[], int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {


printf("Edge \tWeight\n");
for (int i = 1; i < vertices; i++) {
printf("%d - %d\t%d\n", parent[i], i, graph[i][parent[i]]);
}
}

void primMST(int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {


int parent[MAX_VERTICES];
int key[MAX_VERTICES];
int mstSet[MAX_VERTICES];

for (int i = 0; i < vertices; i++) {


key[i] = INT_MAX;
mstSet[i] = 0;
}

key[0] = 0;
parent[0] = -1;

for (int count = 0; count < vertices - 1; count++) {


int u = getMinimumKey(key, mstSet, vertices);
mstSet[u] = 1;
for (int v = 0; v < vertices; v++) {
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}

printMST(parent, graph, vertices);


}

int main() {
int graph[MAX_VERTICES][MAX_VERTICES];
int vertices;

printf("Enter the number of vertices: ");


scanf("%d", &vertices);

printf("Enter the adjacency matrix:\n");


for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph[i][j]);
}
}

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.

You might also like