Skip to content

Maximum flow

networks

Level 3 of 3

Also called Edmonds-Karp algorithm and Ford-Fulkerson method.

how it works

Places keep their letters from the watch page's grid, A to D above and E to H below, so a map can skip a letter. On the watch page's map, a road's number is the room left on it. The number next to the source and the sink is what has been sent so far.

Maximum flow finds the most that can be sent from one place, the source, to another, the sink, along one-way roads that each have a limit. Search outward, nearest places first, for a way from source to sink with room at every step. Each place tries its neighbours in letter order. Send as much as the way's tightest step allows, then search again. A step usually goes forwards along a road with room. It may also go backwards along a road with something sent, which takes some of that back. When no way is left, what was sent is the most there is, and the places the source can still reach mark the narrowest cut, as question 4 shows. Nothing moves backwards on the map: taking back only sends the same amount by another road. This page counts the looks and the ways. A look goes from a place the search has reached to one it has not, whether or not it finds room. A road looked along in two searches counts twice. Where the code says sort, that only keeps letter order.

34333224ABCDEFG
The small map. Send from A to G. Each number is that road's limit, the most it can carry. The first way, drawn heavy, sends 3 and fills all three of its roads.
43322433ABCDEFG
What the second search can use, each step with its room. A to B, B to F and F to G are full, so each shows only its step back. B to C and C to D are tightest, 2 each. So F to B takes back 2 of the 3 sent from B to F, and B sends those 2 to C instead.
searchway it findsroom at its tightest stepsent in alllooks
1A, B, F, G336
2A, E, F, B, C, D, G258
3none56
Two ways and a last search, 20 looks in all. Search 1 looks from A to B and E, from B to C and F, from C to D and from F to G, which is 6. E adds none, as F is already reached. With no step backwards the second search finds nothing, and the run stops at 3.
Every road the grid allows, each carrying 99: 6 moments of its 7 steps, the steps between them left for the watch page
  1. step 1 of 7

    99999999999999999999999999A0BCDEFGH0

    Send as much as possible from A to H. Nothing is sent yet, so every road has all its room.

    what it remembers to know where it is
    way found
    not yet
    sent so far
    0

    Where it says not yet, nothing is held there at that moment.

    watch from here

  2. step 2 of 7

    99999999999999999999999999A0BCDEFGH0

    The shortest way with room on every road is A, B, G and H. Its tightest road has room for 99.

    what it remembers to know where it is
    way found
    ABGH
    sent so far
    0

    watch from here

  3. step 3 of 7

    09999999999999999099099A99BCDEFGH99

    Send 99 along A, B, G and H: each road on the way has that much less room, or gets that much back. 99 sent in all.

    what it remembers to know where it is
    way found
    ABGH
    sent so far
    99

    watch from here

  4. step 4 of 7

    09999999999999999099099A99BCDEFGH99

    The shortest way with room is A, F, G, B, C, D and H. It travels the road from B to G backwards, taking back some of what was sent along it. Its tightest step has room for 99.

    what it remembers to know where it is
    way found
    AFGBCDH
    sent so far
    99

    watch from here

  5. step 5 of 7

    0990990990990009999A198BCDEFGH198

    Send 99 along A, F, G, B, C, D and H: each road on the way has that much less room, or gets that much back. 198 sent in all.

    what it remembers to know where it is
    way found
    AFGBCDH
    sent so far
    198

    watch from here

  6. step 7 of 7

    0990990990990009999A198BCDEFGH198

    No way with room is left, so 198 is the most that can be sent. The places the source can still reach, A, E and F, are on one side of the narrowest cut.

    what it remembers to know where it is
    way found
    not yet
    sent so far
    198

    Where it says not yet, nothing is held there at that moment.

    watch from here

the steps, in plain words
  1. nothing sent yet: every road has all its roomPython lines 11, 12, 13, 14, 15, 16, 17, 18, 19 and 20
  2. find the shortest way from the source to the sink with room on every roadPython lines 21, 22, 23, 24, 25, 26, 27, 28, 29, 33, 34 and 35
  3. a road travelled backwards has as much room as was sent along itPython lines 15 and 40
  4. send as much as the tightest road on the way allows, and repeatPython lines 36, 37, 38, 39, 40 and 41
  5. no way with room left: nothing more can be sentPython line 30
  6. the places the source can still reach are one side of the narrowest cutPython lines 31 and 32
