Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

add Edmund Karp in java #108

Merged
merged 2 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Java/EdmundKarp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.util.*;
import java.io.*;

public class Main {
private static final int MAX_V = 40; // enough for sample graph in Figure 4.24/4.25/4.26
private static final int INF = 1000000000;

// we need these global variables
private static int[][] res = new int[MAX_V][]; // define MAX_V appropriately
private static int mf, f, s, t;
private static Vector < Integer > p = new Vector < Integer > ();

private static void augment(int v, int minEdge) { // traverse the BFS spanning tree as in print_path (section 4.3)
if (v == s) { f = minEdge; return; } // reach the source, record minEdge in a global variable `f'
else if (p.get(v) != -1) { augment(p.get(v), Math.min(minEdge, res[p.get(v)][v])); // recursive call
res[p.get(v)][v] -= f; res[v][p.get(v)] += f; } // alter residual capacities
}

public static void main(String[] args) throws Exception {
int V, k, vertex, weight;

Scanner sc = new Scanner(System.in);

V = sc.nextInt();
s = sc.nextInt();
t = sc.nextInt();

for (int i = 0; i < V; i++) {
res[i] = new int[MAX_V];
k = sc.nextInt();
for (int j = 0; j < k; j++) {
vertex = sc.nextInt();
weight = sc.nextInt();
res[i][vertex] = weight;
}
}

mf = 0;
while (true) { // run O(VE^2) Edmonds Karp to solve the Max Flow problem
f = 0;

// run BFS, please examine parts of the BFS code that is different than in Section 4.3
Queue < Integer > q = new LinkedList < Integer > ();
Vector < Integer > dist = new Vector < Integer > ();
dist.addAll(Collections.nCopies(V, INF)); // #define INF 2000000000
q.offer(s);
dist.set(s, 0);
p.clear();
p.addAll(Collections.nCopies(V, -1)); // (we have to record the BFS spanning tree)
while (!q.isEmpty()) { // (we need the shortest path from s to t!)
int u = q.poll();
if (u == t) break; // immediately stop BFS if we already reach sink t
for (int v = 0; v < MAX_V; v++) // note: enumerating neighbors with AdjMatrix is `slow'
if (res[u][v] > 0 && dist.get(v) == INF) { // res[u][v] can change!
dist.set(v, dist.get(u) + 1);
q.offer(v);
p.set(v, u); // parent of vertex v is vertex u
}
}

augment(t, INF); // find the min edge weight `f' along this path, if any
if (f == 0) break; // if we cannot send any more flow (`f' = 0), terminate the loop
mf += f; // we can still send a flow, increase the max flow!
}

System.out.printf("%d\n", mf); // this is the max flow value of this flow graph
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Reference purposes .
23. Pollard Rho Integer Factorization [cpp](https://github.com/saru95/DSA/blob/master/PRB.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/PRB.py)
24. Binary Indexed Trees / BIT [cpp](https://github.com/saru95/DSA/blob/master/BIT.cpp) [python](https://github.com/DravitLochan/DSA/blob/master/Python/BIT.py)
25. Square Root Decomposition [cpp](https://github.com/saru95/DSA/blob/master/SRD.cpp)
26. Ford-Fulkerson Algorithm for Maximum Flow (BFS)/ Edmonds Karp Algorithm [cpp](https://github.com/saru95/DSA/blob/master/FFA.cpp)
26. Ford-Fulkerson Algorithm for Maximum Flow (BFS)/ Edmonds Karp Algorithm [cpp](https://github.com/saru95/DSA/blob/master/FFA.cpp) [java](https://github.com/saru95/DSA/pull/108/files)
27. Miller Rabin Primality Test [cpp](https://github.com/saru95/DSA/blob/master/MRPT.cpp) [python] (https://github.com/saru95/DSA/blob/master/Python/MRPT.py)
28. Fibonacci Numbers using fast doubling [cpp](https://github.com/saru95/DSA/blob/master/fastdoublingfibo.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/Fourier-Doubling.py) [java](https://github.com/saru95/DSA/blob/master/Java/FastDoublingFibo.java)
29. Generating Non-Fibonacci Numbers [cpp](https://github.com/saru95/DSA/blob/master/NonFibo.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/nNonFib.py)
Expand Down