Created
January 19, 2018 01:47
-
-
Save milon/c71e9c066dca9fb7707525cb2b514cdc to your computer and use it in GitHub Desktop.
Data Structure: Weighted Graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Weighted graph | |
| //Author: Milon | |
| #include<iostream> | |
| #include<cstdio> | |
| using namespace std; | |
| int main(){ | |
| int n; | |
| cout<<"Enter the number of nodes: "; | |
| cin>>n; | |
| int list[n][n]; | |
| //Initialize | |
| for(int i=0;i<n;i++) | |
| for(int j=0;j<n;j++) | |
| list[i][j]=false; | |
| cout<<"Enter an edge(start_node< >end_node< >weight)"<<endl; | |
| cout<<"Press (0 0 0) to end:"<<endl; | |
| //Making graph | |
| while(true){ | |
| int a,b,c; | |
| cin>>a>>b>>c; | |
| if(a==0 && b==0 && c==0) | |
| break; | |
| if(a>n || b>n || a<=0 || b<=0) | |
| cout<<"Invalid input"<<endl; | |
| else{ | |
| list[a-1][b-1]=c; | |
| //list[b-1][a-1]=true; //for undirected weighted graph | |
| } | |
| } | |
| cout<<endl; | |
| //Print adjacency matrix | |
| cout<<"Adjacency matrix:"<<endl; | |
| for(int i=0;i<n;i++) { | |
| for(int j=0;j<n;j++) | |
| printf("%4d",list[i][j]); | |
| cout<<endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment