Implementation of Prim's algorithm in C language

The following is the C language implementation of Prim's Algorithm:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define INF 99999 // Infinity, indicating unreachable
  5. int n; // number of vertices
  6. int m; // number of sides
  7. int graph[10][10]; // graph represented by adjacency matrix
  8. int prim() {
  9.     int parent[10]; //Storage the parent node of each vertex in the constructed MST
  10.     int key[10]; //Storage the distance (weight) from each vertex to MST
  11.     bool visited[10] = {false}; // Mark each vertex whether it has been visited
  12.     for (int i = 0; i < n; i++) {
  13.         key[i] = INF; // Initialize the distance from all vertices to MST to infinity
  14.     }
  15.     key[0] = 0; // Let the first vertex be the first vertex of MST
  16.     parent[0] = -1; // The first vertex is the first vertex of MST
  17.     for (int i = 0; i < n - 1; i++) {
  18.         // Select a vertex v with the smallest distance from the unvisited vertices
  19.         int min_key = INF;
  20.         int min_index = -1;
  21.         for (int j = 0; j < n; j++) {
  22.             if (!visited[j] && key[j] < min_key) {
  23.                 min_key = key[j];
  24.                 min_index = j;
  25.             }
  26.         }
  27.         visited[min_index] = true; // Mark the selected vertex v as visited
  28.         // Update the distance from the vertex adjacent to v to MST
  29.         for (int j = 0; j < n; j++) {
  30.             if (graph[min_index][j] != 0 && !visited[j] && graph[min_index][j] < key[j]) {
  31.                 parent[j] = min_index; // Set the parent node of j to min_index, that is, v
  32.                 key[j] = graph[min_index][j]; // Update key[j], that is, update the distance from j to MST
  33.             }
  34.         }
  35.     }
  36.     // Output the weight of each vertex in the constructed MST and the parent node in the MST
  37.     printf("Edge \tWeight\n");
  38.     for (int i = 1; i < n; i++) {
  39.         printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);
  40.     }
  41.     return 0;
  42. }
  43. int main() {
  44.     printf("Enter the number of vertices and edges: ");
  45.     scanf("%d %d", &n, &m);
  46.     printf("Enter the adjacency matrix of the graph:\n");
  47.     for (int i = 0; i < m; i++) {
  48.         for (int j = 0; j < n; j++) {
  49.             scanf("%d", &graph[i][j]);
  50.         }
  51.     }
  52.     prim(); // Call the Prim function to solve MST and output the result
  53.     return 0;
  54. }

This code implements Prim's Algorithm to find the Minimum Spanning Tree (MST) of a given graph. Here's an explanation of the code's flow:

  1. Enter the number of vertices n and the number of edges m.
  2. Enter the graph represented by the adjacency matrix. The adjacency matrix is ​​an n×n two-dimensional array, where graph[i][j] represents the weight of the edge between vertex i and vertex j, and 0 represents no edge between the two vertices. .
  3. Define an array parent to store the parent node of each vertex in the constructed MST. Initially, the parent node of all vertices except the first vertex (the starting point) is set to -1.
  4. Define an array key to store the distance (weight) of each vertex to MST. During initialization, set the distance from all vertices to MST to infinity, except the distance from the starting point to MST, which is set to 0.
  5. Define an array visited to mark whether each vertex has been visited. Initially, all vertices have not been visited.
  6. In the while loop, select a vertex v with the smallest distance from the unvisited vertices. Selecting a vertex with the smallest distance from these unvisited vertices can be achieved by traversing all unvisited vertices and updating their distances.
  7. Mark the selected vertex v as visited, and update the distance from the vertices adjacent to v to the MST. If the distance between an adjacent vertex is less than the current distance from the vertex to the MST, update the distance from the vertex to the MST and set the parent node of the vertex to v.
  8. Repeat the above steps until all vertices have been visited or MST contains n-1 edges.
  9. Output the weight of each vertex in the constructed MST and the parent node in the MST. This can be achieved by traversing all vertices and outputting their parent nodes and weights.

This code uses an adjacency matrix to represent the graph, which is a more intuitive way. However, in practical applications, in order to reduce memory usage and improve operating efficiency, other data structures such as adjacency lists are usually used to represent graphs.

Guess you like

Origin blog.csdn.net/jiazi1024/article/details/135145323