Repository navigation
Expand file tree
/
Copy pathGraph.hpp
More file actions
251 lines (216 loc) · 6.77 KB
/
Copy pathGraph.hpp
File metadata and controls
251 lines (216 loc) · 6.77 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#ifndef GRAPH_HPP
#define GRAPH_HPP
#include <iostream>
#include "Node.hpp"
#include "Stack.hpp"
#include "Queue.hpp"
using namespace std;
class Graph {
private:
struct EdgeNode {
int vertex;
EdgeNode* next;
EdgeNode(int v) {
vertex = v;
next = nullptr;
}
};
int numVertices;
int numEdges;
bool isDirected;
EdgeNode** adjList; // Array of adjacency lists ie it is array of pointers to head of the Linked list
void dfsHelper(int vertex, bool* visited);
public:
Graph(int vertices, bool directed = true);
~Graph();
void addEdge(int u, int v);
void bfs(int startVertex = -1); // -1 means traverse all components
void dfs(int startVertex = -1); // -1 means traverse all components
void print();
int getNumVertices();
int getNumEdges();
bool isEmpty();
};
// Constructor & Destructor
Graph::Graph(int vertices, bool directed) {
numVertices = vertices;
numEdges = 0;
isDirected = directed;
adjList = new EdgeNode*[numVertices];
for (int i = 0; i < numVertices; i++) {
adjList[i] = nullptr;
}
}
Graph::~Graph() {
for (int i = 0; i < numVertices; i++) {
EdgeNode* current = adjList[i];
while (current != nullptr) {
EdgeNode* temp = current;
current = current->next;
delete temp;
}
}
delete[] adjList;
}
void Graph::addEdge(int u, int v) {
if (u >= 0 && u < numVertices && v >= 0 && v < numVertices) {
// Add edge from u to v
EdgeNode* newNode = new EdgeNode(v);
newNode->next = adjList[u];
adjList[u] = newNode; // This pointing to the new head of the list ie the newly added edge
// If undirected, add edge from v to u as well
if (!isDirected) {
EdgeNode* newNode2 = new EdgeNode(u);
newNode2->next = adjList[v];
adjList[v] = newNode2;
}
numEdges++;
} else {
cerr << "Error: Invalid vertex index" << endl;
}
}
// Breadth First Search using Queue
void Graph::bfs(int startVertex) {
bool* visited = new bool[numVertices];
for (int i = 0; i < numVertices; i++) {
visited[i] = false;
}
// If startVertex is -1, traverse all components
if (startVertex == -1) {
cout << "BFS (all components): ";
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
Queue<int> q;
visited[i] = true;
q.enqueue(i);
while (!q.isEmpty()) {
int current;
q.dequeue(current);
cout << current << " ";
EdgeNode* temp = adjList[current];
while (temp != nullptr) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
visited[adjVertex] = true;
q.enqueue(adjVertex);
}
temp = temp->next;
}
}
}
}
} else {
// Traverse from specific start vertex
if (startVertex < 0 || startVertex >= numVertices) {
cerr << "Error: Invalid start vertex" << endl;
delete[] visited;
return;
}
Queue<int> q;
visited[startVertex] = true;
q.enqueue(startVertex);
cout << "BFS starting from vertex " << startVertex << ": ";
while (!q.isEmpty()) {
int current;
q.dequeue(current);
cout << current << " ";
EdgeNode* temp = adjList[current];
while (temp != nullptr) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
visited[adjVertex] = true;
q.enqueue(adjVertex);
}
temp = temp->next;
}
}
}
cout << endl;
delete[] visited;
}
// Helper function for DFS
void Graph::dfsHelper(int vertex, bool* visited) {
visited[vertex] = true;
cout << vertex << " ";
EdgeNode* temp = adjList[vertex];
while (temp != nullptr) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
dfsHelper(adjVertex, visited);
}
temp = temp->next;
}
}
// Depth First Search using Stack
void Graph::dfs(int startVertex) {
bool* visited = new bool[numVertices];
for (int i = 0; i < numVertices; i++) {
visited[i] = false;
}
// If startVertex is -1, traverse all components
if (startVertex == -1) {
cout << "DFS (all components): ";
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
dfsHelper(i, visited);
}
}
} else {
// Traverse from specific start vertex using stack
if (startVertex < 0 || startVertex >= numVertices) {
cerr << "Error: Invalid start vertex" << endl;
delete[] visited;
return;
}
Stack<int> s;
s.push(startVertex);
cout << "DFS starting from vertex " << startVertex << ": ";
while (!s.isEmpty()) {
int current;
s.pop(current);
if (!visited[current]) {
visited[current] = true;
cout << current << " ";
// All adjacent vertices into stack in reverse order to maintain order
Stack<int> tempStack;
EdgeNode* temp = adjList[current];
while (temp != nullptr) {
if (!visited[temp->vertex]) {
tempStack.push(temp->vertex);
}
temp = temp->next;
}
// From tempStack to main stack in reverses order
while (!tempStack.isEmpty()) {
int v;
tempStack.pop(v);
s.push(v);
}
}
}
}
cout << endl;
delete[] visited;
}
void Graph::print() {
cout << "Graph Adjacency List is " << (isDirected ? "Directed" : "Undirected") << ":" << endl;
for (int i = 0; i < numVertices; i++) {
cout << i << " : ";
EdgeNode* temp = adjList[i];
while (temp != nullptr) {
cout << temp->vertex << " ";
temp = temp->next;
}
cout << endl;
}
}
int Graph::getNumVertices() {
return numVertices;
}
int Graph::getNumEdges() {
return numEdges;
}
bool Graph::isEmpty() {
return (numEdges == 0) ? true : false;
}
#endif