-
Notifications
You must be signed in to change notification settings - Fork 368
/
FordFulkerson.java
119 lines (95 loc) · 4.16 KB
/
FordFulkerson.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
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
import java.util.Arrays;
import java.util.Scanner;
public class FordFulkerson {
// Function to find the minimum of two numbers
private static int min(int a, int b) {
return (a < b) ? a : b;
}
// Function to find if there is a path from source to sink in the residual graph
private static boolean bfs(int[][] residualGraph, int source, int sink, int[] parent) {
int V = residualGraph.length;
boolean[] visited = new boolean[V]; // Create a visited array to keep track of visited vertices
Arrays.fill(visited, false);
// Create a queue for BFS
int[] queue = new int[V];
int front = 0, rear = 0;
queue[rear++] = source;
visited[source] = true;
parent[source] = -1; // Set the parent of the source as -1
// Standard BFS loop
while (front < rear) {
int u = queue[front++];
for (int v = 0; v < V; v++) {
if (!visited[v] && residualGraph[u][v] > 0) {
queue[rear++] = v;
visited[v] = true;
parent[v] = u;
}
}
}
// If we reached the sink in BFS, then there is a path from source to sink
return visited[sink];
}
// Function to implement the Ford-Fulkerson algorithm
private static int fordFulkerson(int[][] graph, int source, int sink) {
int V = graph.length;
// Create a residual graph and initialize it with the original capacities
int[][] residualGraph = new int[V][V];
for (int u = 0; u < V; u++) {
for (int v = 0; v < V; v++) {
residualGraph[u][v] = graph[u][v];
}
}
int[] parent = new int[V]; // This array is filled by BFS and stores the path
int maxFlow = 0; // Initialize the maximum flow
// Augment the flow while there is a path from source to sink in the residual graph
while (bfs(residualGraph, source, sink, parent)) {
// Find the maximum possible flow through the path found in BFS
int pathFlow = Integer.MAX_VALUE;
for (int v = sink; v != source; v = parent[v]) {
int u = parent[v];
pathFlow = min(pathFlow, residualGraph[u][v]);
}
// Update the residual capacities of the edges 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;
}
// Add the path flow to the overall maximum flow
maxFlow += pathFlow;
}
// Return the overall maximum flow
return maxFlow;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int V; // Number of vertices in the graph
int source, sink; // Source and sink vertices
System.out.print("Enter the number of vertices in the graph: ");
V = scanner.nextInt();
int[][] graph = new int[V][V]; // Adjacency matrix representation of the graph
System.out.print("Enter the number of edges in the graph: ");
int numEdges = scanner.nextInt();
// Initialize the graph with zero capacity initially
for (int i = 0; i < V; i++) {
Arrays.fill(graph[i], 0);
}
// Get the edge details from the user and update the graph
for (int i = 0; i < numEdges; i++) {
int u, v, capacity;
System.out.print("Enter the start vertex, end vertex, and capacity of edge " + (i + 1) + ": ");
u = scanner.nextInt();
v = scanner.nextInt();
capacity = scanner.nextInt();
graph[u][v] = capacity;
}
System.out.print("Enter the source vertex: ");
source = scanner.nextInt();
System.out.print("Enter the sink vertex: ");
sink = scanner.nextInt();
// Call the Ford-Fulkerson algorithm to find the maximum flow
int maxFlow = fordFulkerson(graph, source, sink);
System.out.println("The maximum flow in the graph is: " + maxFlow);
}
}