-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_naive_dfs_no_rem.cpp
125 lines (114 loc) · 2.66 KB
/
1_naive_dfs_no_rem.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
#include "1_naive_dfs_no_rem.h"
using namespace ndfs_no_rem;
Graph::Graph(int num_nodes, int search_depth) {
this->graph_size = num_nodes;
adj.resize(num_nodes);
visited.resize(num_nodes, false);
cycles.resize(30, 0);
max_depth = search_depth;
}
void Graph::AddEdge(int v, int w) {
adj[v].push_back(w); // Add w to v’s list.
}
void Graph::RemoveNode(int node)
{
std::vector<int> bfsq = { node };
while (!bfsq.empty())
{
int u = bfsq.back();
bfsq.pop_back();
for (auto u2 : adj[u])
{
adj[u2].erase(std::remove(adj[u2].begin(), adj[u2].end(), u), adj[u2].end());
if (size(adj[u2]) == 1)
bfsq.push_back(u2);
}
adj[u].clear();
}
}
void Graph::Cycles(int v, int d)
{
visited[v] = true;
for (int i : adj[v]) {
#include "0_nodes_vis_counter.h"
if (i == rootnode) {
++cycles[d];
}
else if (d < max_depth) {
if (!visited[i]) {
Cycles(i, d + 1);
}
}
}
visited[v] = false;
}
void Graph::DFS(int rootNodeOrder)
{
int start, end, it;
switch (rootNodeOrder) {
case 0: start = 0; end = 2 * graph_size / 3; it = 1; break;
case 1: start = 2 * graph_size / 3 - 1; end = -1; it = -1; break;
case 2: start = 2 * graph_size / 3; end = graph_size; it = 1; break;
case 3: start = graph_size - 1; end = 2 * graph_size / 3 - 1; it = -1; break;
default: start = 0; end = 2 * graph_size / 3; it = 1; break;
}
for (int i = start; i != end; i += it)
{
rootnode = i;
Cycles(i, 1);
visited[i] = true;
//RemoveNode(i);
adj[i].clear();
}
//Output cycle count, we zero the count of degree-two cycles.
cycles[1] = 0; cycles[2] = 0;
for (auto k : cycles)
std::cout << k / 2 << ' ';
}
void ndfs_no_rem::Foo(std::string codeName, int depth, int rootNodeOrder)
{
//Import data to cpp
int n, k, max_row, max_col, temp;
std::vector<int> numbers;
std::ifstream myfile;
myfile.open(codeName);
myfile >> n >> k >> max_row >> max_col;
while (myfile >> temp)
numbers.push_back(temp);
myfile.close();
//Build graph
int alist = 0;
if (codeName.substr(codeName.size() - 2) == ".a") {
alist = 1;
}
Graph g = Graph(n + k, depth);
int x = 0;
int y = n + k;
while (x < n)
{
for (int z = 0; z < max_row; z++)
{
if (numbers[y] > 0)
{
if (alist) {
g.AddEdge(x, numbers[y] + n - 1);
g.AddEdge(numbers[y] + n - 1, x);
}
else
{
g.AddEdge(x, numbers[y] - 1);
}
}
y++;
}
x++;
}
std::vector<int>().swap(numbers);
//Search for cycles, and start timers
LARGE_INTEGER freq, t1, t2;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t1);
g.DFS(rootNodeOrder);
QueryPerformanceCounter(&t2);
std::cout << ":: " << (t2.QuadPart - t1.QuadPart) * 1.0 / freq.QuadPart << "\n";
}