SlideShare a Scribd company logo
Algorithm Design and Complexity

                         Course 12
Overview
   Flow Networks
   Positive Flow and Net Flow
   Flow Properties
   Maximum Flow
   Minimum Cut
   Ford-Fulkerson Method
   Edmonds-Karp Algorithm
   Sample Applications
Flow Networks
   Graphs that model materials that are transported
    through conduits
   Each edge represents a conduit: it has a capacity
    that is an upper bound for the flow rate
   Some vertices may produce materials: sources
   Other vertices may consume materials: sinks
Flow Networks (2)
   The simplest version of flow networks:
       Think of edges as pipes of different sizes (capacities)
       The flow is the effective quantity of liquid that is transported
        through the pipes

   Flow Networks are also called transportation networks
   But you can transport a lot of things beside liquids, like
    commodities, information, electricity, traffic

   There are some interesting and difficult problems for flow
    networks:
       Maximum flow problem
       Maximum flow with minimum cost problem
       Circulation problem
       Multi-commodity problem
Flow Networks (3)
   G(V, E) – directed graph
   The capacity function c: E → R+
   c(u, v) = the capacity of the edge (u, v)
    (u,v)  E  c(u,v) = 0

   Assume we have a single source vertex: s
   Assume we have a single sink vertex: t
   Assume we have a path s .. v .. t  vV
Positive Flow
   We can define a function called positive flow
       p: V x V  R+

   Properties of positive flow:
   Capacity constraint:
         u, vV: p(u, v) <= c(u, v)
   Flow conservation:
         uV  {s, t}: flow_in(u) = flow_out(u)
         uV  {s, t}: vV(p(v, u)) = vV(p(u, v))
Positive Flow – Example
   Notation on each edge: positive flow/capacity
   Note that the two properties are respected
       The flow is always lower than the capacity of each edge
       The flow is conserved in each vertex
       The source produces flow
       The sink consumes flow
Cancellation for Positive Flow
   Note that in the previous example we have:
       1 unit of flow from x  z
       2 units of flow from z  x
   We can cancel 1 unit of flow in both directions
       0 units of flow from x  z
       1 unit of flow from z  x
       Flow conservation is still respected (we cancel the same
        amount of flow in each direction)
       Capacity constraint is still respected (the flow is decreased)
   In both cases, the “net flow” is 1 from z  x

   Without loss of generality, we may always define a
    positive flow in a flow network such that for each (u, v),
    u,vV, we have positive flow either from u  v or from v
     u, but not in both directions!
Net Flow
   We can define a function called net flow
       f: V x V  R

   Properties of net flow:
   Capacity constraint:
         u, vV: f(u, v) <= c(u, v)
   Flow conservation:
         uV  {s, t}: total_net_flow_in(u) = 0
         uV  {s, t}: vV(f(u, v)) = 0
   Skew symmetry:
         u, vV: f(u, v) = - f(v, u)
Net Flow (2)
   Conservation of net flow can be rewritten:

vV (f(u, v)) = 0

As f(u, v) might be either positive or negative
 uV  {s, t}: vV and f(u,v)<0(f(u, v)) + vV and f(u,v)>0 (f(u, v))
  =0
 uV  {s, t}: vV and f(v,u)>0(f(v, u)) = vV and f(u,v)>0 (f(u, v))
  “total positive flow entering u” = “total positive flow
  leaving u”
  “flow in” = “flow out”
Net Flow – Example
   Only positive flow is illustrated in graphical
    representations
   Nevertheless, this does not mean that the (negative)
    net flow does not exist in the other direction
Positive Flow and Net Flow
   Differences

   Net flow satisfies skew symmetry

   p(u, v) >= 0, while
                      f(u, v) >= 0 or f(u, v) <= 0
Positive Flow and Net Flow (2)
   Equivalence
   We can define net flow using only positive flow and vice-
    versa

   f(u, v) = p(u, v) – p(v, u)  u, vV
   Need to show that using this definition, the net flow still
    satisfies the three properties: capacity constraint,
    conservation and skew symmetry

   p(u, v) = f(u, v)   if f(u, v) > 0
   p(u, v) = 0         otherwise
   Need to show that this definition satisfies the two
    properties of positive flow: capacity constraint and
    conservation
Positive Flow and Net Flow (3)
   f(u, v) = p(u, v) – p(v, u)  u, vV
   Capacity constraint:
    p(u, v) <= c(u, v) and p(v, u) >= 0
    => f(u, v) = p(u, v) – p(v, u) <= p(u, v) <= c(u, v)
   Flow conservation:
    vV (f(u, v)) = vV (p(u, v) – p(v, u)) = vV (p(u, v)) – vV
    (p(v, u)) = (from positive flow conservation) = 0
   Skew symmetry:
    f(u, v) = p(u, v) – p(v, u) = –(p(v, u) – p(u, v)) = –f(v,u)

    Similar for the transformation from f(u, v)  p(u, v). Prove
    them at home!
Value of the Flow
   From now on, we shall only use net flow

   Value of the flow = |f| = vV (f(s, v)) = vV (f(v, t))
       In the example below: |f| = 3
