ac.informatik.uni-freiburg.deac.informatik.uni-freiburg.de/teaching/ws12_13/algo1213/solutions/...class...
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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/1.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/2.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/3.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/4.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/5.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/6.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/7.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/8.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/9.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/10.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/11.jpg)
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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/12.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/13.jpg)
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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/14.jpg)
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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/15.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/16.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/17.jpg)
![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;](https://reader030.fdocuments.in/reader030/viewer/2022040101/5e1ac63a9808ed222b5f3f1e/html5/thumbnails/18.jpg)