Maximum Flow (ADU Feb 21) a WimpyPoint presentation owned by Mark Dettinger * Maximum Flow in a Network * The Ford-Fulkerson Algorithm in C Maximum Flow in a Network Given a directed graph with a source and a sink and capacities assigned to the edges, determine the maximum flow from the source to the sink. * For each edge, the flow must not exceed the edge's capacity. * For each node, the incoming flow must be equal to the outgoing flow. The Ford-Fulkerson Algorithm in C #include Basic Definitions #define WHITE 0 #define GRAY 1 #define BLACK 2 #define MAX_NODES 1000 #define oo 1000000000 Declarations int n; // number of nodes int e; // number of edges int capacity[MAX_NODES][MAX_NODES]; // capacity matrix int flow[MAX_NODES][MAX_NODES]; // flow matrix int color[MAX_NODES]; // needed for breadth-first search int pred[MAX_NODES]; // array to store augmenting path int min (int x, int y) { return x0) { enqueue(v); pred[v] = u; } } } // If the color of the target node is black now, // it means that we reached it. return color[target]==BLACK; } Ford-Fulkerson Algorithm int max_flow (int source, int sink) { int i,j,u; // Initialize empty flow. int max_flow = 0; for (i=0; i=0; u=pred[u]) { increment = min(increment,capacity[pred[u]][u]-flow[pred[u]][u]); } // Now increment the flow. for (u=n-1; pred[u]>=0; u=pred[u]) { flow[pred[u]][u] += increment; flow[u][pred[u]] -= increment; } max_flow += increment; } // No augmenting path anymore. We are done. return max_flow; } Reading the input file and the main program void read_input_file() { int a,b,c,i,j; FILE* input = fopen("mf.in","r"); // read number of nodes and edges fscanf(input,"%d %d",&n,&e); // initialize empty capacity matrix for (i=0; i