Skip to content

Instantly share code, notes, and snippets.

@milon
Created January 19, 2018 01:47
Show Gist options
  • Select an option

  • Save milon/c71e9c066dca9fb7707525cb2b514cdc to your computer and use it in GitHub Desktop.

Select an option

Save milon/c71e9c066dca9fb7707525cb2b514cdc to your computer and use it in GitHub Desktop.
Data Structure: Weighted Graph
//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