the same in Python

The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.

from collections import deque


def maximum_flow(roads, source, sink):
    """Find the most that can be sent from source to sink.

    roads is a list of (from, to, limit), each running one way, at most one
    between two places, each limit at least 1. Returns what is written at the
    two ends: how much left source and how much reached sink, one number.
    """
    room = {}  # what can still go from one place straight to another
    near = {}  # each place's neighbours, along a road running either way
    for a, b, limit in roads:
        room[a, b] = limit  # nothing sent yet: all its room
        room[b, a] = 0  # backwards, nothing sent yet to take back
        near.setdefault(a, []).append(b)
        near.setdefault(b, []).append(a)
    for names in near.values():
        names.sort()  # letter order
    total = 0
    while True:
        came_from = {source: source}  # search, nearest places first
        queue = deque([source])
        while queue and sink not in came_from:
            here = queue.popleft()
            for there in near[here]:
                if there not in came_from and room[here, there] > 0:
                    came_from[there] = here  # a step with room
                    queue.append(there)
        if sink not in came_from:  # no way with room left
            # came_from holds what source can still reach: one side of the narrowest cut
            return {source: total, sink: total}
        way = [sink]
        while way[-1] != source:  # walk back along the way found
            way.append(came_from[way[-1]])
        steps = list(zip(way[1:], way))
        amount = min(room[step] for step in steps)  # its tightest step
        for here, there in steps:
            room[here, there] -= amount  # that much less room this way
            room[there, here] += amount  # and that much more the other way
        total += amount

how it scales

The plot counts the looks on two maps of nn places, every limit 1, measured from 10 to 1,000 places. One is a line, from the first place to the last. The other is a fan: the source has a road to each place between, and each of those a road to the sink.

Each mark across or up is ten times the one before. The line climbs one mark up for each mark across: 100 at 100 places and 1,000 at 1,000, 10 times. The fan climbs two: 9,800 at 100 places and 998,000 at 1,000, about 102 times, near 10 times 10. A slope of 1 means it grows as nn, and a slope of 2 as n2n^2, as section 4's questions 1 and 2 derive.

roads looked along, ten times more at each mark up

a seconda minutean houra dayslope 2102104106108101010121014101102103104105106107a seconda minutean houra dayslope 210110210310410510610710810910101011101210131014101102103104105106107a seconda minutean houra dayslope 210110210310410510610710810910101011101210131014101102103104105106107

nn, n, ten times more at each mark to the right

  • a fan, every limit 1solid where measured by running it; dashed where its formula carries it on to ten millionslope 2: ten times n, 100 times the work
  • a line, every limit 1solid where measured by running it; dashed where its formula carries it on to ten million
how long a fan takes, at a billion looks a second
nnroads looked alongtime
1,000998,000about 998 microseconds
100,0009,999,800,000about 10 seconds
10,000,00099,999,980,000,000about 1.2 days

The roads looked along are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.

make these counts yourself

Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.

"""Count maximum flow's looks yourself, as the plot on this page does.

Run it with Python 3.12 or later. For each size n it runs two maps of n places,
every limit 1: a line from the first place to the last, and a fan, the source
with a road to each place between and each of those with a road to the sink. It
prints the looks on each. The counts are the same on any computer; only the time
differs.
"""

from collections import deque


def maximum_flow(roads, source, sink):
    """Find the most that can be sent from source to sink.

    roads is a list of (from, to, limit), each running one way, at most one
    between two places, each limit at least 1. Returns what is written at the
    two ends: how much left source and how much reached sink, one number.
    """
    room = {}  # what can still go from one place straight to another
    near = {}  # each place's neighbours, along a road running either way
    for a, b, limit in roads:
        room[a, b] = limit  # nothing sent yet: all its room
        room[b, a] = 0  # backwards, nothing sent yet to take back
        near.setdefault(a, []).append(b)
        near.setdefault(b, []).append(a)
    for names in near.values():
        names.sort()  # letter order
    total = 0
    while True:
        came_from = {source: source}  # search, nearest places first
        queue = deque([source])
        while queue and sink not in came_from:
            here = queue.popleft()
            for there in near[here]:
                if there not in came_from and room[here, there] > 0:
                    came_from[there] = here  # a step with room
                    queue.append(there)
        if sink not in came_from:  # no way with room left
            # came_from holds what source can still reach: one side of the narrowest cut
            return {source: total, sink: total}
        way = [sink]
        while way[-1] != source:  # walk back along the way found
            way.append(came_from[way[-1]])
        steps = list(zip(way[1:], way))
        amount = min(room[step] for step in steps)  # its tightest step
        for here, there in steps:
            room[here, there] -= amount  # that much less room this way
            room[there, here] += amount  # and that much more the other way
        total += amount


