Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MinCostFlowModule.h
Go to the documentation of this file.
1 
35 #pragma once
36 
37 #include <ogdf/basic/Graph.h>
38 #include <ogdf/basic/GraphList.h>
39 #include <ogdf/basic/basic.h>
42 
43 namespace ogdf {
44 
45 
49 template<typename TCost>
51 public:
54 
55  // destruction
56  virtual ~MinCostFlowModule() { }
57 
72  virtual bool call(const Graph& G, // directed graph
73  const EdgeArray<int>& lowerBound, // lower bound for flow
74  const EdgeArray<int>& upperBound, // upper bound for flow
75  const EdgeArray<TCost>& cost, // cost of an edge
76  const NodeArray<int>& supply, // supply (if neg. demand) of a node
77  EdgeArray<int>& flow) // computed flow
78  {
79  NodeArray<TCost> dual(G);
80  return call(G, lowerBound, upperBound, cost, supply, flow, dual);
81  }
82 
98  virtual bool call(const Graph& G, // directed graph
99  const EdgeArray<int>& lowerBound, // lower bound for flow
100  const EdgeArray<int>& upperBound, // upper bound for flow
101  const EdgeArray<TCost>& cost, // cost of an edge
102  const NodeArray<int>& supply, // supply (if neg. demand) of a node
103  EdgeArray<int>& flow, // computed flow
104  NodeArray<TCost>& dual // computed dual variables
105  ) = 0;
106 
107 
108  //
109  // static functions
110  //
111 
116  static void generateProblem(Graph& G, int n, int m, EdgeArray<int>& lowerBound,
117  EdgeArray<int>& upperBound, EdgeArray<TCost>& cost, NodeArray<int>& supply);
118 
119 
133  static bool checkProblem(const Graph& G, const EdgeArray<int>& lowerBound,
134  const EdgeArray<int>& upperBound, const NodeArray<int>& supply);
135 
136 
156  static bool checkComputedFlow(const Graph& G, const EdgeArray<int>& lowerBound,
157  const EdgeArray<int>& upperBound, const EdgeArray<TCost>& cost,
158  const NodeArray<int>& supply, const EdgeArray<int>& flow, TCost& value);
159 
178  static bool checkComputedFlow(const Graph& G, const EdgeArray<int>& lowerBound,
179  const EdgeArray<int>& upperBound, const EdgeArray<TCost>& cost,
180  const NodeArray<int>& supply, const EdgeArray<int>& flow) {
181  TCost value;
182  return checkComputedFlow(G, lowerBound, upperBound, cost, supply, flow, value);
183  }
184 };
185 
186 // Implementation
187 
188 template<typename TCost>
190  EdgeArray<int>& upperBound, EdgeArray<TCost>& cost, NodeArray<int>& supply) {
191  ogdf::randomGraph(G, n, m);
192 
193  node s = G.firstNode();
194  node t = G.lastNode();
195 
196  for (node v : G.nodes) {
197  G.newEdge(s, v);
198  G.newEdge(v, t);
199  }
200 
201  for (edge e : G.edges) {
202  lowerBound[e] = 0;
203  upperBound[e] = (e->source() != s) ? ogdf::randomNumber(1, 10) : ogdf::randomNumber(2, 13);
204  cost[e] = static_cast<TCost>(ogdf::randomNumber(0, 100));
205  }
206 
207 
208  for (node v = G.firstNode(), vl = G.lastNode(); true; v = v->succ(), vl = vl->pred()) {
209  if (v == vl) {
210  supply[v] = 0;
211  break;
212  }
213 
214  supply[v] = -(supply[vl] = ogdf::randomNumber(-1, 1));
215 
216  if (vl == v->succ()) {
217  break;
218  }
219  }
220 }
221 
222 template<typename TCost>
224  const EdgeArray<int>& upperBound, const NodeArray<int>& supply) {
225  if (!isConnected(G)) {
226  return false;
227  }
228 
229  for (edge e : G.edges) {
230  if (lowerBound[e] > upperBound[e]) {
231  return false;
232  }
233  }
234 
235  int sum = 0;
236  for (node v : G.nodes) {
237  sum += supply[v];
238  }
239 
240  return sum == 0;
241 }
242 
243 template<typename TCost>
245  const EdgeArray<int>& upperBound, const EdgeArray<TCost>& cost,
246  const NodeArray<int>& supply, const EdgeArray<int>& flow, TCost& value) {
247  value = 0;
248 
249  for (edge e : G.edges) {
250  if (flow[e] < lowerBound[e] || upperBound[e] < flow[e]) {
251  return false;
252  }
253 
254  value += flow[e] * cost[e];
255  }
256 
257  for (node v : G.nodes) {
258  int sum = 0;
259 
260  for (adjEntry adj : v->adjEntries) {
261  edge e = adj->theEdge();
262 
263  if (e->isSelfLoop()) {
264  continue;
265  }
266 
267  if (e->source() == v) {
268  sum += flow[e];
269  } else {
270  sum -= flow[e];
271  }
272  }
273  if (sum != supply[v]) {
274  return false;
275  }
276  }
277 
278  return true;
279 }
280 
281 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::randomGraph
void randomGraph(Graph &G, int n, int m)
Creates a random graph.
Graph.h
Includes declaration of graph class.
ogdf::MinCostFlowModule::checkComputedFlow
static bool checkComputedFlow(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const EdgeArray< TCost > &cost, const NodeArray< int > &supply, const EdgeArray< int > &flow, TCost &value)
checks if a computed flow is a feasible solution to the given problem instance.
Definition: MinCostFlowModule.h:244
graph_generators.h
Declaration of graph generators.
ogdf::NodeElement::pred
node pred() const
Returns the predecessor in the list of all nodes.
Definition: Graph_d.h:295
ogdf::isConnected
bool isConnected(const Graph &G)
Returns true iff G is connected.
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::MinCostFlowModule::~MinCostFlowModule
virtual ~MinCostFlowModule()
Definition: MinCostFlowModule.h:56
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:160
ogdf::MinCostFlowModule::checkComputedFlow
static bool checkComputedFlow(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const EdgeArray< TCost > &cost, const NodeArray< int > &supply, const EdgeArray< int > &flow)
checks if a computed flow is a feasible solution to the given problem instance.
Definition: MinCostFlowModule.h:178
ogdf::EdgeElement::isSelfLoop
bool isSelfLoop() const
Returns true iff the edge is a self-loop (source node = target node).
Definition: Graph_d.h:419
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::MinCostFlowModule
Interface for min-cost flow algorithms.
Definition: MinCostFlowModule.h:50
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::MinCostFlowModule::MinCostFlowModule
MinCostFlowModule()
Initializes a min-cost flow module.
Definition: MinCostFlowModule.h:53
ogdf::MinCostFlowModule::checkProblem
static bool checkProblem(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const NodeArray< int > &supply)
Checks if a given min-cost flow problem instance satisfies the preconditions.
Definition: MinCostFlowModule.h:223
basic.h
Basic declarations, included by all source files.
ogdf::NodeElement::succ
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:292
ogdf::MinCostFlowModule::call
virtual bool call(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const EdgeArray< TCost > &cost, const NodeArray< int > &supply, EdgeArray< int > &flow)
Computes a min-cost flow in the directed graph G.
Definition: MinCostFlowModule.h:72
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::randomNumber
int randomNumber(int low, int high)
Returns random integer between low and high (including).
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::MinCostFlowModule::generateProblem
static void generateProblem(Graph &G, int n, int m, EdgeArray< int > &lowerBound, EdgeArray< int > &upperBound, EdgeArray< TCost > &cost, NodeArray< int > &supply)
Generates an instance of a min-cost flow problem with n nodes and m + n edges.
Definition: MinCostFlowModule.h:189
simple_graph_alg.h
Declaration of simple graph algorithms.
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716