Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. // Number of vertices in the graph
  5. #define V 5
  6.  
  7. // A utility function to find the vertex with minimum key value, from
  8. // the set of vertices not yet included in MST
  9. int minKey(int key[], bool mstSet[])
  10. {
  11.    // Initialize min value
  12.    int min = INT_MAX, min_index;
  13.  
  14.    for (int v = 0; v < V; v++)
  15.      if (mstSet[v] == false && key[v] < min)
  16.          min = key[v], min_index = v;
  17.  
  18.    return min_index;
  19. }
  20.  
  21. // A utility function to print the constructed MST stored in parent[]
  22. int printMST(int parent[], int n, int graph[V][V])
  23. {
  24.    printf("Edge   Weight\n");
  25.    for (int i = 1; i < V; i++)
  26.       printf("%d - %d    %d \n", parent[i], i, graph[i][parent[i]]);
  27. }
  28.  
  29. // Function to construct and print MST for a graph represented using adjacency
  30. // matrix representation
  31. void primMST(int graph[V][V])
  32. {
  33.      int parent[V]; // Array to store constructed MST
  34.      int key[V];   // Key values used to pick minimum weight edge in cut
  35.      bool mstSet[V];  // To represent set of vertices not yet included in MST
  36.  
  37.      // Initialize all keys as INFINITE
  38.      for (int i = 0; i < V; i++)
  39.         key[i] = INT_MAX, mstSet[i] = false;
  40.  
  41.      // Always include first 1st vertex in MST.
  42.      key[0] = 0;     // Make key 0 so that this vertex is picked as first vertex
  43.      parent[0] = -1; // First node is always root of MST
  44.  
  45.      // The MST will have V vertices
  46.      for (int count = 0; count < V-1; count++)
  47.      {
  48.         // Pick the minimum key vertex from the set of vertices
  49.         // not yet included in MST
  50.         int u = minKey(key, mstSet);
  51.  
  52.         // Add the picked vertex to the MST Set
  53.         mstSet[u] = true;
  54.  
  55.         // Update key value and parent index of the adjacent vertices of
  56.         // the picked vertex. Consider only those vertices which are not yet
  57.         // included in MST
  58.         for (int v = 0; v < V; v++)
  59.  
  60.            // graph[u][v] is non zero only for adjacent vertices of m
  61.            // mstSet[v] is false for vertices not yet included in MST
  62.            // Update the key only if graph[u][v] is smaller than key[v]
  63.           if (graph[u][v] && mstSet[v] == false && graph[u][v] <  key[v])
  64.              parent[v]  = u, key[v] = graph[u][v];
  65.      }
  66.  
  67.      // print the constructed MST
  68.      printMST(parent, V, graph);
  69. }
  70.  
  71.  
  72. // driver program to test above function
  73. int main()
  74. {
  75.    int graph[V][V] = {{0, 4, 4, 6, 6},
  76.                       {4, 0, 2, 0, 0},
  77.                       {4, 2, 0, 8, 0},
  78.                       {6, 0, 8, 0, 9},
  79.                       {6, 0, 0, 9, 0},
  80.                      };
  81.  
  82.     // Print the solution
  83.     primMST(graph);
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement