-
Notifications
You must be signed in to change notification settings - Fork 368
/
EdmondsKarpAlgorithm.java
95 lines (71 loc) · 2.83 KB
/
EdmondsKarpAlgorithm.java
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
// Path: Java\Graphs\EdmondsKarpAlgorithm.java
// Time Complexity: O(V * E^2)
// Space Complexity: O(V^2)
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class EdmondsKarpAlgorithm {
static final int V = 6; // Number of vertices in the graph
// Function to perform Breadth-First Search (BFS)
static boolean bfs(int[][] residualGraph, int[] parent, int source, int sink) {
boolean[] visited = new boolean[V];
Queue<Integer> queue = new LinkedList<>();
queue.add(source);
visited[source] = true;
parent[source] = -1;
while (!queue.isEmpty()) {
int u = queue.poll();
for (int v = 0; v < V; ++v) {
if (!visited[v] && residualGraph[u][v] > 0) {
queue.add(v);
parent[v] = u;
visited[v] = true;
}
}
}
return visited[sink]; // Return true if there is a path from source to sink
}
// Function to find the maximum flow using Edmonds-Karp algorithm
static int edmondsKarp(int[][] graph, int source, int sink) {
int[][] residualGraph = new int[V][V]; // Residual graph
int[] parent = new int[V]; // Parent array to store the augmenting path
for (int u = 0; u < V; ++u) {
for (int v = 0; v < V; ++v) {
residualGraph[u][v] = graph[u][v];
}
}
int maxFlow = 0; // Initialize the maximum flow
// Augment the flow while there is a path from source to sink
while (bfs(residualGraph, parent, source, sink)) {
int pathFlow = Integer.MAX_VALUE;
// Find the minimum residual capacity along the augmenting path
for (int v = sink; v != source; v = parent[v]) {
int u = parent[v];
pathFlow = Math.min(pathFlow, residualGraph[u][v]);
}
// Update the residual capacities and reverse edges along the path
for (int v = sink; v != source; v = parent[v]) {
int u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
}
maxFlow += pathFlow; // Add the path flow to the maximum flow
}
return maxFlow;
}
public static void main(String[] args) {
int[][] graph = {
{0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};
int source = 0; // Source vertex
int sink = 5; // Sink vertex
int maxFlow = edmondsKarp(graph, source, sink);
System.out.println("Maximum flow: " + maxFlow);
}
}
// Output>> Maximum flow: 23