-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_Representation_2.cpp
More file actions
46 lines (44 loc) · 941 Bytes
/
Graph_Representation_2.cpp
File metadata and controls
46 lines (44 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Author: Efriem Desalew,[email protected]
Desc: a program on how to reprsent a graph using adjacency list
Compiling: g++ -o [object file name] [source file name]
Running: ./[object file name]
*/
#include<iostream>
#include<vector>
using namespace std;
vector<int> adj[10];
/* display graph*/
void disp(int nodes);
int main(){
int x,y,nodes,edges;
cout<<"Enter number of nodes\n";
cin>>nodes;
cout<<"Enter number of edges\n";
cin>>edges;
for(int i=0;i<nodes;i++){
int x=0;
cout<<"Enter nodes connected to node"<<i+1<<endl;
do{
cin>>x;
adj[i].push_back(x-1);
cout<<"Enter 0 to move to another node."<<endl;
cin>>x;
}while(x!=0);
}
/*display graph*/
cout<<"The graph you entered is \n";
disp(nodes);
return 0;
}
/* display graph */
void disp(int nodes){
for(int i=0;i<nodes;i++){
cout<<"Node "<<i+1<<":->";
for(int j=0;j<adj[i].size();j++)
{
cout<<adj[i][j]+1<<" ";
}
cout<<endl;
}
}