Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/bis12/eecs-340-final-project
Browse files Browse the repository at this point in the history
  • Loading branch information
tdooner committed Nov 30, 2010
2 parents 2e6f424 + 5ef87d0 commit b8b6797
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 83 deletions.
11 changes: 10 additions & 1 deletion build.xml
Expand Up @@ -49,11 +49,20 @@
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" classpathref="classpath" includeantruntime="false">
<javac srcdir="${src}" destdir="${build}" classpathref="classpath" includeantruntime="false" debug="true" debuglevel="lines,vars,source">
<compilerarg value="-Xlint:none"/>
</javac>
</target>

<target name="compile_w_debug" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" classpathref="classpath" includeantruntime="false">
<compilerarg value="-Xlint:none -g"/>
</javac>
</target>


<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
Expand Down
184 changes: 107 additions & 77 deletions src/EdmondsKarp.java
Expand Up @@ -8,90 +8,120 @@

public class EdmondsKarp {

EdmondsVertex currentV;
long capacity;
DirectedSparseGraph<EdmondsVertex,EdmondsEdge> f;
EdmondsVertex source, sink;
EdmondsVertex source, sink;

EdmondsKarp(DirectedSparseGraph<EdmondsVertex, EdmondsEdge> f) {
this.f = f;

// this.source = source;
// this.sink = sink;
for (EdmondsEdge i: f.getEdges()) {
i.setNewFlow(0);
}
}

public int maxFlow(EdmondsVertex source, EdmondsVertex sink) {
for (EdmondsEdge i: f.getEdges()) {
i.setNewFlow(0);
}
public int maxFlow(EdmondsVertex source, EdmondsVertex sink, boolean runFull) {


// While there is a path with available capacity...
long capacity = findPath(source,sink);
System.out.println("Found path of capacity " + capacity + ". It's " + returnPath(sink));
while (capacity != 0) {
// Travel backwards from the sink, adjusting the node capacities
// as we go back.
EdmondsVertex currentV = sink;
while (currentV != source) {
f.findEdge(currentV.parentNode, currentV).addFlow((int)capacity);
currentV = currentV.parentNode;
}
capacity = findPath(source,sink);
System.out.println("Found path of capacity " + capacity + ". It's " + returnPath(sink));
//System.out.println(capacity);
}
Collection<EdmondsVertex> vertices;
return 1;
}
/**
* Finds a path with remaining capacity between s and t.
* Returns the capacity of the path it has found. Also, this method
* stores the path in the EdmondsVertex instances so it is possible
* to work backwards using EdmondsVertex.parentNode from the sink.
* @param s Source node
* @param t Destination node
*/
private long findPath(EdmondsVertex s, EdmondsVertex t) {
// First, reset the Verticies to undiscovered
for(EdmondsVertex i: f.getVertices()) {
i.discoveredState = -1;
i.parentNode = null;
}
s.discoveredState = -2;
// Use a LinkedList as a Queue for finding verticies
LinkedList<EdmondsVertex> discoveredV = new LinkedList<EdmondsVertex>();
// Keep an array of distances from the source
discoveredV.add(s);
// While there is a path with available capacity...
capacity = findPath(source,sink);
System.out.println("Found path of capacity " + capacity + ". It's " + returnPath(sink));
if (runFull){
while (capacity > 0) {
// Travel backwards from the sink, adjusting the node capacities
// as we go back.
currentV = sink;
while (currentV != source) {
f.findEdge(currentV.parentNode, currentV).addFlow((int)capacity);
currentV = currentV.parentNode;
}
capacity = findPath(source,sink);
if (capacity > 0){
System.out.println("Found path of capacity " + capacity + ". It's " + returnPath(sink));
}
else{
System.out.println("Finished");
}
//System.out.println(capacity);
}
}
else{
if (capacity > 0){
currentV = sink;
while (currentV != source) {
f.findEdge(currentV.parentNode, currentV).addFlow((int)capacity);
currentV = currentV.parentNode;
}
capacity = findPath(source,sink);
if (capacity > 0){
System.out.println("Found path of capacity " + capacity + ". It's " + returnPath(sink));
}
else{
System.out.println("Finished");
}
//System.out.println(capacity);
}
}
Collection<EdmondsVertex> vertices;
return 1;
}
/**
* Finds a path with remaining capacity between s and t.
* Returns the capacity of the path it has found. Also, this method
* stores the path in the EdmondsVertex instances so it is possible
* to work backwards using EdmondsVertex.parentNode from the sink.
* @param s Source node
* @param t Destination node
*/
private long findPath(EdmondsVertex s, EdmondsVertex t) {
// First, reset the Verticies to undiscovered
for(EdmondsVertex i: f.getVertices()) {
i.discoveredState = -1;
i.parentNode = null;
}
s.discoveredState = -2;
// Use a LinkedList as a Queue for finding verticies
LinkedList<EdmondsVertex> discoveredV = new LinkedList<EdmondsVertex>();
// Keep an array of distances from the source
//
discoveredV.add(s);

while (discoveredV.size() > 0) {
EdmondsVertex currentV = discoveredV.removeFirst();
for (EdmondsVertex adjV: f.getSuccessors(currentV)) {
EdmondsEdge connection = f.findEdge(currentV, adjV);
if (adjV.discoveredState == -1 && (connection.getRemainingCapacity() > 0)) {
adjV.parentNode = currentV;
adjV.discoveredState = 1; // To prevent cycles
adjV.pathCapacityToNode = Math.min(currentV.pathCapacityToNode, connection.getRemainingCapacity());
if (adjV != t) {
discoveredV.add(adjV);
} else {
return adjV.pathCapacityToNode;
}
}
}
}
return 0;
}
// Follows the path of pointers of parentNode and prints the
// name of each node along the way.
// If you pass this a null reference, I will hunt you down.
public String returnPath(EdmondsVertex dest) {
String end = new String();
EdmondsVertex current = dest;
while (current != null) {
end = current.name + end;
current = current.parentNode;
}
return end;
}
//private int capacity(EdmondsVertex a, EdmondsVertex b) {
while (discoveredV.size() > 0) {
EdmondsVertex currentV = discoveredV.removeFirst();
for (EdmondsVertex adjV: f.getSuccessors(currentV)) {
EdmondsEdge connection = f.findEdge(currentV, adjV);
if (adjV.discoveredState == -1 && (connection.getRemainingCapacity() > 0)) {
adjV.parentNode = currentV;
adjV.discoveredState = 1; // To prevent cycles
adjV.pathCapacityToNode = Math.min(currentV.pathCapacityToNode, connection.getRemainingCapacity());
if (adjV != t) {
discoveredV.add(adjV);
} else {
return adjV.pathCapacityToNode;
}
}
}
}
return 0;
}
// Follows the path of pointers of parentNode and prints the
// name of each node along the way.
// If you pass this a null reference, I will hunt you down.
public String returnPath(EdmondsVertex dest) {
String end = "";
EdmondsVertex current = dest;
while (current != null) {
if (end == ""){
end = current.name + "}";
}
else{
end = current.name + " → " + end;
}
current = current.parentNode;
}
return "{" + end;
}
//private int capacity(EdmondsVertex a, EdmondsVertex b) {

//}
//}
}
38 changes: 33 additions & 5 deletions src/Main.java
Expand Up @@ -17,13 +17,17 @@
import edu.uci.ics.jung.visualization.decorators.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class Main{
static SimpleGraphView sgv;
static Layout<EdmondsVertex, EdmondsEdge> layout;
static VisualizationViewer<EdmondsVertex, EdmondsEdge> vv;
static JFrame frame;
static int graphDims;
public static void main(String[] args){
graphDims = 3;
frame = new JFrame("Simple Graph View");
ControlPanel controls = new ControlPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand Down Expand Up @@ -86,15 +90,19 @@ static class ControlPanel extends JPanel {
JButton beginMaxFlow;
JButton step;
JButton newGraph;
JSlider graphSize;
ControlPanel() {
maxFlow = new JButton("Compute Max Flow");
beginMaxFlow = new JButton("Begin Animation");
step = new JButton("Step");
newGraph = new JButton("New Random Graph");
graphSize = new JSlider(2, 6, graphDims);

maxFlow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sgv.performEdmondsKarp();
//vv.update(frame.getGraphics());
vv.repaint();
}
});

Expand All @@ -106,7 +114,8 @@ public void actionPerformed(ActionEvent e) {
});
step.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//sgv.performEdmondsStep();
sgv.performEdmondsStep();
vv.repaint();
}
});
newGraph.addActionListener(new ActionListener() {
Expand All @@ -123,14 +132,25 @@ public void actionPerformed(ActionEvent e) {
//renderGraph();
}
});
graphSize.setSnapToTicks(true);
graphSize.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
graphDims = graphSize.getValue();
}
});

