0
2
0
| 1 | 1 |
@nodes |
| 2 |
label |
|
| 3 |
0 |
|
| 4 |
1 |
|
| 5 |
2 |
|
| 6 |
3 |
|
| 7 |
4 |
|
| 8 |
5 |
|
| 9 |
6 |
|
| 10 |
7 |
|
| 11 |
8 |
|
| 12 |
9 |
|
| 13 |
@edges |
|
| 14 |
label capacity |
|
| 15 |
0 1 0 20 |
|
| 16 |
0 2 1 0 |
|
| 17 |
1 1 2 3 |
|
| 18 |
1 2 3 8 |
|
| 19 |
1 3 4 8 |
|
| 20 |
2 5 5 5 |
|
| 21 |
3 2 6 5 |
|
| 22 |
3 5 7 5 |
|
| 23 |
3 6 8 5 |
|
| 24 |
4 3 9 3 |
|
| 25 |
5 7 10 3 |
|
| 26 |
5 6 11 10 |
|
| 27 |
5 8 12 10 |
|
| 28 |
6 8 13 8 |
|
| 29 |
8 9 14 20 |
|
| 30 |
8 1 15 5 |
|
| 31 |
9 5 16 5 |
|
| 32 |
|
|
| 2 |
label |
|
| 3 |
0 |
|
| 4 |
1 |
|
| 5 |
2 |
|
| 6 |
3 |
|
| 7 |
4 |
|
| 8 |
5 |
|
| 9 |
6 |
|
| 10 |
7 |
|
| 11 |
8 |
|
| 12 |
9 |
|
| 13 |
@arcs |
|
| 14 |
label capacity |
|
| 15 |
0 1 0 20 |
|
| 16 |
0 2 1 0 |
|
| 17 |
1 1 2 3 |
|
| 18 |
1 2 3 8 |
|
| 19 |
1 3 4 8 |
|
| 20 |
2 5 5 5 |
|
| 21 |
3 2 6 5 |
|
| 22 |
3 5 7 5 |
|
| 23 |
3 6 8 5 |
|
| 24 |
4 3 9 3 |
|
| 25 |
5 7 10 3 |
|
| 26 |
5 6 11 10 |
|
| 27 |
5 8 12 10 |
|
| 28 |
6 8 13 8 |
|
| 29 |
8 9 14 20 |
|
| 30 |
8 1 15 5 |
|
| 31 |
9 5 16 5 |
|
| 32 |
@attributes |
|
| 33 | 33 |
source 1 |
| 34 | 34 |
target 8 |
| 35 |
@end |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <fstream> |
| 20 | 20 |
#include <string> |
| 21 | 21 |
|
| 22 | 22 |
#include "test_tools.h" |
| 23 | 23 |
#include <lemon/smart_graph.h> |
| 24 | 24 |
#include <lemon/preflow.h> |
| 25 | 25 |
#include <lemon/concepts/digraph.h> |
| 26 | 26 |
#include <lemon/concepts/maps.h> |
| 27 | 27 |
#include <lemon/lgf_reader.h> |
| 28 |
#include <lemon/elevator.h> |
|
| 28 | 29 |
|
| 29 | 30 |
using namespace lemon; |
| 30 | 31 |
|
| 31 |
void |
|
| 32 |
void checkPreflowCompile() |
|
| 32 | 33 |
{
|
| 33 | 34 |
typedef int VType; |
| 34 | 35 |
typedef concepts::Digraph Digraph; |
| 35 | 36 |
|
| 36 | 37 |
typedef Digraph::Node Node; |
| 37 | 38 |
typedef Digraph::Arc Arc; |
| 38 | 39 |
typedef concepts::ReadMap<Arc,VType> CapMap; |
| 39 | 40 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap; |
| 40 | 41 |
typedef concepts::WriteMap<Node,bool> CutMap; |
| 41 | 42 |
|
| 43 |
typedef Elevator<Digraph, Digraph::Node> Elev; |
|
| 44 |
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev; |
|
| 45 |
|
|
| 42 | 46 |
Digraph g; |
| 43 | 47 |
Node n; |
| 44 | 48 |
Arc e; |
| 45 | 49 |
CapMap cap; |
| 46 | 50 |
FlowMap flow; |
| 47 | 51 |
CutMap cut; |
| 48 | 52 |
|
| 49 |
Preflow<Digraph, CapMap> |
|
| 53 |
Preflow<Digraph, CapMap> |
|
| 54 |
::SetFlowMap<FlowMap> |
|
| 55 |
::SetElevator<Elev> |
|
| 56 |
::SetStandardElevator<LinkedElev> |
|
| 57 |
::Create preflow_test(g,cap,n,n); |
|
| 50 | 58 |
|
| 51 | 59 |
preflow_test.capacityMap(cap); |
| 52 | 60 |
flow = preflow_test.flowMap(); |
| 53 | 61 |
preflow_test.flowMap(flow); |
| 54 | 62 |
preflow_test.source(n); |
| 55 | 63 |
preflow_test.target(n); |
| 56 | 64 |
|
| 57 | 65 |
preflow_test.init(); |
| 58 | 66 |
preflow_test.init(cap); |
| 59 | 67 |
preflow_test.startFirstPhase(); |
| 60 | 68 |
preflow_test.startSecondPhase(); |
| 61 | 69 |
preflow_test.run(); |
| 62 | 70 |
preflow_test.runMinCut(); |
| 63 | 71 |
|
| 64 | 72 |
preflow_test.flowValue(); |
| 65 | 73 |
preflow_test.minCut(n); |
| 66 | 74 |
preflow_test.minCutMap(cut); |
| 67 | 75 |
preflow_test.flow(e); |
| 68 | 76 |
|
| 69 | 77 |
} |
| 70 | 78 |
|
| 71 | 79 |
int cutValue (const SmartDigraph& g, |
| 72 | 80 |
const SmartDigraph::NodeMap<bool>& cut, |
| 73 | 81 |
const SmartDigraph::ArcMap<int>& cap) {
|
| 74 | 82 |
|
| 75 | 83 |
int c=0; |
| 76 | 84 |
for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
|
| 77 | 85 |
if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e]; |
| 78 | 86 |
} |
| 79 | 87 |
return c; |
| 80 | 88 |
} |
| 81 | 89 |
|
| 82 | 90 |
bool checkFlow(const SmartDigraph& g, |
| 83 | 91 |
const SmartDigraph::ArcMap<int>& flow, |
| 84 | 92 |
const SmartDigraph::ArcMap<int>& cap, |
| 85 | 93 |
SmartDigraph::Node s, SmartDigraph::Node t) {
|
| 86 | 94 |
|
| 87 | 95 |
for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
|
| 88 | 96 |
if (flow[e] < 0 || flow[e] > cap[e]) return false; |
| 89 | 97 |
} |
| 90 | 98 |
|
| 91 | 99 |
for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
|
| 92 | 100 |
if (n == s || n == t) continue; |
| 93 | 101 |
int sum = 0; |
| 94 | 102 |
for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
|
| 95 | 103 |
sum += flow[e]; |
| 96 | 104 |
} |
| 97 | 105 |
for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
|
| 98 | 106 |
sum -= flow[e]; |
| 99 | 107 |
} |
| 100 | 108 |
if (sum != 0) return false; |
| 101 | 109 |
} |
| 102 | 110 |
return true; |
| 103 | 111 |
} |
| 104 | 112 |
|
| 105 | 113 |
int main() {
|
| 106 | 114 |
|
| 107 | 115 |
typedef SmartDigraph Digraph; |
| 108 | 116 |
|
| 109 | 117 |
typedef Digraph::Node Node; |
| 110 | 118 |
typedef Digraph::NodeIt NodeIt; |
| 111 | 119 |
typedef Digraph::ArcIt ArcIt; |
| 112 | 120 |
typedef Digraph::ArcMap<int> CapMap; |
| 113 | 121 |
typedef Digraph::ArcMap<int> FlowMap; |
| 114 | 122 |
typedef Digraph::NodeMap<bool> CutMap; |
| 115 | 123 |
|
| 116 | 124 |
typedef Preflow<Digraph, CapMap> PType; |
| 117 | 125 |
|
| 118 | 126 |
std::string f_name; |
| 119 | 127 |
if( getenv("srcdir") )
|
| 120 | 128 |
f_name = std::string(getenv("srcdir"));
|
| 121 | 129 |
else f_name = "."; |
| 122 | 130 |
f_name += "/test/preflow_graph.lgf"; |
| 123 | 131 |
|
| 124 | 132 |
std::ifstream file(f_name.c_str()); |
| 125 | 133 |
|
| 126 | 134 |
check(file, "Input file '" << f_name << "' not found."); |
| 127 | 135 |
|
| 128 | 136 |
Digraph g; |
| 129 | 137 |
Node s, t; |
| 130 | 138 |
CapMap cap(g); |
| 131 | 139 |
DigraphReader<Digraph>(g,file). |
| 132 | 140 |
arcMap("capacity", cap).
|
| 133 | 141 |
node("source",s).
|
| 134 | 142 |
node("target",t).
|
| 135 | 143 |
run(); |
| 136 | 144 |
|
| 137 | 145 |
PType preflow_test(g, cap, s, t); |
| 138 | 146 |
preflow_test.run(); |
| 139 | 147 |
|
| 140 | 148 |
check(checkFlow(g, preflow_test.flowMap(), cap, s, t), |
| 141 | 149 |
"The flow is not feasible."); |
| 142 | 150 |
|
| 143 | 151 |
CutMap min_cut(g); |
| 144 | 152 |
preflow_test.minCutMap(min_cut); |
| 145 | 153 |
int min_cut_value=cutValue(g,min_cut,cap); |
0 comments (0 inline)