-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph_using_maps.cpp
71 lines (55 loc) · 1.44 KB
/
Graph_using_maps.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<unordered_map>
#include<list>
#include<vector>
using namespace std;
class Graph{
unordered_map<int,list<int>> adjList;
public:
void addEdge(int,int,bool);
vector<vector<int>> AdjMatrix();
void printAdjList();
// vector<int> getAdjNode(int);
void BFS();
};
void Graph :: addEdge(int u,int v,bool direction){
// If direction = 0 --> undirected graph
// If direction = 1 --> directed graph
adjList[u].push_back(v); // This means node u is connect to node v
if(direction==0){
// It means it is undirected , if u is connected to v then v is also connected to u
adjList[v].push_back(u);
}
}
// vector<vector<int>> Graph :: AdjMatrix(){
// }
// vector<int> Graph :: getAdjNode(int node){
// }
void Graph :: printAdjList(){
// AdjList is array of linked list
cout << "\nAdjancency List representation : \n\n";
for(auto i:adjList){
cout << i.first << " -> ";
for(auto j:i.second){
cout << j << " ," ;
}
cout << endl;
}
}
int main(){
Graph g;
int N;
cout << "Enter number of nodes : " ;
cin >> N ;
int M ;
cout << "Enter number of edges : " ;
cin >> M ;
for(int i=0;i<M;i++){
int u,v;
cout << "Enter edge - " << i+1 << " corresponding nodes : " ;
cin >> u >> v ;
g.addEdge(u,v,0);
}
g.printAdjList();
return 0;
}