-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_flow_test.cc
More file actions
146 lines (133 loc) · 4.69 KB
/
max_flow_test.cc
File metadata and controls
146 lines (133 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "max_flow_ford_fulkerson.hpp"
#include "max_flow_push_relabel.hpp"
#include <iostream>
#include "logging_utils.h"
// Being very lazy here...
using namespace graph_utils;
using namespace max_flow;
using std::vector;
using std::pair;
void TestTinyGraphFordFulkerson() {
// Node indexes, edges, and capacities
Graph<int,EdgeCapacity<int>,false> my_g({{0,1,2},{{1,2},{2,0}},{1.0,2.0}});
MaxFlowComputerFordFulkerson<int,int> mfc;
double max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 1) {
cgreen("PASSED: src <1> 2 <2> sink = 1") << endl;
} else {
cred("FAILED: src <1> 2 <2> sink = 1") << endl;
cout << "Expected 1; Got: " << max_flow << endl;
}
}
void TestMediumGraphFordFulkerson() {
// Node indexes, edges, and capacities
Graph<int,EdgeCapacity<int>,false> my_g = Graph<int,EdgeCapacity<int>,false>(
{{0,1,2,3},
{{0,2},{0,3},{2,3},{2,1},{3,1}},
{1.0,3.0,2.0,3.0,2.0}});
MaxFlowComputerFordFulkerson<int,int> mfc;
double max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 4) {
cgreen("PASSED: four nodes") << endl;
} else {
cred("FAILED: four nodes") << endl;
cout << "Expected 4; Got: " << max_flow << endl;
}
}
void TestLargeGraphFordFulkerson() {
Graph<int,EdgeCapacity<int>,false> my_g = Graph<int,EdgeCapacity<int>,false>(
{{0,1,2,3,4,5,6,7,8},
{{0,2},{0,3},{0,4},{2,3},{3,4},{2,8},{2,6},{3,6},{3,5},{4,5},{5,6},
{5,7},{4,7},{8,6},{6,7},{8,1},{6,1},{7,1}},
{12,15,20,5,11,5,2,6,3,4,6,1,8,9,7,18,13,10}});
MaxFlowComputerFordFulkerson<int,int> mfc;
double max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 28) {
cgreen("PASSED: 9 nodes") << endl;
} else {
cred("FAILED: 9 nodes") << endl;
cout << "Expected 28; Got: " << max_flow << endl;
}
}
void TestTinyGraphPushRelabel() {
// Node indexes, edges, and capacities
Graph<int,EdgeCapacity<int>,false> my_g({{0,1,2},{{1,2},{2,0}},{1.0,2.0}});
MaxFlowComputerPushRelabel<int,int> mfc;
double max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 1) {
cgreen("PASSED: src <1> 2 <2> sink = 1") << endl;
} else {
cred("FAILED: src <1> 2 <2> sink = 1") << endl;
cout << "Expected 1; Got: " << max_flow << endl;
}
}
void TestMediumGraphPushRelabel() {
// Node indexes, edges, and capacities
Graph<int,EdgeCapacity<int>,false> my_g = Graph<int,EdgeCapacity<int>,false>(
{{0,1,2,3},
{{0,2},{0,3},{2,3},{2,1},{3,1}},
{1.0,3.0,2.0,3.0,2.0}});
MaxFlowComputerPushRelabel<int,int> mfc;
double max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 4) {
cgreen("PASSED: four nodes") << endl;
} else {
cred("FAILED: four nodes") << endl;
cout << "Expected 4; Got: " << max_flow << endl;
}
}
void TestLargeGraphPushRelabel() {
Graph<int,EdgeCapacity<int>,false> my_g = Graph<int,EdgeCapacity<int>,false>(
{{0,1,2,3,4,5,6,7,8},
{{0,2},{0,3},{0,4},{2,3},{3,4},{2,8},{2,6},{3,6},{3,5},{4,5},{5,6},
{5,7},{4,7},{8,6},{6,7},{8,1},{6,1},{7,1}},
{12,15,20,5,11,5,2,6,3,4,6,1,8,9,7,18,13,10}});
MaxFlowComputerPushRelabel<int,int> mfc;
double max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 28) {
cgreen("PASSED: 9 nodes") << endl;
} else {
cred("FAILED: 9 nodes") << endl;
cout << "Expected 28; Got: " << max_flow << endl;
}
}
void TestLargeGraphPushRelabelRepeat() {
Graph<int,EdgeCapacity<int>,false> my_g = Graph<int,EdgeCapacity<int>,false>(
{{0,1,2,3,4,5,6,7,8},
{{0,2},{0,3},{0,4},{2,3},{3,4},{2,8},{2,6},{3,6},{3,5},{4,5},{5,6},
{5,7},{4,7},{8,6},{6,7},{8,1},{6,1},{7,1}},
{12,15,20,5,11,5,2,6,3,4,6,1,8,9,7,18,13,10}});
MaxFlowComputerPushRelabel<int,int> mfc;
double max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 28) {
cgreen("PASSED: 9 nodes") << endl;
} else {
cred("FAILED: 9 nodes") << endl;
cout << "Expected 28; Got: " << max_flow << endl;
}
my_g = Graph<int,EdgeCapacity<int>,false>(
{{0,1,2,3,4,5,6,7,8},
{{0,2},{0,3},{0,4},{2,3},{3,4},{2,8},{2,6},{3,6},{3,5},{4,5},{5,6},
{5,7},{4,7},{8,6},{6,7},{8,1},{6,1},{7,1}},
{12,15,20,5,11,5,2,6,3,4,6,1,8,9,7,18,13,10}});
max_flow = mfc.compute_max_flow(&my_g);
if (max_flow == 28) {
cgreen("PASSED: 9 nodes repeat") << endl;
} else {
cred("FAILED: 9 nodes repeat") << endl;
cout << "Expected 28; Got: " << max_flow << endl;
}
}
int main() {
logging_utils::print_stack_traces();
cout << "Running max flow tests. Nodes are indexed. 0=source, 1=sink."
<< endl;
TestTinyGraphFordFulkerson();
TestMediumGraphFordFulkerson();
TestLargeGraphFordFulkerson();
TestTinyGraphPushRelabel();
TestMediumGraphPushRelabel();
TestLargeGraphPushRelabel();
TestLargeGraphPushRelabelRepeat();
return 0;
}