-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphAlgorithms.cpp
178 lines (171 loc) · 3.38 KB
/
GraphAlgorithms.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
#include "pch.h"
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
typedef std::vector<std::vector<int>> adjacent_list;
typedef std::vector<std::vector<bool>> adjacent_matrix;
adjacent_list ReadGraphAsAdjasentList(bool directed = true)
{
int n, m;
std::cin >> n >> m;
adjacent_list list(n);
for (int i = 0; i < m; ++i)
{
int a1, a2;
std::cin >> a1 >> a2;
list[a1].push_back(a2);
if (!directed)
{
list[a2].push_back(a1);
}
}
return list;
}
void DFSRecursive(adjacent_list& graph, int v, std::vector<bool>& visited)
{
visited[v] = true;
for (int vertex : graph[v])
{
if (!visited[vertex])
{
DFSRecursive(graph, vertex, visited);
}
}
}
void DFSIterative(adjacent_list& graph, int v, std::vector<bool>& visited)
{
std::stack<int> NodeStack;
NodeStack.push(v);
while (!NodeStack.empty())
{
int elem = NodeStack.top();
NodeStack.pop();
if (!visited[elem])
{
std::cout << elem << " ";
visited[elem] = true;
}
for (int i : graph[elem])
{
if (!visited[i])
{
NodeStack.push(i);
}
}
}
}
void BFSRecursive(adjacent_list& graph, std::queue<int>& q, std::vector<bool>& visited)
{
if (q.empty())
return;
int v = q.front();
q.pop();
std::cout << v << " ";
for (int u : graph[v])
{
if (!visited[u])
{
visited[u] = true;
q.push(u);
}
}
BFSRecursive(graph, q, visited);
}
void BFSIterative(adjacent_list& graph, int v, std::vector<bool>& visited)
{
std::queue<int> q;
q.push(v);
visited[v] = true;
while (!q.empty())
{
v = q.front();
q.pop();
std::cout << v << " ";
for (int u : graph[v])
{
if (!visited[u])
{
q.push(u);
visited[u] = true;
}
}
}
}
enum class color
{
not_set,
red,
blue
};
color get_opposite(color c)
{
return c == color::red ? color::blue : color::red;
}
bool is_Bipartite_bfs(adjacent_list& graph, int start_v, std::vector<color>& colors)
{
std::queue<int> q;
q.push(start_v);
colors[start_v] = color::blue;
while (!q.empty())
{
int cur_vertex = q.front();
q.pop();
for (int neighbour : graph[cur_vertex])
{
if (colors[neighbour] == color::not_set)
{
q.push(neighbour);
colors[neighbour] == get_opposite(colors[cur_vertex]);
}
else if (colors[neighbour] == colors[cur_vertex])
{
return false;
}
}
}
return true;
}
std::vector<int> compareTriplets(std::vector<int> a, std::vector<int> b)
{
std::vector<int> out(2, 0);
for (int i = 0; i < a.size(); ++i)
{
if (a[i] != b[i])
{
if (a[i] < b[i])
++out[1];
else ++out[0];
}
return out;
}
}
int main()
{
/*adjacent_list graph = ReadGraphAsAdjasentList(false);
std::vector<bool> visited(graph.size(),false);
//DFSIterative(graph, 3, visited);
std::queue<int> q;
q.push(0);
//BFSRecursive(graph, q, visited);
//BFSIterative(graph, 1, visited);
std::vector<color> colors(graph.size(), color::not_set);
for (int i = 0; i < graph.size(); ++i)
{
if (colors[i] == color::not_set)
{
if (!is_Bipartite_bfs(graph, i, colors))
{
std::cout << "Not bicolorable";
return false;
}
}
}
std::cout << "bicolorable";
*/
std::vector<int> v1({ 1,2,3 });
std::vector<int> v2({ 3,2,1 });
std::vector<int> out = compareTriplets(v1, v2);
std::cout << out[0] << " " << out[1];
}