-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graphs linked list.cpp
158 lines (128 loc) · 3.31 KB
/
Graphs linked list.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <list>
#include<queue>
#include<stack>
#include <vector>
using namespace std;
// Linked list implementation of graph
class Graph {
vector<list<int>> adjList;
int size;
public:
Graph(int nodes){
// Created a vector of size n
adjList.resize(nodes);
size = nodes;
}
void addEdge(int,int);
void printAdjList();
void printAdjNodes(int);
vector<int> getAdjNodes(int);
void printBFS();
void printDFS();
void recBFS(int);
};
void Graph::addEdge(int n1, int n2) {
adjList[n1].push_back(n2);
adjList[n2].push_back(n1);
}
void Graph :: printDFS(){
// Initialise all false array
cout << "Depth First Search (DFS) : ";
bool visited[size] = {false};
stack<int> st;
st.push(0);
while(!st.empty()){
int Front = st.top();
cout << Front << " " ;
visited[Front] = true;
st.pop();
vector<int> adjNodes = getAdjNodes(Front);
int adjNodesSize = adjNodes.size();
for(int i=0;i<adjNodesSize;i++){
if(visited[adjNodes[i]]!=true){
// We did not visit them
// So push into queue
st.push(adjNodes[i]);
}
}
}
cout << endl;
}
void Graph :: printBFS(){
cout << "Breadth First Search (DFS) : ";
// Initialise all false array
bool visited[size] = {false};
queue<int> qu;
qu.push(0);
while(!qu.empty()){
int Front = qu.front();
cout << Front << " " ;
visited[Front] = true;
qu.pop();
vector<int> adjNodes = getAdjNodes(Front);
int adjNodesSize = adjNodes.size();
for(int i=0;i<adjNodesSize;i++){
if(visited[adjNodes[i]]!=true){
// We did not visit them
// So push into queue
qu.push(adjNodes[i]);
}
}
}
cout << endl;
}
void Graph::printAdjList() {
cout << "\nAdjacency List representation using vector of list : \n\n";
cout << "Nodes AdjNodes\n";
for (int i = 0; i < adjList.size(); i++) {
cout << i << " -> ";
for (int node : adjList[i]) {
cout << node << ", ";
}
cout << endl;
}
cout << endl;
}
vector<int> Graph :: getAdjNodes(int node){
// Get the list at the given node
vector<int> adjNodes;
list<int> lst = adjList[node];
for(int n : lst){
adjNodes.push_back(n);
}
return adjNodes;
}
void Graph :: printAdjNodes(int node){
// Get the list at the given node
list<int> lst = adjList[node];
cout << "Adjacent Nodes of " << node << " are : " ;
for(int n : lst){
cout << n << " " ;
}cout << endl;
}
int main() {
int N;
cout << "Enter number of nodes: ";
cin >> N;
Graph g(N);
int M;
cout << "Enter number of edges: ";
cin >> M;
for (int i = 1; i <= M; i++) {
int u, v;
cout << "Enter connection " << i << " : ";
cin >> u >> v;
g.addEdge(u, v);
}
// g.printAdjList();
// int node;
// cout << "Enter any node : " ;
// cin >> node;
// g.printAdjNodes(node);
g.printBFS();
cout << endl;
g.printDFS();
cout << endl;
return 0;
}