-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd3fc82
commit 97d88da
Showing
16 changed files
with
91 additions
and
103 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package algorithms; | ||
|
||
import java.util.*; | ||
|
||
public class MaxFlowAlgorithm { | ||
int V; | ||
|
||
public MaxFlowAlgorithm(int v){ | ||
V = v; | ||
} | ||
|
||
boolean search(int arrGraph[][], int s, int t, int pare[]) { | ||
boolean vis[] = new boolean[V]; | ||
for (int i = 0; i < V; ++i) | ||
vis[i] = false; | ||
|
||
LinkedList<Integer> queue = new LinkedList<Integer>(); | ||
queue.add(s); | ||
vis[s] = true; | ||
pare[s] = -1; | ||
|
||
while (queue.size() != 0) { | ||
int u = queue.poll(); | ||
|
||
for (int v = 0; v < V; v++) { | ||
if (vis[v] == false && arrGraph[u][v] > 0) { | ||
queue.add(v); | ||
pare[v] = u; | ||
vis[v] = true; | ||
} | ||
} | ||
} | ||
|
||
return (vis[t] == true); | ||
} | ||
|
||
public int getMaxFlow(int graph[][], int s, int t) { | ||
int u, v; | ||
|
||
int arrGraph[][] = new int[V][V]; | ||
|
||
for (u = 0; u < V; u++) | ||
for (v = 0; v < V; v++) | ||
arrGraph[u][v] = graph[u][v]; | ||
|
||
int pare[] = new int[V]; | ||
|
||
int max_flow = 0; | ||
|
||
while (search(arrGraph, s, t, pare)) { | ||
int fPath = Integer.MAX_VALUE; | ||
for (v = t; v != s; v = pare[v]) { | ||
u = pare[v]; | ||
fPath = Math.min(fPath, arrGraph[u][v]); | ||
} | ||
|
||
for (v = t; v != s; v = pare[v]) { | ||
u = pare[v]; | ||
arrGraph[u][v] -= fPath; | ||
arrGraph[v][u] += fPath; | ||
} | ||
|
||
max_flow += fPath; | ||
} | ||
|
||
return max_flow; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters