Created
January 19, 2018 01:44
-
-
Save milon/623b6b76b137a8bb6b30a9dc03a3eedc to your computer and use it in GitHub Desktop.
Data Structure: Directed 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
| //Directed graph | |
| //Author: Milon | |
| #include<iostream> | |
| #include<cstdio> | |
| using namespace std; | |
| int main(){ | |
| int n; | |
| cout<<"Enter the number of nodes: "; | |
| cin>>n; | |
| bool 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)"<<endl; | |
| cout<<"Press (0 0) to end:"<<endl; | |
| //Making graph | |
| while(true){ | |
| int a,b; | |
| cin>>a>>b; | |
| if(a==0 && b==0) | |
| break; | |
| if(a>n || b>n || a<=0 || b<=0) | |
| cout<<"Invalid input"<<endl; | |
| else{ | |
| list[a-1][b-1]=true; | |
| //list[b-1][a-1]=true; //for undirected 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