- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
Latest commit
200 lines (170 loc) · 5.6 KB
/
Copy pathGraph.cpp
File metadata and controls
200 lines (170 loc) · 5.6 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
#include<iostream>
#include<stdexcept>
#include<queue>
#include<stack>
#include"Graph.h"
std::ostream &operator<<(std::ostream& os, const Graph& graph) {
for (int i = 0; i < graph.adjList.size(); ++i) {
for (autoconst& j : graph.adjList[i]) { os << i << " -> " << j << std::endl; }
}
return os;
}
Graph::Graph(int n) : adjList(std::vector<std::list<int>>(n)) { }
Graph::Graph(std::vector<std::pair<int, int>> edges) {
int max = 0;
for (int i = 0; i < edges.size(); ++i) {
if (edges[i].first < 0 || edges[i].second < 0) { throwstd::invalid_argument("negative value for vertex"); }
max = (edges[i].first > max) ? edges[i].first : max;
max = (edges[i].second > max) ? edges[i].second : max;
}
this->adjList = std::vector<std::list<int>>(max + 1);
for (int i = 0; i < edges.size(); ++i) {
addEdge(edges[i]);
}
}
boolGraph::existVertex(constint n) { return (n < this->adjList.size()); }
size_tGraph::addVertex() {
this->adjList.emplace_back(std::list<int>());
returnthis->adjList.size() - 1;
}
boolGraph::existEdge(constint b, constint e) {
return (this->edges.find({b, e}) != this->edges.end());
}
boolGraph::existEdge(const std::pair<int, int> edge) {
return (this->edges.find(edge) != this->edges.end());
}
voidGraph::addEdge(constint b, constint e) {
this->adjList[b].push_back(e);
this->edges.insert({b, e});
}
voidGraph::addEdge(const std::pair<int, int> edge) {
this->adjList[edge.first].push_back(edge.second);
this->edges.insert(edge);
}
boolGraph::removeEdge(constint b, constint e) {
this->adjList[b].remove(e);
returnthis->edges.erase({b, e});
}
boolGraph::removeEdge(const std::pair<int, int> edge) {
this->adjList[edge.first].remove(edge.second);
returnthis->edges.erase(edge);
}
voidGraph::turnUndirected() {
/*
* Turn Undirected
* - Complete directed graph to undirected graph
*/
for (int i = 0; i < this->adjList.size(); ++i) {
for (autoconst& j : this->adjList[i]) {
if (!this->existEdge(j, i)) {
addEdge(j, i);
}
}
}
}
std::vector<bool> Graph::bfs(constint source) {
if (source >= this->adjList.size()) { throwstd::invalid_argument("node not in graph"); }
std::queue<int> search;
std::vector<bool> explored(this->adjList.size(), false);
search.push(source);
explored[source] = true;
while (!search.empty()) {
constint &vertex = search.front();
search.pop();
for (autoconst& i : this->adjList[vertex]) {
if (!explored[i]) {
explored[i] = true;
search.push(i);
}
}
}
return explored;
}
boolGraph::bfs(constint source, constint goal) {
if (source >= this->adjList.size() || goal >= this->adjList.size()) { throwstd::invalid_argument("node not in graph"); }
std::queue<int> search;
std::vector<bool> explored(this->adjList.size(), false);
search.push(source);
explored[source] = true;
while (!search.empty()) {
constint &vertex = search.front();
search.pop();
if (vertex == goal) { returntrue; }
for (autoconst& i : this->adjList[vertex]) {
if (!explored[i]) {
explored[i] = true;
search.push(i);
}
}
}
returnfalse;
}
std::vector<bool> Graph::dfs(constint source) {
if (source >= this->adjList.size()) { throwstd::invalid_argument("node not in graph"); }
std::stack<int> search;
std::vector<bool> explored(this->adjList.size(), false);
search.push(source);
while (!search.empty()) {
constint &vertex = search.top();
search.pop();
if (!explored[vertex]) {
explored[vertex] = true;
for (autoconst& i : this->adjList[vertex]) {
search.push(i);
}
}
}
return explored;
}
boolGraph::dfs(constint source, constint goal) {
if (source >= this->adjList.size() || goal >= this->adjList.size()) { throwstd::invalid_argument("node not in graph"); }
std::stack<int> search;
std::vector<bool> explored(this->adjList.size(), false);
search.push(source);
while (!search.empty()) {
constint &vertex = search.top();
search.pop();
if (vertex == goal) { returntrue; }
if (!explored[vertex]) {
explored[vertex] = true;
for (autoconst& i : this->adjList[vertex]) {
search.push(i);
}
}
}
returnfalse;
}
std::vector<int> Graph::topoSort() {
/*
* Topological Sort
* - Find topological sort of graph
* - Returns empty vector if graph contains circles
* - inspired by https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution
*/
std::vector<int> indegree(this->adjList.size());
for (int i = 0; i < this->adjList.size(); ++i) {
for (autoconst& j : this->adjList[i]) {
++indegree[j];
}
}
std::queue<int> queue;
for (int i = 0; i < this->adjList.size(); ++i) {
if (indegree[i] == 0) {
queue.push(i);
}
}
int count = 0;
std::vector<int> order;
while (!queue.empty()) {
constint &vertex = queue.front();
queue.pop();
order.push_back(vertex);
for (autoconst& i : this->adjList[vertex]) {
--indegree[i];
if (indegree[i] == 0) { queue.push(i); }
}
++count;
}
if (count != this->adjList.size()) { return std::vector<int>(); } // graph is not acyclic
return order;
}