intdequeue() { int x = q[head]; head++; color[x] = C; return x; }
// Using BFS as a searching algorithm intbfs(int start, int target) { int u, v; for (u = 0; u < n; u++) { color[u] = A; } head = tail = 0; enqueue(start); pred[start] = -1; while (head != tail) { u = dequeue(); for (v = 0; v < n; v++) { if (color[v] == A && capacity[u][v] - flow[u][v] > 0) { enqueue(v); pred[v] = u; } } } return color[target] == C; }
// Applying fordfulkerson algorithm intfordFulkerson(int source, int sink) { int i, j, u; int max_flow = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { flow[i][j] = 0; } }
// Updating the residual values of edges while (bfs(source, sink)) { int increment = O; for (u = n - 1; pred[u] >= 0; u = pred[u]) { increment = min(increment, capacity[pred[u]][u] - flow[pred[u]][u]); } for (u = n - 1; pred[u] >= 0; u = pred[u]) { flow[pred[u]][u] += increment; flow[u][pred[u]] -= increment; } // Adding the path flows max_flow += increment; } return max_flow; }
intmain() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { capacity[i][j] = 0; } } n = 6; e = 7;