Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

5Explore
/ 5 lives left Get Premium

Study plan
Computer science → Algorithms and structures → Algorithms → Graph
algorithms
Map → Maximum flow algorithms

Ford-Fulkerson
Repeat what you've learned algorithm

0% completed
Find topic
Check to skip Go to practice

9 minutes reading

By now, you are already familiar with the problem of


finding the maximum flow in a network. Moreover, you
even know the gist of the most popular algorithms that
solve it. However, sometimes in order to use an
algorithm successfully in various real-life tasks, a deeper
understanding is needed. So, why not dive into exploring
the Ford-Fulkerson algorithm?

Algorithm
Before discussing the steps of the algorithm, let's
remember the definitions of some important structures
that are used in it.

First off, a flow network is a directed graph, which has a


source and a sink, and each edge of which has flow and
capacity. Secondly, a residual graph is used in this
algorithm. It is a graph, each edge of which has a
residual capacity assigned to it. Don't forget that the
residual capacity is the difference between the capacity
and the flow of an edge. Finally, an augmenting path is a
path from the source to the sink that consists only of
edges with a positive residual capacity.

Now, it is time to move on to the algorithm. Do you recall


from previous topics that the word flow can have many
https://hyperskill.org/learn/step/27203 1/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

meanings? For this very specific reason, this topic will


use different symbols to denote different types of flow.

Let f (v, w) be the flow of the edge that connects


vertices v and w . Additionally, faug stands for the flow of

an augmenting path found on every iteration of the


algorithm. The steps of the algorithm are as follows:

1. Initialize the flows of the edges with 0.


2. Find any augmenting path and determine its flow,
faug . Let x1 , x2 , ..., xn be the residual capacities of
​ ​

the edges this path consists of. Then obviously


faug = min(x1 , x2 , ..., xn ).Indeed, the flow that
​ ​ ​ ​

you send through the augmenting path equals the


minimal residual capacity of its edges. It is pretty
straightforward, isn't it?
3. Update the flows of the edges of the network. For
every edge that goes from node v to a node w :

f (v, w) = f (v, w) + faug ​

f (w, v) = f (w, v) − faug ​

4. Repeat steps 2 and 3, while there is an augmenting


path in the residual graph of the network.

That's it! Doesn't sound too difficult, does it?

However, as you may have noticed, there is one detail


that the algorithm lacks: it doesn't tell you how exactly
you should find an augmenting path. This is why
sometimes it is called not the Ford-Fulkerson algorithm,
but the Ford-Fulkerson method. Just as any other
method, it has some implementations, one of which is
the Edmonds-Karp algorithm.

This algorithm is basically the same as the Ford-


Fulkerson one. There is only one difference: in the
Edmonds-Karp algorithm, you push the flow through the
augmenting path that is the shortest and not just
through any one. In this case, the shortest means

https://hyperskill.org/learn/step/27203 2/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

consisting of the smallest number of edges. For example,


imagine that you have the following residual graph:

It has three augmenting paths: A-B-C-D, A-B-D, and A-D.


If you use the Ford-Fulkerson algorithm, you may choose
any one of them. In the Edmonds-Karp algorithm,
however, you would choose A-D, as it is the shortest one.

Example
Now, let's look at how the algorithm would work for the
following network. The numbers near each edge are
capacities.

As mentioned above, the Ford-Fulkerson algorithm


doesn't determine the exact way you should select each
augmenting path. Therefore, if you try to find the
maximum flow yourself, don't get scared if the flows of
the edges differ from the ones in the example at some
point. The main thing here is to find the correct total
flow of the network.

After each step, you will see a picture of the network on


the left and the residual graph on the right. In the
beginning of the algorithm, the flow of each edge in a
network equals 0.

https://hyperskill.org/learn/step/27203 3/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

So, let's choose the first augmenting path. Let it be A-B-


C-E. It goes through the edges AB, BC, and CE with
residual capacities 7, 8, and 5, respectively. Therefore,
the maximum flow you can send through it is 5, as it is
the smallest capacity among them.

Now that the flows of the edges are updated accordingly,


it's time to check if there are any more augmenting
paths. Indeed, there are several of them: A-B-E, A-C-B-E,
A-D-C-B-E. Let's look at A-B-E. The flow sent along the
path this time is the following:
fABE = min(2, 10) = 2.

You can see that no more flow may be pushed through


the edges AB and CE. You wouldn't be able to find any
https://hyperskill.org/learn/step/27203 4/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

other path if you looked only at the network itself.


However, there is a residual graph and you can still find
some augmenting paths in it. Let's choose the path A-C-
B-E. The flow equals min(4, 5, 8) = 4.

There is only one augmenting path left in the residual


graph, therefore, there are no options here. Let's push
the flow of 1 along the path A-D-C-B-E.

Hooray, no augmenting paths are left, which means that


you've finally found the maximum flow of the network,
which equals 12.

Complexity and correctness


Given f is the maximum flow and E is the number of
edges in a network, the complexity of the algorithm is
O(f E), which may look a bit unusual. Why is it like
this? Imagine that you have a network that looks like
this:

https://hyperskill.org/learn/step/27203 5/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

The worst-case scenario for it could be the following:


first you find an augmenting path A-B-C-D-E, and then
the path A-D-C-B-E.

As you can see, on each step you find an augmenting


path with the flow of 1. You repeat these steps on and
on, leading to f iterations in total. The complexity of
finding an augmenting path is O(E), therefore, the
complexity of the whole algorithm is O(f E). As for the
Edmonds-Karp algorithm, its complexity doesn't depend
on the maximum flow and equals O(V E 2 ), where V is
the number of vertices and E is the number of edges.

You already know how the algorithm works and what its
complexity is. However, have you wondered why the

https://hyperskill.org/learn/step/27203 6/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

algorithm works at all? In other words, why is the result


found at the end of the algorithms the maximum flow
possible?

First thing you need to understand is whether the


algorithm terminates at some point. Indeed, it does, as
the capacities of the edges are finite, so the flow that is
pushed through them can't be increased forever.

The next question: why is the total flow of the network


that the algorithm finds actually the biggest one
possible? Suppose it is not. Consequently, an
augmenting path can be found. However, in this case, the
algorithm would still be running, because, as you
remember, it stops only when there are no more
augmenting paths from the source to the sink. Therefore,
our assumption is wrong, and the flow found by the Ford-
Fulkerson algorithm is maximum for the network.

Fun fact: the Ford-Fulkerson algorithm may not


terminate if the capacities are not integers. It is
nontrivial and goes far beyond the scope of this
topic, however, if you wish, you can read about it in
this lecture.

Conclusion
To sum up, let's go through the main points of the topic:

The Ford-Fulkerson algorithm is used to solve the


maximum flow problem.
The idea behind the algorithm is the following: you
find the augmenting paths in a residual graph and
sum the flows of these paths. When there are no
such paths left, the algorithm stops.
As the way of choosing augmenting paths is not
specified, the Ford-Fulkerson algorithm is

https://hyperskill.org/learn/step/27203 7/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

sometimes called the Ford-Fulkerson method. The


Edmonds-Karp algorithm is an implementation of it.
The complexity of the Ford-Fulkerson algorithm is
O(f E), where f is the maximum flow and E is the
number of edges in a network.
The complexity of the Edmonds-Karp algorithm is
O(V E 2 ), where V is the number of vertices and E
is the number of edges.
For some networks with non-integer capacities, the
Ford-Fulkerson algorithm may not terminate.

Related topics

15 learners liked this piece of theory. 3 didn't like it. What about
you?

Start practicing Check to skip

Comments (1) Useful links (0) Show discussion

Career paths C++ Data Analysis


Beginner-friendly Generative AI Mobile
Top tracks Math
Python Frontend
Java SQL and Databases
JavaScript Scala
Kotlin Data Science
Go Bioinformatics

https://hyperskill.org/learn/step/27203 8/9
4/17/24, 11:05 PM Ford-Fulkerson algorithm · Hyperskill

Backend
Full catalog DevOps

Resources Hyperskill
Blog About
University Careers
For Content Creators
Subscription
For Business Support
Pricing Help Center
Terms

Become beta tester

Be the first to see what's new

https://hyperskill.org/learn/step/27203 9/9

You might also like