rootUser

Prim (OWN)

Jul 9th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. //note : do not use INT_MAX for comparison,use a big number (i.e. : 999999) instead
  2. //note : non-directed graph
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. int vertex,edge,source;
  7. int adjacency_matrix[100][100],weight_matrix[100][100];
  8. int parent[100],cost[100],visited[100];
  9.  
  10. void inputGraph();
  11. void Initialize_Single_Source();
  12. int Extract_Min();
  13. void Prim();
  14. void printCostAndPath();
  15.  
  16. int main(void)
  17. {
  18.     inputGraph();
  19.     Initialize_Single_Source();
  20.     Prim();
  21.     printCostAndPath();
  22.     return 0;
  23. }
  24. void inputGraph()
  25. {
  26.     cout<<"Total vertex : ";
  27.     cin>>vertex;
  28.     cout<<"Total edge   : ";
  29.     cin>>edge;
  30.     int i,j;
  31.     //make all weight infinity
  32.     for(i=1; i<=vertex; i++)
  33.     {
  34.         for(j=1; j<=vertex; j++)
  35.         {
  36.             weight_matrix[i][j]=999999;
  37.         }
  38.  
  39.     }
  40.     // scan adjacency matrix and weight
  41.     for(i=1; i<=edge; i++)
  42.     {
  43.         int start,finish;
  44.         cout<<"Enter start and end node for edge "<<i<<" : ";
  45.         cin>>start;
  46.         cin>>finish;
  47.         //non directed
  48.         adjacency_matrix[start][finish]=1;
  49.         adjacency_matrix[finish][start]=1;
  50.         cout<<"Enter weight for edge "<<i<<" : ";
  51.         int wt;
  52.         cin>>wt;
  53.         //non directed
  54.         weight_matrix[start][finish]=wt;
  55.         weight_matrix[finish][start]=wt;
  56.     }
  57.     cout<<endl<<"The adjacency matrix is : "<<endl<<endl;
  58.     for(i=1; i<=vertex; i++)
  59.     {
  60.         for(j=1; j<=vertex; j++)
  61.         {
  62.             cout<<adjacency_matrix[i][j]<<" ";
  63.         }
  64.         cout<<endl;
  65.     }
  66.     cout<<endl<<"The weight matrix is : "<<endl<<endl;
  67.     for(i=1; i<=vertex; i++)
  68.     {
  69.         for(j=1; j<=vertex; j++)
  70.         {
  71.             cout<<weight_matrix[i][j]<<" ";
  72.         }
  73.         cout<<endl;
  74.     }
  75. }
  76. void Initialize_Single_Source()
  77. {
  78.     cout<<endl<<"Enter source : ";
  79.     cin>>source;
  80.     int i;
  81.     for(i=1; i<=vertex; i++)
  82.     {
  83.         cost[i]=999999;
  84.         parent[i]=0;
  85.         //extra line
  86.         visited[i]=0;
  87.     }
  88.     cost[source]=0;
  89.     cout<<endl;
  90. }
  91. int Extract_Min()
  92. {
  93.     int minimumCost = 999999;
  94.     int i=0,saveNode=0;
  95.     for(i=1; i<=vertex; i++)
  96.     {
  97.         if(visited[i]==0)
  98.         {
  99.             if(cost[i]<minimumCost)
  100.             {
  101.                 minimumCost=cost[i];
  102.                 saveNode=i;
  103.             }
  104.         }
  105.     }
  106.     visited[saveNode]=1;
  107.     return saveNode;
  108. }
  109. void relax(int u,int v)
  110. {
  111.     if( cost[v]> (cost[u]+weight_matrix[u][v]))
  112.     {
  113.         cost[v]=(cost[u]+weight_matrix[u][v]);
  114.         parent[v]=u;
  115.     }
  116. }
  117. void Prim()
  118. {
  119.     int i,j,totalVertex=1;
  120.     while(totalVertex<=vertex)
  121.     {
  122.         int minCost = Extract_Min();
  123.         for(j=1; j<=vertex; j++)
  124.         {
  125.             if(adjacency_matrix[minCost][j]==1)
  126.             {
  127.                 if(visited[j]==0)
  128.                 {
  129.                     if(weight_matrix[minCost][j]<cost[j])
  130.                     {
  131.                         parent[j]=minCost;
  132.                         cost[j]=weight_matrix[minCost][j];
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.         totalVertex++;
  138.     }
  139. }
  140.  
  141. void printCostAndPath()
  142. {
  143.     cout<<endl<<"Safe Edges and Weights : "<<endl<<endl;
  144.     int i,totalMincost=0;
  145.     for(i=1; i<=vertex; i++)
  146.     {
  147.         //cout<<"For node "<<i<<" : "<<endl;
  148.         //cout<<"Cost : "<<cost[i]<<endl;
  149.  
  150.  
  151.         //cout<<"Path : End <- "<<i<<" <- ";
  152.         int l = parent[i];
  153.         while(true)
  154.         {
  155.             if(l==source||l==0)
  156.             {
  157.                 if(l==source)
  158.                 {
  159.                     //cout<<l<<" <- ";
  160.                 }
  161.                 break;
  162.             }
  163.             else
  164.             {
  165.                 //cout<<l<<" <- ";
  166.                 l=parent[l];
  167.             }
  168.         }
  169.         //cout<<" Start"<<endl;
  170.         if(i!=source)
  171.         {
  172.             cout<<"Edge : ("<<i<<","<<parent[i]<<") \t Weight : "<<cost[i]<<endl<<endl;
  173.         }
  174.         totalMincost=totalMincost+cost[i];
  175.     }
  176.     /*
  177.     //for minimum cost
  178.     int minCost = 999999;
  179.     int nodeSaver;
  180.     for(i=1; i<=vertex; i++)
  181.     {
  182.         // do not count if it is source node
  183.         if(i!=source)
  184.         {
  185.             if(cost[i]<minCost)
  186.             {
  187.                 minCost=cost[i];
  188.                 nodeSaver=i;
  189.             }
  190.         }
  191.     }
  192.     cout<<"From source node the minimum cost (without source node) is : "<<endl;
  193.     cout<<"Node number : "<<nodeSaver<<endl;
  194.     cout<<"Cost : "<<minCost<<endl;
  195.     cout<<"Path : End <- "<<nodeSaver<<" <- ";
  196.     int l = parent[nodeSaver];
  197.     while(true)
  198.     {
  199.         if(l==source||l==0)
  200.         {
  201.             if(l==source)
  202.             {
  203.                 cout<<l<<" <- ";
  204.                 break;
  205.             }
  206.         }
  207.         else
  208.         {
  209.             cout<<l<<" <- ";
  210.             l=parent[l];
  211.         }
  212.     }
  213.     cout<<" Start"<<endl;
  214.     */
  215.     cout<<endl<<"Total minimum weight : "<<totalMincost<<endl;
  216. }
Add Comment
Please, Sign In to add comment