Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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), m_et(epsilonTest) { }
68 
72  virtual bool call(const Graph &graph, const EdgeArray<TCost> &weight, node s, node t, List<edge> &edgeList, edge e_st) override;
73 
77  virtual bool call(const Graph &graph, node s, node t, List<edge> &edgeList, edge e_st) override {
78  EdgeArray<TCost> weight(graph, 1);
79  return call(graph, weight, s, t, edgeList, e_st);
80  }
81 
91  void call(const Graph &graph, const EdgeArray<TCost> &weights, const EdgeArray<TCost> &flow, const node source, const node target);
92 
96  enum class cutType {
97  FRONT_CUT,
98  BACK_CUT,
99  NO_CUT
100  };
101 
106  {
107  m_et.reset(et);
108  }
109 
117  {
120  }
121 
125  bool isFrontCutEdge(const edge e) const
126  {
128  return m_nodeSet[e->source()] == cutType::FRONT_CUT
129  && m_nodeSet[e->target()] != cutType::FRONT_CUT;
130  }
131 
135  bool isBackCutEdge(const edge e) const
136  {
138  return m_nodeSet[e->target()] == cutType::BACK_CUT
139  && m_nodeSet[e->source()] != cutType::BACK_CUT;
140  }
141 
146  bool isInFrontCut(const node v) const
147  {
149  return m_nodeSet[v] == cutType::FRONT_CUT;
150  }
151 
158  bool isInBackCut(const node v) const
159  {
161  return m_nodeSet[v] == cutType::BACK_CUT;
162  }
163 
175  bool isOfType(const node v, cutType type) const
176  {
177  return m_nodeSet[v] == type;
178  }
179 
180 private:
182  std::unique_ptr<MaxFlowModule<TCost>> m_mfModule;
184  bool m_treatAsUndirected = false;
186  bool m_primaryCut = true;
192  bool m_calculateOtherCut = true;
193 
194 
195  std::unique_ptr<EpsilonTest> m_et;
202 
210  void markCut(node startNode, bool frontCut, std::function<node(node)> origNode) {
211  List<node> queue;
212  queue.pushBack(startNode);
213  m_nodeSet[origNode(startNode)] = (frontCut ? cutType::FRONT_CUT : cutType::BACK_CUT);
214 
215  while (!queue.empty()) {
216  const node v = queue.popFrontRet();
217  for (adjEntry adj : v->adjEntries) {
218  const node w = adj->twinNode();
219  const edge e = adj->theEdge();
220  if (m_nodeSet[origNode(w)] == cutType::NO_CUT
221  && (((frontCut ? e->source() :e->target()) == v &&
222  m_et->less(m_flow[e], m_weight[e]))
223  || ((frontCut ? e->target() : e->source()) == v &&
224  m_et->greater(m_flow[e], TCost(0))))) {
225  queue.pushBack(w);
226  m_nodeSet[origNode(w)] = (frontCut ? cutType::FRONT_CUT : cutType::BACK_CUT);
227  frontCut ? m_frontCutCount++ : m_backCutCount++;
228  }
229  }
230  }
231  }
232 
243  void computeCut(const Graph &graph, std::function<edge(edge)> origEdge, std::function<node(node)> origNode,
244  const node source, const node target, List<edge> &edgeList) {
245  m_frontCutCount = 0;
246  m_backCutCount = 0;
247  m_totalCount = graph.numberOfNodes();
248 
249 
250  List<node> queue;
251  m_nodeSet.init(graph, cutType::NO_CUT);
252 
253  if(m_primaryCut || m_calculateOtherCut) {
254  // front cut
255  markCut(source, true, origNode);
256  }
257 
258  if(!m_primaryCut || m_calculateOtherCut) {
259  // back cut
260  markCut(target, false, origNode);
261  }
262 
263  StackPure<edge> stack;
264  EdgeArray<bool> visited(graph, false);
265  node startNode = (m_primaryCut ? source : target);
266  adjEntry startAdj = startNode->firstAdj();
267  if (startAdj->theEdge()->adjTarget() != startAdj) {
268  stack.push(startAdj->theEdge());
269  }
270  for (adjEntry adj = (m_primaryCut ? startAdj->cyclicSucc() : startAdj->cyclicPred());
271  adj != startAdj;
272  adj = (m_primaryCut ? adj->cyclicSucc() : adj->cyclicPred()))
273  {
274  if (adj->theEdge()->adjTarget() != adj) {
275  stack.push(adj->theEdge());
276  }
277  }
278  while (!stack.empty()) {
279  edge e = stack.pop();
280  if (visited[origEdge(e)]) {
281  continue;
282  }
283  visited[origEdge(e)] = true;
284  if (m_nodeSet[origNode(e->source())] == cutType::FRONT_CUT &&
285  m_nodeSet[origNode(e->target())] != cutType::FRONT_CUT) {
286  edgeList.pushBack(origEdge(e));
287 
288  if(m_gc->numberOfEdges() != 0) {
289  MinSTCutModule<TCost>::m_direction[origEdge(e)] = m_gc->copy(origEdge(e)) == e;
290  }
291  } else {
292  startAdj = e->adjTarget();
293  for (adjEntry adj = (m_primaryCut ? startAdj->cyclicSucc() : startAdj->cyclicPred());
294  adj != startAdj;
295  adj = (m_primaryCut ? adj->cyclicSucc() : adj->cyclicPred()))
296  {
297  if (adj->theEdge()->adjTarget() != adj) {
298  stack.push(adj->theEdge());
299  }
300  }
301  }
302  }
303  }
304 };
305 
306 template<typename TCost>
307 bool MinSTCutMaxFlow<TCost>::call(const Graph &graph, const EdgeArray<TCost> &weight,
308  node source, node target, List<edge> &edgeList, edge e_st) {
309  MinSTCutModule<TCost>::m_direction.init(graph, -1);
310  delete m_gc;
311  m_gc = new GraphCopy(graph);
312 
313  if (e_st != nullptr) {
314  m_gc->delEdge(m_gc->copy(e_st));
315  }
316  node s = m_gc->copy(source);
317  node t = m_gc->copy(target);
318  List<edge> edges;
319  m_gc->allEdges(edges);
320  EdgeArray<edge> originalEdge(*m_gc, nullptr);
321  for (edge e : edges) {
322  if (m_treatAsUndirected) {
323  // a reversed edge is made and placed directly next to e
324  edge revEdge = m_gc->newEdge(e->target(), e->source());
325  m_gc->move(revEdge, e->adjTarget(), ogdf::Direction::before, e->adjSource(), ogdf::Direction::after);
326  originalEdge[revEdge] = m_gc->original(e);
327  }
328  originalEdge[e] = m_gc->original(e);
329  }
330 
331  m_flow.init(*m_gc, -1);
332  m_weight.init(*m_gc, 1);
333  for (edge e : m_gc->edges) {
334  edge origEdge = originalEdge[e];
335  m_weight[e] = weight[origEdge];
336  OGDF_ASSERT(m_weight[e] >= 0);
337  }
338 
339  m_mfModule->init(*m_gc);
340  m_mfModule->computeFlow(m_weight, s, t, m_flow);
341 
342  computeCut(graph,
343  [&](edge e) -> edge {return originalEdge[e];},
344  [&](node v) -> node {return m_gc->original(v);},
345  s, t, edgeList);
346 
347  return true;
348 }
349 
350 template<typename TCost>
352  const EdgeArray<TCost> &weights,
353  const EdgeArray<TCost> &flow,
354  const node source, const node target) {
355  delete m_gc;
356  m_gc = new GraphCopy();
357  m_flow = flow;
358  m_weight = weights;
359  List<edge> edgeList;
360  computeCut(graph,
361  [&](edge e) -> edge {return e;},
362  [&](node v) -> node {return v;},
363  source, target, edgeList);
364 }
365 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
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:66
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:95
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:336
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:320
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
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:292
Min-st-cut algorithm, that calculates the cut via maxflow.
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:278
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.
void push(const E &x)
Adds element x as top-most element to the stack.
Definition: Stack.h:127
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:548
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:581
List-based stacks.
Definition: SList.h:39
NodeArray< cutType > m_nodeSet
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:105
Class for the representation of edges.
Definition: Graph_d.h:291
void allEdges(EDGELIST &edgeList) const
Returns a list with all edges of the graph.
Definition: Graph_d.h:643
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1360
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:250
NodeElement * node
The type of nodes.
Definition: Graph_d.h:62
virtual bool call(const Graph &graph, node s, node t, List< edge > &edgeList, edge e_st) override
The actual algorithm call.
Class for the representation of nodes.
Definition: Graph_d.h:167
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:287
adjEntry cyclicPred() const
Returns the cyclic predecessor in the adjacency list.
Definition: Graph_d.h:283
E pop()
Removes the top-most element from the stack and returns it.
Definition: Stack.h:141
bool m_treatAsUndirected
states whether or not edges are considered undirected while calculating the maxflow ...
node is not part of any cut
virtual bool call(const Graph &graph, const EdgeArray< TCost > &weight, node s, node t, List< edge > &edgeList, edge e_st) override
The actual algorithm call.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
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.
bool empty() const
Returns true iff the stack is empty.
Definition: Stack.h:96
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:578
Template of base class of min-st-cut algorithms.
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:211
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:332
bool empty() const
Returns true iff the list is empty.
Definition: List.h:236
Computes a max flow via Preflow-Push (global relabeling and gap relabeling heuristic).
Class for adjacency list elements.
Definition: Graph_d.h:78
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:198