1package aud.example.graph;
24 double widthfactor = 3.0;
27 ((String)
null,
"aud.example.graph.MaxFlowExample");
35 super(
"aud.example.graph.MaxFlowExample");
55 void setFlow(MyNode i,MyNode j,
double f) {
56 flow_.
set(i.index(),j.index(),f);
65 while (!Double.isInfinite(cp=findAugmentingPath())) {
78 double findAugmentingPath() {
82 n.d=Double.POSITIVE_INFINITY;
94 for (MyEdge e : g_.getOutEdges(u)) {
95 MyNode v=(MyNode) e.destination();
97 double capacity=e.getWeight();
98 double residual=capacity-getFlow(u,v);
100 if (v.p==
null && residual>0.0) {
102 v.d=(u.d<=residual ? u.d : residual);
110 assert((t_.
p==
null && Double.isInfinite(t_.
d)) ||
111 (t_.
p!=
null && !Double.isInfinite(t_.
d)));
117 void addPath(
double cp) {
118 assert(t_.
p!=
null && !Double.isInfinite(t_.
d));
124 MyEdge fe=g_.getEdge(u,v);
125 MyEdge be=g_.getEdge(v,u);
133 double f=getFlow(u,v);
135 assert(getFlow(v,u)==-f);
143 fe.penwidth=cp*widthfactor;
144 fe.setLabel(
""+f+
"/"+fe.getWeight());
145 be.setLabel(
""+(-f)+
"/"+be.getWeight());
147 System.err.println(u);
153 halt(
"add path with capacity "+cp);
156 static final String BACKEDGE =
"red";
159 void initialize(MyGraph g) {
161 throw new RuntimeException(
"expect directed graph!");
172 for (MyEdge e : g.edges()) {
173 MyNode s=(MyNode) e.source(), t=(MyNode) e.destination();
182 for (MyEdge e : g_.edges()) {
183 if (e.getWeight()==0.0) {
184 assert(e.color==BACKEDGE);
188 double f=getFlow((MyNode) e.source(),(MyNode) e.destination());
189 e.penwidth=(f>0.0 ? f*widthfactor : -1.0);
190 e.color=(f>0.0 ?
"blue" :
null);
194 for (MyEdge e : backward_edges)
198 for (MyEdge e : g_.getOutEdges(s_))
199 f+=getFlow(s_,(MyNode) e.destination());
201 halt(
"RESULT with max flow = "+f);
205 MyNode findSource(MyGraph g) {
208 if (g.getInDegree(n)==0) {
210 throw new RuntimeException(
"not implemented: multiple sources!");
214 throw new RuntimeException(
"no source node found");
218 MyNode findSink(MyGraph g) {
221 if (g.getOutDegree(n)==0) {
223 throw new RuntimeException(
"not implemented: multiple sinks!");
227 throw new RuntimeException(
"no sink node found");
233 "java aud.example.graph.MaxFlowExample "+
234 "[-g graphfile] [-w factor]");
245 t.setPosition(4.0,0.0);
246 a.setPosition(2.0,2.0);
247 b.setPosition(2.0,-2.0);
257 public static void main(String[] args) {
261 double widthfactor=2.0;
263 for (
int i=0;i<args.length;++i) {
264 if (args[i].compareTo(
"-g")==0)
265 graph=
new File(args[++i]);
266 if (args[i].compareTo(
"-w")==0)
267 widthfactor=Double.parseDouble(args[++i]);
271 app.widthfactor=widthfactor;
Implementation of AbstractQueue as a (dynamically resized) circular buffer based on array.
void enqueue(T x)
Enqueue element at end of queue.
boolean is_empty()
Is queue empty?
T dequeue()
Remove front element from queue.
Implementation of an array-based vector.
void push_back(T obj)
insert new entry obj at the end [O(1) for sufficient capacity]
Parse text to build graph.
void parse(String input)
parse input
Example for computing the maximum flow using a Ford-Fulkerson type algorithm.
static void main(String[] args)
example (see also usage)
static MyGraph createGraph()
create a small example graph
MaxFlowExample()
create application
void computeMaxFlow(MyGraph g)
main algorithm (calls findAugmentingPath and addPath
graph based on aud.graph.GraphAM
AbstractGraph< AbstractNode, AbstractEdge > getAbstractGraph()
view this graph as an AbstractGraph
node with all possible attributes that we require ;-)
MyNode p
node from which node was reached (defines spanning tree)
String color
color as string
double d
distance to start node (sum of weighs or edge count if no weights defined)
int index()
get index, i.e., unique integer id within graph
Edge addEdge(Node source, Node destination)
String getLabel()
get text description
void setLabel(String label)
set label (default label if label==null)
void setPosition(double x, double y)
helper for drawing the graph (if supported)
T get(int i, int j)
get entry (i,j) [O(log(nnz))]
Simple sparse matrix data structure.
T set(int i, int j, T data)
Set entry (i,j) to data [O(log(nnz))].
Simple viewer for Graphvizable.
void display(String code)
display dot code
Simple framework for single stepping code.
void halt(String text, int timeout)
display text and wait for user or timeout
sparse matrices for encoding adjacency
Graph data structures and algorithms.
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.