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
- //note : non-directed graph
- #include<iostream>
- using namespace std;
- int vertex,edge,source;
- int adjacency_matrix[100][100],weight_matrix[100][100];
- int parent[100],cost[100],visited[100];
- void inputGraph();
- void Initialize_Single_Source();
- int Extract_Min();
- void Prim();
- void printCostAndPath();
- int main(void)
- {
- inputGraph();
- Initialize_Single_Source();
- Prim();
- printCostAndPath();
- 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;
- }
- }
- // 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;
- //non directed
- adjacency_matrix[start][finish]=1;
- adjacency_matrix[finish][start]=1;
- cout<<"Enter weight for edge "<<i<<" : ";
- int wt;
- cin>>wt;
- //non directed
- weight_matrix[start][finish]=wt;
- weight_matrix[finish][start]=wt;
- }
- 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]<<" ";
- }
- cout<<endl;
- }
- cout<<endl<<"The weight matrix is : "<<endl<<endl;
- for(i=1; i<=vertex; i++)
- {
- for(j=1; j<=vertex; j++)
- {
- cout<<weight_matrix[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- void Initialize_Single_Source()
- {
- cout<<endl<<"Enter source : ";
- cin>>source;
- int i;
- for(i=1; i<=vertex; i++)
- {
- cost[i]=999999;
- parent[i]=0;
- //extra line
- visited[i]=0;
- }
- cost[source]=0;
- cout<<endl;
- }
- int Extract_Min()
- {
- int minimumCost = 999999;
- int i=0,saveNode=0;
- for(i=1; i<=vertex; i++)
- {
- if(visited[i]==0)
- {
- if(cost[i]<minimumCost)
- {
- minimumCost=cost[i];
- saveNode=i;
- }
- }
- }
- visited[saveNode]=1;
- return saveNode;
- }
- void relax(int u,int v)
- {
- if( cost[v]> (cost[u]+weight_matrix[u][v]))
- {
- cost[v]=(cost[u]+weight_matrix[u][v]);
- parent[v]=u;
- }
- }
- void Prim()
- {
- int i,j,totalVertex=1;
- while(totalVertex<=vertex)
- {
- int minCost = Extract_Min();
- for(j=1; j<=vertex; j++)
- {
- if(adjacency_matrix[minCost][j]==1)
- {
- if(visited[j]==0)
- {
- if(weight_matrix[minCost][j]<cost[j])
- {
- parent[j]=minCost;
- cost[j]=weight_matrix[minCost][j];
- }
- }
- }
- }
- totalVertex++;
- }
- }
- void printCostAndPath()
- {
- cout<<endl<<"Safe Edges and Weights : "<<endl<<endl;
- int i,totalMincost=0;
- for(i=1; i<=vertex; i++)
- {
- //cout<<"For node "<<i<<" : "<<endl;
- //cout<<"Cost : "<<cost[i]<<endl;
- //cout<<"Path : End <- "<<i<<" <- ";
- int l = parent[i];
- while(true)
- {
- if(l==source||l==0)
- {
- if(l==source)
- {
- //cout<<l<<" <- ";
- }
- break;
- }
- else
- {
- //cout<<l<<" <- ";
- l=parent[l];
- }
- }
- //cout<<" Start"<<endl;
- if(i!=source)
- {
- cout<<"Edge : ("<<i<<","<<parent[i]<<") \t Weight : "<<cost[i]<<endl<<endl;
- }
- totalMincost=totalMincost+cost[i];
- }
- /*
- //for minimum cost
- int minCost = 999999;
- int nodeSaver;
- for(i=1; i<=vertex; i++)
- {
- // do not count if it is source node
- if(i!=source)
- {
- if(cost[i]<minCost)
- {
- minCost=cost[i];
- nodeSaver=i;
- }
- }
- }
- cout<<"From source node the minimum cost (without source node) is : "<<endl;
- cout<<"Node number : "<<nodeSaver<<endl;
- cout<<"Cost : "<<minCost<<endl;
- cout<<"Path : End <- "<<nodeSaver<<" <- ";
- int l = parent[nodeSaver];
- while(true)
- {
- if(l==source||l==0)
- {
- if(l==source)
- {
- cout<<l<<" <- ";
- break;
- }
- }
- else
- {
- cout<<l<<" <- ";
- l=parent[l];
- }
- }
- cout<<" Start"<<endl;
- */
- cout<<endl<<"Total minimum weight : "<<totalMincost<<endl;
- }
Add Comment
Please, Sign In to add comment