this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
this.add(maxFlow);
this.add(beginMaxFlow);
this.add(step);
this.add(newGraph);
this.add(graphSize);
}
}

public static int getDims(){
return graphDims;
}
}

class SimpleGraphView{
Expand All @@ -141,10 +161,10 @@ class SimpleGraphView{
private Factory<EdmondsEdge> edgeFact;
private Factory<EdmondsVertex> vertFact;
private Factory<DirectedGraph<EdmondsVertex, EdmondsEdge>> graphFact;
//Fix the random thing soon
private Random R = new Random();
EdmondsVertex[] vertices;
int s, t;
private EdmondsKarp ek;
public SimpleGraphView(){

cart = new HashMap<EdmondsEdge, Double>();
Expand Down Expand Up @@ -177,7 +197,7 @@ public DirectedGraph<EdmondsVertex, EdmondsEdge> create(){
}

private void generateGraph(){
Lattice2DGenerator kswg = new Lattice2DGenerator(graphFact, vertFact, edgeFact, 3, false);
Lattice2DGenerator kswg = new Lattice2DGenerator(graphFact, vertFact, edgeFact, Main.getDims(), false);
g = (DirectedSparseGraph)kswg.create();
vertices = new EdmondsVertex[g.getVertexCount()];
int c = 0;
Expand All @@ -187,6 +207,7 @@ private void generateGraph(){
}
//use these for the source and sink, select out
//of first half and second half to avoid selecting the same node
//wgttt
s = R.nextInt(vertices.length/2);
t = R.nextInt(vertices.length/2) + vertices.length/2;
vertices[s].s = true;
Expand All @@ -203,7 +224,14 @@ public void generateNewGraph() {
}
public void performEdmondsKarp() {
//Lets now try our edmonds-karp algorithm... fingers crossed
EdmondsKarp ek = new EdmondsKarp(g);
ek.maxFlow(vertices[s], vertices[t]);
ek = new EdmondsKarp(g);
ek.maxFlow(vertices[s], vertices[t], true);
}

public void performEdmondsStep() {
if (ek == null){
ek = new EdmondsKarp(g);
}
ek.maxFlow(vertices[s], vertices[t], false);
}
}

0 comments on commit b8b6797

Please sign in to comment.