Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MinCostFlowReinelt.h
Go to the documentation of this file.
1 
33 #pragma once
34 
36 #include <ogdf/basic/EpsilonTest.h>
37 
38 
39 namespace ogdf {
40 
41 
43 
46 template<typename TCost>
48 {
49 public:
51  : m_eps()
52  {
53  }
54 
56 
72  virtual bool call(
73  const Graph &G, // directed graph
74  const EdgeArray<int> &lowerBound, // lower bound for flow
75  const EdgeArray<int> &upperBound, // upper bound for flow
76  const EdgeArray<TCost> &cost, // cost of an edge
77  const NodeArray<int> &supply, // supply (if neg. demand) of a node
78  EdgeArray<int> &flow, // computed flow
79  NodeArray<TCost> &dual) override; // computed dual variables
80 
81  int infinity() const { return numeric_limits<int>::max(); }
82 
83 private:
84 
85  struct arctype;
86 
87  struct nodetype {
88  nodetype *father; /* ->father in basis tree */
89  nodetype *successor; /* ->successor in preorder */
90  arctype *arc_id; /* ->arc (node,father) */
91  bool orientation; /* false<=>basic arc=(father->node)*/
92  TCost dual; /* value of dual variable */
93  int flow; /* flow in basic arc (node,father) */
94  int name; /* identification of node = node-nr*/
95  nodetype *last; /* last node in subtree */
96  int nr_of_nodes; /* number of nodes in subtree */
97  };
98 
99  struct arctype {
100  arctype *next_arc; /* -> next arc in list */
101  nodetype *tail; /* -> tail of arc */
102  nodetype *head; /* -> head of arc */
103  TCost cost; /* cost of unit flow */
104  int upper_bound; /* capacity of arc */
105  int arcnum; /* number of arc in input */
106 
108  };
109 
110 
111  int mcf(
112  int mcfNrNodes,
113  int mcfNrArcs,
114  Array<int> &mcfSupply,
115  Array<int> &mcfTail,
116  Array<int> &mcfHead,
117  Array<int> &mcfLb,
118  Array<int> &mcfUb,
119  Array<TCost> &mcfCost,
120  Array<int> &mcfFlow,
121  Array<TCost> &mcfDual,
122  TCost *mcfObj
123  );
124 
125  void start(Array<int> &supply);
126 
127  void beacircle(arctype **eplus, arctype **pre, bool *from_ub);
128  void beadouble(arctype **eplus, arctype **pre, bool *from_ub);
129 
131 
132  Array<nodetype> nodes; /* node space */
133  Array<arctype> arcs; /* arc space */
134  //Array<nodetype *> p; /*used for starting procedure*/
135 
136  nodetype *root; /*->root of basis tree*/
138 
139  arctype *last_n1; /*->start for search for entering arc in N' */
140  arctype *last_n2; /*->start for search for entering arc in N''*/
141  arctype *start_arc; /* -> initial arc list*/
142  arctype *start_b; /* -> first basic arc*/
143  arctype *start_n1; /* -> first nonbasic arc in n'*/
144  arctype *start_n2; /* -> first nonbasic arc in n''*/
145  arctype *startsearch; /* ->start of search for basis entering arc */
146  arctype *searchend; /* ->end of search for entering arc in bea */
147  arctype *searchend_n1; /*->end of search for entering arc in N' */
148  arctype *searchend_n2; /*->end of search for entering arc in N''*/
149 
150  //int artvalue; /*cost and upper_bound of artificial arc */
151  TCost m_maxCost; // maximum of the cost of all input arcs
152 
153  int nn; /*number of original nodes*/
154  int mm; /*number of original arcs*/
155 };
156 
157 
158 } // end namespace ogdf
159 
160 // Implementation
161 
162 namespace ogdf {
163 
164 // computes min-cost-flow, call front-end for mcf()
165 // returns true if a feasible minimum cost flow could be found
166 template<typename TCost>
168  const Graph &G,
169  const EdgeArray<int> &lowerBound,
170  const EdgeArray<int> &upperBound,
171  const EdgeArray<TCost> &cost,
172  const NodeArray<int> &supply,
175 {
176  OGDF_ASSERT(this->checkProblem(G, lowerBound, upperBound, supply));
177 
178  const int n = G.numberOfNodes();
179  const int m = G.numberOfEdges();
180 
181  // assign indices 0, ..., n-1 to nodes in G
182  // (this is not guaranteed for v->index() )
183  NodeArray<int> vIndex(G);
184  // assigning supply
185  Array<int> mcfSupply(n);
186 
187  int i = 0;
188  for(node v : G.nodes) {
189  mcfSupply[i] = supply[v];
190  vIndex[v] = ++i;
191  }
192 
193 
194  // allocation of arrays for arcs
195  Array<int> mcfTail(m);
196  Array<int> mcfHead(m);
197  Array<int> mcfLb(m);
198  Array<int> mcfUb(m);
199  Array<TCost> mcfCost(m);
200  Array<int> mcfFlow(m);
201  Array<TCost> mcfDual(n+1); // dual[n] = dual variable of root struct
202 
203  // set input data in edge arrays
204  int nSelfLoops = 0;
205  i = 0;
206  for (edge e : G.edges) {
207  // We handle self-loops in the network already in the front-end
208  // (they are just set to the lower bound below when copying result)
209  if (e->isSelfLoop()) {
210  ++nSelfLoops;
211  continue;
212  }
213 
214  mcfTail[i] = vIndex[e->source()];
215  mcfHead[i] = vIndex[e->target()];
216  mcfLb[i] = lowerBound[e];
217  mcfUb[i] = upperBound[e];
218  mcfCost[i] = cost[e];
219 
220  ++i;
221  }
222 
223 
224  int retCode = 0; // return (error or success) code
225  TCost objVal; // value of flow
226 
227  // call actual min-cost-flow function
228  // mcf does not support single nodes
229  if (n > 1) {
230  //mcf does not support single edges
231  if (m < 2) {
232  if (m == 1) {
233  edge eFirst = G.firstEdge();
234  flow[eFirst] = lowerBound[eFirst];
235  }
236  } else {
237  retCode = mcf(n, m - nSelfLoops, mcfSupply, mcfTail, mcfHead, mcfLb, mcfUb, mcfCost, mcfFlow, mcfDual, &objVal);
238  }
239  }
240 
241  // copy resulting flow for return
242  i = 0;
243  for (edge e : G.edges) {
244  if (e->isSelfLoop()) {
245  flow[e] = lowerBound[e];
246  continue;
247  }
248 
249  flow[e] = mcfFlow[i];
250  if (retCode == 0) {
251  OGDF_ASSERT(flow[e] >= lowerBound[e]);
252  OGDF_ASSERT(flow[e] <= upperBound[e]);
253  }
254  ++i;
255  }
256 
257  // copy resulting dual values for return
258  i = 0;
259  for (node v : G.nodes) {
260  dual[v] = mcfDual[i];
261  ++i;
262  }
263 
264  // successful if retCode == 0
265  return retCode == 0;
266 }
267 
268 
269 template<typename TCost>
271 {
272  // determine intial basis tree and initialize data structure
273 
274  /* initialize artificial root node */
275  root->father = root;
276  root->successor = &nodes[1];
277  root->arc_id = nullptr;
278  root->orientation = false;
279  root->dual = 0;
280  root->flow = 0;
281  root->nr_of_nodes = nn + 1;
282  root->last = &nodes[nn];
283  root->name = nn + 1;
284  // artificials = nn; moved to mcf() [CG]
285  TCost highCost = 1 + (nn+1) * m_maxCost;
286 
287  for (int i = 1; i <= nn; ++i) { /* for every node an artificial arc is created */
288  arctype *ep = new arctype;
289  if (supply[i - 1] >= 0) {
290  ep->tail = &nodes[i];
291  ep->head = root;
292  } else {
293  ep->tail = root;
294  ep->head = &nodes[i];
295  }
296  ep->cost = highCost;
297  ep->upper_bound = infinity();
298  ep->arcnum = mm + i - 1;
299  ep->next_arc = start_b;
300  start_b = ep;
301  nodes[i].father = root;
302  if (i < nn)
303  nodes[i].successor = &nodes[i+1];
304  else
305  nodes[i].successor = root;
306  if (supply[i - 1] < 0) {
307  nodes[i].orientation = false;
308  nodes[i].dual = -highCost;
309  } else {
310  nodes[i].orientation = true;
311  nodes[i].dual = highCost;
312  }
313  nodes[i].flow = abs(supply[i - 1]);
314  nodes[i].nr_of_nodes = 1;
315  nodes[i].last = &nodes[i];
316  nodes[i].arc_id = ep;
317  } /* for i */
319 } /*start*/
320 
321 
322 // circle variant for determine basis entering arc
323 template<typename TCost>
325  arctype **eplus,
326  arctype **pre,
327  bool *from_ub)
328 {
329  //the first arc with negative reduced costs is taken, but the search is
330  //started at the successor of the successor of eplus in the last iteration
331 
332  bool found = false; /* true<=>entering arc found */
333 
334  *pre = startsearch;
335  if (*pre != nullptr) {
336  *eplus = (*pre)->next_arc;
337  } else {
338  *eplus = nullptr;
339  }
340  searchend = *eplus;
341 
342  if (!*from_ub) {
343 
344  while (*eplus != nullptr && !found) { /* search in n' for an arc with negative reduced costs */
345  if (m_eps.less((*eplus)->cost + (*eplus)->head->dual, (*eplus)->tail->dual)) {
346  found = true;
347  } else {
348  *pre = *eplus; /* save predecessor */
349  *eplus = (*eplus)->next_arc; /* go to next arc */
350  }
351  } /* while */
352 
353  if (!found) { /* entering arc still not found */
354  /* search in n'' */
355  *from_ub = true;
356  *eplus = start_n2;
357  *pre = nullptr;
358 
359  while (*eplus != nullptr && !found) {
360  if (m_eps.less((*eplus)->tail->dual, (*eplus)->head->dual + (*eplus)->cost)) {
361  found = true;
362  } else {
363  *pre = *eplus; /* save predecessor */
364  *eplus = (*eplus)->next_arc; /* go to next arc */
365  }
366  } /* while */
367 
368 
369  if (!found) { /* search again in n' */
370  *from_ub = false;
371  *eplus = start_n1;
372  *pre = nullptr;
373 
374  while (*eplus != searchend && !found) {
375  if (m_eps.less((*eplus)->cost + (*eplus)->head->dual, (*eplus)->tail->dual)) {
376  found = true;
377  } else {
378  *pre = *eplus; /* save predecessor */
379  *eplus = (*eplus)->next_arc; /* go to next arc */
380  }
381  } /* while */
382 
383  } /* search in n'' */
384  } /* serch again in n' */
385  } /* if from_ub */
386  else { /* startsearch in n'' */
387 
388  while (*eplus != nullptr && !found) {
389  if (m_eps.less((*eplus)->tail->dual, (*eplus)->head->dual + (*eplus)->cost)) {
390  found = true;
391  } else {
392  *pre = *eplus; /* save predecessor */
393  *eplus = (*eplus)->next_arc; /* go to next arc */
394  }
395  } /* while */
396 
397  if (!found) { /* search now in n' */
398  *from_ub = false;
399  *eplus = start_n1;
400  *pre = nullptr;
401 
402  while (*eplus != nullptr && !found) {
403  if (m_eps.less((*eplus)->cost + (*eplus)->head->dual, (*eplus)->tail->dual)) {
404  found = true;
405  } else {
406  *pre = *eplus; /* save predecessor */
407  *eplus = (*eplus)->next_arc; /* go to next arc */
408  }
409  } /* while */
410 
411 
412  if (!found) { /* search again in n'' */
413  *from_ub = true;
414  *eplus = start_n2;
415  *pre = nullptr;
416 
417  while (*eplus != searchend && !found) {
418  if (m_eps.less((*eplus)->tail->dual, (*eplus)->head->dual + (*eplus)->cost)) {
419  found = true;
420  } else {
421  *pre = *eplus; /* save predecessor */
422  *eplus = (*eplus)->next_arc; /* go to next arc */
423  }
424  } /* while */
425 
426  } /* search in n' */
427  } /* search in n'' */
428  } /* from_ub = true */
429 
430 
431 
432  if (!found) {
433  *pre = nullptr;
434  *eplus = nullptr;
435  } else {
436  startsearch = (*eplus)->next_arc;
437  }
438 
439 } /* beacircle */
440 
441 
442 // doublecircle variant for determine basis entering arc
443 template<typename TCost>
445  arctype **eplus,
446  arctype **pre,
447  bool *from_ub)
448 {
449  /* search as in procedure beacircle, but in each list the search started is
450  at the last movement
451  */
452  bool found = false; /* true<=>entering arc found */
453 
454  if (!*from_ub) {
455  *pre = last_n1;
456  if (*pre != nullptr)
457  *eplus = (*pre)->next_arc;
458  else
459  *eplus = nullptr;
460  searchend_n1 = *eplus;
461 
462  while (*eplus != nullptr && !found) { /* search in n' for an arc with negative reduced costs */
463  if (m_eps.less((*eplus)->cost + (*eplus)->head->dual, (*eplus)->tail->dual)) {
464  found = true;
465  } else {
466  *pre = *eplus; /* save predecessor */
467  *eplus = (*eplus)->next_arc; /* go to next arc */
468  }
469  } /* while */
470 
471  if (!found) { /* entering arc still not found */
472  /* search in n'' beginning at the last movement */
473  *from_ub = true;
474  *pre = last_n2;
475  if (*pre != nullptr)
476  *eplus = (*pre)->next_arc;
477  else
478  *eplus = nullptr;
479  searchend_n2 = *eplus;
480 
481  while (*eplus != nullptr && !found) {
482  if (m_eps.less((*eplus)->tail->dual, (*eplus)->head->dual + (*eplus)->cost)) {
483  found = true;
484  } else {
485  *pre = *eplus; /* save predecessor */
486  *eplus = (*eplus)->next_arc; /* go to next arc */
487  }
488 
489  } /* while */
490 
491  if (!found) { /* entering arc still not found */
492  /* search in n'' in the first part of the list */
493  *eplus = start_n2;
494  *pre = nullptr;
495 
496  while (*eplus != searchend_n2 && !found) {
497  if (m_eps.less((*eplus)->tail->dual, (*eplus)->head->dual + (*eplus)->cost)) {
498  found = true;
499  } else {
500  *pre = *eplus; /* save predecessor */
501  *eplus = (*eplus)->next_arc; /* go to next arc */
502  }
503 
504  } /* while */
505 
506 
507  if (!found) {
508  /* search again in n' in the first part of the list*/
509  *from_ub = false;
510  *eplus = start_n1;
511  *pre = nullptr;
512 
513  while (*eplus != searchend_n1 && !found) {
514  if (m_eps.less((*eplus)->cost + (*eplus)->head->dual, (*eplus)->tail->dual)) {
515  found = true;
516  } else {
517  *pre = *eplus; /* save predecessor */
518  *eplus = (*eplus)->next_arc; /* go to next arc */
519  }
520  } /* while */
521  } /* first part n' */
522  } /* first part n'' */
523  } /* second part n'' */
524  } /* if from_ub */
525  else { /* startsearch in n'' */
526  *pre = last_n2;
527  if (*pre != nullptr)
528  *eplus = (*pre)->next_arc;
529  else
530  *eplus = nullptr;
531  searchend_n2 = *eplus;
532 
533  while (*eplus != nullptr && !found) {
534  if (m_eps.less((*eplus)->tail->dual, (*eplus)->head->dual + (*eplus)->cost)) {
535  found = true;
536  } else {
537  *pre = *eplus; /* save predecessor */
538  *eplus = (*eplus)->next_arc; /* go to next arc */
539  }
540  } /* while */
541 
542  if (!found) { /* search now in n' beginning at the last movement */
543  *from_ub = false;
544  *pre = last_n1;
545  if (*pre != nullptr)
546  *eplus = (*pre)->next_arc;
547  else
548  *eplus = nullptr;
549  searchend_n1 = *eplus;
550 
551  while (*eplus != nullptr && !found) {
552  if (m_eps.less((*eplus)->cost + (*eplus)->head->dual, (*eplus)->tail->dual)) {
553  found = true;
554  } else {
555  *pre = *eplus; /* save predecessor */
556  *eplus = (*eplus)->next_arc; /* go to next arc */
557  }
558  } /* while */
559 
560 
561  if (!found) { /* search now in n' in the first part */
562  *eplus = start_n1;
563  *pre = nullptr;
564 
565  while (*eplus != searchend_n1 && !found) {
566  if (m_eps.less((*eplus)->cost + (*eplus)->head->dual, (*eplus)->tail->dual)) {
567  found = true;
568  } else {
569  *pre = *eplus; /* save predecessor */
570  *eplus = (*eplus)->next_arc; /* go to next arc */
571  }
572  } /* while */
573 
574 
575  if (!found) { /* search again in n'' in the first part */
576  *from_ub = true;
577  *eplus = start_n2;
578  *pre = nullptr;
579 
580  while (*eplus != searchend_n2 && !found) {
581  if (m_eps.less((*eplus)->tail->dual, (*eplus)->head->dual + (*eplus)->cost)) {
582  found = true;
583  } else {
584  *pre = *eplus; /* save predecessor */
585  *eplus = (*eplus)->next_arc; /* go to next arc */
586  }
587  } /* while */
588  } /* first part of n'' */
589  } /* first part of n' */
590  } /* second part of n' */
591  } /* from_ub = true */
592 
593 
594 
595  if (!found) {
596  *pre = nullptr;
597  *eplus = nullptr;
598  return;
599  }
600 
601  if (*from_ub)
602  last_n2 = (*eplus)->next_arc;
603  else
604  last_n1 = (*eplus)->next_arc;
605 
606 } /* beadouble */
607 
608 
609 // Min Cost Flow Function
610 template<typename TCost>
612  int mcfNrNodes,
613  int mcfNrArcs,
614  Array<int> &supply,
615  Array<int> &mcfTail,
616  Array<int> &mcfHead,
617  Array<int> &mcfLb,
618  Array<int> &mcfUb,
619  Array<TCost> &mcfCost,
620  Array<int> &mcfFlow,
621  Array<TCost> &mcfDual,
622  TCost *mcfObj)
623 {
624  int i;
625  int low,up;
626 
627  /* 1: Allocations (malloc's no longer required) */
628 
629  root = &rootStruct;
630 
631  /* 2: Initializations */
632 
633  /* Number of nodes/arcs */
634  nn = mcfNrNodes;
635  OGDF_ASSERT(nn >= 2);
636  mm = mcfNrArcs;
637  OGDF_ASSERT(mm >= 2);
638 
639  // number of artificial basis arcs
640  int artificials = nn;
641 
642 
643  /* Node space and pointers to nodes */
644  nodes.init(nn+1);
645  nodes[0].name = -1; // for debuggin, should not occur(?)
646  for(i = 1; i <= nn; ++i)
647  nodes[i].name = i;
648 
649  /* Arc space and arc data */
650  arcs.init(mm+1);
651 
652  TCost lb_cost = 0; // cost of lower bound
653  m_maxCost = 0;
654  int from = mcfTail[0]; // name of tail (input)
655  int toh = mcfHead[0]; // name of head (input)
656  low = mcfLb[0];
657  up = mcfUb[0];
658  TCost c = mcfCost[0]; // cost (input)
659  if (from <= 0 || from > nn || toh <= 0 || toh > nn || up < 0 || low > up || low < 0) {
660  return 4;
661  }
662  TCost abs_c = c < 0 ? -c : c;
663  if (abs_c > m_maxCost) {
664  m_maxCost = abs_c;
665  }
666 
667  start_arc = &arcs[1];
668  start_arc->tail = &nodes[from];
669  start_arc->head = &nodes[toh];
670  start_arc->cost = c;
671  start_arc->upper_bound = up - low;
672  start_arc->arcnum = 0;
673  supply[from - 1] -= low;
674  supply[toh - 1] += low;
675  lb_cost += start_arc->cost * low;
676 
677  arctype *e = start_arc;
678 
679  int l; // lower bound (input)
680  for (l = 2; l <= mm; ++l) {
681  from = mcfTail[l-1];
682  toh = mcfHead[l-1];
683  low = mcfLb[l-1];
684  up = mcfUb[l-1];
685  c = mcfCost[l-1];
686  if (from <= 0 || from > nn || toh <= 0 || toh > nn || up < 0 || low > up || low < 0) {
687  return 4;
688  }
689  abs_c = c < 0 ? -c : c;
690  if (abs_c > m_maxCost) {
691  m_maxCost = abs_c;
692  }
693 
694  arctype *ep = &arcs[l];
695  e->next_arc = ep;
696  ep->tail = &nodes[from];
697  ep->head = &nodes[toh];
698  ep->cost = c;
699  ep->upper_bound = up - low;
700  ep->arcnum = l-1;
701  supply[from-1] -= low;
702  supply[toh-1] += low;
703  lb_cost += ep->cost * low;
704  e = ep;
705  }
706 
707  e->next_arc = nullptr;
708  // feasible = true <=> feasible solution exists
709  bool feasible = true;
710 
711 
712  /* 3: Starting solution */
713 
714  start_n1 = nullptr;
715  start_n2 = nullptr;
716  start_b = nullptr;
717 
718  start(supply);
719 
720  int step = 1; /* initialize iteration counter */
721 
722  /* 4: Iteration loop */
723 
724  /* 4.1: Determine basis entering arc */
725 
726  // finished = true <=> iteration finished
727  bool finished = false;
728  // from_ub = true <=> entering arc at upper bound
729  bool from_ub = false;
731 #if 0
732  startsearchpre = nullptr;
733 #endif
734  last_n1 = nullptr;
735  last_n2 = nullptr;
736  nodetype *np; // general nodeptr
737 
738  do {
739  arctype *eplus; // ->basis entering arc
740  arctype *pre; // ->predecessor of eplus in list
741  beacircle(&eplus, &pre, &from_ub);
742 
743  if (eplus == nullptr) {
744  finished = true;
745  } else {
746 
747  nodetype *iplus = eplus->tail; // -> tail of basis entering arc
748  nodetype *jplus = eplus->head; // -> head of basis entering arc
749 
750  /* 4.2: Determine leaving arc and maximal flow change */
751 
752  int delta = eplus->upper_bound; // maximal flow change
753  nodetype *iminus = nullptr; // -> tail of basis leaving arc
754  nodetype *p1 = iplus;
755  nodetype *p2 = jplus;
756 
757  bool to_ub; // to_ub = true <=> leaving arc goes to upperbound
758  bool xchange; // xchange = true <=> exchange iplus and jplus
759  while (p1 != p2) {
760  if (p1->nr_of_nodes <= p2->nr_of_nodes) {
761  np = p1;
762  if (from_ub == np->orientation) {
763  if (delta > np->arc_id->upper_bound - np->flow) {
764  iminus = np;
765  delta = np->arc_id->upper_bound - np->flow;
766  xchange = false;
767  to_ub = true;
768  }
769  }
770  else if (delta > np->flow) {
771  iminus = np;
772  delta = np->flow;
773  xchange = false;
774  to_ub = false;
775  }
776  p1 = np->father;
777  continue;
778  }
779  np = p2;
780  if (from_ub != np->orientation) {
781  if (delta > np->arc_id->upper_bound - np->flow) {
782  iminus = np;
783  delta = np->arc_id->upper_bound - np->flow;
784  xchange = true;
785  to_ub = true;
786  }
787  }
788  else if (delta > np->flow) {
789  iminus = np;
790  delta = np->flow;
791  xchange = true;
792  to_ub = false;
793  }
794  p2 = np->father;
795  }
796  // paths from iplus and jplus to root meet at w
797  nodetype *w = p1;
798  nodetype *iw;
799  nodetype *jminus; // -> head of basis leaving arc
800 
801  arctype *eminus;
802  if (iminus == nullptr) {
803  to_ub = !from_ub;
804  eminus = eplus;
805  iminus = iplus;
806  jminus = jplus;
807  }
808  else {
809  if (xchange) {
810  iw = jplus;
811  jplus = iplus;
812  iplus = iw;
813  }
814  jminus = iminus->father;
815  eminus = iminus->arc_id;
816  }
817 
818  // artif_to_lb = true <=> artif. arc goes to lower bound
819  bool artif_to_lb = false;
820  if (artificials > 1) {
821  if (iminus == root || jminus == root) {
822  if (jplus != root && iplus != root) {
823  artificials--;
824  artif_to_lb = true;
825  }
826  else if (eminus == eplus) {
827  if (from_ub) {
828  artificials--;
829  artif_to_lb = true;
830  } else
831  artificials++;
832  }
833  }
834  else {
835  if (iplus == root || jplus == root)
836  artificials++;
837  }
838  }
839 
840  /* 4.3: Update of data structure */
841 
842  TCost sigma; // change of dual variables
843 
844  if (eminus == eplus) {
845  if (from_ub) delta = -delta;
846 
847  bool s_orientation;
848  s_orientation = eminus->tail == iplus;
849 
850  np = iplus;
851  while (np != w) {
852  if (np->orientation == s_orientation) {
853  np->flow -= delta;
854  }
855  else {
856  np->flow += delta;
857  }
858  np = np->father;
859  }
860 
861  np = jplus;
862  while (np != w) {
863  if (np->orientation == s_orientation) {
864  np->flow += delta;
865  }
866  else {
867  np->flow -= delta;
868  }
869  np = np->father;
870  }
871 
872  } else {
873  /* 4.3.2.1 : initialize sigma */
874 
875  if (eplus->tail == iplus)
876  sigma = eplus->cost + jplus->dual - iplus->dual;
877  else
878  sigma = jplus->dual - iplus->dual - eplus->cost;
879 
880  // 4.3.2.2 : find new succ. of jminus if current succ. is iminus
881 
882  nodetype *newsuc = jminus->successor; // -> new successor
883  if (newsuc == iminus) {
884  for (i= 1; i <= iminus->nr_of_nodes; ++i) {
885  newsuc = newsuc->successor;
886  }
887  }
888 
889  /* 4.3.2.3 : initialize data for iplus */
890 
891  nodetype *s_father = jplus; // save area
892  bool s_orientation = (eplus->tail != jplus);
893 
894  // eplus_ori = true <=> eplus = (iplus,jplus)
895  bool eplus_ori = s_orientation;
896 
897  int s_flow;
898  if (from_ub) {
899  s_flow = eplus->upper_bound - delta;
900  delta = -delta;
901  } else {
902  s_flow = delta;
903  }
904 
905  arctype *s_arc_id = eminus;
906  int oldnumber = 0;
907  nodetype *nd = iplus; // -> current node
908  nodetype *f = nd->father; // ->father of nd
909 
910  /* 4.3.2.4 : traverse subtree under iminus */
911 
912  while (nd != jminus) {
913  nodetype *pred = f; // ->predecessor of current node
914  while (pred->successor != nd) pred = pred->successor;
915  nodetype *lastnode = nd; // -> last node of subtree
916  i = 1;
917  int non = nd->nr_of_nodes - oldnumber;
918  while (i < non) {
919  lastnode = lastnode->successor;
920  lastnode->dual += sigma;
921  i++;
922  }
923  nd->dual += sigma;
924  pred->successor = lastnode->successor;
925 
926  if (nd != iminus) lastnode->successor = f;
927  else lastnode->successor = jplus->successor;
928 
929  nodetype *w_father = nd; // save area
930  arctype *w_arc_id = nd->arc_id; // save area
931 
932  bool w_orientation;
933  w_orientation = nd->arc_id->tail != nd;
934 
935  int w_flow;
936  if (w_orientation == eplus_ori) {
937  w_flow = nd->flow + delta;
938  }
939  else {
940  w_flow = nd->flow - delta;
941  }
942 
943  nd->father = s_father;
944  nd->orientation = s_orientation;
945  nd->arc_id = s_arc_id;
946  nd->flow = s_flow;
947  s_father = w_father;
948  s_orientation = w_orientation;
949  s_arc_id = w_arc_id;
950  s_flow = w_flow;
951 
952  oldnumber = nd->nr_of_nodes;
953  nd = f;
954  f = f->father;
955 
956  }
957 
958  jminus->successor = newsuc;
959  jplus->successor = iplus;
960 
961  // 4.3.2.5: assign new nr_of_nodes in path from iminus to iplus
962 
963  oldnumber = iminus->nr_of_nodes;
964  np = iminus;
965  while (np != iplus) {
966  np->nr_of_nodes = oldnumber - np->father->nr_of_nodes;
967  np = np->father;
968  }
969 
970  iplus->nr_of_nodes = oldnumber;
971 
972  // 4.3.2.6: update flows and nr_of_nodes in path from jminus to w
973 
974  np = jminus;
975  while (np != w) {
976  np->nr_of_nodes -= oldnumber;
977  if (np->orientation != eplus_ori) {
978  np->flow += delta;
979  }
980  else {
981  np->flow -= delta;
982  }
983  np = np->father;
984  }
985 
986  // 4.3.2.7 update flows and nr_of_nodes in path from jplus to w
987 
988  np = jplus;
989  while (np != w) {
990  np->nr_of_nodes += oldnumber;
991  if (np->orientation == eplus_ori) {
992  np->flow += delta;
993  }
994  else {
995  np->flow -= delta;
996  }
997  np = np->father;
998  }
999 
1000  }
1001 
1002  /* 4.4: Update lists B, N' and N'' */
1003 
1004  if (eminus == eplus) {
1005  if (!from_ub) {
1006  if (pre == nullptr)
1007  start_n1 = eminus->next_arc;
1008  else
1009  pre->next_arc = eminus->next_arc;
1010 
1011  eminus->next_arc = start_n2;
1012  start_n2 = eminus;
1013  } else {
1014  if (pre == nullptr)
1015  start_n2 = eminus->next_arc;
1016  else
1017  pre->next_arc = eminus->next_arc;
1018  eminus->next_arc = start_n1;
1019  start_n1 = eminus;
1020  }
1021  } else {
1022  TCost wcost = eminus->cost;
1023  int wub = eminus->upper_bound;
1024  int wnum = eminus->arcnum;
1025  nodetype *w_head = eminus->head;
1026  nodetype *w_tail = eminus->tail;
1027  eminus->tail = eplus->tail;
1028  eminus->head = eplus->head;
1029  eminus->upper_bound = eplus->upper_bound;
1030  eminus->arcnum = eplus->arcnum;
1031  eminus->cost = eplus->cost;
1032  eplus->tail = w_tail;
1033  eplus->head = w_head;
1034  eplus->upper_bound = wub;
1035  eplus->cost = wcost;
1036  eplus->arcnum = wnum;
1037  arctype *ep = eplus;
1038 
1039  if (pre != nullptr)
1040  pre->next_arc = ep->next_arc;
1041  else {
1042  if (from_ub) start_n2 = ep->next_arc;
1043  else start_n1 = ep->next_arc;
1044  }
1045 
1046  if (to_ub) {
1047  ep->next_arc = start_n2;
1048  start_n2 = ep;
1049  } else {
1050  if (!artif_to_lb) {
1051  ep->next_arc = start_n1;
1052  start_n1 = ep;
1053  }
1054  }
1055  }
1056 
1057  step++;
1058 
1059  /* 4.5: Eliminate artificial arcs and artificial root node */
1060 
1061  if (artificials == 1) {
1062  artificials = 0;
1063  nodetype *nd = root->successor;
1064  arctype *e1 = nd->arc_id;
1065 
1066  if (nd->flow>0) {
1067  feasible = false;
1068  finished = true;
1069  } else {
1070  feasible = true;
1071  if (e1 == start_b) {
1072  start_b = e1->next_arc;
1073  } else {
1074  e = start_b;
1075  while (e->next_arc != e1)
1076  e = e->next_arc;
1077  e->next_arc = e1->next_arc;
1078  }
1079 
1080  iw = root;
1081  root = root->successor;
1082  root->father = root;
1083  sigma = root->dual;
1084 
1085  np = root;
1086  while (np->successor != iw) {
1087  np->dual -= sigma;
1088  np = np->successor;
1089  }
1090 
1091  np->dual -= sigma;
1092  np->successor = root;
1093 
1094  }
1095 
1096  }
1097 
1098  }
1099 
1100  } while (!finished);
1101 
1102  /* 5: Return results */
1103 
1104  /* Feasible solution? */
1105  if (artificials != 0
1106  && feasible) {
1107  np = root->successor;
1108  do {
1109  if (np->father == root
1110  && np->flow > 0) {
1111  feasible = false;
1112  np = root;
1113  }
1114  else
1115  np = np->successor;
1116  } while (np != root);
1117 
1118  arctype *ep = start_n2;
1119  while (ep != nullptr && feasible) {
1120  if (ep == nullptr)
1121  break;
1122  if (ep->tail == root && ep->head == root)
1123  feasible = false;
1124  ep = ep->next_arc;
1125  }
1126  }
1127 
1128  int retValue = 0;
1129 
1130  if (feasible) {
1131  /* Objective function value */
1132  TCost zfw = 0; // current total cost
1133  np = root->successor;
1134  while (np != root) {
1135  if (np->flow != 0) {
1136  zfw += np->flow * np->arc_id->cost;
1137  }
1138  np = np->successor;
1139  }
1140  arctype *ep = start_n2;
1141  while (ep != nullptr) {
1142  zfw += ep->cost * ep->upper_bound;
1143  ep = ep->next_arc;
1144  }
1145  *mcfObj = zfw + lb_cost;
1146 
1147  /* Dual variables */
1148  // CG: removed computation of duals
1149  np = root->successor;
1150  while (np != root) {
1151  mcfDual[np->name-1] = np->dual;
1152  np = np->successor;
1153  }
1154  mcfDual[root->name-1] = root->dual;
1155 
1156  /* Arc flows */
1157  for (i = 0; i < mm; ++i)
1158  mcfFlow[i] = mcfLb[i];
1159 
1160  np = root->successor;
1161  while (np != root) {
1162  // flow on artificial arcs has to be 0 to be ignored! [CG]
1163  OGDF_ASSERT(np->arc_id->arcnum < mm || np->flow == 0);
1164 
1165  if (np->arc_id->arcnum < mm)
1166  mcfFlow[np->arc_id->arcnum] += np->flow;
1167 
1168  np = np->successor;
1169  }
1170 
1171  ep = start_n2;
1172  while (ep != nullptr) {
1173  mcfFlow[ep->arcnum] += ep->upper_bound;
1174  ep = ep->next_arc;
1175  }
1176 
1177  } else {
1178  retValue = 10;
1179  }
1180 
1181  // deallocate artificial arcs
1182  for(i = 1; i <= nn; ++i)
1183 #if 0
1184  delete p[i]->arc_id;
1185 #endif
1186  delete nodes[i].arc_id;
1187 
1188  return retValue;
1189 }
1190 
1191 
1192 } // end namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
int mcf(int mcfNrNodes, int mcfNrArcs, Array< int > &mcfSupply, Array< int > &mcfTail, Array< int > &mcfHead, Array< int > &mcfLb, Array< int > &mcfUb, Array< TCost > &mcfCost, Array< int > &mcfFlow, Array< TCost > &mcfDual, TCost *mcfObj)
Interface for min-cost flow algorithms.
Compare floating point numbers with epsilons and integral numbers with normal compare operators...
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.
std::enable_if< std::is_integral< T >::value, bool >::type less(const T &x, const T &y) const
Compare if x is LESS than y for integral types.
Definition: EpsilonTest.h:57
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
edge firstEdge() const
Returns the first edge in the list of all edges.
Definition: Graph_d.h:603
Template of base class of min-cost-flow algorithms.
Class for the representation of edges.
Definition: Graph_d.h:291
Class for the representation of nodes.
Definition: Graph_d.h:167
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
bool isSelfLoop() const
Definition: Graph_d.h:337
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
void start(Array< int > &supply)
The namespace for all OGDF objects.
Computes a min-cost flow using a network simplex method.
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, NodeArray< TCost > &dual) override
Computes a min-cost flow in the directed graph G using a network simplex method.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:578
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
void beadouble(arctype **eplus, arctype **pre, bool *from_ub)
void beacircle(arctype **eplus, arctype **pre, bool *from_ub)
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41