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