rootUser

Dijkstra (OWN)

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