-
Notifications
You must be signed in to change notification settings - Fork 50
/
adjacency_list.hpp
166 lines (144 loc) · 4.22 KB
/
adjacency_list.hpp
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
#include <iostream>
#include <vector>
#include <map>
#include "stlmp.h"
#include <algorithm>
using namespace stlmp::Queue;
using namespace stlmp::Graph::AdjacencyList;
using namespace stlmp::Stack;
template<typename T>
int Graph<T>::connected_components() {
for (int i = 0; i < count; i++) visited[i] = false;
int components = 0;
for (int i = 0; i < count; i++) {
if (!visited[i]) {
visited[i] = true;
dfs(i);
components++;
}
}
return components;
}
template<typename T>
void Graph<T>::print_graph() {
cout << "Printing adjacency list of graph:" << endl;
for (int i = 0; i < count; i++) {
vector<int> v = connections[i];
cout << "vertices connected to " << i << ":" << endl;
for (int j = 0; j < v.size(); j++) {
cout << connections[i][j] << " ";
}
cout << endl;
}
}
template<typename T>
bool Graph<T>::connected(int i, int j) {
return bfs(i, j);
}
template<typename T>
void Graph<T>::dfs(int i) {
for (int j = 0; j < connections[i].size(); j++) {
if (!visited[connections[i][j]]) {
visited[connections[i][j]] = true;
dfs(connections[i][j]);
}
}
}
template<typename T>
bool Graph<T>::bfs(int i, int j) {
auto *q = new Queue::Queue<int>();
// else, try searching though the connections
for (int w = 0; w < this->count; w++) visited[w] = false;
q->push(i);
visited[i] = true;
while (q->size) {
int v = q->pop();
for (int w = 0; w < connections[v].size(); w++) {
if (!visited[connections[v][w]]) {
if (connections[v][w] == j) return true;
q->push(connections[v][w]);
visited[connections[v][w]] = true;
}
}
}
return false;
}
template<typename T>
bool Graph<T>::contains(vector<int> v, int n) {
return find(v.begin(), v.end(), n) != v.end();
}
template<typename T>
void Graph<T>::connect_both_sides(int i, int j) {
connect(i, j);
connect(j, i);
}
template<typename T>
void Graph<T>::connect(int i, int j) {
connections[i].push_back(j);
}
template<typename T>
Graph<T>::Graph(T *vertices, int count) : count(count), visited(new bool[count]) {
for (int i = 0; i < count; i++) {
this->vertices.push_back(vertices[i]);
}
}
/**
* Topological sorting
* */
// util function
template<class T>
void Graph<T>::topologicalSortUtil(int v, bool *visited, Stack::Stack<int> *st) {
visited[v] = true;
// go to all vertices adjacent to this one
for (int w = 0; w < connections[v].size(); w++) {
int new_node = connections[v][w];
if (!visited[new_node]) topologicalSortUtil(new_node, visited, st);
}
st->push(v);
}
template<class T>
Stack<T> Graph<T>::topologicalSort() {
Stack::Stack<int> st;
// mark all vertices as not visited
bool *visited = new bool[vertices.size()];
for (int i = 0; i < vertices.size(); i++) visited[i] = false;
// call helper function to store topological sort
// starting from all vertices one by one
for (int i = 0; i < vertices.size(); i++) {
if (!visited[i]) topologicalSortUtil(i, visited, &st);
}
Stack::Stack<int> st2;
st2 = st;
cout << "Printing graph after topological sort: " << endl;
while (!st.empty()) {
cout << st.peek() << " ";
st.pop();
}
cout << endl;
return st2;
}
/*
* Shortest path in Unweighted graph
*/
template<class T>
std::pair< vector<int>, vector<int> > Graph<T>::shortestPathFromVertex(int s){
std::vector<int> distance(connections.size(), -1);
std::vector<int> path(connections.size());
Queue::Queue<int> q;
int v, w;
q.push(s);
distance[s] = 0;
while(q.size) {
v = q.peek();
q.pop();
for(int k = 0; k < connections[v].size(); k++){
w = connections[v][k];
if(distance[w] == -1){
distance[w] = distance[v] + 1;
path[w] = v;
q.push(w);
}
}
}
return std::make_pair(distance, path);
}