ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class...

18

Transcript of ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class...

Page 1: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 2: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 3: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 4: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 5: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 6: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 7: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 8: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 9: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 10: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 11: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;

class Node { ...

int height; Node nextNode; Node lastNode; ...

void insertNode( Node node);}

// Add node of height "this.height - 1"// to list of waiting nodes.

void Node::insertNode(Node node) { if (node == SOURCE || node == SINK) return; node.nextNode = lastNode.nextNode; if (node.nextNode == NULL || node.nextNode.height != node.height) node.lastNode = node; else node.lastNode = node.nextNode.lastNode; lastNode.nextNode = node;}

Fabian Reiter

hₖ hₖ₋₁hₖhₖ hₖ₋₁ h₁ h₁hₖ₋₁ hₖ₋₁

nextNode

lastNode

h > h > > hₖ ₖ₋₁ ⋯ ₁

Page 12: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 13: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;

class Node { int excess; int height; Node nextNode; Node lastNode; Edge firstEdge; Edge currEdge; Node doOperation(); void relabel(); Node push(); void insertNode( Node node);}

class Edge { int resCap; Node targetNode; Edge reverseEdge; Edge nextEdge;}

// Preflow‐Push Algorithm.// Requires initialized graph.

void preflowPush(Node currNode) { while (currNode != NULL) { currNode = currNode.doOperation(); }}

// Does a push if possible.// Otherwise a relabel.

Node Node::doOperation() { while (height <= currEdge.targetNode.height || currEdge.resCap == 0) { currEdge = currEdge.nextEdge; if (currEdge == NULL) { relabel(); currEdge = firstEdge; return this; } } return push();}

Fabian Reiter

Page 14: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;

class Node { int excess; int height; Node nextNode; Node lastNode; Edge firstEdge; Edge currEdge; Node doOperation(); void relabel(); Node push(); void insertNode( Node node);}

class Edge { int resCap; Node targetNode; Edge reverseEdge; Edge nextEdge;}

// Relabel: Increment height by 1.

void Node::relabel() { height += 1; lastNode = this;

}

// Push as much flow as possible// through current edge.

Node Node::push() { int pushVol = min(excess, currEdge.resCap); Node neighbour = currEdge.targetNode;

// Insert neighbour into list // if not already there. if (neighbour.excess == 0) insertNode(neighbour);

// Perform the push (update 4 values). excess -= pushVol; neighbour.excess += pushVol; currEdge.resCap -= pushVol; currEdge.reverseEdge.resCap += pushVol;

// Remove this node from list // if excess is zero. return (excess > 0)? this : nextNode;}

Fabian Reiter

Page 15: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 16: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 17: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;
Page 18: ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class Node {int excess; int height; Node nextNode; Node lastNode; Edge firstEdge;