def count(roads, source, sink):
    """The looks the run makes: the same run, with a count added.

    A look goes from a place the search has reached to one it has not, and it
    counts whether or not it finds room.
    """
    room = {}
    near = {}
    for a, b, limit in roads:
        room[a, b] = limit
        room[b, a] = 0
        near.setdefault(a, []).append(b)
        near.setdefault(b, []).append(a)
    for names in near.values():
        names.sort()
    looks = 0
    while True:
        came_from = {source: source}
        queue = deque([source])
        while queue and sink not in came_from:
            here = queue.popleft()
            for there in near[here]:
                if there in came_from:
                    continue
                looks += 1
                if room[here, there] > 0:
                    came_from[there] = here
                    queue.append(there)
        if sink not in came_from:
            return looks
        way = [sink]
        while way[-1] != source:
            way.append(came_from[way[-1]])
        steps = list(zip(way[1:], way))
        amount = min(room[step] for step in steps)
        for here, there in steps:
            room[here, there] -= amount
            room[there, here] += amount


def names(n):
    """n names that sort in the order they count."""
    return [f"p{k:04}" for k in range(1, n + 1)]


def line(n):
    """Places one after another in letter order, each limit 1."""
    places = names(n)
    return [(places[k], places[k + 1], 1) for k in range(n - 1)], places[0], places[-1]


def fan(n):
    """The source, a road to each place between, and a road from each to the sink, each limit 1."""
    places = names(n)
    source, sink = places[0], places[-1]
    return [road for p in places[1:-1] for road in ((source, p, 1), (p, sink, 1))], source, sink


if __name__ == "__main__":
    print(f"{'n':>6} {'line':>8} {'fan':>8} {'n(n - 2)':>10}")
    for n in [10, 100, 1000]:
        print(f"{n:>6} {count(*line(n)):>8} {count(*fan(n)):>8} {n * (n - 2):>10}")

best, worst and average

Here best and worst only name two maps. Best is a line, with one way to send along, and worst is a fan, with one for each place between. The table below has a map cheaper than both and a map dearer than both. On any map the ceiling is O(nm2)O(nm^2), from question 6. No average is worked out, as there is no one way to pick a map.

A case is one kind of map, such as a fan. A bound says how fast that case's cost grows as places are added.

The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
besta line, every limit 1worsta fan, every limit 1
roads looked alongnn, from 2 places onn(n−2)n(n - 2), from 3 places on
ways sent along1n−2n - 2, from 3 places on
how the roads looked along grow in each case
tight, Θ\ThetaΘ(n)\Theta(n)Θ(n2)\Theta(n^2)
ceiling, OOO(n)O(n)O(n2)O(n^2)
floor, Ω\OmegaΩ(n)\Omega(n)Ω(n2)\Omega(n^2)

extra memory: Two numbers and two names for each road, and at most nn places each in the queue, the note of where each place came from, and the way.

Extra memory is the room it needs on top of the input itself, while it runs.

1111ABEF
A fan of 4, from A to F. Each way fills the two roads through one place between, and every search looks along both of A's roads.
31122ABEF
Dearer than a fan, on 4 places too, from A to F. Its searches make 3, 4, 5 and 5 looks, which is 17.
mapplacesroadswayslooks
a line of 44314
a fan of 44428
cheaper than both4201
dearer than both45317
the small map78220
Cheaper is A to B and H to D, from A to H, where the one look finds no way on.

where the formula comes from

Questions 1 and 2 are the counts the plot draws.