Cancellation of (Net) Flow
   Let’s say that f(u, v) = 5 and c(u, v) = 8
       We can “ship” at most 3 units of flow from u  v
       => f(u, v) = 8 = c(u, v)
       But we can also “ship” up to 5 units of flow back from v 
        u
       => f(u, v) = 3 if we cancel 2 units of flow!
       Called flow cancellation
   If (v, u) E => f(v, u) <= c(v, u) = 0
       We are not allowed to have a positive flow from v  u
       But we are allowed to have negative flow!
       This negative flow is equal in absolute value with the
        positive value from u  v
Cancellation of (Net) Flow (2)
   Due to cancellation, a flow only gives us this so-
    called “net” effect. We cannot reconstruct actual
    “shipments” of products from a net flow.
   A flow f(u, v) = 5 and f(v, u) = -5 can mean either:

    5 units u  v     6 units u  v       8 units u  v
    0 units v  u     1 units v  u       3 units v  u
Flow Operations
   Extend the definition of the flow to sets of vertices
   X,Y  V
   f(X,Y) = ΣxXΣyY f(x,y) = total flow between X and Y

   Possible operations:
       XV f(X,X) = 0;
       X,YV f(X,Y) = -f(Y,X);
       X,Y,ZV si Y⊆X
           f(X Y, Z) = f(X,Z) - f(Y,Z);
           f(Z, X Y) = f(Z,X) - f(Z,Y);
       X,Y,ZV si XY=
           f(X Y, Z) = f(X,Z) + f(Y,Z);
           f(Z, X Y) = f(Z,X) + f(Z,Y)
       |f| = f(s,V) = f(V,t)
Examples (1)

                           X             1/2
                                    a               b
                     1/8                                  2/2

                               4/4            3/3
                 s                                            t

                                                        3/9   Y
                                c                   d
                                        3/5


                       f(X,Y) = ΣxXΣyY f(x,y)


f(X,X) = f(s,a) + f(a,s) + f(s,b) + f(b,s) + f(a,b) + f(b,a) = 0
f(X,Y) = f(b,c) + f(b,t) = -f(c,b) - f(t,b) = -f(Y,X)
                                        Proiectarea Algoritmilor 2010
Examples (2)

                           X              1/2
                                     a             b
                     1/8                                        2/2
                                                 Y
                 s             4/4           3/3                               t

                                c                                        3/9       Z
                                                 3/5           d


 X, Y, Z  V si Y ⊆ X
            f(X Y, Z) = f(X, Z) - f(Y, Z)
            f(Z, X Y) = f(Z, X) - f(Z, Y)

     f(X Y, Z) = 0 = f(b,t) + f(c,d) - f(b,t) - f(c,d) = f(X,Z) - f(Y,Z)
     f(Z, X Y) = 0 = f(t,b) + f(d,c) - f(t,b) - f(d,c) = f(Z,X) - f(Z,Y)

                                         Proiectarea Algoritmilor 2010
Examples (3)
                                                      Z
                         X             1/2
                                  a               b
                   1/8                                    2/2

               s             4/4            3/3              t

                                                       3/9   Y
                              c                   d
                                      3/5

 X, Y, Z  V si X  Y = 
            f(XY, Z) = f(X,Z) + f(Y,Z)
            f(Z, XY) = f(Z,X) + f(Z,Y)

    f(X Y, Z) = f(s,b) + f(a,b) + f(t,b) + f(d,c) = f(X,Z) + f(Y,Z)
    f(Z, X Y) = f(b,a) + f(b,s) + f(b,t) + f(c,d) = f(Z,X) + f(Z,Y)

                                      Proiectarea Algoritmilor 2010
Examples (4)

                                1/2
                            a               b
                 1/8                                2/2
                       4/4            3/3
             s                                            t

                                                  3/9
                        c                   d
                                3/5



  f(s, V) = f(V, t)

  f(s, V) = f(s,a) + f(s,b) = 5 = f(d,t) + f(b,t) = f(V, t)


                                 Proiectarea Algoritmilor 2010
Maximum Flow Problem
   Given G(V, E), c: E  R+, sV and tV
   Find the maximum flow that can be “transported” from s
    to t

   We start with an initial void flow on all edges
   We want to compute the net flow between each pair of
    vertices that determines a maximum flow!

   Various methods are used
   Only look at one of the simplest:
       Ford-Fulkerson and Edmonds-Karp
       Uses the notion of residual network
Residual Network
   Given G(V, E), c: E  R+, sV and tV
   Knowing a given flow in the graph, f: E  R

   We can compute the residual capacity for any pair of
    vertices:
       cf(u, v) = c(u, v) – f(u, v) >= 0 (due to capacity constraint)
       Residual means: how much flow we can still transport from u 
        v
   We can compute the residual network of the graph:
       It is a graph Gf = (V, Ef)
       Ef = {(u, v)  V x V | cf(u, v) > 0}
       It is a graph that contains edges that have residual capacity
        (that can still transport flow!)
       How many edges does Gf have? Find the minimum and the
        maximum value!
