CSC 172 DATA STRUCTURES
FLOW
FLOW “I can go with the flow.” - Queens of the Stone Age
“Let your body go with the flow.” “I can go with the flow.” - Queens of the Stone Age “Let your body go with the flow.” - Madonna
“Let your body go with the flow.” “I can go with the flow.” - Queens of the Stone Age “Let your body go with the flow.” - Madonna “Use the flow, Luke!” - Obi-wan Kenobi
FLOW NETWORKS Three categories: Distribution problems Matching problems Cut problems
FLOW NETWORKS Distribution problems -moving objects from one place to another in a network Merchandise distribution Communication Traffic flow 1 2 3 4 5 6 7 8 9
FLOW NETWORKS Matching problems -connecting pairs Job Placement Minimum distance point matching 1 1 2 2 3 3 4 4 A B C D E
MATCHING JOB ASSINGMENTS Abe – Adobe, Apple, HP Bea – Adobe, Apple, Yahoo Cal – HP, IBM, Sun Deb – Adobe, Apple Eli – IBM, Sun, Yahoo Fay – HP, Sun, Yahoo Adobe – Abe, Bea, Deb Apple – Abe, Bea, Deb HP – Abe, Cal, Fay IBM – Cal, Eli Sun – Cal, Eli, Fay Yahoo – Bea, Eli, Fay
FLOW NETWORKS Cut problems -remove edges to break graph into two pieces Network reliability Cutting supply lines 1 1 2 2 3 3 4 4 5
FLOW NETWORKS (DEF) Flow Networks :
FLOW NETWORKS (DEF) s Flow Networks : Digraphs A B C D E t
FLOW NETWORKS (DEF) Flow Networks : Digraphs Weights on edges (capacities) 2 2 1 A B 2 2 1 2 C 2 4 D E 2 1 1 t
FLOW NETWORKS (DEF) Flow Networks : Digraphs source s Flow Networks : Digraphs Weights on edges (capacities) Two special verticies Source ; s Sink : t 2 2 1 A B 2 2 1 2 C 2 4 D E 1 2 1 t sink
CAPACITY AND FLOW Edge Capacity non-negative weights t s 2 2 1 A B 2 2 4 D E 1 2 1 1 t
CAPACITY AND FLOW Edge Capacity non-negative weights Flow 0 <= flow <= capacity flow in = flow out 2 2 1 2 1 A B 2 2 1 1 1 1 1 2 C 2 4 D 1 E 1 2 1 1 2 1 t
CAPACITY AND FLOW Edge Capacity non-negative weights Flow value = 3 s Edge Capacity non-negative weights Flow 0 <= flow <= capacity flow in = flow out Value outflow of source inflow of sink 2 2 1 2 1 A B 2 2 1 1 1 1 1 2 C 2 4 D 1 E 1 2 1 1 2 1 t value = 3
RESIDUAL GRAPH Residual = capacity - flow t s 2 2 1 2 1 A B 2 2 1 1 1 value = 3 s Residual = capacity - flow 2 2 1 2 1 A B 2 2 1 1 1 1 1 2 C 2 4 D 1 E 1 2 1 1 2 1 t value = 3
RESIDUAL CAPACITY Residual = capacity - flow t s 2 2 1 1 1 2 1 A B 1 2 value = 3 s Residual = capacity - flow 2 2 1 1 1 2 1 A B 1 2 2 1 1 1 1 1 1 2 C 1 2 4 1 4 D 1 E 1 2 1 1 1 2 1 t value = 3
RESIDUAL GRAPH Residual = capacity - flow t s 1 A 1 B 1 1 1 C 1 D 4 E
AUGMENTATION PATH Augmentation Path s Augmentation Path - a path from source to sink through the residual graph 1 A 1 B 1 1 1 C 1 D 4 E 1 t
BACKEDGE For every edge, there is a virtual reverse back edge the capacity of a back edge is the flow of the edge 2 2 1 2 1 A B 2 2 1 1 1 1 2 1 C 2 4 D 1 E 1 2 1 1 2 1 t
BACKEDGE For every edge, there is a virtual reverse back edge the capacity of a back edge is the flow of the edge (whoa!) Backedge(v,w) = flow(w,v) 2 2 2 1 1 2 1 A B 1 2 2 1 1 1 1 1 2 1 C 1 2 4 1 D 1 E 1 2 1 1 2 1 2 1 t
BACKEDGE GRAPH For every edge, there is a virtual reverse back edge the capacity of a back edge is the flow of the edge (whoa!) Backedge(v,w) = flow(w,v) 2 1 A B 1 1 1 C 1 1 D E 1 2 t
RESIDUAL GRAPH Residual(w,v) = capacity(w,v) – flow(w,v) Backedge(v,w) = flow(w,v) 2 1 1 A A 1 1 B B 1 1 1 1 1 C C 1 1 D D 1 4 E E 1 1 2 t t
Of course, in real life : There is one graph public class Edge { int v, w ; int capacity, flow; int residual, backflow; // methods }
Define Capacity Graph Flow graph residual graph (capacity – flow at each edge) back edge graph – reverse edge directions augmenting path – from s to t along non-zero capacity edges
Why all the complexity? Sometimes, in graph theory as in life, it turns out that greed leads us to sub-optimal results.
s 3 2 1 A B 3 4 2 D E 2 3 t
s 3 2 1 A B 3 4 2 D E 2 3 t
s 3 2 3 1 A B 3 4 2 3 D E 2 3 3 t
s 2 1 A B 3 1 2 D E 2 t
capacity, flow, residual, backedge 3 2 3 2 1 1 A B A B 4 3 2 3 2 4 D E D E 2 3 3 2 t t
capacity, flow, residual, backedge 3 2 3 2 3 1 1 A B A B 4 3 2 3 2 4 3 D E D E 2 3 3 3 2 t t
capacity, flow, residual, backedge 3 2 2 3 3 1 1 A B A B 1 3 2 3 2 4 3 3 D E D E 2 3 3 2 3 t t
capacity, flow, residual, backedge 3 2 2 3 3 1 1 A B A B 1 3 2 3 2 4 3 3 D E D E 2 3 3 2 3 t t
capacity, flow, residual, backedge 3 2 2 3 2 3 1 1 A B A B 1 3 2 2 2 3 2 4 1 3 D E D E 2 3 2 3 2 3 t t
capacity, flow, residual, backedge 3 2 3 2 3 1 2 1 A B A B 3 3 2 2 2 3 2 2 4 1 1 D E D E 2 3 2 2 3 3 t t
capacity, flow, residual, backedge 3 2 3 2 3 1 2 1 A B A B 3 3 2 2 2 3 2 2 4 1 1 D E D E 2 3 2 2 3 3 t t