Question 1: how many looks, LL, does a run make on a line of nn places?
  1. the move: the first search

    the line:
    the first search makesn−1 looks\begin{gathered} \text{the first search makes} \\ n - 1 \text{ looks} \end{gathered}
    111ABCD
    A line of 4, from A to D.
    searchlooks alonglooks
    1A to B, B to C, C to D3
    2A to B, now full1
    Search by search.

    why it is allowed: Each place it reaches has one road onward. The road behind leads to a place already reached, so it gets no look. For 4 places, 3.

  2. the move: the last search

    the line:
    the second searchmakes 1 look\begin{gathered} \text{the second search} \\ \text{makes 1 look} \end{gathered}

    why it is allowed: The one way sends 1, which fills every road. So the first road has no room, the source has no other, and the search ends there.

  3. the move: add them

    the line:
    L=nL = n

    why it is allowed: Lines 1 and 2, n−1n - 1 and 1. For 4 places, 3 + 1, which is 4.

  4. the move: a ceiling

    the line:
    L≤nL \le n

    why it is allowed: Line 3 says they are equal, so this holds.

  5. the move: read the bound

    the line:
    L∈O(n)L \in O(n)

    why it is allowed: By line 4, with the fixed multiple 1. ∈\in reads is in, and O(n)O(n) means at most a fixed multiple of nn, from some nn on.

  6. the move: a floor

    the line:
    L≥nL \ge n

    why it is allowed: Line 3 again, the other way round.

  7. the move: read the bound

    the line:
    L∈Ω(n)L \in \Omega(n)

    why it is allowed: By line 6, with the fixed multiple 1. Ω(n)\Omega(n) means at least a fixed multiple of nn, above 0, from some nn on.

  8. the move: both together

    the line:
    L∈Θ(n)L \in \Theta(n)

    why it is allowed: Lines 4 and 6 bound LL by the same growth. Θ(n)\Theta(n) means both.

try it with a small nn: at n=4n = 4, line 3 gives L=4L = 4, which you can work out by hand

Question 2: how many looks, FF, does a run make on a fan of nn places?
  1. the move: the ways

    the line:
    n−2 ways, one througheach place between\begin{gathered} n - 2 \text{ ways, one through} \\ \text{each place between} \end{gathered}
    searchlooks from Alooks from the next placeway it finds
    121A, B, F
    221A, E, F
    320none
    The fan of 4. Search 2 still looks along A to B, now full. In all, 3 + 3 + 2, which is 8.

    why it is allowed: Each way sends 1, which fills its two roads, so no place between is on a second way. While one still has room, a search finds it. For 4 places, 2.

  2. the move: a search that finds one

    the line:
    n−1 looks: n−2 from thesource, then 1 more\begin{gathered} n - 1 \text{ looks: } n - 2 \text{ from the} \\ \text{source, then 1 more} \end{gathered}

    why it is allowed: The source looks along all its roads, full or not. Then the first place between with room looks along its road to the sink, and the search ends.

  3. the move: the last search

    the line:
    n−2 looks, all fromthe source\begin{gathered} n - 2 \text{ looks, all from} \\ \text{the source} \end{gathered}

    why it is allowed: Every road from the source is full now, so the search reaches nothing more.

  4. the move: add them

    the line:
    F=(n−2)(n−1)+(n−2)F = (n - 2)(n - 1) + (n - 2)

    why it is allowed: Lines 1 to 3, a search for each way and the last one. For 4 places, 2 times 3 plus 2, which is 8.

  5. the move: take out n - 2

    the line:
    F=n(n−2)F = n(n - 2)

    why it is allowed: Both parts hold n−2n - 2, once n−1n - 1 times and once more, so nn times in all.

  6. the move: a ceiling

    the line:
    F≤n2F \le n^2

    why it is allowed: n−2n - 2 is below nn. For 4 places, FF is 8, at most 16.

  7. the move: read the bound

    the line:
    F∈O(n2)F \in O(n^2)

    why it is allowed: By line 6, with the fixed multiple 1.

  8. the move: a floor

    the line:
    F≥n22F \ge \frac{n^2}{2}

    why it is allowed: From 4 places on, n−2n - 2 is at least half of nn. For 4 places, FF is 8, at least 8.

  9. the move: read the bound

    the line:
    F∈Ω(n2)F \in \Omega(n^2)

    why it is allowed: By line 8, with the fixed multiple a half.

  10. the move: both together

    the line:
    F∈Θ(n2)F \in \Theta(n^2)

    why it is allowed: Lines 6 and 8 bound FF by the same growth, only a fixed multiple apart.