Residual Network – Example
Augmenting Path
   Any path from the residual graph admits more flow
    from the start vertex to the end vertex
   Therefore, we want to find a path from the source s
    to the sink t in the residual graph: augmenting path

   Look for p = s..t in Gf
   If we can find this augmenting path, compute how
    much flow we can send along it!
       We are constrained by the minimum residual capacity of
        any edge along the augmenting path
       cf(p) = min(cf(u, v) | (u, v) is on p)
Augmenting Path – Example
   p = <(s, w), (w, y), (y, z), (z, t)>
   Is it unique? Can you find another?
Summing Flows
   If the graph G has a flow f
   And we can find another flow f’
   We can sum up the two flows to find the new flow:
          f(u,v) + f’(u,v)
   The value of the new flow is |f| + |f’|

   Therefore, if we can find an augmenting path, p, in the residual
    network of a graph, we can also find another flow, fp: V x V 
    R
   fp(u,v) = cf(p) if (u,v) is on p
   fp(u,v) = -cf(p) if (v,u) is on p
   fp(u,v) = 0

   We can thus define a new flow in G: |f’| = |f| + |fp| > |f|
       Thus, we can improve the existing flow!
Graph Cuts
   A cut (S, T) of a flow network G(V, E) is a partitionof
    the vertices into two sets S (sS) and T = V  S (t
    T)
       Already know cuts for minimum spanning trees
   The flow across the cut is f(S, T)
   The capacity of the cut is c(S, T) = summing up the
    capacities of all the edges starting from a vertex in S
    and ending in a vertex in T
   A minimum cut in G is a cut whose capacity is
    minimum over all the possible cuts in G
Graph Cuts – Example
   S = {s, w, y}, T = {x, z, t}
   f(S, T) = f(w, x) + f (y, x) + f(y, z) = 2 + (-1) + 2 = 3
       Includes positive flow that goes either direction across the
        cut
       But only takes into account net flow that goes from any
        vertex in S to any vertex in T
   c(S, T) = c(w, x) + c(y, z) = 2 + 3 = 5
Max-Flow Min-Cut Theorem
   The following statements are equivalent:
       f is a maximum flow in G
       There is no augmenting path in Gf
       |f| = c(S, T) for all minimum cuts (S, T)

   This theorem gives us a method to compute the
    maximum flow: push flow along augmenting paths
    until you cannot find any
   This theorem also tells us how to compute a
    minimum cut once the maximum flow is known
       When f is a maximum flow, all the vertices reachable from
        s in Gf are in S, the others are in T => (S, T) is a minimum
        cut
Ford-Fulkerson Method
1.        Start with a void flow
2.        Compute the residual graph for the given flow
3.        If there is an augmenting path in this residual graph
     a)     Increase the flow along it
     b)     Go to step 2
4.        The maximum flow has been reached!

         The disadvantage of this method is that it does not
          specify how to look for the augmenting path in step 3
           Any method can be used
           Pick a random augmenting path
           Use any search algorithms to compute the path: DFS, BFS
           Be greedy: look for the fattest pipe each time! How?
Ford-Fulkerson Method (2)
   If the augmenting path is not picked smartly, then the
    worst case running time for the algorithm is O(E*|fmax|)
   Why?
   Each augmenting path has at most O(E) edges that will
    cause a change in the value of the flow along them at
    each step!
   Maximum number of steps is fmax if a each step cf(p)=1
   See the example on the next slides!
   It is not good that the running time depends on fmax
       What happens if the capacities of the edges are irrational
        number? => so is fmax
Exemplu Ford – Fulkerson (1)
                                                   1000         B   1000
    0/1000    B         0/1000
                                                                1
                                                                                   G
              0/1                      Gf      A                               D
A                                 D
                                                   1000         C   1000
    0/1000    C         0/1000
                                              Residual Path: A-B-C-D; Cf = 1
                                                   1
     1/1000   B         0/1000                              B     1000
                                                          999                      G
                  1/1                  Gf      A                1              D
A                                 D                                 999

    0/1000    C          1/1000                    1000         C          1

                                              Residual Path : A-B-C-D; Cf = 1
    1/1000    B         1/1000                  1          B      999 1
                                       Gf                 999                      G
A             0/1                 D            A                1          D
                                                                    999
    1/1000    C         1/1000                         999 C          1
                                                   1
              Proiectarea Algoritmilor 2010
                                               Residual Path : A-B-C-D; Cf = 1
Exemplu Ford – Fulkerson (2)

                                                    2
    2/1000      B      1/1000                                  B         999        1
                                                        998                                 G
                 1/1                Gf                         1
A                               D           A                                       D
                                                                       998
                                                    999        C
    1/1000      C      2/1000                   1                               2



                           …                Residual Path: A-C-B-D; Cf = 1


1000/1000       B      1000/1000                               B
                                                        1000                 1000
                 0/1
                                    Gf
A                               D             A                    1                    D
 1000/1000      C      1000/1000                        1000   C             1000

                                                        Residual Path: Ø
             How many steps are needed to reach the maximum flow?

                                    Proiectarea Algoritmilor 2010
Edmonds-Karp Algorithm
   Is a variant of the Ford-Fulkerson algorithm that
    guarantees to have a running time that is
    independent of fmax
   Always picks the augmenting path using BFS on Gf
       Source vertex for BFS is s
       You may stop when t is reached
   This way, the length of the augmenting paths is
    increasing when running the algorithm
       We always pick an augmenting path with an equal or
        greater length than the previous one
