Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //note : do not use INT_MAX for comparison,use a big number (i.e. : 999999) instead
- #include<iostream>
- using namespace std;
- int vertex,edge,source;
- int adjacency_matrix[100][100],weight_matrix[100][100],parent_matrix[100][100];
- int weight_matrix_1[100][100][100],parent_matrix_1[100][100][100];
- int parent[100],cost[100],visited[100];
- void inputGraph();
- int findMinimum(int,int);
- void initializeMatrix();
- void showMatrix();
- void output();
- int main(void)
- {
- inputGraph();
- initializeMatrix();
- showMatrix();
- output();
- return 0;
- }
- void inputGraph()
- {
- cout<<"Total vertex : ";
- cin>>vertex;
- cout<<"Total edge : ";
- cin>>edge;
- int i,j;
- //make all weight infinity
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- weight_matrix[i][j]=999999;
- parent_matrix[i][j]=0;
- if(i==j)
- {
- weight_matrix[i][j]=0;
- }
- }
- }
- // scan adjacency matrix and weight
- for(i=1; i<=edge; i++)
- {
- int start,finish;
- cout<<"Enter start and end node for edge "<<i<<" : ";
- cin>>start;
- cin>>finish;
- adjacency_matrix[start][finish]=1;
- parent_matrix[start][finish]=start;
- cout<<"Enter weight for edge "<<i<<" : ";
- cin>>weight_matrix[start][finish];
- }
- cout<<endl<<"The adjacency matrix is : "<<endl<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<adjacency_matrix[i][j]<<" \t\t";
- }
- cout<<endl;
- }
- cout<<endl;
- cout<<"The weight matrix is : "<<endl<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<weight_matrix[i][j]<<" \t\t";
- }
- cout<<endl;
- }
- cout<<endl;
- cout<<"The parent matrix is : "<<endl<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<parent_matrix[i][j]<<" \t\t";
- }
- cout<<endl;
- }
- cout<<endl;
- }
- int findMinimum(int a,int b)
- {
- if(a<b)
- {
- return a;
- }
- return b;
- }
- void initializeMatrix()
- {
- int i,j,k;
- //take input
- for(k=0; k<=vertex; k++)
- {
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- //calculate distance-dij
- if(k==0)
- {
- weight_matrix_1[k][i][j]=weight_matrix[i][j];
- }
- else
- {
- 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]));
- }
- //calculate parent-Pij
- if(k==0)
- {
- parent_matrix_1[k][i][j]=parent_matrix[i][j];
- }
- else
- {
- if(weight_matrix_1[k-1][i][j]<=(weight_matrix_1[k-1][i][k]+weight_matrix_1[k-1][k][j]))
- {
- parent_matrix_1[k][i][j]=parent_matrix_1[k-1][i][j];
- }
- else if(weight_matrix_1[k-1][i][j]>(weight_matrix_1[k-1][i][k]+weight_matrix_1[k-1][k][j]))
- {
- parent_matrix_1[k][i][j]=parent_matrix_1[k-1][k][j];
- }
- }
- }
- }
- }
- }
- void showMatrix()
- {
- int i,j,k;
- //show output
- for(k=0; k<=vertex; k++)
- {
- //print weight
- cout<<"Dij K["<<k<<"] : "<<endl<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<weight_matrix_1[k][i][j]<<" \t\t";
- }
- cout<<endl;
- }
- cout<<endl;
- //print parent
- cout<<"Pij K["<<k<<"] : "<<endl<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<parent_matrix_1[k][i][j]<<" \t\t";
- }
- cout<<endl;
- }
- cout<<endl;
- cout<<endl;
- }
- }
- void output()
- {
- int i,j,k=vertex,sum;
- for(i=1; i<=vertex; i++)
- {
- cout<<endl<<"For node "<<i<<" : "<<endl<<endl;
- sum=0;
- for(j=1; j<=vertex; j++)
- {
- if(parent_matrix_1[k][i][j]==0)
- {
- cout<<"Shortest distance from "<<i<<" to "<<j<<" is : "<<weight_matrix_1[vertex][i][j]<<" \t\tParent : NIL"<<endl;
- }
- else
- {
- cout<<"Shortest distance from "<<i<<" to "<<j<<" is : "<<weight_matrix_1[vertex][i][j]<<" \t\tParent : "<<parent_matrix_1[k][i][j]<<endl;
- }
- sum = sum + weight_matrix_1[k][i][j];
- }
- cout<<"Shortest distance for visiting all nodes from node "<<i<<" is : "<<sum<<endl;
- }
- }
- /*
- input:
- 5
- 9
- 1 2
- 3
- 3 2
- 4
- 4 3
- -5
- 5 4
- 6
- 1 5
- -4
- 1 3
- 8
- 4 1
- 2
- 2 5
- 7
- 2 4
- 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement