Advertisement
rootUser

Floyed-Warshall (OWN)

Jul 9th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 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],parent_matrix[100][100];
  7. int weight_matrix_1[100][100][100],parent_matrix_1[100][100][100];
  8. int parent[100],cost[100],visited[100];
  9.  
  10. void inputGraph();
  11. int findMinimum(int,int);
  12. void initializeMatrix();
  13. void showMatrix();
  14. void output();
  15.  
  16. int main(void)
  17. {
  18.     inputGraph();
  19.     initializeMatrix();
  20.     showMatrix();
  21.     output();
  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.             parent_matrix[i][j]=0;
  38.             if(i==j)
  39.             {
  40.                 weight_matrix[i][j]=0;
  41.             }
  42.         }
  43.     }
  44.     // scan adjacency matrix and weight
  45.     for(i=1; i<=edge; i++)
  46.     {
  47.         int start,finish;
  48.         cout<<"Enter start and end node for edge "<<i<<" : ";
  49.         cin>>start;
  50.         cin>>finish;
  51.         adjacency_matrix[start][finish]=1;
  52.         parent_matrix[start][finish]=start;
  53.         cout<<"Enter weight for edge "<<i<<" : ";
  54.         cin>>weight_matrix[start][finish];
  55.     }
  56.     cout<<endl<<"The adjacency matrix is : "<<endl<<endl;
  57.     for(i=1; i<=vertex; i++)
  58.     {
  59.         for(j=1; j<=vertex; j++)
  60.         {
  61.             cout<<adjacency_matrix[i][j]<<" \t\t";
  62.         }
  63.         cout<<endl;
  64.     }
  65.     cout<<endl;
  66.     cout<<"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]<<" \t\t";
  72.         }
  73.         cout<<endl;
  74.     }
  75.     cout<<endl;
  76.     cout<<"The parent matrix is : "<<endl<<endl;
  77.     for(i=1; i<=vertex; i++)
  78.     {
  79.         for(j=1; j<=vertex; j++)
  80.         {
  81.             cout<<parent_matrix[i][j]<<" \t\t";
  82.         }
  83.         cout<<endl;
  84.     }
  85.     cout<<endl;
  86. }
  87. int findMinimum(int a,int b)
  88. {
  89.     if(a<b)
  90.     {
  91.         return a;
  92.     }
  93.     return b;
  94. }
  95. void initializeMatrix()
  96. {
  97.     int i,j,k;
  98.     //take input
  99.     for(k=0; k<=vertex; k++)
  100.     {
  101.         for(i=1; i<=vertex; i++)
  102.         {
  103.             for(j=1; j<=vertex; j++)
  104.             {
  105.                 //calculate distance-dij
  106.                 if(k==0)
  107.                 {
  108.                     weight_matrix_1[k][i][j]=weight_matrix[i][j];
  109.                 }
  110.                 else
  111.                 {
  112.                     weight_matrix_1[k][i][j]=findMinimum(weight_matrix_1[k-1][i][j],(weight_matrix_1[k-1][i][k]+weight_matrix_1[k-1][k][j]));
  113.                 }
  114.                 //calculate parent-Pij
  115.                 if(k==0)
  116.                 {
  117.                     parent_matrix_1[k][i][j]=parent_matrix[i][j];
  118.                 }
  119.                 else
  120.                 {
  121.                     if(weight_matrix_1[k-1][i][j]<=(weight_matrix_1[k-1][i][k]+weight_matrix_1[k-1][k][j]))
  122.                     {
  123.                         parent_matrix_1[k][i][j]=parent_matrix_1[k-1][i][j];
  124.                     }
  125.                     else if(weight_matrix_1[k-1][i][j]>(weight_matrix_1[k-1][i][k]+weight_matrix_1[k-1][k][j]))
  126.                     {
  127.                         parent_matrix_1[k][i][j]=parent_matrix_1[k-1][k][j];
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.     }
  133. }
  134. void showMatrix()
  135. {
  136.     int i,j,k;
  137.     //show output
  138.     for(k=0; k<=vertex; k++)
  139.     {
  140.         //print weight
  141.         cout<<"Dij K["<<k<<"] : "<<endl<<endl;
  142.         for(i=1; i<=vertex; i++)
  143.         {
  144.             for(j=1; j<=vertex; j++)
  145.             {
  146.                 cout<<weight_matrix_1[k][i][j]<<" \t\t";
  147.             }
  148.             cout<<endl;
  149.         }
  150.         cout<<endl;
  151.         //print parent
  152.         cout<<"Pij K["<<k<<"] : "<<endl<<endl;
  153.         for(i=1; i<=vertex; i++)
  154.         {
  155.             for(j=1; j<=vertex; j++)
  156.             {
  157.                 cout<<parent_matrix_1[k][i][j]<<" \t\t";
  158.             }
  159.             cout<<endl;
  160.         }
  161.         cout<<endl;
  162.         cout<<endl;
  163.     }
  164. }
  165. void output()
  166. {
  167.     int i,j,k=vertex,sum;
  168.     for(i=1; i<=vertex; i++)
  169.     {
  170.         cout<<endl<<"For node "<<i<<" : "<<endl<<endl;
  171.         sum=0;
  172.         for(j=1; j<=vertex; j++)
  173.         {
  174.             if(parent_matrix_1[k][i][j]==0)
  175.             {
  176.                 cout<<"Shortest distance from "<<i<<" to "<<j<<" is : "<<weight_matrix_1[vertex][i][j]<<" \t\tParent : NIL"<<endl;
  177.             }
  178.             else
  179.             {
  180.                 cout<<"Shortest distance from "<<i<<" to "<<j<<" is : "<<weight_matrix_1[vertex][i][j]<<" \t\tParent : "<<parent_matrix_1[k][i][j]<<endl;
  181.             }
  182.             sum = sum + weight_matrix_1[k][i][j];
  183.         }
  184.         cout<<"Shortest distance for visiting all nodes from node "<<i<<" is : "<<sum<<endl;
  185.     }
  186. }
  187. /*
  188. input:
  189. 5
  190. 9
  191. 1 2
  192. 3
  193. 3 2
  194. 4
  195. 4 3
  196. -5
  197. 5 4
  198. 6
  199. 1 5
  200. -4
  201. 1 3
  202. 8
  203. 4 1
  204. 2
  205. 2 5
  206. 7
  207. 2 4
  208. 1
  209. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement