MySQL 8.4.3
Source Code Documentation
depth_first_search.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef DEPTH_FIRST_SEARCH_INCLUDED
27#define DEPTH_FIRST_SEARCH_INCLUDED
28
29#include <functional>
30#include <set>
31#include <stack>
32#include <tuple>
33
34/**
35 @file include/depth_first_search.h
36*/
37
38/**
39 Performs a Depth First Search algorithm on a graph defined by a set of
40 vertices being an subset of universum of values of TVertex type and, and set
41 of directed edges generated on demand by get_neighbors() method supplied.
42 Search starts with a selected vertex, for each vertex that is encountered a
43 visitor_start() method supplied is called and when the DFS finishes traversal
44 from that vertex, a visitor_end() method is called. The set of visited nodes
45 is maintained, so the input graph don't have to be a Directed Acyclic Graph.
46
47 @param start_vertex A vertex to start a search from.
48 @param visitor_start A method or lambda that will be called when a specified
49 vertex starts to be processed, the vertex is supplied as the only argument.
50 @param visitor_end A method or lambda that will be called when a specified
51 vertex ends to be processed, i.e. all its neighbors were already processed,
52 the vertex is supplied as the only argument.
53 @param get_neighbors A method or lambda that takes a vertex as an argument and
54 returns a enumerable list of all vertices to which the edges exists.
55 @param [in,out] visited_set A set of vertices that were visited. This can be
56 used to run multiple DFS runs and assure any vertex will not be visited more
57 than once.
58*/
59template <typename TVertex, typename TVisit_start, typename TVisit_end,
60 typename TGet_neighbors, typename TVertex_less = std::less<TVertex>>
61void depth_first_search(TVertex start_vertex, TVisit_start visitor_start,
62 TVisit_end visitor_end, TGet_neighbors get_neighbors,
63 std::set<TVertex> &visited_set = std::set<TVertex>{}) {
64 /* A constant for a index denoting if the search from specified vertex has
65 just been started of just ends. */
66 constexpr int START_VISITING = 0;
67 /* A constant for a index denoting the vertex. */
68 constexpr int CURRENT_VERTEX = 1;
69
70 /* An actual stack for search. */
71 std::stack<std::tuple<bool, TVertex>> stack;
72 /* Check if this vertex was not already visited - this may happen if
73 the visited_set was returned by another DFS run. */
74 if (!visited_set.insert(start_vertex).second) {
75 return;
76 }
77 stack.emplace(true, start_vertex);
78
79 while (!stack.empty()) {
80 std::tuple<bool, TVertex> &elem = stack.top();
81 if (std::get<START_VISITING>(elem)) {
82 visitor_start(std::get<CURRENT_VERTEX>(elem));
83
84 std::get<START_VISITING>(elem) = false;
85 for (TVertex neighbor : get_neighbors(std::get<CURRENT_VERTEX>(elem))) {
86 /* Check if this vertex was not visited yet in this or previous runs. */
87 if (visited_set.insert(neighbor).second) {
88 stack.emplace(true, neighbor);
89 }
90 }
91 } else {
92 visitor_end(std::get<CURRENT_VERTEX>(elem));
93 stack.pop();
94 }
95 }
96}
97
98#endif
void depth_first_search(TVertex start_vertex, TVisit_start visitor_start, TVisit_end visitor_end, TGet_neighbors get_neighbors, std::set< TVertex > &visited_set=std::set< TVertex >{})
Performs a Depth First Search algorithm on a graph defined by a set of vertices being an subset of un...
Definition: depth_first_search.h:61
task_env * stack
Definition: task.cc:892