try it with a small nn: at n=4n = 4, line 4 gives F=8F = 8, which you can work out by hand

Question 3: why does a step backwards break no rule? Let RR be the steps on a line of nn places.
  1. the move: one road, two steps

    the line:
    forwards: its roombackwards: what was sent\begin{gathered} \text{forwards: its room} \\ \text{backwards: what was sent} \end{gathered}
    road B to Froom forwardsroom backwards
    at the start30
    after way 103
    after way 221
    On the small map. Its limit is 3, and the two columns add up to 3 in every row.

    why it is allowed: Together they always add up to the road's limit. Sending along one takes from it and adds to the other.

  2. the move: within its limit

    the line:
    what was sent along a roadstays between 0 and its limit\begin{gathered} \text{what was sent along a road} \\ \text{stays between 0 and its limit} \end{gathered}

    why it is allowed: A way sends no more than its tightest step's room. So a forwards step cannot pass the limit, and a backwards step cannot take back more than was sent.

  3. the move: each place stays even

    the line:
    a place between sends onall it takes in\begin{gathered} \text{a place between sends on} \\ \text{all it takes in} \end{gathered}
    step instep outtakes insends on
    forwardsforwardsk morek more
    forwardsbackwardsk more and k lesssame
    backwardsforwardssamek less and k more
    backwardsbackwardsk lessk less
    Here k is the amount the way sends. Way 2 reaches F forwards and leaves it backwards, so F takes 2 more from E and 2 less from B, and still sends 3 to G.

    why it is allowed: A way meets a place once, with one step in and one step out. Whichever way each step points, the place's takings and sendings stay level, as the table shows.

  4. the move: more arrives

    the line:
    each way adds its amount towhat leaves and what arrives\begin{gathered} \text{each way adds its amount to} \\ \text{what leaves and what arrives} \end{gathered}

    why it is allowed: A way leaves the source once and never returns, and it reaches the sink once and stops there. So what left and what arrived stay one number.

  5. the move: so no rule breaks

    the line:
    a step backwardsbreaks no rule\begin{gathered} \text{a step backwards} \\ \text{breaks no rule} \end{gathered}

    why it is allowed: Lines 2 to 4 are the rules, and each way keeps them.

  6. the move: how many steps

    the line:
    R=2(n−1)R = 2(n - 1)

    why it is allowed: Line 1 gives each road two steps, with room or not, so mm roads give 2m2m. A line has n−1n - 1 roads. For 4 places, 6.

try it with a small nn: at n=4n = 4, line 6 gives R=6R = 6, which you can work out by hand