Edmonds-Karp Algorithm (2)
Edmonds–Karp(G, s, t)
   FOREACH (u,v) in E
      f(u,v) = f(v,u) = 0          // O(E)
   WHILE (1)                       // O(E*V)
           p = find-augmenting-path-using-bfs(G, f, s, t) // O(E)
           IF (p == null)
              BREAK
           cf(p) = min{cf(u,v) | (u,v) in p}  // O(E)
           FOREACH (u,v) in p                 // O(E)
                f(u,v) = f(u,v) + cf(p)
                f(v,u) = -f(u,v)
   RETURN f

    Complexity: O(V*E2)
Edmonds-Karp – Example
                                                            1000           B   1000
    0/1000      B         0/1000
                                                                           1
                                                                                                     G
                0/1                            Gf       A                                   D
A                                   D
                                                            1000           C       1000
    0/1000      C         0/1000
                                                      Residual Path: A-B-D; Cf = 1000
    1000/1000   B         1000/1000                             1000       B
                                                                                    1000
                    0/1                        Gf                          1
                                                                                                     G
A                                    D                  A                                   D
    0/1000      C          1/1000                            1000          C        1000

                                                       Residual Path: A-C-D; Cf = 1000
1000/1000       B          1000/1000
                                                              1000     B
                                                                                          1000
                    0/1                         Gf
A                                    D                                         1
                                                            A                                    D
    1000/1000   C          1000/1000                                1000           1000
                                                                             C
                                    Proiectarea Algoritmilor 2010
                                                                       Residual Path: Ø
Application: Maximum Bipartite Matching
   Bipartite graph: we can partition the graph into two sets
    of vertices L, R such as L U R = V and for all (u, v)E,
    uL, vR
   Matching: a subset of edges, M, such that for all vV at
    most an edge in M is incident in v
   Need to transform this problem into a maximum flow
Application: The Escape Problem
   http://webcourse.cs.technion.ac.il/234247/Spring2006/ho/
    WCFiles/The%20Escape%20Problem.pdf
Conclusions
   Flow networks are met very often in real life
   A lot of applications and interesting problems to be solved:
    maximum flow is only one of them

   It is important to know the main concepts of flow networks:
    positive flow, net flow, cuts, augmenting networks, etc.

   Ford-Fulkerson gives us a generic method to compute the
    maximum flow in a graph
       Edmonds-Karp is a variant of FF that runs in O(V*E2)
   There are other algorithms that work better

   A lot of problems can be solved using maximum flow, but you
    need to be able to “transform” them into a flow problem!
References
   CLRS – Chapter 26

   http://www.cs.princeton.edu/courses/archive/fall04/cos22
    6/lectures/maxflow.4up.pdf

   www.brics.dk/~sskyum/dSoegOpt/public_html/ek.pdf

More Related Content

PPT
Algorithm Design and Complexity - Course 8
PPTX
Algorithm Design and Complexity - Course 7
PPT
Algorithm Design and Complexity - Course 9
PDF
Topological sorting
DOC
BFS, Breadth first search | Search Traversal Algorithm
PDF
Longest common subsequence
PDF
Introduccio al calculo vectorial
PDF
Single source shortes path in dag
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 9
Topological sorting
BFS, Breadth first search | Search Traversal Algorithm
Longest common subsequence
Introduccio al calculo vectorial
Single source shortes path in dag

What's hot (20)

PPT
PDF
Minimum spanning tree
PPT
Algorithm Design and Complexity - Course 10
PPTX
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
PDF
PDF
Bellman ford
PDF
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
PPTX
Minimum spanning Tree
PDF
Derivatives Lesson Oct 14
PDF
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
PPTX
PPT
minimum spanning trees Algorithm
PDF
My presentation all shortestpath
PDF
A Generalization of QN-Maps
PPTX
Minimum spanning tree algorithms by ibrahim_alfayoumi
PDF
PPT
Linear transformation.ppt
PPTX
Divergence,curl,gradient
PPT
B.tech admission in india
PPTX
Linear transformations-thestuffpoint.com
Minimum spanning tree
Algorithm Design and Complexity - Course 10
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Bellman ford
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Minimum spanning Tree
Derivatives Lesson Oct 14
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
minimum spanning trees Algorithm
My presentation all shortestpath
A Generalization of QN-Maps
Minimum spanning tree algorithms by ibrahim_alfayoumi
Linear transformation.ppt
Divergence,curl,gradient
B.tech admission in india
Linear transformations-thestuffpoint.com
Ad

Similar to Algorithm Design and Complexity - Course 12 (20)

