-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDepthFirstOrder.h
53 lines (40 loc) · 1.18 KB
/
DepthFirstOrder.h
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
#ifndef ALGS4_DEPTHFIRSTORDER_H
#define ALGS4_DEPTHFIRSTORDER_H
#include <list>
#include <type_traits>
#include <vector>
#include "DirectedEdge.h"
#include "GraphBase.h"
class DepthFirstOrder {
private:
std::vector<bool> marked;
std::list<int> pre_, post_, reversePost_;
template<typename T>
void dfs(const GraphBase<T> &G, int v);
public:
template<typename T>
explicit DepthFirstOrder(const GraphBase<T> &G);
std::list<int> pre() const { return pre_; }
std::list<int> post() const { return post_; }
std::list<int> reversePost() const { return reversePost_; }
};
template<typename T>
void DepthFirstOrder::dfs(const GraphBase<T> &G, int v) {
pre_.push_back(v);
marked[v] = true;
for (const auto &e: G.adj(v)) {
int w;
if constexpr (std::is_same_v<std::decay_t<decltype(e)>, DirectedEdge>) w = e.to();
else w = e;
if (!marked[w]) dfs(G, w);
}
post_.push_back(v);
reversePost_.push_front(v);
}
template<typename T>
DepthFirstOrder::DepthFirstOrder(const GraphBase<T> &G) : marked(G.V()) {
for (int v = 0; v < G.V(); ++v) {
if (!marked[v]) dfs(G, v);
}
}
#endif //ALGS4_DEPTHFIRSTORDER_H