Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MaxFlowSTPlanarItaiShiloach.h
Go to the documentation of this file.
1 
35 #pragma once
36 
37 #include <limits>
41 
42 namespace ogdf {
43 
45 
48 template<typename TCap>
50 {
51 private:
52 
59  return m_prioritizedEdges->priority(e) - TCap(1);
60  }
61 
67  return m_prioritizedEdges->topPriority() - TCap(1);
68  }
69 
75  TCap shiftPriority(TCap priority) {
76  OGDF_ASSERT(priority < std::numeric_limits<TCap>::max());
77  return priority + TCap(1);
78  }
79 
83  enum class NodeType {
84  New,
85  Path,
86  Done
87  };
88 
94  enum class EdgePathType {
95  NoPath,
96  SourcePath,
97  TargetPath,
98  Unknown
99  };
100 
102 
105 
108 
111 
114 
117 
120 
121 public:
126  delete m_prioritizedEdges;
127  }
128 
138  TCap computeValue(const EdgeArray<TCap> &originalCapacities, const node &source, const node &target)
139  {
140  OGDF_ASSERT(source != target);
141  OGDF_ASSERT(isSTPlanar(*this->m_G, source, target));
142 
143  // initialize auxiliary structures
144 
145  m_partialFlow = (TCap) 0;
146 
147  this->m_s = &source;
148  this->m_t = &target;
149  this->m_cap = &originalCapacities;
150  this->m_flow->init(*this->m_G, (TCap) 0);
151 
152  // establish s-t-planarity
153  ConstCombinatorialEmbedding embedding(*this->m_G);
154  OGDF_ASSERT(embedding.consistencyCheck());
155  adjEntry secondCommonAdj;
156  m_commonFaceAdj = embedding.findCommonFace(*this->m_s, *this->m_t, secondCommonAdj);
157  OGDF_ASSERT(m_commonFaceAdj != nullptr);
158 
159  m_pred.init(*this->m_G, nullptr);
160  m_status.init(*this->m_G, NodeType::New);
161  m_visited.init(*this->m_G, false);
162  m_edgeCounter.init(*this->m_G, 0);
163  m_status[*this->m_s] = NodeType::Path;
164  m_status[*this->m_t] = NodeType::Path;
165 
166  delete m_prioritizedEdges;
167  m_prioritizedEdges = new PrioritizedMapQueue<edge, TCap>(*this->m_G);
168 
169  // saturate all paths
170 
171  edge lastSaturated = nullptr;
172  while(findUppermostPath(lastSaturated)) {
173 
174  lastSaturated = m_prioritizedEdges->topElement();
175  m_partialFlow = unshiftedTopPriority();
176  m_prioritizedEdges->pop();
177 
178  (*this->m_flow)[lastSaturated] = (*this->m_cap)[lastSaturated];
179 
180  m_pred[lastSaturated->target()] = nullptr;
181  OGDF_ASSERT(m_status[lastSaturated->target()] == NodeType::Path);
182  OGDF_ASSERT(m_status[lastSaturated->source()] == NodeType::Path);
183  }
184 
185  return m_partialFlow;
186  }
187 
196  {
197  // the flow value is only computed if the edge is deleted
198  while (!m_prioritizedEdges->empty()) {
199  edge e = m_prioritizedEdges->topElement();
200  dropEdge(e);
201  }
202  }
203 
204  // use methods from super class
210 
211 private:
212 
218  bool findUppermostPath(const edge saturatedEdge)
219  {
220  node v, u;
221  adjEntry initialAdj;
222 
223  if (saturatedEdge == nullptr) {
224  v = *this->m_s;
225  u = *this->m_t;
226  initialAdj = m_commonFaceAdj;
227  } else {
228  v = saturatedEdge->source();
229  u = saturatedEdge->target();
230  initialAdj = saturatedEdge->adjSource()->cyclicSucc();
231  }
232 
233  OGDF_ASSERT(u != v);
234  OGDF_ASSERT(v != *this->m_t);
235  OGDF_ASSERT(u != *this->m_s);
236 
237  for(adjEntry adj = initialAdj;
238  m_edgeCounter[v] < v->degree();
239  adj = adj->cyclicSucc())
240  {
241  m_edgeCounter[v]++;
242  edge e = adj->theEdge();
243 
244  bool visited = m_visited[e];
245  m_visited[e] = true;
246 
247  if(!visited
248  && e->target() != v
249  && m_status[e->target()] != NodeType::Done)
250  {
251  EdgePathType pathType = getPathType(e);
252  OGDF_ASSERT(pathType != EdgePathType::Unknown);
253 
254  if(pathType == EdgePathType::NoPath) {
255  // extend the path
256  appendEdge(e);
257  adj = e->adjTarget();
258  v = e->target();
259  } else if(pathType == EdgePathType::TargetPath) {
260  // merge path
261  node w = e->target();
262 
263  // remove tangling target path
264  edge f;
265  while(m_pred[w] != nullptr) {
266  f = m_pred[w];
267  w = f->source();
268  dropEdge(f);
269  }
270 
271  m_status[w] = NodeType::Done;
272  appendEdge(e);
273 
274  return true;
275  } else {
276  // remove source path cycle
278  node w = e->target();
279 
280  node formerSource;
281  while(e->source() != w) {
282  formerSource = e->source();
283  if(e->target() != w) {
284  dropEdge(e);
285  }
286  e = m_pred[formerSource]->adjSource()->theEdge();
287  }
288 
289  dropEdge(e);
290  adj = e->adjSource();
291  v = e->source();
292  u = e->target();
293  }
294  }
295  }
296 
297  // v is a deadend
298  if(v == *this->m_s) {
299  return false;
300  } else {
301  edge e = m_pred[v];
302  dropEdge(e);
303  return findUppermostPath(e);
304  }
305  }
306 
312  void appendEdge(const edge e) {
313  node v = e->target();
314  OGDF_ASSERT(m_pred[v] == nullptr);
315 
316  // update path predecessor
317  m_pred[v] = e;
318  m_status[v] = NodeType::Path;
319 
320  // insert into priority queue while
321  // taking into account the partial flow
322  TCap value = m_partialFlow + (*this->m_cap)[e];
323  m_prioritizedEdges->push(e, shiftPriority(value));
324  }
325 
332  void dropEdge(const edge e) {
333  node v = e->target();
334  OGDF_ASSERT(m_pred[v] == e);
335  OGDF_ASSERT(m_status[v] == NodeType::Path);
336 
337  // update path predecessor
338  m_pred[v] = nullptr;
339  m_status[v] = v == *this->m_t ? NodeType::Path : NodeType::Done;
340 
341  // remove edge from priority queue
342  TCap modCap = unshiftedPriority(e);
343  m_prioritizedEdges->decrease(e, std::numeric_limits<TCap>::min());
344 #ifdef OGDF_DEBUG
345  edge f = m_prioritizedEdges->topElement();
346 #endif
347  m_prioritizedEdges->pop();
348 
349  // determine the flow on this edge
350  (*this->m_flow)[e] = m_partialFlow - (modCap - (*this->m_cap)[e]);
351 
352  OGDF_ASSERT(e == f);
353  }
354 
361  EdgePathType getPathType(const edge e) const {
362  node v = e->source();
363  node w = e->target();
364 
366 
367  while(result == EdgePathType::Unknown) {
368  if(v == e->target() || w == *this->m_s) {
369  result = EdgePathType::SourcePath;
370  } else if(m_pred[v] == nullptr || m_pred[w] == nullptr) {
371  result = EdgePathType::TargetPath;
372  } else if(m_pred[w]->source() == *this->m_s) {
373  result = EdgePathType::SourcePath;
374  } else {
375  OGDF_ASSERT(w != m_pred[w]->source());
376  OGDF_ASSERT(v != m_pred[v]->source());
377 
378  w = m_pred[w]->source();
379  v = m_pred[v]->source();
380  }
381  }
382 
383  return result;
384  }
385 };
386 
387 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
TCap unshiftedTopPriority()
see unshiftedPriority
EdgeArray< bool > m_visited
Whether each edge has was visited.
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:320
adjEntry adjSource() const
Returns the corresponding adjacancy entry at source node.
Definition: Graph_d.h:330
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
int degree() const
Returns the degree of the node (indegree + outdegree).
Definition: Graph_d.h:208
Combinatorial embeddings of planar graphs.
const node * m_t
Pointer to the sink node.
Definition: MaxFlowModule.h:48
void appendEdge(const edge e)
Appends an edge to the current path.
TCap unshiftedPriority(edge e)
To work with unsigned, we need a priority shifting of the elements in the priority queue...
Computes a max flow in s-t-planar network via uppermost paths.
NodeArray< edge > m_pred
The predecessor of each node in the currently uppermost path.
Interface for Max Flow Algorithms.
Declaration of CombinatorialEmbedding and face.
const Graph * m_G
Pointer to the given graph.
Definition: MaxFlowModule.h:45
Prioritized queue interface wrapper for heaps.
adjEntry cyclicSucc() const
Returns the cyclic successor in the adjacency list.
Definition: Graph_d.h:278
adjEntry findCommonFace(const node v, const node w, bool left=true) const
Identifies a common face of two nodes and returns the respective adjacency entry. ...
EdgePathType getPathType(const edge e) const
Performs an alternating backtracking from source and target to determine whether the new node is part...
~MaxFlowSTPlanarItaiShiloach()
Free allocated ressources.
const E & topElement() const
Returns the topmost element in the queue.
const node * m_s
Pointer to the source node.
Definition: MaxFlowModule.h:47
EdgePathType
Each edge may be part of the source or target path.
bool consistencyCheck()
Checks the consistency of the data structure.
void dropEdge(const edge e)
Removes a single edge from the current path.
EdgeArray< TCap > * m_flow
Pointer to (extern) flow array.
Definition: MaxFlowModule.h:44
PrioritizedMapQueue< edge, TCap > * m_prioritizedEdges
A priority queue for storing all edges currently in a path.
Class for the representation of edges.
Definition: Graph_d.h:291
void computeFlowAfterValue()
Computes the actual flow on each edge.
Class for the representation of nodes.
Definition: Graph_d.h:167
Priority queue interface wrapping various heaps.
bool findUppermostPath(const edge saturatedEdge)
Establishes the next uppermost path.
NodeArray< NodeType > m_status
The status of each node.
bool isSTPlanar(const Graph &graph, const node s, const node t)
Returns whether G is s-t-planar (i.e.
bool empty() const
Checks whether the queue is empty.
NodeType
Each node has a certain type depending on its participation in any path.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
The namespace for all OGDF objects.
TCap computeValue(const EdgeArray< TCap > &originalCapacities, const node &source, const node &target)
Computes the maximal flow from source to sink.
TCap m_partialFlow
The flow reached thus far (monotonically increasing).
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition: Graph_d.h:332
const EdgeArray< TCap > * m_cap
Pointer to the given capacity array.
Definition: MaxFlowModule.h:46
TCap shiftPriority(TCap priority)
see unshiftedPriority
Class for adjacency list elements.
Definition: Graph_d.h:78
NodeArray< int > m_edgeCounter
The number of edges visited from each node.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41