GraphLibrary  0.0.1
A simple library that implements various graph algorithms.
 All Classes Namespaces Files Functions Variables Typedefs Macros
DFS.hpp
Go to the documentation of this file.
1 #ifndef GL_DFS_HPP
2 #define GL_DFS_HPP
3 
4 #include "../structures/Graph.hpp"
5 
6 namespace gl {
7 namespace algorithm {
8 
16  template <class GRAPH>
17  void DFS_recursive (const GRAPH& graph, const typename GRAPH::idx_t node, typename GRAPH::visit_list_t& visited, typename GRAPH::ordered_list_t& out) {
18  graph.checkRange(node);
19  typename GRAPH::idx_list_t tempList; // temporary node lists
20  typename GRAPH::DFS_queue_t queue; // nodes to check the neighbours of
21 
22  visited[node] = true;
23  out.emplace_back(node);
24  tempList = graph.getUnvisitedNeighbours(node,visited);
25 
26  for (auto v : tempList) {
27  if (!visited[v]) {
28  DFS_recursive(graph, v, visited, out);
29  }
30  }
31  }
32 
40  template <class Graph>
41  typename Graph::ordered_list_t DFS (const Graph& graph, const typename Graph::idx_t node) {
42  typename Graph::visit_list_t visited(graph.numNodes(),false); // list of visited nodes
43  typename Graph::ordered_list_t out; // result node lists
44  graph.checkRange(node);
45  DFS_recursive(graph, node, visited, out);
46  return out;
47  }
48 
49 } // namespace algorithm
50 } // namespace gl
51 
52 #endif // GL_DFS_HPP
void DFS_recursive(const GRAPH &graph, const typename GRAPH::idx_t node, typename GRAPH::visit_list_t &visited, typename GRAPH::ordered_list_t &out)
Recursive call for DFS.
Definition: DFS.hpp:17
Graph::ordered_list_t DFS(const Graph &graph, const typename Graph::idx_t node)
Traverses the connected component of node using DFS.
Definition: DFS.hpp:41
gl::index_type idx_t
Index type.
Definition: Graph.hpp:44
Stores and implements a Graph.
Definition: Graph.hpp:39
idx_t numNodes() const
Returns the number of nodes currently in the graph.
Definition: Graph.hpp:406
std::list< idx_t > ordered_list_t
Ordered List type.
Definition: Graph.hpp:49
std::vector< bool > visit_list_t
Visited-List type.
Definition: Graph.hpp:50
void checkRange(const idx_t &idx1) const
Only one index that gets range checked.
Definition: Graph.hpp:1389