dfs.h
Go to the documentation of this file.
1 /*
2  * This file is part of the alglib project.
3  *
4  * (c) Divyanshu Kakwani <divkakwani@gmail.com>
5  *
6  * For the full copyright and license information, please view the LICENSE file
7  * that was distributed with this source code.
8  */
9 
10 #pragma once
11 
13 
14 namespace alglib {
15 namespace graph {
16 
17 /*
18  * \brief Does a single preorder dfs on G starting from `start`
19  *
20  * It is intended to be used inside the preorder dfs function. For that reason,
21  * another argument visited is provided to maintain the vertices visited so far.
22  */
23 template<typename GraphType, typename OutputIter>
24 OutputIter _single_preorder_dfs(const GraphType& G,
25  const typename GraphType::vertex_type& start,
27  OutputIter dest) {
28  *dest++ = start; // Put it on the output stream
29  visited[start] = true;
30 
31  /* Explore adjacent vertices */
32  for(auto it = G.avbegin(start); it != G.avend(start); it++)
33  if(!visited(*it))
34  dest = _single_preorder_dfs(G, *it, visited, dest);
35 
36  return dest;
37 }
38 
39 
40 template<typename GraphType, typename OutputIter>
41 OutputIter single_preorder_dfs(const GraphType& G,
42  const typename GraphType::vertex_type& start,
43  OutputIter dest) {
44  vertex_property<GraphType, bool> visited(G, false);
45  return _single_preorder_dfs(G, start, visited, dest);
46 }
47 
48 template<typename GraphType, typename OutputIter>
49 OutputIter _single_postorder_dfs(const GraphType& G,
50  const typename GraphType::vertex_type& start,
52  OutputIter dest) {
53  visited[start] = true;
54  for(auto it = G.avbegin(start); it != G.avend(start); it++)
55  if(!visited(*it))
56  dest = _single_postorder_dfs(G, *it, visited, dest);
57  *dest++ = start; // Put it on the output stream
58  return dest;
59 }
60 
61 template<typename GraphType, typename OutputIter>
62 OutputIter single_postorder_dfs(const GraphType& G,
63  const typename GraphType::vertex_type& start,
64  OutputIter dest) {
65  vertex_property<GraphType, bool> visited(G, false);
66  return _single_postorder_dfs(G, start, visited, dest);
67 }
68 
69 template<typename GraphType, typename OutputIter>
70 OutputIter preorder_dfs(const GraphType& G,
71  const typename GraphType::vertex_type start,
72  OutputIter dest) {
73 
74  /* visited property, initialized with false for each vertex */
75  vertex_property<GraphType, bool> visited(G, false);
76 
77  /* do a dfs on the start vertex first */
78  dest = _single_preorder_dfs(G, start, visited, dest);
79 
80  /* check all the vertices and perform dfs on the unvisited ones */
81  for(auto it = G.vbegin(); it != G.vend(); it++)
82  if(!visited(*it))
83  dest = _single_preorder_dfs(G, *it, visited, dest);
84 
85  return dest;
86 }
87 
88 template<typename GraphType, typename OutputIter>
89 OutputIter preorder_dfs(const GraphType& G, OutputIter dest) {
90  return preorder_dfs(G, *G.vbegin(), dest);
91 }
92 
93 
94 template<typename GraphType, typename OutputIter>
95 OutputIter postorder_dfs(const GraphType& G,
96  const typename GraphType::vertex_type start,
97  OutputIter dest) {
98 
99  /* visited property on G, each initialized with false */
100  vertex_property<GraphType, bool> visited(G, false);
101 
102  /* do a dfs on the start vertex first */
103  dest = _single_postorder_dfs(G, start, visited, dest);
104 
105  /* check all the vertices and perform dfs on the unvisited ones */
106  for(auto it = G.vbegin(); it != G.vend(); it++)
107  if(!visited(*it))
108  dest = _single_postorder_dfs(G, *it, visited, dest);
109 
110  return dest;
111 }
112 
113 
114 template<typename GraphType, typename OutputIter>
115 OutputIter postorder_dfs(const GraphType& G, OutputIter dest) {
116  return postorder_dfs(G, *G.vbegin(), dest);
117 }
118 
119 } // end of graph namespace
120 } // end of alglib namespace
121 
OutputIter single_postorder_dfs(const GraphType &G, const typename GraphType::vertex_type &start, OutputIter dest)
Definition: dfs.h:62
Definition: graph_property.h:20
Definition: bimap.h:17
OutputIter _single_preorder_dfs(const GraphType &G, const typename GraphType::vertex_type &start, vertex_property< GraphType, bool > &visited, OutputIter dest)
Definition: dfs.h:24
OutputIter preorder_dfs(const GraphType &G, const typename GraphType::vertex_type start, OutputIter dest)
Definition: dfs.h:70
OutputIter _single_postorder_dfs(const GraphType &G, const typename GraphType::vertex_type &start, vertex_property< GraphType, bool > &visited, OutputIter dest)
Definition: dfs.h:49
OutputIter postorder_dfs(const GraphType &G, const typename GraphType::vertex_type start, OutputIter dest)
Definition: dfs.h:95
OutputIter single_preorder_dfs(const GraphType &G, const typename GraphType::vertex_type &start, OutputIter dest)
Definition: dfs.h:41