PPT
Network Information Processing
DOCX
23Network FlowsAuthor Arthur M. Hobbs, Department of .docx
PPTX
Graph theory
PPTX
Graph theory 1
PDF
07 - Graphs
PDF
Orchap10
PDF
Network analysis
PPT
Flow Network Talk
PPTX
Matrix Representation Of Graph
PPT
MaximumFlow.ppt
PDF
Dijkstra
PDF
Dijkstra's Algorithm
PDF
Dijkstra
PDF
lecture8-final.pdf ( analysis and design of algorithm)
KEY
Verification with LoLA: 7 Implementation
PPT
5 spatial filtering p1
PDF
Lec01 network flows
DOCX
Alg 2 trig item analysis seton nv sv
PDF
Ford Fulkerson wjgnejgbnhjbdreryjerhsrgjhegeujtgsruyw (1).pdf
PDF
MATHEON Center Days: Index determination and structural analysis using Algori...
Network Information Processing
23Network FlowsAuthor Arthur M. Hobbs, Department of .docx
Graph theory
Graph theory 1
07 - Graphs
Orchap10
Network analysis
Flow Network Talk
Matrix Representation Of Graph
MaximumFlow.ppt
Dijkstra
Dijkstra's Algorithm
Dijkstra
lecture8-final.pdf ( analysis and design of algorithm)
Verification with LoLA: 7 Implementation
5 spatial filtering p1
Lec01 network flows
Alg 2 trig item analysis seton nv sv
Ford Fulkerson wjgnejgbnhjbdreryjerhsrgjhegeujtgsruyw (1).pdf
MATHEON Center Days: Index determination and structural analysis using Algori...
Ad

More from Traian Rebedea (20)

PPTX
An Evolution of Deep Learning Models for AI2 Reasoning Challenge
PDF
AI @ Wholi - Bucharest.AI Meetup #5
PDF
Deep neural networks for matching online social networking profiles
PDF
Intro to Deep Learning for Question Answering
PPTX
What is word2vec?
PPT
How useful are semantic links for the detection of implicit references in csc...
PPT
A focused crawler for romanian words discovery
PPTX
Detecting and Describing Historical Periods in a Large Corpora
PDF
Practical machine learning - Part 1
PPT
Propunere de dezvoltare a carierei universitare
PPT
Automatic plagiarism detection system for specialized corpora
PPT
Relevance based ranking of video comments on YouTube
PPT
Opinion mining for social media and news items in Romanian
PPT
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...
PPT
Importanța algoritmilor pentru problemele de la interviuri
PPTX
Web services for supporting the interactions of learners in the social web - ...
PPT
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...
PPT
Conclusions and Recommendations of the Romanian ICT RTD Survey
PPT
Istoria Web-ului - part 2 - tentativ How to Web 2009
PPT
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009
An Evolution of Deep Learning Models for AI2 Reasoning Challenge
AI @ Wholi - Bucharest.AI Meetup #5
Deep neural networks for matching online social networking profiles
Intro to Deep Learning for Question Answering
What is word2vec?
How useful are semantic links for the detection of implicit references in csc...
A focused crawler for romanian words discovery
Detecting and Describing Historical Periods in a Large Corpora
Practical machine learning - Part 1
Propunere de dezvoltare a carierei universitare
Automatic plagiarism detection system for specialized corpora
Relevance based ranking of video comments on YouTube
Opinion mining for social media and news items in Romanian
PhD Defense: Computer-Based Support and Feedback for Collaborative Chat Conve...
Importanța algoritmilor pentru problemele de la interviuri
Web services for supporting the interactions of learners in the social web - ...
Automatic assessment of collaborative chat conversations with PolyCAFe - EC-T...
Conclusions and Recommendations of the Romanian ICT RTD Survey
Istoria Web-ului - part 2 - tentativ How to Web 2009
Istoria Web-ului - part 1 (2) - tentativ How to Web 2009

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Basic Mud Logging Guide for educational purpose
PDF
Pre independence Education in Inndia.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Business Ethics Teaching Materials for college
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Insiders guide to clinical Medicine.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Basic Mud Logging Guide for educational purpose
Pre independence Education in Inndia.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Business Ethics Teaching Materials for college
human mycosis Human fungal infections are called human mycosis..pptx
Microbial diseases, their pathogenesis and prophylaxis
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Insiders guide to clinical Medicine.pdf
01-Introduction-to-Information-Management.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

