-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.hpp
120 lines (101 loc) · 4.25 KB
/
dfs.hpp
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
// Copyright (c) 2024 Jakub Musiał
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
#pragma once
#include "common.hpp"
#include <stack>
namespace gl::algorithm::impl {
template <
type_traits::c_graph GraphType,
type_traits::c_optional_vertex_callback<GraphType, bool> VisitVertexPredicate,
type_traits::c_vertex_callback<GraphType, bool, types::id_type> VisitCallback,
type_traits::
c_vertex_callback<GraphType, std::optional<bool>, const typename GraphType::edge_type&>
EnqueueVertexPred,
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
algorithm::empty_callback,
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
algorithm::empty_callback>
void dfs(
const GraphType& graph,
const typename GraphType::vertex_type& root_vertex,
const VisitVertexPredicate& visit_vertex_pred,
const VisitCallback& visit,
const EnqueueVertexPred& enqueue_vertex_pred,
const PreVisitCallback& pre_visit = {},
const PostVisitCallback& post_visit = {}
) {
using vertex_stack_type = std::stack<algorithm::vertex_info>;
if constexpr (not type_traits::c_empty_callback<VisitVertexPredicate>)
if (not visit_vertex_pred(root_vertex))
return;
// prepare the vertex stack
vertex_stack_type vertex_stack;
vertex_stack.emplace(root_vertex.id());
// search the graph
while (not vertex_stack.empty()) {
const auto vinfo = vertex_stack.top();
vertex_stack.pop();
const auto& vertex = graph.get_vertex(vinfo.id);
if constexpr (not type_traits::c_empty_callback<VisitVertexPredicate>)
if (not visit_vertex_pred(vertex))
continue;
if constexpr (not type_traits::c_empty_callback<PreVisitCallback>)
pre_visit(vertex);
visit(vertex, vinfo.source_id);
for (const auto& edge : graph.adjacent_edges(vinfo.id)) {
const auto& incident_vertex = edge.incident_vertex(vertex);
if (enqueue_vertex_pred(incident_vertex, edge))
vertex_stack.emplace(incident_vertex.id(), vinfo.id);
}
if constexpr (not type_traits::c_empty_callback<PostVisitCallback>)
post_visit(vertex);
}
}
template <
type_traits::c_graph GraphType,
type_traits::c_vertex_callback<GraphType, bool> VisitVertexPredicate,
type_traits::c_vertex_callback<GraphType, bool, types::id_type> VisitCallback,
type_traits::
c_vertex_callback<GraphType, std::optional<bool>, const typename GraphType::edge_type&>
EnqueueVertexPred,
type_traits::c_optional_vertex_callback<GraphType, void> PreVisitCallback =
algorithm::empty_callback,
type_traits::c_optional_vertex_callback<GraphType, void> PostVisitCallback =
algorithm::empty_callback>
void r_dfs(
const GraphType& graph,
const typename GraphType::vertex_type& vertex,
const types::id_type source_id,
const VisitVertexPredicate& visit_vertex_pred,
const VisitCallback& visit,
const EnqueueVertexPred& enqueue_vertex_pred,
const PreVisitCallback& pre_visit = {},
const PostVisitCallback& post_visit = {}
) {
if constexpr (not type_traits::c_empty_callback<VisitVertexPredicate>)
if (not visit_vertex_pred(vertex))
return;
if constexpr (not type_traits::c_empty_callback<PreVisitCallback>)
pre_visit(vertex);
visit(vertex, source_id);
// recursively search vertices adjacent to the current vertex
const auto vertex_id = vertex.id();
for (const auto& edge : graph.adjacent_edges(vertex_id)) {
const auto& incident_vertex = edge.incident_vertex(vertex);
if (enqueue_vertex_pred(incident_vertex, edge))
r_dfs(
graph,
incident_vertex,
vertex_id,
visit_vertex_pred,
visit,
enqueue_vertex_pred,
pre_visit,
post_visit
);
}
if constexpr (not type_traits::c_empty_callback<PostVisitCallback>)
post_visit(vertex);
}
} // namespace gl::algorithm::impl