Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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