Advertisement
rootUser

Bellman-Ford (OWN)

Jul 9th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 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();
  13. int BellmanFord();
  14. void printCostAndPath(int);
  15.  
  16. int main(void)
  17. {
  18.     inputGraph();
  19.     Initialize_Single_Source();
  20.     int i = BellmanFord();
  21.     printCostAndPath(i);
  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.     // scan adjacency matrix and weight
  40.     for(i=1; i<=edge; i++)
  41.     {
  42.         int start,finish;
  43.         cout<<"Enter start and end node for edge "<<i<<" : ";
  44.         cin>>start;
  45.         cin>>finish;
  46.         adjacency_matrix[start][finish]=1;
  47.         cout<<"Enter weight for edge "<<i<<" : ";
  48.         cin>>weight_matrix[start][finish];
  49.     }
  50.     cout<<"The adjacency matrix is : "<<endl;
  51.     for(i=1; i<=vertex; i++)
  52.     {
  53.         for(j=1; j<=vertex; j++)
  54.         {
  55.             cout<<adjacency_matrix[i][j]<<" ";
  56.         }
  57.         cout<<endl;
  58.     }
  59.     cout<<"The weight matrix is : "<<endl;
  60.     for(i=1; i<=vertex; i++)
  61.     {
  62.         for(j=1; j<=vertex; j++)
  63.         {
  64.             cout<<weight_matrix[i][j]<<" ";
  65.         }
  66.         cout<<endl;
  67.     }
  68. }
  69. void Initialize_Single_Source()
  70. {
  71.     cout<<"Enter source : ";
  72.     cin>>source;
  73.     int i;
  74.     for(i=1; i<=vertex; i++)
  75.     {
  76.         cost[i]=999999;
  77.         parent[i]=0;
  78.         //extra line
  79.         visited[i]=0;
  80.     }
  81.     cost[source]=0;
  82. }
  83. /*
  84. int Extract_Min()
  85. {
  86.     int minimumCost = 999999;
  87.     int i=0,saveNode=0;
  88.     for(i=1; i<=vertex; i++)
  89.     {
  90.         if(visited[i]==0)
  91.         {
  92.             if(cost[i]<minimumCost)
  93.             {
  94.                 minimumCost=cost[i];
  95.                 saveNode=i;
  96.             }
  97.         }
  98.     }
  99.     visited[saveNode]=1;
  100.     return saveNode;
  101. }
  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. int BellmanFord()
  112. {
  113.     int i,j,totalVertex=1;;
  114.     while(totalVertex<=vertex)
  115.     {
  116.         //int minCost = Extract_Min();
  117.         for(i=1; i<=vertex; i++)
  118.         {
  119.             for(j=1; j<=vertex; j++)
  120.             {
  121.                 if(adjacency_matrix[i][j]==1)
  122.                 {
  123.                     relax(i,j);
  124.                 }
  125.             }
  126.         }
  127.         totalVertex++;
  128.     }
  129.     int m,n;
  130.     int flag=1;
  131.     for(m=1; m<=vertex; m++)
  132.     {
  133.         for(n=1; n<=vertex; n++)
  134.         {
  135.             if(adjacency_matrix[m][n]==1)
  136.             {
  137.                 if(cost[m]+weight_matrix[m][n]<cost[n])
  138.                 {
  139.                     flag=0;
  140.                 }
  141.             }
  142.         }
  143.     }
  144.     if(flag==1)
  145.     {
  146.         //cout<<"True"<<endl;
  147.         return 1;
  148.     }
  149.     else if(flag==0)
  150.     {
  151.         //cout<<"False"<<endl;
  152.         return 0;
  153.     }
  154. }
  155. void printCostAndPath(int isPossible)
  156. {
  157.     if(isPossible==1)
  158.     {
  159.         cout<<"Bellman ford is possible for this graph"<<endl;
  160.         int i;
  161.         for(i=1; i<=vertex; i++)
  162.         {
  163.             cout<<"For node "<<i<<" : "<<endl;
  164.             cout<<"Cost : "<<cost[i]<<endl;
  165.  
  166.             cout<<"Path : End <- "<<i<<" <- ";
  167.             int l = parent[i];
  168.             while(true)
  169.             {
  170.                 if(l==source||l==0)
  171.                 {
  172.                     if(l==source)
  173.                     {
  174.                         cout<<l<<" <- ";
  175.                     }
  176.                     break;
  177.                 }
  178.                 else
  179.                 {
  180.                     cout<<l<<" <- ";
  181.                     l=parent[l];
  182.  
  183.                 }
  184.             }
  185.             cout<<" Start"<<endl;
  186.  
  187.         }
  188.  
  189.         //for minimum cost
  190.         int minCost = 999999;
  191.         int nodeSaver;
  192.         for(i=1; i<=vertex; i++)
  193.         {
  194.             // do not count if it is source node
  195.             if(i!=source)
  196.             {
  197.                 if(cost[i]<minCost)
  198.                 {
  199.                     minCost=cost[i];
  200.                     nodeSaver=i;
  201.                 }
  202.             }
  203.         }
  204.         cout<<"From source node the minimum cost (without source node) is : "<<endl;
  205.         cout<<"Node number : "<<nodeSaver<<endl;
  206.         cout<<"Cost : "<<minCost<<endl;
  207.         cout<<"Path : End <- "<<nodeSaver<<" <- ";
  208.         int l = parent[nodeSaver];
  209.         while(true)
  210.         {
  211.             if(l==source||l==0)
  212.             {
  213.                 if(l==source)
  214.                 {
  215.                     cout<<l<<" <- ";
  216.                     break;
  217.                 }
  218.             }
  219.             else
  220.             {
  221.                 cout<<l<<" <- ";
  222.                 l=parent[l];
  223.  
  224.             }
  225.         }
  226.         cout<<" Start"<<endl;
  227.     }
  228.     else if(isPossible==0)
  229.     {
  230.         cout<<"Bellman Ford is not possible for this graph"<<endl;
  231.     }
  232. }
  233. /*
  234. input:
  235. 5
  236. 10
  237. 1 2
  238. 6
  239. 1 5
  240. 7
  241. 2 3
  242. 5
  243. 2 4
  244. -4
  245. 2 5
  246. 8
  247. 3 2
  248. -2
  249. 4 1
  250. 2
  251. 4 4
  252. 7
  253. 5 3
  254. -3
  255. 5 4
  256. 9
  257. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement