-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepthFS.cc
77 lines (72 loc) · 1.81 KB
/
DepthFS.cc
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
#include "deque.hh"
#include <cstdlib>
#include <ctime>
#include <print>
using AdjList = ns::deque<ns::deque<int>>;
struct DepthFirstSearch {
enum class genre { UNDISCOVERED, DISCOVERED, VISITED };
int clock{0};
ns::deque<genre> status;
ns::deque<int> dTime, fTime;
DepthFirstSearch(const AdjList &adj, int src)
: status(adj.size(), genre::UNDISCOVERED), dTime(adj.size()),
fTime(adj.size()) {
dfs(adj, src);
}
// Tree Edge: DISCOVERED v to UNDISCOVERED w
// Backward Edge: DISCOVERED v to DISCOVERED w
// Forward Edge: DISCOVERED v to VISITED w && dTime(v) < dTime(w)
// Cross Edge: DISCOVERED v to VISITED w && dTime(v) > dTime(w)
void dfs(const AdjList &adj, int v) {
dTime[v] = ++clock;
status[v] = genre::DISCOVERED;
for (int w : adj[v])
switch (status[w]) {
case genre::UNDISCOVERED:
dfs(adj, w);
break;
case genre::DISCOVERED:
break;
case genre::VISITED:
break;
}
status[v] = genre::VISITED;
fTime[v] = clock++;
}
};
int main() {
constexpr int v{8}, e{16};
std::srand(std::time(nullptr));
AdjList G(v);
ns::deque<int> a(e), b(e);
int i{0};
while (i < e) {
a[i] = std::rand() % v, b[i] = std::rand() % v;
if (a[i] == b[i])
continue;
int j{0};
for (; j < i; ++j) {
if (a[i] == a[j] && b[i] == b[j])
break;
if (a[i] == b[j] && b[i] == a[j])
break;
}
if (i != j)
continue;
G[a[i]].push_back(b[i]);
++i;
}
DepthFirstSearch DFS(G, 0);
std::print("vertex\t");
for (int i = 0; i < v; ++i)
std::print("v{}\t", i);
std::print("\n");
std::print("dTime\t");
for (int v : DFS.dTime)
std::print("{}\t", v);
std::print("\n");
std::print("fTime\t");
for (int v : DFS.fTime)
std::print("{}\t", v);
std::print("\n");
}