-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphExplained.cpp
189 lines (155 loc) · 4.5 KB
/
GraphExplained.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <stack>
using namespace std;
/* ----------------------------------------------
Adjacency List using unordered_map (for sparse graphs)
- Efficient for sparse graphs
- Vertices can be non-sequential or dynamically added
------------------------------------------------*/
unordered_map<int, vector<int>> adjList;
// Function to add edge in Adjacency List
void addEdgeAdjList(int u, int v) {
adjList[u].push_back(v);
adjList[v].push_back(u); // For undirected graph
}
// BFS for Adjacency List
void bfsAdjList(int start) {
queue<int> q;
unordered_map<int, bool> visited;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
// Visit all neighbors
for (int neighbor : adjList[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
cout << endl;
}
// DFS for Adjacency List
void dfsAdjList(int start) {
stack<int> s;
unordered_map<int, bool> visited;
s.push(start);
while (!s.empty()) {
int node = s.top();
s.pop();
if (!visited[node]) {
cout << node << " ";
visited[node] = true;
// Visit all neighbors
for (int neighbor : adjList[node]) {
if (!visited[neighbor]) {
s.push(neighbor);
}
}
}
}
cout << endl;
}
/* ----------------------------------------------
Adjacency Matrix using 2D vector (for dense graphs)
- Efficient for dense graphs
- Fast access to check existence of edges
------------------------------------------------*/
vector<vector<int>> adjMatrix;
// Function to add edge in Adjacency Matrix
void addEdgeAdjMatrix(int u, int v) {
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1; // For undirected graph
}
// BFS for Adjacency Matrix
void bfsAdjMatrix(int start, int n) {
queue<int> q;
vector<bool> visited(n, false);
q.push(start);
visited[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
// Visit all neighbors (checking each entry in the matrix row)
for (int i = 0; i < n; ++i) {
if (adjMatrix[node][i] == 1 && !visited[i]) {
visited[i] = true;
q.push(i);
}
}
}
cout << endl;
}
// DFS for Adjacency Matrix
void dfsAdjMatrix(int start, int n) {
stack<int> s;
vector<bool> visited(n, false);
s.push(start);
while (!s.empty()) {
int node = s.top();
s.pop();
if (!visited[node]) {
cout << node << " ";
visited[node] = true;
// Visit all neighbors (checking each entry in the matrix row)
for (int i = 0; i < n; ++i) {
if (adjMatrix[node][i] == 1 && !visited[i]) {
s.push(i);
}
}
}
}
cout << endl;
}
int main() {
// Number of nodes
int n = 5;
/* -------------------------------
Adjacency List Example
- Better for sparse graphs
- Dynamic vertex management
---------------------------------*/
cout << "Adjacency List (unordered_map) BFS and DFS:\n";
// Add edges to the adjacency list
addEdgeAdjList(0, 1);
addEdgeAdjList(0, 4);
addEdgeAdjList(1, 2);
addEdgeAdjList(1, 3);
addEdgeAdjList(1, 4);
addEdgeAdjList(2, 3);
addEdgeAdjList(3, 4);
// BFS and DFS using Adjacency List
cout << "BFS starting from node 0: ";
bfsAdjList(0);
cout << "DFS starting from node 0: ";
dfsAdjList(0);
/* -------------------------------
Adjacency Matrix Example
- Better for dense graphs
- Fast access to edges
---------------------------------*/
cout << "\nAdjacency Matrix (2D vector) BFS and DFS:\n";
// Initialize adjacency matrix (n x n)
adjMatrix.resize(n, vector<int>(n, 0));
// Add edges to the adjacency matrix
addEdgeAdjMatrix(0, 1);
addEdgeAdjMatrix(0, 4);
addEdgeAdjMatrix(1, 2);
addEdgeAdjMatrix(1, 3);
addEdgeAdjMatrix(1, 4);
addEdgeAdjMatrix(2, 3);
addEdgeAdjMatrix(3, 4);
// BFS and DFS using Adjacency Matrix
cout << "BFS starting from node 0: ";
bfsAdjMatrix(0, n);
cout << "DFS starting from node 0: ";
dfsAdjMatrix(0, n);
return 0;
}