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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <utility>
#include <limits>
using namespace std;
const double LARGE = numeric_limits<double>::max();
//======================================================================
template <typename Container> void print( string text, const Container &C ) // General-purpose print routine
{
cout << text;
for ( auto e : C ) cout << e << " ";
cout << '\n';
}
//=====================================================================
template <typename T> T error( string text, T value ) // Used for error-reporting
{
cout << text << '\n';
return value;
}
//=====================================================================
struct Edge
{
int v; // Adjacent vertex
double wt; // POSSIBLE flow TO that vertex
double flow; // CURRENT flow TO that vertex (-ve means FROM)
};
//======================================================================
struct Graph
{
int nvert;
vector< vector<Edge> > adj;
Graph( int n = 0 );
void addEdge( int i, int j, double wt = 1.0, bool directed = false );
void read( istream &in, bool directed = false );
void print( ostream &out = cout );
map < pair<int,int>, Edge* > edgePointers();
};
Graph::Graph( int n )
{
nvert = n;
adj = vector< vector<Edge> >( n );
}
void Graph::addEdge( int i, int j, double w, bool directed )
{
int mx = max( i, j );
if ( mx >= nvert ) { nvert = mx + 1; adj.resize( nvert ); }
adj[i].push_back( { j, w, 0.0 } );
if ( !directed ) adj[j].push_back( { i, w, 0.0 } );
}
void Graph::read( istream &in, bool directed )
{
int i, j;
double wt;
while ( in >> i >> j >> wt ) addEdge( i, j, wt, directed );
}
void Graph::print( ostream &out )
{
for ( int i = 0; i < nvert; i++ )
{
out << "Vertex " << i << ":\n";
for ( auto e : adj[i] )
{
out << " -> " << e.v
<< ": " << e.wt
<< " ( " << e.flow << " )"
<< '\n';
}
}
}
map < pair<int,int>, Edge* > Graph::edgePointers()
{
map< pair<int,int>, Edge* > ptr;
for ( int i = 0; i < nvert; i++ )
{
for ( Edge &e : adj[i] ) ptr[{i,e.v}] = &e;
}
// cout << "MAP:\n";
// for ( auto x : ptr ) cout << x.first.first << " - " << x.first.second << ": " << (*x.second).wt << '\n';
return ptr;
}
//=====================================================================
bool pathFinder( const Graph &g, int start, int finish, vector<int> &path ) // Find a path WITH CAPACITY
{ // from start to finish
path.clear();
int N = g.nvert;
if ( min( start, finish ) < 0 || max( start, finish ) >= N ) return error<bool>( "Invalid start/finish", false );
queue<int> Q;
vector<int> prev( N, -1 );
Q.push( start );
while ( !Q.empty() )
{
int n = Q.front();
Q.pop();
for ( Edge e : g.adj[n] )
{
if ( e.flow < e.wt && prev[e.v] < 0 ) // Added to tree if has capacity
{ // and not previously accessed
prev[e.v] = n;
Q.push( e.v );
}
}
if ( prev[finish] >= 0 ) break; // Found a path with capacity
}
if ( prev[finish] < 0 ) return false; // No path with capacity available
// Create the path back to the source
int num = finish;
path.push_back( num );
while ( num != start )
{
num = prev[num];
path.push_back( num );
}
reverse( path.begin(), path.end() );
return true;
}
//======================================================================
double FordFulkerson( Graph &g, int start, int finish )
{
int N = g.nvert;
if ( min( start, finish ) < 0 || max( start, finish ) >= N ) return error<double>( "Invalid start/finish", -1.0 );
map< pair<int,int>, Edge* > ptr = g.edgePointers();
vector<int> path;
double flow = 0.0;
while( pathFinder( g, start, finish, path ) )
{
double extra = LARGE;
for ( int i = 0; i < path.size() - 1; i++ )
{
Edge e = *ptr[ { path[i], path[i+1] } ];
extra = min( extra, e.wt - e.flow ); // Extra flow on this path is limited by smallest free capacity
}
flow += extra;
for ( int i = 0; i < path.size() - 1; i++ )
{
Edge &e = *ptr[ { path[i], path[i+1] } ]; e.flow += extra; // Add extra flow to this direction ...
Edge &f = *ptr[ { path[i+1], path[i] } ]; f.flow -= extra; // ... equivalent to negative in other direction
}
}
return flow;
}
//======================================================================
int main()
{
bool directed = true; // true means directed arcs
// For directed arcs, capacity in BOTH directions must be specified,
// even if one is 0
// For non-directed arcs, just give one direction; the other is automatic
// Following example from:
// https://www.geeksforgeeks.org/ford-fulkerson-algorithm-for-maximum-flow-problem/
// ifstream in( "graph.txt" ); // read from file
stringstream in( " 0 1 16 1 0 0 \n" // or fake with stringstream
" 0 2 13 2 0 0 \n"
" 1 2 10 2 1 4 \n"
" 1 3 12 3 1 0 \n"
" 2 3 0 3 2 9 \n"
" 2 4 14 4 2 0 \n"
" 3 4 0 4 3 7 \n"
" 3 5 20 5 3 0 \n"
" 4 5 4 5 4 0 \n" );
// Set up graph
Graph g;
g.read( in, directed );
// Calculate maximum flow
int start = 0, finish = 5;
// cout << "Input start, finish: "; cin >> start >> finish;
double flow = FordFulkerson( g, start, finish );
cout << "\nTotal flow from " << start << " to " << finish << " is " << flow << "\n\n";
g.print();
}
|