Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
MinSTCutBFS.h
Go to the documentation of this file.
1
33#pragma once
34
37#include <ogdf/basic/Graph.h>
40#include <ogdf/basic/List.h>
41#include <ogdf/basic/Queue.h>
42#include <ogdf/basic/basic.h>
44
45#include <functional>
46
47namespace ogdf {
48
58template<typename TCost>
59class MinSTCutBFS : public MinSTCutModule<TCost> {
60public:
62
66 virtual bool call(const Graph& graph, node s, node t, List<edge>& edgeList,
67 edge e_st = nullptr) override {
68 return call(graph, nullptr, s, t, edgeList, e_st);
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 return call(graph, &weight, s, t, edgeList, e_st);
77 }
78
79 using MinSTCutModule<TCost>::m_gc;
80
81private:
87 bool call(const Graph& graph, const EdgeArray<TCost>* weight, node s, node t,
88 List<edge>& edgeList, edge e_st);
89};
90
91template<typename TCost>
92bool MinSTCutBFS<TCost>::call(const Graph& graph, const EdgeArray<TCost>* weight, node s, node t,
93 List<edge>& edgeList, edge e_st) {
94 bool weighted = (weight != nullptr);
95 delete m_gc;
96 m_gc = new GraphCopy(graph);
98 GraphCopy m_weightedGc;
99 EdgeArray<edge> mapE(m_weightedGc, nullptr);
100
101 std::function<edge(edge)> orig = [&](edge e) -> edge { return m_gc->original(e); };
102
103 if (weighted) {
104 m_weightedGc.init(graph);
105 if (e_st != nullptr) {
106 e_st = m_weightedGc.copy(e_st);
107 }
108 s = m_weightedGc.copy(s);
109 t = m_weightedGc.copy(t);
110 List<edge> edges;
111 graph.allEdges(edges);
112 for (edge e : edges) {
113 mapE[m_weightedGc.copy(e)] = e;
114 if (m_weightedGc.copy(e) == e_st) {
115 continue;
116 }
117 OGDF_ASSERT((*weight)[e] >= 1);
118 TCost i = 1;
119 for (; i < (*weight)[e]; i++) {
120 edge copyEdge = m_weightedGc.copy(e);
121 edge newEdge = m_weightedGc.newEdge(copyEdge->source(), copyEdge->target());
122 m_weightedGc.move(newEdge, copyEdge->adjSource(), ogdf::Direction::before,
123 copyEdge->adjTarget(), ogdf::Direction::after);
124 mapE[newEdge] = e;
125 }
126 OGDF_ASSERT((*weight)[e] == i);
127 }
128 // we can't reinit m_gc, because it's possible that the original graph of m_gc
129 // doesn't exist anymore.
130 delete m_gc;
131 m_gc = new GraphCopy();
132 m_gc->init(m_weightedGc);
133 orig = [&](edge e) -> edge { return mapE[m_gc->original(e)]; };
134 MinSTCutModule<TCost>::preprocessingDual(m_weightedGc, *m_gc, CE, s, t, e_st);
135 } else {
136 MinSTCutModule<TCost>::preprocessingDual(graph, *m_gc, CE, s, t, e_st);
137 }
138
140
141 DualGraph dual(CE);
142 if (e_st != nullptr) {
143 e_st = m_gc->copy(e_st);
144 } else {
145 e_st = m_gc->searchEdge(m_gc->copy(s), m_gc->copy(t));
146 }
147
148 edgeList.clear();
149 node source = dual.dualNode(CE.rightFace(e_st->adjSource()));
150 node target = dual.dualNode(CE.leftFace(e_st->adjSource()));
151
152 NodeArray<edge> spPred(dual, nullptr);
153 EdgeArray<node> prev(dual, nullptr);
154 EdgeArray<bool> direction(dual, true);
155 QueuePure<edge> queue;
156 for (adjEntry adj : source->adjEntries) {
157 if (dual.primalEdge(adj->theEdge()) != e_st) {
158 queue.append(adj->theEdge());
159 prev[adj->theEdge()] = source;
160 }
161 }
162 // actual search (using bfs on directed dual)
163 for (;;) {
164 // next candidate edge
165 edge eCand = queue.pop();
166 bool dir = (eCand->source() == prev[eCand]);
167 node v = (dir ? eCand->target() : eCand->source());
168
169 // leads to an unvisited node?
170 if (spPred[v] == nullptr) {
171 // yes, then we set v's predecessor in search tree
172 spPred[v] = eCand;
173 direction[eCand] = dir;
174
175 // have we reached t ...
176 if (v == target) {
177 // ... then search is done.
178 // constructed list of used edges (translated to crossed
179 // edges entries in G) from t back to s (including first
180 // and last!)
181
182 do {
183 edge eDual = spPred[v];
184 OGDF_ASSERT(eDual != nullptr);
185 // this should be the right way round
186 edge eG = dual.primalEdge(eDual);
187 OGDF_ASSERT(eG != nullptr);
188 edgeList.pushBack(orig(eG));
189 MinSTCutModule<TCost>::m_direction[orig(eG)] = !direction[eDual];
190 v = prev[eDual];
191 } while (v != source);
192
193 break;
194 }
195
196 // append next candidate edges to queue
197 // (all edges leaving v)
198 for (adjEntry adj : v->adjEntries) {
199 if (prev[adj->theEdge()] == nullptr) {
200 queue.append(adj->theEdge());
201 prev[adj->theEdge()] = v;
202 }
203 }
204 }
205 }
206 if (weighted) {
207 auto prevIt = edgeList.begin();
208 for (auto it = prevIt.succ(); it != edgeList.end(); prevIt = it++) {
209 if (*prevIt == *it) {
210 edgeList.del(prevIt);
211 }
212 }
213 }
214 return true;
215}
216}
Declaration of CombinatorialEmbedding and face.
Includes declaration of dual graph class.
Includes declaration of graph class.
Declaration of graph copy classes.
Decralation of GraphElement and GraphList classes.
Declaration of doubly linked lists and iterators.
Template of base class of min-st-cut algorithms.
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
Combinatorial embeddings of planar graphs with modification functionality.
face rightFace(adjEntry adj) const
Returns the face to the right of adj, i.e., the face containing adj.
face leftFace(adjEntry adj) const
Returns the face to the left of adj, i.e., the face containing the twin of adj.
A dual graph including its combinatorial embedding of an embedded graph.
Definition DualGraph.h:61
const edge & primalEdge(edge e) const
Returns the edge in the primal graph corresponding to e.
Definition DualGraph.h:161
const node & dualNode(face f) const
Returns the node in the dual graph corresponding to f.
Definition DualGraph.h:175
Class for the representation of edges.
Definition Graph_d.h:364
adjEntry adjSource() const
Returns the corresponding adjacancy entry at source node.
Definition Graph_d.h:408
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition Graph_d.h:411
node target() const
Returns the target node of the edge.
Definition Graph_d.h:402
node source() const
Returns the source node of the edge.
Definition Graph_d.h:399
void init(const Graph &G)
Re-initializes the copy using G, creating copies for all nodes and edges in G.
Definition GraphCopy.h:72
Copies of graphs supporting edge splitting.
Definition GraphCopy.h:390
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition GraphCopy.h:463
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
void allEdges(CONTAINER &edgeContainer) const
Returns a container with all edges of the graph.
Definition Graph_d.h:1043
void move(edge e, adjEntry adjSrc, Direction dirSrc, adjEntry adjTgt, Direction dirTgt)
Moves edge e to a different adjacency list.
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
void clear()
Removes all elements from the list.
Definition List.h:1626
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition List.h:1547
void del(iterator it)
Removes it from the list.
Definition List.h:1611
iterator begin()
Returns an iterator to the first element of the list.
Definition List.h:391
iterator end()
Returns an iterator to one-past-last element of the list.
Definition List.h:409
Min-st-cut algorithm, that calculates the cut by doing a depth first search over the dual graph of of...
Definition MinSTCutBFS.h:59
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:66
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:74
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...
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
Implementation of list-based queues.
Definition Queue.h:55
iterator append(const E &x)
Adds x at the end of queue.
Definition Queue.h:171
E pop()
Removes front element and returns it.
Definition Queue.h:183
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
EdgeElement * edge
The type of edges.
Definition Graph_d.h:75
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.