Question 4: why is what the run sends the most there is? Let CC be the size of the narrowest cut on a fan.
  1. the move: a cut

    the line:
    split the places in two: the sourceon one side, the sink on the other\begin{gathered} \text{split the places in two: the source} \\ \text{on one side, the sink on the other} \end{gathered}
    source's sideroads leaving itroad running backsize
    AA to B 3, A to E 4none7
    A, EA to B 3, E to F 3none6
    A, E, FA to B 3, F to G 3B to F6
    A, B, E, FB to C 2, F to G 3none5
    Four of the small map's cuts. 5 is the smallest of them all.

    why it is allowed: A cut's size is the limits of the roads from the source's side to the sink's side, added up. Roads running the other way do not count.

  2. the move: add up over the side

    the line:
    over the source’s side,add what leaves each place,less what arrives\begin{gathered} \text{over the source's side,} \\ \text{add what leaves each place,} \\ \text{less what arrives} \end{gathered}
    side A, B, Fleavesarrivesleaves less arrives
    A505
    B330
    F330
    in all1165
    The small map at the end. A sent 5.

    why it is allowed: A sending is any amounts on the roads, each within its limit, each place between sending on all it takes in. Question 3 shows the run's is one. So the sum is what the source sent.

  3. the move: only the crossing roads

    the line:
    that leaves what crosses out,less what crosses back\begin{gathered} \text{that leaves what crosses out,} \\ \text{less what crosses back} \end{gathered}

    why it is allowed: A road inside the side counts once leaving and once arriving, so it cancels. Here A to B's 3 and B to F's 1 cancel. A to E 2, B to C 2 and F to G 3 cross out, 7 in all, and E to F's 2 crosses back. 7 less 2 is 5.

  4. the move: any cut is a ceiling

    the line:
    what is sent is at mostany cut’s size\begin{gathered} \text{what is sent is at most} \\ \text{any cut's size} \end{gathered}

    why it is allowed: What crosses back is at least 0. What crosses out is at most the limits of those roads, which is the size. Here 5 is at most 7, and 7 at most the size, 9.

  5. the move: where the run stops

    the line:
    take what the source canstill reach as its side\begin{gathered} \text{take what the source can} \\ \text{still reach as its side} \end{gathered}
    32123222ABCDEFG
    The small map at the end, each number what was sent. A reaches E, F, then B by taking back 1 of what B sent to F. B to C and F to G leave the shade, at their limits.

    why it is allowed: No way is left, so the sink is not among those places. So this is a cut.

  6. the move: each road out is full

    the line:
    each road leavingthe side is full\begin{gathered} \text{each road leaving} \\ \text{the side is full} \end{gathered}

    why it is allowed: If it had room, a step along it would reach its far end, which would then be on the side.

  7. the move: each road in is empty

    the line:
    nothing was sent alonga road entering the side\begin{gathered} \text{nothing was sent along} \\ \text{a road entering the side} \end{gathered}

    why it is allowed: If something had been, a step backwards would reach its start, which would then be on the side. On the small map, no road enters the side.

  8. the move: so they meet

    the line:
    what was sent equalsthis cut’s size\begin{gathered} \text{what was sent equals} \\ \text{this cut's size} \end{gathered}

    why it is allowed: In line 3, what crosses out is the whole size, by line 6, and what crosses back is 0, by line 7. On the small map, 2 + 3, which is 5.

  9. the move: nothing sends more

    the line:
    nothing cansend more\begin{gathered} \text{nothing can} \\ \text{send more} \end{gathered}

    why it is allowed: By line 4 no sending passes this cut's size, and by line 8 the run sends all of it.

  10. the move: no cut is smaller

    the line:
    no cut is smallerthan this one\begin{gathered} \text{no cut is smaller} \\ \text{than this one} \end{gathered}

    why it is allowed: By line 4 every cut is at least what was sent, and by line 8 this cut is just that. Books call this the max-flow min-cut theorem.

  11. the move: on a fan

    the line:
    C=n−2C = n - 2

    why it is allowed: By line 10 the run's last side is the narrowest cut. On a fan the source's roads are all full at the end, so that side is the source alone, with n−2n - 2 roads of limit 1. For 4 places, 2.

try it with a small nn: at n=4n = 4, line 11 gives C=2C = 2, which you can work out by hand

Question 5: how many ways can a run need? Let HH be a ceiling on the times one step is tightest.
  1. the move: hops

    the line:
    a way of fewest steps goesone hop further at each step\begin{gathered} \text{a way of fewest steps goes} \\ \text{one hop further at each step} \end{gathered}
    hops tobefore way 1after way 1
    A00
    B13
    C24
    D35
    E11
    F22
    G36
    On the small map. No place's hops go down, and the sink goes from 3 hops to 6.

    why it is allowed: A place's hops are the fewest steps with room from the source to it. The search goes nearest first, so it finds a way of fewest steps. Each place on it has as many hops as steps taken to reach it, as a shorter route to it would make the whole way shorter. Way 1, A, B, F, G, reads 0, 1, 2, 3.

  2. the move: a new step

    the line:
    sending adds only stepsthat go one hop back\begin{gathered} \text{sending adds only steps} \\ \text{that go one hop back} \end{gathered}

    why it is allowed: A step can gain room only if the way travelled it the other way round. By line 1, that one went one hop further, so the new one goes one hop back.

  3. the move: suppose one falls

    the line:
    suppose some hops fall:take the lowest of those places\begin{gathered} \text{suppose some hops fall:} \\ \text{take the lowest of those places} \end{gathered}

    why it is allowed: Compare hops before a way is sent with hops after it. Suppose some places fall. Of those, take y, with the fewest hops after. Let x come just before y on a route of fewest steps after. x has one hop less than y, so x did not fall.

  4. the move: no place falls

    the line:
    no place’s hopsever fall\begin{gathered} \text{no place's hops} \\ \text{ever fall} \end{gathered}
    step x to yx aftery afterx beforey before
    had room before23at most 2at most 3
    is new23at most 2at most 1
    Say y has 3 hops after, so x has 2. In both rows y before is at most 3, so y did not fall.

    why it is allowed: Before, x had at most its hops after. If x to y had room before, y before had at most one more than x before. If x to y is new, y before had one less than x before, by line 2. Either way y did not fall, yet line 3 chose y as one that fell. So no place can fall.

  5. the move: tightest twice

    the line:
    between two times tightest,a step’s start risesby at least 2 hops\begin{gathered} \text{between two times tightest,} \\ \text{a step's start rises} \\ \text{by at least 2 hops} \end{gathered}

    why it is allowed: The tightest step, x to y, has no room left, with y one hop past x. It gets room again only when a way travels y to x, and then x is one hop past y. By line 4, y has not fallen, so x has risen by at least 2. On the small map, way 1 fills B to F with B at 1 hop, and way 2 travels F to B with B at 3.

  6. the move: how many times

    the line:
    H=n2H = \frac{n}{2}

    why it is allowed: A way meets no place twice, so a step's end has at most n−1n - 1 hops and its start at most n−2n - 2. Rising by at least 2 each time from 0 or more, that fits half of nn times at most. For 4 places, a start has 0, 1 or 2 hops, which fits 2 times at most.

  7. the move: every way has one

    the line:
    at most nm ways\begin{gathered} \text{at most } nm \text{ ways} \end{gathered}

    why it is allowed: Each way has a tightest step, and question 3 gives 2m2m steps. So there are at most 2m2m times half of nn ways, which is nmnm. On the small map, 7 times 8, which is 56, against the 2 it takes.

try it with a small nn: at n=4n = 4, line 6 gives H=2H = 2, which you can work out by hand

Question 6: what does the whole method cost? Let TT be the ceiling on looks, on a fan.
  1. the move: one search

    the line:
    a search looks alongeach road at most once\begin{gathered} \text{a search looks along} \\ \text{each road at most once} \end{gathered}
    the small map's searchlooksroads
    168
    288
    368
    No search looks along more than its 8 roads.

    why it is allowed: A look goes from a place the search has reached to one it has not, and the search leaves each place once. Take a road from B to F. The end the search leaves first looks along the road only if the other end is not yet reached. When the search leaves that other end, the first is already reached, so there is no second look.

  2. the move: every search

    the line:
    at most nm+1searches\begin{gathered} \text{at most } nm + 1 \\ \text{searches} \end{gathered}

    why it is allowed: Question 5's ways, and one last search that finds none. For the small map, 57.

  3. the move: their looks

    the line:
    at most (nm+1)mlooks\begin{gathered} \text{at most } (nm + 1)m \\ \text{looks} \end{gathered}

    why it is allowed: Line 1 gives at most mm looks a search. For the small map, 57 times 8, which is 456, and the run makes 20.

  4. the move: on a fan

    the line:
    T=2(n−2)(2n(n−2)+1)T = 2(n - 2)(2n(n - 2) + 1)

    why it is allowed: A fan has 2(n−2)2(n - 2) roads, put in for mm. For 4 places, 17 times 4, which is 68, and question 2 counts 8.

  5. the move: the rest of one search

    the line:
    the rest of a search is atmost a fixed multiple of m\begin{gathered} \text{the rest of a search is at} \\ \text{most a fixed multiple of } m \end{gathered}

    why it is allowed: A road is on two places' lists, so a search reads at most 2m2m names. A way walks back and sends along at most nn steps. A road has 2 ends and every place is on a road, so nn is at most 2m2m.

  6. the move: all of it

    the line:
    a fixed multiple of (nm+1)m,which is nm2+m\begin{gathered} \text{a fixed multiple of } (nm + 1)m, \\ \text{which is } nm^2 + m \end{gathered}

    why it is allowed: Lines 2, 3 and 5. For the small map, 448 + 8, which is 456.

  7. the move: read the bound

    the line:
    the whole methodin O(nm2)\begin{gathered} \text{the whole method} \\ \text{in } O(nm^2) \end{gathered}

    why it is allowed: mm is at most nm2nm^2, so all of it is at most a fixed multiple of nm2nm^2. Reading the roads is a fixed multiple of mm. Sorting the lists, 2m2m names in all, compares each name with at most every other, so a fixed multiple of m2m^2. Neither grows faster than nm2nm^2. A computer does each look or send in a fixed few actions.

  8. the move: in books

    the line:
    in books,O(VE2)\begin{gathered} \text{in books,} \\ O(VE^2) \end{gathered}

    why it is allowed: In books VV is the places and EE the roads.

try it with a small nn: at n=4n = 4, line 4 gives T=68T = 68, which you can work out by hand

count it yourself

A fan of 4, every limit 1. The watch page writes this map as A>B 1, A>E 1, B>F 1, E>F 1; A F. A>B 1 means a road from A to B with a limit of 1, and after the semicolon come the source and the sink. Each way takes two rows of the table, one finding it and one sending. It is counted when found. Only a search adds looks, the last one too, though it finds none.

A>B 1, A>E 1, B>F 1, E>F 1; A F, every step written down
stepwhat happensways sent alongroads looked along
1Send as much as possible from A to F. Nothing is sent yet, so every road has all its room.00
2The shortest way with room on every road is A, B and F. Its tightest road has room for 1.13
3Send 1 along A, B and F: each road on the way has that much less room, or gets that much back. 1 sent in all.13
4The shortest way with room on every road is A, E and F. Its tightest road has room for 1.26
5Send 1 along A, E and F: each road on the way has that much less room, or gets that much back. 2 sent in all.26
6Search once more for a way from A to F with room on every road. There is none.28
7No way with room is left, so 2 is the most that can be sent. The only place the source can still reach, A, is on one side of the narrowest cut.28

In all, 8 roads looked along. The formula from section 4, n(n−2)n(n - 2), gives F=8F = 8 at n=4n = 4: the same.

where you meet it

It began with rail lines, and it still finds the cheapest place to split a network.

  • Rail lines

    Lester Ford and Delbert Fulkerson's 1956 paper opens on a rail network between two cities, a question from T. E. Harris. Harris's report with F. S. Ross, for the US Air Force, looked for the narrowest cut of the Soviet rail network.

    source: L. R. Ford and D. R. Fulkerson, Maximal flow through a network (opens in a new tab), Canadian Journal of Mathematics 8, A. Schrijver, a history of the problem (opens in a new tab), Mathematical Programming 91(3), and the books' volume four, chapter 42

  • Selecting part of a photograph

    You pick a few pixels to keep and a few to drop. A new source has a road to each one to keep, and each one to drop has a road to a new sink. Each pixel is a place, joined to its neighbours by roads whose limits grow the more alike they look. The narrowest cut is the cheapest border to draw.

    source: Y. Boykov and M.-P. Jolly, Interactive graph cuts (opens in a new tab), ICCV 2001, and the books' volume four, chapter 42

  • networkx and SciPy

    Both, ready-made Python code, offer this page's method as edmonds_karp. Unless told otherwise, their maximum_flow takes a different method, preflow_push in networkx and dinic in SciPy.

    source: J. Edmonds and R. M. Karp, their 1972 paper (opens in a new tab), Journal of the ACM 19(2), the networkx source, edmondskarp.py (opens in a new tab) and maxflow.py (opens in a new tab), and the SciPy source, _flow.pyx (opens in a new tab)

what is used instead

For large maps, use ready-made code's faster method.

how it compares

This page's method is the Edmonds-Karp algorithm, one kind of Ford-Fulkerson method. With no taking back, a run can stop short. The small map stops at 3 if A, B, F, G goes first, and reaches 5 if A, E, F, G does. With taking back and any way, each way sends at least 1 when the limits are whole, so the run ends. But its ways can grow with the limits, as the map below shows.

methodwhich waytakes backways at most
no taking backany with roomnocan stop below the most
Ford and Fulkersonany with roomyesthe amount sent, with whole limits
Edmonds and Karp, this page'sfewest stepsyesnmnm, whatever the limits
The last row's ceiling uses no road's limit.
3020202202030ABCEFG
Taking any way with room, a run can go A, B, F, G, then A, E, F, B, C, G, and those two in turn again, each sending 2. B to F goes forwards, then backwards. Sending all 42 takes 21 ways. Fewest steps takes 3, sending 20, 2 and 20. Times 3, all but B to F, 61 ways against 3.