Algorithm Design and Complexity - Course 12

  • 1. Algorithm Design and Complexity Course 12
  • 2. Overview  Flow Networks  Positive Flow and Net Flow  Flow Properties  Maximum Flow  Minimum Cut  Ford-Fulkerson Method  Edmonds-Karp Algorithm  Sample Applications
  • 3. Flow Networks  Graphs that model materials that are transported through conduits  Each edge represents a conduit: it has a capacity that is an upper bound for the flow rate  Some vertices may produce materials: sources  Other vertices may consume materials: sinks
  • 4. Flow Networks (2)  The simplest version of flow networks:  Think of edges as pipes of different sizes (capacities)  The flow is the effective quantity of liquid that is transported through the pipes  Flow Networks are also called transportation networks  But you can transport a lot of things beside liquids, like commodities, information, electricity, traffic  There are some interesting and difficult problems for flow networks:  Maximum flow problem  Maximum flow with minimum cost problem  Circulation problem  Multi-commodity problem
  • 5. Flow Networks (3)  G(V, E) – directed graph  The capacity function c: E → R+  c(u, v) = the capacity of the edge (u, v)   (u,v)  E  c(u,v) = 0  Assume we have a single source vertex: s  Assume we have a single sink vertex: t  Assume we have a path s .. v .. t  vV
  • 6. Positive Flow  We can define a function called positive flow p: V x V  R+  Properties of positive flow:  Capacity constraint:  u, vV: p(u, v) <= c(u, v)  Flow conservation:  uV {s, t}: flow_in(u) = flow_out(u)  uV {s, t}: vV(p(v, u)) = vV(p(u, v))
  • 7. Positive Flow – Example  Notation on each edge: positive flow/capacity  Note that the two properties are respected  The flow is always lower than the capacity of each edge  The flow is conserved in each vertex  The source produces flow  The sink consumes flow
  • 8. Cancellation for Positive Flow  Note that in the previous example we have:  1 unit of flow from x  z  2 units of flow from z  x  We can cancel 1 unit of flow in both directions  0 units of flow from x  z  1 unit of flow from z  x  Flow conservation is still respected (we cancel the same amount of flow in each direction)  Capacity constraint is still respected (the flow is decreased)  In both cases, the “net flow” is 1 from z  x  Without loss of generality, we may always define a positive flow in a flow network such that for each (u, v), u,vV, we have positive flow either from u  v or from v  u, but not in both directions!
  • 9. Net Flow  We can define a function called net flow f: V x V  R  Properties of net flow:  Capacity constraint:  u, vV: f(u, v) <= c(u, v)  Flow conservation:  uV {s, t}: total_net_flow_in(u) = 0  uV {s, t}: vV(f(u, v)) = 0  Skew symmetry:  u, vV: f(u, v) = - f(v, u)
  • 10. Net Flow (2)  Conservation of net flow can be rewritten: vV (f(u, v)) = 0 As f(u, v) might be either positive or negative  uV {s, t}: vV and f(u,v)<0(f(u, v)) + vV and f(u,v)>0 (f(u, v)) =0  uV {s, t}: vV and f(v,u)>0(f(v, u)) = vV and f(u,v)>0 (f(u, v)) “total positive flow entering u” = “total positive flow leaving u” “flow in” = “flow out”
  • 11. Net Flow – Example  Only positive flow is illustrated in graphical representations  Nevertheless, this does not mean that the (negative) net flow does not exist in the other direction
  • 12. Positive Flow and Net Flow  Differences  Net flow satisfies skew symmetry  p(u, v) >= 0, while f(u, v) >= 0 or f(u, v) <= 0
  • 13. Positive Flow and Net Flow (2)  Equivalence  We can define net flow using only positive flow and vice- versa  f(u, v) = p(u, v) – p(v, u)  u, vV  Need to show that using this definition, the net flow still satisfies the three properties: capacity constraint, conservation and skew symmetry  p(u, v) = f(u, v) if f(u, v) > 0  p(u, v) = 0 otherwise  Need to show that this definition satisfies the two properties of positive flow: capacity constraint and conservation
  • 14. Positive Flow and Net Flow (3)  f(u, v) = p(u, v) – p(v, u)  u, vV  Capacity constraint: p(u, v) <= c(u, v) and p(v, u) >= 0 => f(u, v) = p(u, v) – p(v, u) <= p(u, v) <= c(u, v)  Flow conservation: vV (f(u, v)) = vV (p(u, v) – p(v, u)) = vV (p(u, v)) – vV (p(v, u)) = (from positive flow conservation) = 0  Skew symmetry: f(u, v) = p(u, v) – p(v, u) = –(p(v, u) – p(u, v)) = –f(v,u) Similar for the transformation from f(u, v)  p(u, v). Prove them at home!
  • 15. Value of the Flow  From now on, we shall only use net flow  Value of the flow = |f| = vV (f(s, v)) = vV (f(v, t))  In the example below: |f| = 3
  • 16. Cancellation of (Net) Flow  Let’s say that f(u, v) = 5 and c(u, v) = 8  We can “ship” at most 3 units of flow from u  v  => f(u, v) = 8 = c(u, v)  But we can also “ship” up to 5 units of flow back from v  u  => f(u, v) = 3 if we cancel 2 units of flow!  Called flow cancellation  If (v, u) E => f(v, u) <= c(v, u) = 0  We are not allowed to have a positive flow from v  u  But we are allowed to have negative flow!  This negative flow is equal in absolute value with the positive value from u  v
  • 17. Cancellation of (Net) Flow (2)  Due to cancellation, a flow only gives us this so- called “net” effect. We cannot reconstruct actual “shipments” of products from a net flow.  A flow f(u, v) = 5 and f(v, u) = -5 can mean either: 5 units u  v 6 units u  v 8 units u  v 0 units v  u 1 units v  u 3 units v  u
  • 18. Flow Operations  Extend the definition of the flow to sets of vertices  X,Y  V  f(X,Y) = ΣxXΣyY f(x,y) = total flow between X and Y  Possible operations:  XV f(X,X) = 0;  X,YV f(X,Y) = -f(Y,X);  X,Y,ZV si Y⊆X  f(X Y, Z) = f(X,Z) - f(Y,Z);  f(Z, X Y) = f(Z,X) - f(Z,Y);  X,Y,ZV si XY=  f(X Y, Z) = f(X,Z) + f(Y,Z);  f(Z, X Y) = f(Z,X) + f(Z,Y)  |f| = f(s,V) = f(V,t)
  • 19. Examples (1) X 1/2 a b 1/8 2/2 4/4 3/3 s t 3/9 Y c d 3/5 f(X,Y) = ΣxXΣyY f(x,y) f(X,X) = f(s,a) + f(a,s) + f(s,b) + f(b,s) + f(a,b) + f(b,a) = 0 f(X,Y) = f(b,c) + f(b,t) = -f(c,b) - f(t,b) = -f(Y,X) Proiectarea Algoritmilor 2010
  • 20. Examples (2) X 1/2 a b 1/8 2/2 Y s 4/4 3/3 t c 3/9 Z 3/5 d X, Y, Z  V si Y ⊆ X f(X Y, Z) = f(X, Z) - f(Y, Z) f(Z, X Y) = f(Z, X) - f(Z, Y) f(X Y, Z) = 0 = f(b,t) + f(c,d) - f(b,t) - f(c,d) = f(X,Z) - f(Y,Z) f(Z, X Y) = 0 = f(t,b) + f(d,c) - f(t,b) - f(d,c) = f(Z,X) - f(Z,Y) Proiectarea Algoritmilor 2010
  • 21. Examples (3) Z X 1/2 a b 1/8 2/2 s 4/4 3/3 t 3/9 Y c d 3/5 X, Y, Z  V si X  Y =  f(XY, Z) = f(X,Z) + f(Y,Z) f(Z, XY) = f(Z,X) + f(Z,Y) f(X Y, Z) = f(s,b) + f(a,b) + f(t,b) + f(d,c) = f(X,Z) + f(Y,Z) f(Z, X Y) = f(b,a) + f(b,s) + f(b,t) + f(c,d) = f(Z,X) + f(Z,Y) Proiectarea Algoritmilor 2010
  • 22. Examples (4) 1/2 a b 1/8 2/2 4/4 3/3 s t 3/9 c d 3/5 f(s, V) = f(V, t) f(s, V) = f(s,a) + f(s,b) = 5 = f(d,t) + f(b,t) = f(V, t) Proiectarea Algoritmilor 2010
  • 23. Maximum Flow Problem  Given G(V, E), c: E  R+, sV and tV  Find the maximum flow that can be “transported” from s to t  We start with an initial void flow on all edges  We want to compute the net flow between each pair of vertices that determines a maximum flow!  Various methods are used  Only look at one of the simplest:  Ford-Fulkerson and Edmonds-Karp  Uses the notion of residual network
  • 24. Residual Network  Given G(V, E), c: E  R+, sV and tV  Knowing a given flow in the graph, f: E  R  We can compute the residual capacity for any pair of vertices:  cf(u, v) = c(u, v) – f(u, v) >= 0 (due to capacity constraint)  Residual means: how much flow we can still transport from u  v  We can compute the residual network of the graph:  It is a graph Gf = (V, Ef)  Ef = {(u, v)  V x V | cf(u, v) > 0}  It is a graph that contains edges that have residual capacity (that can still transport flow!)  How many edges does Gf have? Find the minimum and the maximum value!
  • 26. Augmenting Path  Any path from the residual graph admits more flow from the start vertex to the end vertex  Therefore, we want to find a path from the source s to the sink t in the residual graph: augmenting path  Look for p = s..t in Gf  If we can find this augmenting path, compute how much flow we can send along it!  We are constrained by the minimum residual capacity of any edge along the augmenting path  cf(p) = min(cf(u, v) | (u, v) is on p)
  • 27. Augmenting Path – Example  p = <(s, w), (w, y), (y, z), (z, t)>  Is it unique? Can you find another?
  • 28. Summing Flows  If the graph G has a flow f  And we can find another flow f’  We can sum up the two flows to find the new flow: f(u,v) + f’(u,v)  The value of the new flow is |f| + |f’|  Therefore, if we can find an augmenting path, p, in the residual network of a graph, we can also find another flow, fp: V x V  R  fp(u,v) = cf(p) if (u,v) is on p  fp(u,v) = -cf(p) if (v,u) is on p  fp(u,v) = 0  We can thus define a new flow in G: |f’| = |f| + |fp| > |f|  Thus, we can improve the existing flow!
  • 29. Graph Cuts  A cut (S, T) of a flow network G(V, E) is a partitionof the vertices into two sets S (sS) and T = V S (t T)  Already know cuts for minimum spanning trees  The flow across the cut is f(S, T)  The capacity of the cut is c(S, T) = summing up the capacities of all the edges starting from a vertex in S and ending in a vertex in T  A minimum cut in G is a cut whose capacity is minimum over all the possible cuts in G
  • 30. Graph Cuts – Example  S = {s, w, y}, T = {x, z, t}  f(S, T) = f(w, x) + f (y, x) + f(y, z) = 2 + (-1) + 2 = 3  Includes positive flow that goes either direction across the cut  But only takes into account net flow that goes from any vertex in S to any vertex in T  c(S, T) = c(w, x) + c(y, z) = 2 + 3 = 5
  • 31. Max-Flow Min-Cut Theorem  The following statements are equivalent:  f is a maximum flow in G  There is no augmenting path in Gf  |f| = c(S, T) for all minimum cuts (S, T)  This theorem gives us a method to compute the maximum flow: push flow along augmenting paths until you cannot find any  This theorem also tells us how to compute a minimum cut once the maximum flow is known  When f is a maximum flow, all the vertices reachable from s in Gf are in S, the others are in T => (S, T) is a minimum cut
  • 32. Ford-Fulkerson Method 1. Start with a void flow 2. Compute the residual graph for the given flow 3. If there is an augmenting path in this residual graph a) Increase the flow along it b) Go to step 2 4. The maximum flow has been reached!  The disadvantage of this method is that it does not specify how to look for the augmenting path in step 3  Any method can be used  Pick a random augmenting path  Use any search algorithms to compute the path: DFS, BFS  Be greedy: look for the fattest pipe each time! How?
  • 33. Ford-Fulkerson Method (2)  If the augmenting path is not picked smartly, then the worst case running time for the algorithm is O(E*|fmax|)  Why?  Each augmenting path has at most O(E) edges that will cause a change in the value of the flow along them at each step!  Maximum number of steps is fmax if a each step cf(p)=1  See the example on the next slides!  It is not good that the running time depends on fmax  What happens if the capacities of the edges are irrational number? => so is fmax
  • 34. Exemplu Ford – Fulkerson (1) 1000 B 1000 0/1000 B 0/1000 1 G 0/1 Gf A D A D 1000 C 1000 0/1000 C 0/1000 Residual Path: A-B-C-D; Cf = 1 1 1/1000 B 0/1000 B 1000 999 G 1/1 Gf A 1 D A D 999 0/1000 C 1/1000 1000 C 1 Residual Path : A-B-C-D; Cf = 1 1/1000 B 1/1000 1 B 999 1 Gf 999 G A 0/1 D A 1 D 999 1/1000 C 1/1000 999 C 1 1 Proiectarea Algoritmilor 2010 Residual Path : A-B-C-D; Cf = 1
  • 35. Exemplu Ford – Fulkerson (2) 2 2/1000 B 1/1000 B 999 1 998 G 1/1 Gf 1 A D A D 998 999 C 1/1000 C 2/1000 1 2 … Residual Path: A-C-B-D; Cf = 1 1000/1000 B 1000/1000 B 1000 1000 0/1 Gf A D A 1 D 1000/1000 C 1000/1000 1000 C 1000 Residual Path: Ø How many steps are needed to reach the maximum flow? Proiectarea Algoritmilor 2010
  • 36. Edmonds-Karp Algorithm  Is a variant of the Ford-Fulkerson algorithm that guarantees to have a running time that is independent of fmax  Always picks the augmenting path using BFS on Gf  Source vertex for BFS is s  You may stop when t is reached  This way, the length of the augmenting paths is increasing when running the algorithm  We always pick an augmenting path with an equal or greater length than the previous one
  • 37. Edmonds-Karp Algorithm (2) Edmonds–Karp(G, s, t) FOREACH (u,v) in E f(u,v) = f(v,u) = 0 // O(E) WHILE (1) // O(E*V) p = find-augmenting-path-using-bfs(G, f, s, t) // O(E) IF (p == null) BREAK cf(p) = min{cf(u,v) | (u,v) in p} // O(E) FOREACH (u,v) in p // O(E) f(u,v) = f(u,v) + cf(p) f(v,u) = -f(u,v) RETURN f Complexity: O(V*E2)
  • 38. Edmonds-Karp – Example 1000 B 1000 0/1000 B 0/1000 1 G 0/1 Gf A D A D 1000 C 1000 0/1000 C 0/1000 Residual Path: A-B-D; Cf = 1000 1000/1000 B 1000/1000 1000 B 1000 0/1 Gf 1 G A D A D 0/1000 C 1/1000 1000 C 1000 Residual Path: A-C-D; Cf = 1000 1000/1000 B 1000/1000 1000 B 1000 0/1 Gf A D 1 A D 1000/1000 C 1000/1000 1000 1000 C Proiectarea Algoritmilor 2010 Residual Path: Ø
  • 39. Application: Maximum Bipartite Matching  Bipartite graph: we can partition the graph into two sets of vertices L, R such as L U R = V and for all (u, v)E, uL, vR  Matching: a subset of edges, M, such that for all vV at most an edge in M is incident in v  Need to transform this problem into a maximum flow
  • 40. Application: The Escape Problem  http://webcourse.cs.technion.ac.il/234247/Spring2006/ho/ WCFiles/The%20Escape%20Problem.pdf
  • 41. Conclusions  Flow networks are met very often in real life  A lot of applications and interesting problems to be solved: maximum flow is only one of them  It is important to know the main concepts of flow networks: positive flow, net flow, cuts, augmenting networks, etc.  Ford-Fulkerson gives us a generic method to compute the maximum flow in a graph  Edmonds-Karp is a variant of FF that runs in O(V*E2)  There are other algorithms that work better  A lot of problems can be solved using maximum flow, but you need to be able to “transform” them into a flow problem!
  • 42. References  CLRS – Chapter 26  http://www.cs.princeton.edu/courses/archive/fall04/cos22 6/lectures/maxflow.4up.pdf  www.brics.dk/~sskyum/dSoegOpt/public_html/ek.pdf