Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
BFS.h
Go to the documentation of this file.
1
31#pragma once
32
33#ifdef OGDF_INCLUDE_CGAL
34
37
38# include <iostream>
39# include <queue>
40
41namespace ogdf {
42namespace internal {
43namespace gcm {
44namespace graph {
45
46template<typename Graph = OGDFGraphWrapper, typename Flags = datastructure::TimestampFlags>
47class BFS {
48private:
49 using Node = typename Graph::Node;
50 using Edge = typename Graph::Edge;
51
52 const Graph& graph;
53 std::queue<Node> queue;
54 Flags visited;
55
56 static void settle_nothing(Node) { /* nothing to do*/
57 }
58
59 static bool expand_all(Node, typename Graph::Edge) { return true; }
60
61 static void traverse_nothing(Node, Edge) { /* nothing to do*/
62 }
63
64public:
65 BFS(const Graph& graph_) : graph(graph_), visited(graph.max_node_index() + 1) {
66 /*nothing to do*/
67 }
68
69 template<typename Range, typename FSettle, typename FExpandEdge, typename FTraverseEdge,
70 typename InitVisit, typename AlreadyVisited, typename MarkVisited>
72 MarkVisited&& mark_visited, FSettle&& settle_node = settle_nothing,
73 FExpandEdge&& expand_edge = expand_all, FTraverseEdge&& traverse_edge = traverse_nothing) {
74 init_visit();
75 for (auto v : sources) {
76 queue.push(v);
77 mark_visited(v);
78 }
79 unsigned int iterations = 0;
80 while (!queue.empty()) {
81 OGDF_ASSERT(iterations <= graph.number_of_nodes());
82 ++iterations;
83 Node current_node = queue.front();
84 queue.pop();
86 for (const Edge& edge : graph.edges(current_node)) {
87 Node opposite = edge->opposite(current_node);
88 if (expand_edge(current_node, edge) && !already_visited(opposite)) {
90 queue.push(opposite);
91 mark_visited(opposite);
92 }
93 }
94 }
95 }
96
97 template<typename Range, typename FSettle, typename FExpandEdge, typename FTraverseEdge>
98 void traverse(Range sources, FSettle&& settle_node = settle_nothing,
99 FExpandEdge&& expand_edge = expand_all, FTraverseEdge&& traverse_edge = traverse_nothing) {
100 traverse2(
101 sources, [&]() { visited.clear(); },
102 [&](Node u) { return visited.is_set(u->index()); },
103 [&](Node u) { visited.set(u->index()); }, settle_node, expand_edge, traverse_edge);
104 }
105
106 template<typename Range, typename FSettle>
107 void traverse(Range& source, FSettle&& settle_node) {
108 traverse(source, settle_node, expand_all, traverse_nothing);
109 }
110
111 template<typename FSettle, typename FExpandEdge, typename FTraverseEdge>
112 void traverse(Node& source, FSettle&& settle_node = settle_nothing,
113 FExpandEdge&& expand_edge = expand_all, FTraverseEdge&& traverse_edge = traverse_nothing) {
114 std::vector<Node> sources = {source};
116 }
117};
118}
119}
120}
121}
122
123#endif
node opposite(node v) const
Returns the adjacent node different from v.
Definition Graph_d.h:350
EdgeElement * edge
The type of edges.
Definition Graph_d.h:68
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.