Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MinSTCutBFS.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/DualGraph.h>
36 #include <ogdf/basic/GraphCopy.h>
37 #include <ogdf/basic/Queue.h>
39 #include <ogdf/graphalg/Dijkstra.h>
41 
42 namespace ogdf {
43 
53 template<typename TCost>
54 class MinSTCutBFS : public MinSTCutModule<TCost> {
55 public:
57 
61  virtual bool call(const Graph& graph, node s, node t, List<edge>& edgeList,
62  edge e_st = nullptr) override {
63  return call(graph, nullptr, s, t, edgeList, e_st);
64  }
65 
69  virtual bool call(const Graph& graph, const EdgeArray<TCost>& weight, node s, node t,
70  List<edge>& edgeList, edge e_st = nullptr) override {
71  return call(graph, &weight, s, t, edgeList, e_st);
72  }
73 
75 
76 private:
82  bool call(const Graph& graph, const EdgeArray<TCost>* weight, node s, node t,
83  List<edge>& edgeList, edge e_st);
84 };
85 
86 template<typename TCost>
87 bool MinSTCutBFS<TCost>::call(const Graph& graph, const EdgeArray<TCost>* weight, node s, node t,
88  List<edge>& edgeList, edge e_st) {
89  bool weighted = (weight != nullptr);
90  delete m_gc;
91  m_gc = new GraphCopy(graph);
93  GraphCopy m_weightedGc;
94  EdgeArray<edge> mapE(m_weightedGc, nullptr);
95 
96  std::function<edge(edge)> orig = [&](edge e) -> edge { return m_gc->original(e); };
97 
98  if (weighted) {
99  m_weightedGc.init(graph);
100  if (e_st != nullptr) {
101  e_st = m_weightedGc.copy(e_st);
102  }
103  s = m_weightedGc.copy(s);
104  t = m_weightedGc.copy(t);
105  List<edge> edges;
106  graph.allEdges(edges);
107  for (edge e : edges) {
108  mapE[m_weightedGc.copy(e)] = e;
109  if (m_weightedGc.copy(e) == e_st) {
110  continue;
111  }
112  OGDF_ASSERT((*weight)[e] >= 1);
113  TCost i = 1;
114  for (; i < (*weight)[e]; i++) {
115  edge copyEdge = m_weightedGc.copy(e);
116  edge newEdge = m_weightedGc.newEdge(copyEdge->source(), copyEdge->target());
117  m_weightedGc.move(newEdge, copyEdge->adjSource(), ogdf::Direction::before,
118  copyEdge->adjTarget(), ogdf::Direction::after);
119  mapE[newEdge] = e;
120  }
121  OGDF_ASSERT((*weight)[e] == i);
122  }
123  // we can't reinit m_gc, because it's possible that the original graph of m_gc
124  // doesn't exist anymore.
125  delete m_gc;
126  m_gc = new GraphCopy();
127  m_gc->init(m_weightedGc);
128  orig = [&](edge e) -> edge { return mapE[m_gc->original(e)]; };
129  MinSTCutModule<TCost>::preprocessingDual(m_weightedGc, *m_gc, CE, s, t, e_st);
130  } else {
131  MinSTCutModule<TCost>::preprocessingDual(graph, *m_gc, CE, s, t, e_st);
132  }
133 
135 
136  DualGraph dual(CE);
137  if (e_st != nullptr) {
138  e_st = m_gc->copy(e_st);
139  } else {
140  e_st = m_gc->searchEdge(m_gc->copy(s), m_gc->copy(t));
141  }
142 
143  edgeList.clear();
144  node source = dual.dualNode(CE.rightFace(e_st->adjSource()));
145  node target = dual.dualNode(CE.leftFace(e_st->adjSource()));
146 
147  NodeArray<edge> spPred(dual, nullptr);
148  EdgeArray<node> prev(dual, nullptr);
149  EdgeArray<bool> direction(dual, true);
150  QueuePure<edge> queue;
151  for (adjEntry adj : source->adjEntries) {
152  if (dual.primalEdge(adj->theEdge()) != e_st) {
153  queue.append(adj->theEdge());
154  prev[adj->theEdge()] = source;
155  }
156  }
157  // actual search (using bfs on directed dual)
158  for (;;) {
159  // next candidate edge
160  edge eCand = queue.pop();
161  bool dir = (eCand->source() == prev[eCand]);
162  node v = (dir ? eCand->target() : eCand->source());
163 
164  // leads to an unvisited node?
165  if (spPred[v] == nullptr) {
166  // yes, then we set v's predecessor in search tree
167  spPred[v] = eCand;
168  direction[eCand] = dir;
169 
170  // have we reached t ...
171  if (v == target) {
172  // ... then search is done.
173  // constructed list of used edges (translated to crossed
174  // edges entries in G) from t back to s (including first
175  // and last!)
176 
177  do {
178  edge eDual = spPred[v];
179  OGDF_ASSERT(eDual != nullptr);
180  // this should be the right way round
181  edge eG = dual.primalEdge(eDual);
182  OGDF_ASSERT(eG != nullptr);
183  edgeList.pushBack(orig(eG));
184  MinSTCutModule<TCost>::m_direction[orig(eG)] = !direction[eDual];
185  v = prev[eDual];
186  } while (v != source);
187 
188  break;
189  }
190 
191  // append next candidate edges to queue
192  // (all edges leaving v)
193  for (adjEntry adj : v->adjEntries) {
194  if (prev[adj->theEdge()] == nullptr) {
195  queue.append(adj->theEdge());
196  prev[adj->theEdge()] = v;
197  }
198  }
199  }
200  }
201  if (weighted) {
202  auto prevIt = edgeList.begin();
203  for (auto it = prevIt.succ(); it != edgeList.end(); prevIt = it++) {
204  if (*prevIt == *it) {
205  edgeList.del(prevIt);
206  }
207  }
208  }
209  return true;
210 }
211 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::DualGraphBase
A dual graph including its combinatorial embedding of an embedded graph.
Definition: DualGraph.h:42
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:108
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:276
extended_graph_alg.h
Declaration of extended graph algorithms.
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:270
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:54
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:384
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:1038
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:135
ogdf::edge
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:67
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:66
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:153
ogdf::Direction::after
@ after
ogdf::MinSTCutBFS::MinSTCutBFS
MinSTCutBFS()
Definition: MinSTCutBFS.h:56
ogdf::QueuePure::append
iterator append(const E &x)
Adds x at the end of queue.
Definition: Queue.h:166
ogdf::Graph::move
void move(edge e, adjEntry adjSrc, Direction dirSrc, adjEntry adjTgt, Direction dirTgt)
Moves edge e to a different adjacency list.
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:264
ogdf::MinSTCutModule
Definition: MinSTCutModule.h:40
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:61
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:391
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:651
ogdf::EdgeElement::adjSource
adjEntry adjSource() const
Returns the corresponding adjacancy entry at source node.
Definition: Graph_d.h:400
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
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:69
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:457
ogdf::EdgeElement::adjTarget
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition: Graph_d.h:403
ogdf::QueuePure
Implementation of list-based queues.
Definition: Queue.h:50
ogdf::List::del
void del(iterator it)
Removes it from the list.
Definition: List.h:1601
ogdf::DualGraphBase::primalEdge
const edge & primalEdge(edge e) const
Returns the edge in the primal graph corresponding to e.
Definition: DualGraph.h:156
ogdf::CombinatorialEmbedding
Combinatorial embeddings of planar graphs with modification functionality.
Definition: CombinatorialEmbedding.h:397
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
Dijkstra.h
Implementation of Dijkstra's single source shortest path algorithm.
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:394
ogdf::QueuePure::pop
E pop()
Removes front element and returns it.
Definition: Queue.h:178
ogdf::DualGraphBase::dualNode
const node & dualNode(face f) const
Returns the node in the dual graph corresponding to f.
Definition: DualGraph.h:170
MinSTCutModule.h
Template of base class of min-st-cut algorithms.
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1537
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709
ogdf::List::clear
void clear()
Removes all elements from the list.
Definition: List.h:1616