Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MinSTCutMaxFlow.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <memory>
37 
38 namespace ogdf {
39 
45 template<typename TCost>
46 class MinSTCutMaxFlow: public MinSTCutModule<TCost> {
48 public:
64  explicit MinSTCutMaxFlow(bool treatAsUndirected = true, MaxFlowModule<TCost> *mfModule = new MaxFlowGoldbergTarjan<TCost>(),
65  bool primaryCut = true, bool calculateOtherCut = true,
66  EpsilonTest *epsilonTest = new EpsilonTest()) :
67  m_mfModule(mfModule), m_treatAsUndirected(treatAsUndirected), m_primaryCut(primaryCut),
68  m_calculateOtherCut(calculateOtherCut), m_et(epsilonTest)
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 
80  virtual bool call(const Graph &graph, node s, node t, List<edge> &edgeList, edge e_st = nullptr) override {
81  EdgeArray<TCost> weight(graph, 1);
82  return call(graph, weight, s, t, edgeList, e_st);
83  }
84 
94  void call(const Graph &graph, const EdgeArray<TCost> &weights, const EdgeArray<TCost> &flow, const node source, const node target);
95 
99  enum class cutType {
100  FRONT_CUT,
101  BACK_CUT,
102  NO_CUT
103  };
104 
109  {
110  m_et.reset(et);
111  }
112 
120  {
123  }
124 
128  bool isFrontCutEdge(const edge e) const
129  {
131  return m_nodeSet[e->source()] == cutType::FRONT_CUT
132  && m_nodeSet[e->target()] != cutType::FRONT_CUT;
133  }
134 
138  bool isBackCutEdge(const edge e) const
139  {
141  return m_nodeSet[e->target()] == cutType::BACK_CUT
142  && m_nodeSet[e->source()] != cutType::BACK_CUT;
143  }
144 
149  bool isInFrontCut(const node v) const
150  {
152  return m_nodeSet[v] == cutType::FRONT_CUT;
153  }
154 
161  bool isInBackCut(const node v) const
162  {
164  return m_nodeSet[v] == cutType::BACK_CUT;
165  }
166 
178  bool isOfType(const node v, cutType type) const
179  {
180  return m_nodeSet[v] == type;
181  }
182 
183 private:
185  std::unique_ptr<MaxFlowModule<TCost>> m_mfModule;
187  bool m_treatAsUndirected = false;
189  bool m_primaryCut = true;
195  bool m_calculateOtherCut = true;
196 
197 
198  std::unique_ptr<EpsilonTest> m_et;
205 
213  void markCut(node startNode, bool frontCut, std::function<node(node)> origNode) {
214  List<node> queue;
215  queue.pushBack(startNode);
216  m_nodeSet[origNode(startNode)] = (frontCut ? cutType::FRONT_CUT : cutType::BACK_CUT);
217  frontCut ? m_frontCutCount++ : m_backCutCount++;
218 
219  while (!queue.empty()) {
220  const node v = queue.popFrontRet();
221  for (adjEntry adj : v->adjEntries) {
222  const node w = adj->twinNode();
223  const edge e = adj->theEdge();
224  if (m_nodeSet[origNode(w)] == cutType::NO_CUT
225  && (((frontCut ? e->source() :e->target()) == v &&
226  m_et->less(m_flow[e], m_weight[e]))
227  || ((frontCut ? e->target() : e->source()) == v &&
228  m_et->greater(m_flow[e], TCost(0))))) {
229  queue.pushBack(w);
230  m_nodeSet[origNode(w)] = (frontCut ? cutType::FRONT_CUT : cutType::BACK_CUT);
231  frontCut ? m_frontCutCount++ : m_backCutCount++;
232  }
233  }
234  }
235  }
236 
247  void computeCut(const Graph &graph, std::function<edge(edge)> origEdge, std::function<node(node)> origNode,
248  const node source, const node target, List<edge> &edgeList) {
249  m_frontCutCount = 0;
250  m_backCutCount = 0;
251  m_totalCount = graph.numberOfNodes();
252 
253 
254  List<node> queue;
255  m_nodeSet.init(graph, cutType::NO_CUT);
256 
257  if(m_primaryCut || m_calculateOtherCut) {
258  // front cut
259  markCut(source, true, origNode);
260  }
261 
262  if(!m_primaryCut || m_calculateOtherCut) {
263  // back cut
264  markCut(target, false, origNode);
265  }
266 
267  ArrayBuffer<edge> stack;
268  EdgeArray<bool> visited(graph, false);
269  node startNode = (m_primaryCut ? source : target);
270  adjEntry startAdj = startNode->firstAdj();
271  if (startAdj == nullptr) {
272  return;
273  }
274  if (startAdj->theEdge()->adjTarget() != startAdj) {
275  stack.push(startAdj->theEdge());
276  }
277  for (adjEntry adj = (m_primaryCut ? startAdj->cyclicSucc() : startAdj->cyclicPred());
278  adj != startAdj;
279  adj = (m_primaryCut ? adj->cyclicSucc() : adj->cyclicPred()))
280  {
281  if (adj->theEdge()->adjTarget() != adj) {
282  stack.push(adj->theEdge());
283  }
284  }
285  while (!stack.empty()) {
286  edge e = stack.popRet();
287  if (visited[origEdge(e)]) {
288  continue;
289  }
290  visited[origEdge(e)] = true;
291  if (m_nodeSet[origNode(e->source())] == cutType::FRONT_CUT &&
292  m_nodeSet[origNode(e->target())] != cutType::FRONT_CUT) {
293  edgeList.pushBack(origEdge(e));
294 
295  if(m_gc->numberOfEdges() != 0) {
296  MinSTCutModule<TCost>::m_direction[origEdge(e)] = m_gc->copy(origEdge(e)) == e;
297  }
298  } else {
299  startAdj = e->adjTarget();
300  for (adjEntry adj = (m_primaryCut ? startAdj->cyclicSucc() : startAdj->cyclicPred());
301  adj != startAdj;
302  adj = (m_primaryCut ? adj->cyclicSucc() : adj->cyclicPred()))
303  {
304  if (adj->theEdge()->adjTarget() != adj) {
305  stack.push(adj->theEdge());
306  }
307  }
308  }
309  }
310  }
311 };
312 
313 template<typename TCost>
314 bool MinSTCutMaxFlow<TCost>::call(const Graph &graph, const EdgeArray<TCost> &weight,
315  node source, node target, List<edge> &edgeList, edge e_st) {
316  MinSTCutModule<TCost>::m_direction.init(graph, -1);
317  delete m_gc;
318  m_gc = new GraphCopy(graph);
319 
320  if (e_st != nullptr) {
321  m_gc->delEdge(m_gc->copy(e_st));
322  }
323  node s = m_gc->copy(source);
324  node t = m_gc->copy(target);
325  List<edge> edges;
326  m_gc->allEdges(edges);
327  EdgeArray<edge> originalEdge(*m_gc, nullptr);
328  for (edge e : edges) {
329  if (m_treatAsUndirected) {
330  // a reversed edge is made and placed directly next to e
331  edge revEdge = m_gc->newEdge(e->target(), e->source());
332  m_gc->move(revEdge, e->adjTarget(), ogdf::Direction::before, e->adjSource(), ogdf::Direction::after);
333  originalEdge[revEdge] = m_gc->original(e);
334  }
335  originalEdge[e] = m_gc->original(e);
336  }
337 
338  m_flow.init(*m_gc, -1);
339  m_weight.init(*m_gc, 1);
340  for (edge e : m_gc->edges) {
341  edge origEdge = originalEdge[e];
342  m_weight[e] = weight[origEdge];
343  OGDF_ASSERT(m_weight[e] >= 0);
344  }
345 
346  m_mfModule->init(*m_gc);
347  m_mfModule->computeFlow(m_weight, s, t, m_flow);
348 
349  computeCut(graph,
350  [&](edge e) -> edge {return originalEdge[e];},
351  [&](node v) -> node {return m_gc->original(v);},
352  s, t, edgeList);
353 
354  return true;
355 }
356 
357 template<typename TCost>
359  const EdgeArray<TCost> &weights,
360  const EdgeArray<TCost> &flow,
361  const node source, const node target) {
362  delete m_gc;
363  m_gc = new GraphCopy();
364  m_flow = flow;
365  m_weight = weights;
366  List<edge> edgeList;
367  computeCut(graph,
368  [&](edge e) -> edge {return e;},
369  [&](node v) -> node {return v;},
370  source, target, edgeList);
371 }
372 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
void move(edge e, adjEntry adjSrc, Direction dirSrc, adjEntry adjTgt, Direction dirTgt)
Moves edge e to a different adjacency list.
void markCut(node startNode, bool frontCut, std::function< node(node)> origNode)
Mark the all nodes which are in the same cut partition as the startNode.
EdgeArray< TCost > m_weight
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:67
std::unique_ptr< EpsilonTest > m_et
void setEpsilonTest(EpsilonTest *et)
Assigns a new epsilon test.
bool isBackCutEdge(const edge e) const
Returns whether this edge is entering the back cut.
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:335
bool frontCutIsComplementOfBackCut() const
Returns whether the front cut is the complement of the backcut.
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:283
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
bool m_calculateOtherCut
iff true, the other (near to s for primaryCut == false, near to t for primaryCut == true) cut should ...
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
Min-st-cut algorithm, that calculates the cut via maxflow.
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
cutType
The three types of cuts.
bool m_primaryCut
true if the algorithm should search for the mincut nearest to s, false if it should be near to t...
adjEntry cyclicSucc() const
Returns the cyclic successor in the adjacency list.
Definition: Graph_d.h:279
EdgeArray< TCost > m_flow
Declaration and implementation of Goldberg-Tarjan max-flow algorithm with global relabeling and gap r...
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
bool isOfType(const node v, cutType type) const
Return whether this node is of the specified type.
virtual void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e&#39;s original edge.
bool isInFrontCut(const node v) const
Returns whether this node is part of the front cut.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:601
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:116
virtual bool call(const Graph &graph, node s, node t, List< edge > &edgeList, edge e_st=nullptr) override
The actual algorithm call.
NodeArray< cutType > m_nodeSet
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class for the representation of edges.
Definition: Graph_d.h:292
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1417
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:249
NodeElement * node
The type of nodes.
Definition: Graph_d.h:63
Class for the representation of nodes.
Definition: Graph_d.h:169
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.
bool isFrontCutEdge(const edge e) const
Returns whether this edge is leaving the front cut.
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:286
adjEntry cyclicPred() const
Returns the cyclic predecessor in the adjacency list.
Definition: Graph_d.h:284
bool m_treatAsUndirected
states whether or not edges are considered undirected while calculating the maxflow ...
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:113
node is not part of any cut
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
void computeCut(const Graph &graph, std::function< edge(edge)> origEdge, std::function< node(node)> origNode, const node source, const node target, List< edge > &edgeList)
Partitions the nodes to front and back cut.
bool isInBackCut(const node v) const
Returns whether this node is part of the back cut.
The namespace for all OGDF objects.
void allEdges(CONTAINER &edgeContainer) const
Returns a container with all edges of the graph.
Definition: Graph_d.h:661
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
Template of base class of min-st-cut algorithms.
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:213
MinSTCutMaxFlow(bool treatAsUndirected=true, MaxFlowModule< TCost > *mfModule=new MaxFlowGoldbergTarjan< TCost >(), bool primaryCut=true, bool calculateOtherCut=true, EpsilonTest *epsilonTest=new EpsilonTest())
Constructor.
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition: Graph_d.h:335
bool empty() const
Returns true iff the list is empty.
Definition: List.h:256
Computes a max flow via Preflow-Push (global relabeling and gap relabeling heuristic).
Class for adjacency list elements.
Definition: Graph_d.h:79
std::unique_ptr< MaxFlowModule< TCost > > m_mfModule
the module used for calculating the maxflow
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200