Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
214 lines (206 loc) · 5.12 KB
/
Copy pathmain.cpp
File metadata and controls
214 lines (206 loc) · 5.12 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
/*
Made by Akshay Anand, MMSS 2022
NOTE: Next step is to utilize Dynamic Programming and store already discovered paths when doing DFS in order to be available for G/S check. Store all Eulerian paths.
INPUT DETAILS:
Line one: an integer (n) specifying the order of the graph
For every vertex (0 to n-1 inclusive):
Line # 1: Specify the degree of the vertex, Line #2: space separated integers specifying which vertices from 0 to n-1 are adjacent to this vertex
EXAMPLE (entering a complete graph of order 4):
4
3
1 2 3
3
2 3 0
3
3 0 1
3
0 1 2
Time Complexity:
Eulerian Check: O(N)
Hamiltonian Check: O(N^2)
G/S Check: O(N^2 * 2^N)
*/
#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
int N;
bool eulerian;
bool tough;
bool hamiltonian;
// checks if every vertex has an even degree
void checkEulerian(std::vector<std::vector<int>> &graph)
{
for (int i = 0; i < graph.size(); i++)
{
if (graph[i].size() % 2 != 0)
{
eulerian = false;
break;
}
}
}
// DFS algorithm that searches for a hamiltonian cycle
void checkHamiltonian(int vertex, std::vector<int> &points_visited, std::vector<std::vector<int>> &graph, int target)
{
if (vertex == target && points_visited.size() == graph.size())
{
hamiltonian = true;
return;
}
for (int i = 0; i < points_visited.size(); i++)
{
if (points_visited[i] == vertex)
{
return;
}
}
for (int i = 0; i < graph[vertex].size(); i++)
{
std::vector<int> tmp = std::vector<int>();
for (int j = 0; j < points_visited.size(); j++)
{
tmp.push_back(points_visited[j]);
}
tmp.push_back(vertex);
checkHamiltonian(graph[vertex][i], tmp, graph, target);
}
}
// finds all possible points reachable from one given starting point - helper method for getNumComponents()
void findAllPossibleVisitedPoints(int vertex, std::vector<std::vector<int>> &graph, std::vector<int> &visited, std::vector<int> &set)
{
for (int i = 0; i < visited.size(); i++)
{
if (vertex == visited[i])
{
return;
}
}
visited.push_back(vertex);
for (int i = 0; i < graph[vertex].size(); i++)
{
bool inK = false;
for (int j = 0; j < set.size(); j++)
{
if (graph[vertex][i] == set[j])
{
inK = true;
break;
}
}
if (!inK)
{
findAllPossibleVisitedPoints(graph[vertex][i], graph, visited, set);
}
}
}
// gives the number of components in a graph
int getNumComponents(std::vector<std::vector<int>> &graph, std::vector<int> &set)
{
std::vector<int> pointsLeft = std::vector<int>();
for (int i = 0; i < N; i++)
{
bool check = true;
for (int k = 0; k < set.size(); k++)
{
if (set[k] == i)
{
check = false;
break;
}
}
if (check)
{
pointsLeft.push_back(i);
}
}
int numComps = 0;
while (pointsLeft.size() > 0)
{
std::vector<int> group = std::vector<int>();
findAllPossibleVisitedPoints(pointsLeft[0], graph, group, set);
numComps++;
for (int i = 0; i < group.size(); i++)
{
for (int j = 0; j < pointsLeft.size(); j++)
{
if (group[i] == pointsLeft[j])
{
pointsLeft.erase(pointsLeft.begin() + (j));
j--;
}
}
}
}
return numComps;
}
// recursive algorithm that checks if every possible combination of vertices removed results in more components of the graph created
void checkToughness(std::vector<std::vector<int>> &graph, std::vector<int> &set, int num)
{
if (graph.size() == num)
{
int comps = getNumComponents(graph, set);
if (comps > set.size() && set.size() > 0)
{
tough = false;
return;
}
}
else
{
std::vector<int> tmp1 = std::vector<int>();
std::vector<int> tmp2 = std::vector<int>();
for (int i = 0; i < set.size(); i++)
{
tmp1.push_back(set[i]);
tmp2.push_back(set[i]);
}
tmp1.push_back(num);
checkToughness(graph, tmp1, num + 1);
checkToughness(graph, tmp2, num + 1);
}
}
int main(){
cin>>N;
std::vector<std::vector<int>> graph = std::vector<std::vector<int>>();
for (int i = 0; i < N; i++)
{
int m;
cin>>m;
string line;
getline(cin >> ws,line);
std::vector<int> vec = std::vector<int>();
std::stringstream iss(line);
int num;
while ( iss >> num ){
vec.push_back( num );
}
graph.push_back(vec);
}
// ^Stored graph in a modified adjacency list
eulerian = true;
checkEulerian(graph);
string b = eulerian?"true":"false";
std::cout << "Eulerian: " + b << std::endl;
hamiltonian = false;
std::vector<int> vec = std::vector<int>();
checkHamiltonian(0,vec,graph,0);
b = hamiltonian?"true":"false";
std::cout << "Hamiltonian: " + b << std::endl;
tough = true;
std::vector<int> vec2 = std::vector<int>();
checkToughness(graph,vec2,0);
b = tough?"true":"false";
std::cout << "Tough: " + b << std::endl;
if (!hamiltonian && tough && eulerian)
{
std::cout << "This is a Harris Graph" << std::endl;
}
else
{
std::cout << "This isn\'t a Harris Graph" << std::endl;
}
return 0;
}