-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
99 lines (89 loc) · 3.89 KB
/
main.cpp
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
#include <iostream>
#include "utils/GraphUtils.h"
#include "algorithms/MaximumFlowAlgorithms.h"
#include "algorithms/MinimumCostFlowAlgorithms.h"
int main(int argc, char **argv) {
std::string filename {};
// Check if file name was given else ask for it
if (argc < 2) {
// Filename must be with extension and without spaces
// If the file is not in the same directory as the executable, the full path must be given
std::cout << "Insert input filename: ";
std::cin >> filename;
} else {
filename = argv[1];
}
try {
// read graph from file
auto graph = utils::GraphUtils::CreateGraphFromJSON(filename);
std::cout << "Select the network flow problem:" << std::endl;
std::cout << "1. Maximum flow (EdmondsKarp)" << std::endl;
std::cout << "2. Minimum cost flow (Choose algorithm...)" << std::endl;
std::cout << "3. Exit" << std::endl;
std::cout << "Enter your choice: ";
int choice {};
std::cin >> choice;
std::cout << std::endl;
// for simplicity, we assume that the source is the first node and the sink is the last node
int source {};
int sink { graph->getNumNodes() - 1 };
std::shared_ptr<dto::FlowResult> result;
switch (choice) {
case 1: {
result = algorithms::MaximumFlowAlgorithms::EdmondsKarp(graph, source, sink);
std::cout << "Graph with flow: " << std::endl;
auto opt_graph = utils::GraphUtils::GetOptimalGraph(result->getGraph(), utils::GraphUtils::GetResidualGraph(graph));
std::cout << opt_graph->toString() << std::endl;
std::cout << "Maximum flow: " << result->getFlow() << std::endl;
break;
}
case 2: {
std::cout << "Select the algorithm:" << std::endl;
std::cout << "1. Cycle-cancelling" << std::endl;
std::cout << "2. Successive shortest path" << std::endl;
std::cout << "3. Primal-dual" << std::endl;
std::cout << "4. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
std::cout << std::endl;
switch (choice) {
case 1 : {
std::cout << "Cycle-cancelling selected!" << std::endl;
result = algorithms::MinimumCostFlowAlgorithms::CycleCancelling(graph, source, sink);
break;
}
case 2 : {
std::cout << "Successive shortest path selected!" << std::endl;
result = algorithms::MinimumCostFlowAlgorithms::SuccessiveShortestPath(graph, source, sink);
break;
}
case 3 : {
std::cout << "Primal-dual selected!" << std::endl;
result = algorithms::MinimumCostFlowAlgorithms::PrimalDual(graph, source, sink);
break;
}
case 4: {
return EXIT_SUCCESS;;
}
default: {
throw std::invalid_argument("Invalid choice!");
}
}
std::cout << "Graph with flow: " << std::endl;
std::cout << result->getGraph()->toString() << std::endl;
std::cout << "Minimum cost flow: " << result->getFlow() << std::endl;
break;
}
case 3: {
break;
}
default: {
throw std::invalid_argument("Invalid choice");
}
}
} catch (std::invalid_argument& e) {
std::cout << "ERROR: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}