Skip to content

Commit

Permalink
Finished MaxFlow Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
AliiMahmoud committed May 23, 2020
1 parent dd3fc82 commit 97d88da
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 103 deletions.
Binary file removed bin/algorithms/MaxFlow.class
Binary file not shown.
Binary file added bin/algorithms/MaxFlowAlgorithm.class
Binary file not shown.
Binary file modified bin/gui/GraphPanel$1.class
Binary file not shown.
Binary file modified bin/gui/GraphPanel$2.class
Binary file not shown.
Binary file modified bin/gui/GraphPanel$3.class
Binary file not shown.
Binary file modified bin/gui/GraphPanel$4.class
Binary file not shown.
Binary file modified bin/gui/GraphPanel$5.class
Binary file not shown.
Binary file modified bin/gui/PrintingFrame$1.class
Binary file not shown.
Binary file removed bin/gui/PrintingFrame$2.class
Binary file not shown.
Binary file removed bin/gui/PrintingFrame$3.class
Binary file not shown.
Binary file modified bin/gui/PrintingFrame$listen.class
Binary file not shown.
Binary file modified bin/gui/PrintingFrame.class
Binary file not shown.
Binary file modified program.exe
Binary file not shown.
73 changes: 0 additions & 73 deletions src/algorithms/MaxFlow.java

This file was deleted.

68 changes: 68 additions & 0 deletions src/algorithms/MaxFlowAlgorithm.java
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;
}
}
53 changes: 23 additions & 30 deletions src/gui/PrintingFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,41 +175,34 @@ private void maxFlow(String from, String to) {
msg.setText("Invalid Source Or Sink");
return;
}
Transformer<String, Number> nev = new Transformer<String, Number>() {
public Number transform(String arg0) {
String cost = arg0.trim();
if (cost == null || cost.equals("")) {
return 0;
} else {
if (arg0.contains("."))
return Double.valueOf(arg0.trim());
else
return Integer.valueOf(arg0.trim());
}
HashedMap<String, Integer> map = new HashedMap<String, Integer>();
int i = 0;
for (Object o : graph.getVertices().toArray()) {
map.put(o.toString(), i);
i++;
}
int[][] repGraph = new int[graph.getVertexCount()][graph.getVertexCount()];
for (i = 0; i < graph.getVertexCount(); ++i) {
for (int j = 0; j < graph.getVertexCount(); ++j) {
repGraph[i][j] = 0;
}
};

Map<String, Number> edgeFlowMap = new HashMap<String, Number>();
}
for (Object e : graph.getEdges().toArray()) {
String f = graph.getEndpoints(e.toString()).getFirst();
String t = graph.getEndpoints(e.toString()).getSecond();

Factory<String> edgeFactory = new Factory<String>() {
int count;
public String create() {
Random r = new Random();
return "" + r.nextInt() % 1000 + 1;
i = map.get(f);
int j = map.get(t);
int weight = 0;
if (!e.toString().trim().equals("")) {
weight = Integer.parseInt(e.toString().trim());
}
};
DirectedGraph dGraph = new DirectedSparseMultigraph<String, String>();
for (String v : graph.getVertices()) {
dGraph.addVertex(v);
}
for (String v : graph.getEdges()) {
dGraph.addEdge(v, graph.getEndpoints(v).getFirst(), graph.getEndpoints(v).getSecond(), EdgeType.DIRECTED);
repGraph[i][j] = weight;
}
MaxFlowAlgorithm maxflow = new MaxFlowAlgorithm(graph.getVertexCount());
int result = maxflow.getMaxFlow(repGraph, map.get(from), map.get(to));
msg.setText(msg.getText() + result);

EdmondsKarpMaxFlow<String, String> maxFlow = new EdmondsKarpMaxFlow<String, String>(dGraph, from, to, nev,
edgeFlowMap, edgeFactory);
maxFlow.evaluate();
System.out.println(maxFlow.getMaxFlow());
}

public void adjacencyList() {
Expand Down

0 comments on commit 97d88da

Please sign in to comment.