Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MinSTCutBFS.h
Go to the documentation of this file.
1 
33 #pragma once
34 
36 #include <ogdf/basic/DualGraph.h>
37 #include <ogdf/basic/Graph.h>
38 #include <ogdf/basic/GraphCopy.h>
39 #include <ogdf/basic/GraphList.h>
40 #include <ogdf/basic/List.h>
41 #include <ogdf/basic/Queue.h>
42 #include <ogdf/basic/basic.h>
44 
45 #include <functional>
46 
47 namespace ogdf {
48 
58 template<typename TCost>
59 class MinSTCutBFS : public MinSTCutModule<TCost> {
60 public:
62 
66  virtual bool call(const Graph& graph, node s, node t, List<edge>& edgeList,
67  edge e_st = nullptr) override {
68  return call(graph, nullptr, s, t, edgeList, e_st);
69  }
70 
74  virtual bool call(const Graph& graph, const EdgeArray<TCost>& weight, node s, node t,
75  List<edge>& edgeList, edge e_st = nullptr) override {
76  return call(graph, &weight, s, t, edgeList, e_st);
77  }
78 
80 
81 private:
87  bool call(const Graph& graph, const EdgeArray<TCost>* weight, node s, node t,
88  List<edge>& edgeList, edge e_st);
89 };
90 
91 template<typename TCost>
92 bool MinSTCutBFS<TCost>::call(const Graph& graph, const EdgeArray<TCost>* weight, node s, node t,
93  List<edge>& edgeList, edge e_st) {
94  bool weighted = (weight != nullptr);
95  delete m_gc;
96  m_gc = new GraphCopy(graph);
98  GraphCopy m_weightedGc;
99  EdgeArray<edge> mapE(m_weightedGc, nullptr);
100 
101  std::function<edge(edge)> orig = [&](edge e) -> edge { return m_gc->original(e); };
102 
103  if (weighted) {
104  m_weightedGc.init(graph);
105  if (e_st != nullptr) {
106  e_st = m_weightedGc.copy(e_st);
107  }
108  s = m_weightedGc.copy(s);
109  t = m_weightedGc.copy(t);
110  List<edge> edges;
111  graph.allEdges(edges);
112  for (edge e : edges) {
113  mapE[m_weightedGc.copy(e)] = e;
114  if (m_weightedGc.copy(e) == e_st) {
115  continue;
116  }
117  OGDF_ASSERT((*weight)[e] >= 1);
118  TCost i = 1;
119  for (; i < (*weight)[e]; i++) {
120  edge copyEdge = m_weightedGc.copy(e);
121  edge newEdge = m_weightedGc.newEdge(copyEdge->source(), copyEdge->target());
122  m_weightedGc.move(newEdge, copyEdge->adjSource(), ogdf::Direction::before,
123  copyEdge->adjTarget(), ogdf::Direction::after);
124  mapE[newEdge] = e;
125  }
126  OGDF_ASSERT((*weight)[e] == i);
127  }
128  // we can't reinit m_gc, because it's possible that the original graph of m_gc
129  // doesn't exist anymore.
130  delete m_gc;
131  m_gc = new GraphCopy();
132  m_gc->init(m_weightedGc);
133  orig = [&](edge e) -> edge { return mapE[m_gc->original(e)]; };
134  MinSTCutModule<TCost>::preprocessingDual(m_weightedGc, *m_gc, CE, s, t, e_st);
135  } else {
136  MinSTCutModule<TCost>::preprocessingDual(graph, *m_gc, CE, s, t, e_st);
137  }
138 
140 
141  DualGraph dual(CE);
142  if (e_st != nullptr) {
143  e_st = m_gc->copy(e_st);
144  } else {
145  e_st = m_gc->searchEdge(m_gc->copy(s), m_gc->copy(t));
146  }
147 
148  edgeList.clear();
149  node source = dual.dualNode(CE.rightFace(e_st->adjSource()));
150  node target = dual.dualNode(CE.leftFace(e_st->adjSource()));
151 
152  NodeArray<edge> spPred(dual, nullptr);
153  EdgeArray<node> prev(dual, nullptr);
154  EdgeArray<bool> direction(dual, true);
155  QueuePure<edge> queue;
156  for (adjEntry adj : source->adjEntries) {
157  if (dual.primalEdge(adj->theEdge()) != e_st) {
158  queue.append(adj->theEdge());
159  prev[adj->theEdge()] = source;
160  }
161  }
162  // actual search (using bfs on directed dual)
163  for (;;) {
164  // next candidate edge
165  edge eCand = queue.pop();
166  bool dir = (eCand->source() == prev[eCand]);
167  node v = (dir ? eCand->target() : eCand->source());
168 
169  // leads to an unvisited node?
170  if (spPred[v] == nullptr) {
171  // yes, then we set v's predecessor in search tree
172  spPred[v] = eCand;
173  direction[eCand] = dir;
174 
175  // have we reached t ...
176  if (v == target) {
177  // ... then search is done.
178  // constructed list of used edges (translated to crossed
179  // edges entries in G) from t back to s (including first
180  // and last!)
181 
182  do {
183  edge eDual = spPred[v];
184  OGDF_ASSERT(eDual != nullptr);
185  // this should be the right way round
186  edge eG = dual.primalEdge(eDual);
187  OGDF_ASSERT(eG != nullptr);
188  edgeList.pushBack(orig(eG));
189  MinSTCutModule<TCost>::m_direction[orig(eG)] = !direction[eDual];
190  v = prev[eDual];
191  } while (v != source);
192 
193  break;
194  }
195 
196  // append next candidate edges to queue
197  // (all edges leaving v)
198  for (adjEntry adj : v->adjEntries) {
199  if (prev[adj->theEdge()] == nullptr) {
200  queue.append(adj->theEdge());
201  prev[adj->theEdge()] = v;
202  }
203  }
204  }
205  }
206  if (weighted) {
207  auto prevIt = edgeList.begin();
208  for (auto it = prevIt.succ(); it != edgeList.end(); prevIt = it++) {
209  if (*prevIt == *it) {
210  edgeList.del(prevIt);
211  }
212  }
213  }
214  return true;
215 }
216 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
Graph.h
Includes declaration of graph class.
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::DualGraphBase
A dual graph including its combinatorial embedding of an embedded graph.
Definition: DualGraph.h:47
ogdf::MinSTCutModule::preprocessingDual
bool preprocessingDual(const Graph &graph, GraphCopy &gc, CombinatorialEmbedding &CE, node source, node target, edge e_st)
This method preprocesses gc for minstcut calculations, by adding an st-edge if needed and embedding g...
Definition: MinSTCutModule.h:113
ogdf::ConstCombinatorialEmbedding::leftFace
face leftFace(adjEntry adj) const
Returns the face to the left of adj, i.e., the face containing the twin of adj.
Definition: CombinatorialEmbedding.h:285
ogdf::ConstCombinatorialEmbedding::rightFace
face rightFace(adjEntry adj) const
Returns the face to the right of adj, i.e., the face containing adj.
Definition: CombinatorialEmbedding.h:279
ogdf::MinSTCutBFS
Min-st-cut algorithm, that calculates the cut by doing a depth first search over the dual graph of of...
Definition: MinSTCutBFS.h:59
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:391
Queue.h
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
ogdf::Direction::before
@ before
ogdf::Graph::allEdges
void allEdges(CONTAINER &edgeContainer) const
Returns a container with all edges of the graph.
Definition: Graph_d.h:1046
ogdf::GraphCopy::newEdge
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::edge
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:74
ogdf::GraphCopyBase::init
void init(const Graph &G)
Re-initializes the copy using G, creating copies for all nodes and edges in G.
Definition: GraphCopy.h:73
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:160
ogdf::Direction::after
@ after
ogdf::MinSTCutBFS::MinSTCutBFS
MinSTCutBFS()
Definition: MinSTCutBFS.h:61
ogdf::QueuePure::append
iterator append(const E &x)
Adds x at the end of queue.
Definition: Queue.h:171
ogdf::Graph::move
void move(edge e, adjEntry adjSrc, Direction dirSrc, adjEntry adjTgt, Direction dirTgt)
Moves edge e to a different adjacency list.
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
ogdf::MinSTCutModule
Definition: MinSTCutModule.h:45
ogdf::MinSTCutBFS::call
virtual bool call(const Graph &graph, node s, node t, List< edge > &edgeList, edge e_st=nullptr) override
The actual algorithm call.
Definition: MinSTCutBFS.h:66
DualGraph.h
Includes declaration of dual graph class.
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
GraphCopy.h
Declaration of graph copy classes.
ogdf::List< edge >
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::EdgeElement::adjSource
adjEntry adjSource() const
Returns the corresponding adjacancy entry at source node.
Definition: Graph_d.h:407
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::MinSTCutBFS::call
virtual bool call(const Graph &graph, const EdgeArray< TCost > &weight, node s, node t, List< edge > &edgeList, edge e_st=nullptr) override
The actual algorithm call.
Definition: MinSTCutBFS.h:74
ogdf::GraphCopy::copy
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition: GraphCopy.h:464
ogdf::EdgeElement::adjTarget
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition: Graph_d.h:410
ogdf::QueuePure
Implementation of list-based queues.
Definition: Queue.h:55
ogdf::List::del
void del(iterator it)
Removes it from the list.
Definition: List.h:1611
basic.h
Basic declarations, included by all source files.
CombinatorialEmbedding.h
Declaration of CombinatorialEmbedding and face.
ogdf::DualGraphBase::primalEdge
const edge & primalEdge(edge e) const
Returns the edge in the primal graph corresponding to e.
Definition: DualGraph.h:161
ogdf::CombinatorialEmbedding
Combinatorial embeddings of planar graphs with modification functionality.
Definition: CombinatorialEmbedding.h:406
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
List.h
Declaration of doubly linked lists and iterators.
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
ogdf::QueuePure::pop
E pop()
Removes front element and returns it.
Definition: Queue.h:183
ogdf::DualGraphBase::dualNode
const node & dualNode(face f) const
Returns the node in the dual graph corresponding to f.
Definition: DualGraph.h:175
MinSTCutModule.h
Template of base class of min-st-cut algorithms.
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1547
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::List::clear
void clear()
Removes all elements from the list.
Definition: List.h:1626