-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphify.h
executable file
·61 lines (61 loc) · 1.64 KB
/
Graphify.h
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
#ifndef test2_h
#define test2_h
#include <vector>
#include <queue>
using namespace std;
class Graph {
int numOfNodes;
vector<vector<int>> adjList;
public:
Graph(int numOfNodes, vector<vector<int>> adjList) {
this->numOfNodes = numOfNodes;
this->adjList = adjList;
}
void displayAdjacencyList();
vector<int> breadthFirstSearch(int);
vector<int> depthFirstSearch(int);
void dfsHelper(int, vector<int>&, vector<bool>&);
};
void Graph::displayAdjacencyList() {
for (int i = 0; i < numOfNodes; i++) {
cout << i << " -> ";
for (int j : adjList[i])
cout << j << " -> ";
cout << "null" << "\n";
}
}
vector<int> Graph:: breadthFirstSearch(int src) {
vector<int> nodes;
vector<bool> visited(numOfNodes + 1, false);
queue<int> queue;
queue.push(src);
visited[src] = true;
while (!queue.empty()) {
int explorer = queue.front();
queue.pop();
nodes.push_back(explorer);
for (auto neighbour: adjList[explorer]) {
if (!visited[neighbour]) {
queue.push(neighbour);
visited[neighbour] = true;
}
}
}
return nodes;
}
void Graph:: dfsHelper(int src, vector<int>& nodes, vector<bool>& visited) {
visited[src] = true;
nodes.push_back(src);
for (auto neighbour: adjList[src]) {
if (!visited[neighbour]) {
dfsHelper(neighbour, nodes, visited);
}
}
}
vector<int> Graph:: depthFirstSearch(int src) {
vector<bool> visited(numOfNodes + 1, 0);
vector<int> nodes;
dfsHelper(src, nodes, visited);
return nodes;
}
#endif // TEST.H