-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhateverFP.cc
157 lines (150 loc) · 3.57 KB
/
WhateverFP.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
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
#include "Graph.hh"
// 图可达性
struct DepthFirstPaths {
ns::deque<bool> marked;
ns::deque<int> edgeTo;
int s;
DepthFirstPaths(const Graph &G, int s)
: marked(G.V, false), edgeTo(G.V, -1), s{s} {
assert(0 <= s && s < G.V);
dfs(G, s);
}
// 递归dfs
void dfs(const Graph &G, int v) {
marked[v] = true;
for (int w : G.adj[v]) {
if (!marked[w]) {
edgeTo[w] = v;
dfs(G, w);
}
}
}
DepthFirstPaths(const Digraph &G, int s)
: marked(G.V, false), edgeTo(G.V, -1), s{s} {
assert(0 <= s && s < G.V);
dfs(G, s);
}
// 迭代dfs
void dfs(const Digraph &G, int s) {
marked[s] = true;
ns::deque<int> stack;
stack.push_back(s);
while (!stack.empty()) {
int v = stack.back();
stack.pop_back();
for (int w : G.adj[v])
if (!marked[w]) {
marked[w] = true;
edgeTo[w] = v;
stack.push_back(w);
}
}
}
bool hasPathTo(int v) const { return marked[v]; }
ns::deque<int> pathTo(int v) const {
ns::deque<int> path;
if (hasPathTo(v)) {
for (int x = v; x != s; x = edgeTo[x])
path.push_front(x);
path.push_front(s);
}
return path;
}
};
struct BreadthFirstPaths {
ns::deque<bool> marked;
ns::deque<int> edgeTo, distTo;
int s;
BreadthFirstPaths(const Graph &G, int s)
: marked(G.V, false), edgeTo(G.V, -1), distTo(G.V, 0xff), s{s} {
assert(0 <= s && s < G.V);
distTo[s] = 0;
bfs(G, s);
}
// 第一类bfs
void bfs(const Graph &G, int s) {
marked[s] = true;
ns::deque<int> queue;
queue.push_back(s);
while (!queue.empty()) {
int v = queue.front();
queue.pop_front();
for (int w : G.adj[v])
if (!marked[w]) {
marked[w] = true;
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
queue.push_back(w);
}
}
}
BreadthFirstPaths(const Digraph &G, int s)
: marked(G.V, false), edgeTo(G.V, -1), distTo(G.V, 0xff), s{s} {
assert(0 <= s && s < G.V);
distTo[s] = 0;
bfs(G, s);
}
// 第二类bfs
void bfs(const Digraph &G, int s) {
marked[s] = true;
ns::deque<int> queue;
queue.push_back(s);
while (!queue.empty()) {
for (int i(0), sz(queue.size()); i < sz; ++i) {
int v = queue.front();
queue.pop_front();
for (int w : G.adj[v])
if (!marked[w]) {
marked[w] = true;
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
queue.push_back(w);
}
}
}
}
bool hasPathTo(int v) const { return marked[v]; }
ns::deque<int> pathTo(int v) const {
ns::deque<int> path;
if (hasPathTo(v)) {
for (int x = v; x != s; x = edgeTo[x])
path.push_front(x);
path.push_front(s);
}
return path;
}
};
template <class P> void printPath(const P &p, int v) {
for (int i = 0; i < v; ++i) {
std::print("v{}\t", i);
if (p.hasPathTo(i)) {
auto path{p.pathTo(i)};
for (int v : path)
std::print("{}\t", v);
} else
std::print("No Path");
std::print("\n");
}
}
int main() {
constexpr int v{8}, e{12};
Digraph DIG(v);
generateGraph(DIG, e);
printGraph(DIG);
std::print("\n");
std::print("dfs\n");
DepthFirstPaths dfs(DIG, 0);
printPath(dfs, v);
std::print("\n");
std::print("bfs\n");
BreadthFirstPaths bfs(DIG, 0);
printPath(bfs, v);
std::print("vertex\t");
for (int i = 0; i < v; ++i)
std::print("v{}\t", i);
std::print("\n");
std::print("distTo\t");
for (int v : bfs.distTo)
std::print("{}\t", v);
std::print("\n");
}