Advertisement
kartikkukreja

Prim's MST algorithm

May 22nd, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 KB | None | 0 0
  1. #include <cstdio>
  2. #include <climits>
  3. #include <cstring>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. /*
  8.  * Indexed min priority queue
  9.  * Supports insertion in O(log N), deletion of any key (regardless of whether
  10.  * the key is the minimum key or not) in O(log N) and changes to key values
  11.  * in O(log N), where N is the number of
  12.  * elements currently in the PQ
  13.  */
  14. class MinIndexedPQ {
  15.     int NMAX, N, *heap, *index, *keys;
  16.  
  17.     void swap(int i, int j) {
  18.         int t = heap[i]; heap[i] = heap[j]; heap[j] = t;
  19.         index[heap[i]] = i; index[heap[j]] = j;
  20.     }
  21.  
  22.     void bubbleUp(int k)    {
  23.         while(k > 1 && keys[heap[k/2]] > keys[heap[k]])   {
  24.             swap(k, k/2);
  25.             k = k/2;
  26.         }
  27.     }
  28.  
  29.     void bubbleDown(int k)  {
  30.         int j;
  31.         while(2*k <= N) {
  32.             j = 2*k;
  33.             if(j < N && keys[heap[j]] > keys[heap[j+1]])
  34.                 j++;
  35.             if(keys[heap[k]] <= keys[heap[j]])
  36.                 break;
  37.             swap(k, j);
  38.             k = j;
  39.         }
  40.     }
  41.  
  42. public:
  43.     // Create an empty MinIndexedPQ which can contain atmost NMAX elements
  44.     MinIndexedPQ(int NMAX)  {
  45.         this->NMAX = NMAX;
  46.         N = 0;
  47.         keys = new int[NMAX + 1];
  48.         heap = new int[NMAX + 1];
  49.         index = new int[NMAX + 1];
  50.         for(int i = 0; i <= NMAX; i++)
  51.             index[i] = -1;
  52.     }
  53.     ~MinIndexedPQ() {
  54.         delete [] keys;
  55.         delete [] heap;
  56.         delete [] index;
  57.     }
  58.  
  59.     // check if the PQ is empty
  60.     bool isEmpty()  {
  61.         return N == 0;
  62.     }
  63.  
  64.     // check if i is an index on the PQ
  65.     bool contains(int i)    {
  66.         return index[i] != -1;
  67.     }
  68.  
  69.     // associate key with index i; 0 < i < NMAX
  70.     void insert(int i, int key) {
  71.         N++;
  72.         index[i] = N;
  73.         heap[N] = i;
  74.         keys[i] = key;
  75.         bubbleUp(N);
  76.     }
  77.  
  78.     // delete the minimal key and return its associated index
  79.     // Warning: Don't try to read from this index after calling this function
  80.     int deleteMin() {
  81.         int min = heap[1];
  82.         swap(1, N--);
  83.         bubbleDown(1);
  84.         index[min] = -1;
  85.         heap[N+1] = -1;
  86.         return min;
  87.     }
  88.  
  89.     // decrease the key associated with index i to the specified value
  90.     void decreaseKey(int i, int key)    {
  91.         keys[i] = key;
  92.         bubbleUp(index[i]);
  93.     }
  94. };
  95.  
  96.  
  97. // representation of directed edge to vertex 'to' having weight 'weight'
  98. struct Edge {
  99.     int to, weight;
  100. };
  101.  
  102. typedef vector <Edge*> adjList;
  103.  
  104. int main()  {
  105.     int V, E, i, u, v, cost, *dist, *edgeTo;
  106.     bool *marked;
  107.     Edge *tmp;
  108.     adjList *G;
  109.  
  110.     // Assuming vertices are labeled 0...V-1
  111.     //freopen("input1.txt", "r", stdin);
  112.  
  113.     scanf("%d %d", &V, &E); // Enter the number of vertices and edges
  114.     G = new adjList[V];
  115.     for(i=0; i<E; i++)  {   // Enter E undirected edges (u, v, weight)
  116.         scanf("%d %d %d", &u, &v, &cost);
  117.         tmp = new Edge;
  118.         tmp->to = v; tmp->weight = cost;
  119.         G[u].push_back(tmp);    // add edge to adjacency list of u
  120.         tmp = new Edge;
  121.         tmp->to = u; tmp->weight = cost;
  122.         G[v].push_back(tmp);    // add edge to adjacency list of v
  123.     }
  124.  
  125.     dist = new int[V];
  126.     marked = new bool[V];
  127.     for(i=1; i<V; i++)
  128.         dist[i] = INT_MAX;
  129.     dist[0] = 0;
  130.     edgeTo = new int[V];
  131.     memset(marked, false, V*sizeof(bool));
  132.  
  133.     MinIndexedPQ pq(V);
  134.     pq.insert(0, 0);
  135.  
  136.     while(!pq.isEmpty())    {
  137.         u = pq.deleteMin();
  138.         marked[u] = true;
  139.         for(adjList::iterator it = G[u].begin(); it != G[u].end(); it++)    {
  140.             v = (*it)->to;
  141.             if(marked[v]) continue;
  142.             if((*it)->weight < dist[v])    {
  143.                 dist[v] = (*it)->weight;
  144.                 edgeTo[v] = u;
  145.                 if(pq.contains(v)) pq.decreaseKey(v, dist[v]);
  146.                 else pq.insert(v, dist[v]);
  147.             }
  148.         }
  149.     }
  150.  
  151.     printf("Edges in MST :\n");
  152.     for(i=1, cost=0; i<V; i++)  {
  153.         cost += dist[i];
  154.         printf("%d %d %d\n", edgeTo[i], i, dist[i]);
  155.     }
  156.     printf("Total cost of MST : %d\n", cost);
  157.  
  158.     delete [] dist;
  159.     delete [] edgeTo;
  160.     delete [] marked;
  161.     delete [] G;
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement