Mark As Completed Discussion

Network Flows

In graph theory, a network flow is a directed graph where each edge has a capacity and a flow. The capacity defines the maximum amount of flow that can pass through the edge, while the flow represents the actual amount of flow passing through the edge.

Finding the maximum flow in a network is a common problem in computer science and has applications in various domains such as transportation network optimization, network design, and data compression.

One popular algorithm to find the maximum flow in a network is the Ford-Fulkerson algorithm. This algorithm uses a combination of breadth-first search and capacity augmentation to incrementally find the maximum flow.

Here's an example Java code that demonstrates the Ford-Fulkerson algorithm:

TEXT/X-JAVA
1// Example code for finding the maximum flow in a network
2
3class Main {
4  public static void main(String[] args) {
5    // Example graph with capacities
6    int[][] graph = {
7      {0, 16, 13, 0, 0, 0},
8      {0, 0, 10, 12, 0, 0},
9      {0, 4, 0, 0, 14, 0},
10      {0, 0, 9, 0, 0, 20},
11      {0, 0, 0, 7, 0, 4},
12      {0, 0, 0, 0, 0, 0}
13    };
14
15    int source = 0;
16    int sink = 5;
17
18    int maxFlow = fordFulkerson(graph, source, sink);
19    System.out.println("Maximum Flow: " + maxFlow);
20  }
21
22  private static int fordFulkerson(int[][] graph, int source, int sink) {
23    int[][] residualGraph = new int[graph.length][graph[0].length];
24    for (int i = 0; i < graph.length; i++) {
25      for (int j = 0; j < graph[0].length; j++) {
26        residualGraph[i][j] = graph[i][j];
27      }
28    }
29
30    int[] parent = new int[graph.length];
31    int maxFlow = 0;
32
33    while (bfs(residualGraph, source, sink, parent)) {
34      int pathFlow = Integer.MAX_VALUE;
35
36      for (int vertex = sink; vertex != source; vertex = parent[vertex]) {
37        int u = parent[vertex];
38        pathFlow = Math.min(pathFlow, residualGraph[u][vertex]);
39      }
40
41      for (int vertex = sink; vertex != source; vertex = parent[vertex]) {
42        int u = parent[vertex];
43        residualGraph[u][vertex] -= pathFlow;
44        residualGraph[vertex][u] += pathFlow;
45      }
46
47      maxFlow += pathFlow;
48    }
49
50    return maxFlow;
51  }
52
53  private static boolean bfs(int[][] graph, int source, int sink, int[] parent) {
54    boolean[] visited = new boolean[graph.length];
55
56    Queue<Integer> queue = new LinkedList<>();
57    queue.add(source);
58    visited[source] = true;
59    parent[source] = -1;
60
61    while (!queue.isEmpty()) {
62      int u = queue.poll();
63
64      for (int v = 0; v < graph.length; v++) {
65        if (!visited[v] && graph[u][v] > 0) {
66          queue.add(v);
67          parent[v] = u;
68          visited[v] = true;
69        }
70      }
71    }
72
73    return visited[sink];
74  }
75}

The code defines a graph with capacities and uses the Ford-Fulkerson algorithm to find the maximum flow from a given source to a sink vertex. The maximum flow is then printed to the console.

Feel free to modify the code and test it with different network configurations to understand the concept of network flows and the Ford-Fulkerson algorithm better.

JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment