Professional Documents
Culture Documents
Unit 7: Graphs
A graph is a collection of vertices (or nodes) and the connections (edges) between them.
Generally, no restriction is imposed on the number of vertices in the graph or on the number
of connections one vertex can have to other vertices.
A simple graph G = (V, E) consists of a nonempty set V of vertices and a possibly empty
set E of edges, each edge being a set of two vertices from V. The number of vertices and
edges is denoted by |V| and |E |, respectively.
Graphs are versatile data structures that can represent a large number of different situations
and events from diverse domains.
Directed edge
ordered pair of vertices (u, v)
first vertex u is the origin second vertex v is the destination
(u,v) ≠ (v,u)
Undirected edge:
unordered pair of vertices (u, v)
(u,v) = (v,u)
Directed graph
All the edges are directed
Also called as digraph
1
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Undirected graph
All the edges are undirected
When an edge connects two vertices, the vertices are said to be adjacent to each other and the
edge is incident on both vertices.
Path : a,b,c,d,e
Circuit: a,b,c,d,e,b,a
Cycle: a,b,c,d,e,a
A graph is connected if there is a path from every vertex to every other vertex. In connected
graph, at least one path exists between every pair of vertices. Example:
2
Collected by Bipin Timalsina
DSA with Java/Unit- 7
A graph in which there does not exist any path between at least one pair of vertices is
called as a disconnected graph. Example:
A graph is called a weighted graph if each edge has an assigned number. Depending on
the context in which such graphs are used, the number assigned to an edge is called its
weight, cost, distance, length, or some other name
A graph with n vertices is called complete and is denoted Kn if for each pair of distinct
vertices there is exactly one edge connecting them; that is, each vertex can be connected to
any other vertex
3
Collected by Bipin Timalsina
DSA with Java/Unit- 7
A (self ) loop is an edge that connects a vertex to itself.(edge that has same endpoints)
Two edges are parallel if they connect the same pair of vertices
A multigraph is a graph in which two vertices can be joined by multiple edges.
Contains some parallel edges but no self- loops.
Graph Representation
In graph theory, a graph representation is a technique to store graph into the memory of computer.
There are various ways to represent a graph. Some of them are :
Adjacency list
Adjacency matrix
Incident matrix
Adjacency List Representation
Adjacency List specifies all vertices adjacent to each vertex of the graph.
Every vertex of the graph contains list of its adjacent vertices.
Each node is holding a list of nodes, which are directly connected with that vertices.
4
Collected by Bipin Timalsina
DSA with Java/Unit- 7
This list can be implemented as a table, in which case it is called a star representation
(Figure 1b).
The list can also be implemented as linked list.(Figure 1c)
Example:
5
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Adjacency matrix
An adjacency matrix of graph G = (V,E) is a binary |V| × |V| matrix such that each entry of
this matrix
Example:
6
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Incident matrix
An incidence matrix of graph G = (V, E) is a |V| × |E| matrix such that
Example:
Graph Traversals
Process of visiting every node of a graph exactly once.
Two popular graph traversal techniques :
o DFS (Depth First Search) Traversal
o BFS (Breadth First Search) Traversal
7
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Example:
Let 0 be the starting node or source node. First, we mark it as visited and add it to the visited
list. Then we push all its adjacent nodes in the stack.
8
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Next, we take one of the adjacent nodes to process i.e. the top of the stack which is 1. We mark it
as visited by adding it to the visited list. Now look for the adjacent nodes of 1. As 0 is already in
the visited list, we ignore it and we visit 2 which is the top of the stack.
Next, we mark node 2 as visited. Its adjacent node 4 is added to the stack.
Next, we mark 4 which is the top of the stack as visited. Node 4 has only node 2 as its adjacent
which is already visited, hence we ignore it.
At this stage, only node 3 is present in the stack. Its adjacent node 0 is already visited, hence we
ignore it. Now we mark 3 as visited.
9
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Now the stack is empty and the visited list shows the sequence of the depth-first traversal of the
given graph.
10
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Example:
Let 0 be the starting node or source node. First, we add to the visited list and all its adjacent nodes
in the queue.
Next, we take one of the adjacent nodes to process i.e. 1. We mark it as visited by removing it
from the queue and put its adjacent nodes in the queue (2 and 3 already in queue). As 0 is already
visited, we ignore it.
Next, we dequeue node 2 and mark it as visited. Then, its adjacent node 4 is added to the queue.
11
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Next, we dequeue 3 from the queue and mark it as visited. Node 3 has only one adjacent node i.e.
0 which is already visited. Hence, we ignore it.
At this stage, only node 4 is present in the queue. Its adjacent node 2 is already visited, hence we
ignore it. Now we mark 4 as visited.
Next, the sequence present in the visited list is the breadth-first traversal of the given graph.
12
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Shortest Paths
Finding the shortest path is a classical problem in graph theory, and a large number of
different solutions have been proposed.
Edges are assigned certain weights representing, for example, distances between cities,
times separating the execution of certain tasks, costs of transmitting information between
locations, amounts of some substance transported from one place to another, and so on.
When determining the shortest path from vertex v to vertex u, information about distances
between intermediate vertices w has to be recorded. This information can be recorded as a
label associated with these vertices, where the label is only the distance from v to w or the
distance along with the predecessor of w in this path.
The methods of finding the shortest path rely on these labels.
Depending on how many times these labels are updated, the methods solving the shortest
path problem are divided in two classes: label-setting methods and label-correcting
methods.
For label-setting methods, in each pass through the vertices still to be processed, one
vertex is set to a value that remains unchanged to the end of the execution. This, however,
limits such methods to processing graphs with only positive weights.
The second category includes label-correcting methods, which allow for the changing of
any label during application of the method. The latter methods can be applied to graphs
with negative weights and with no negative cycle—a cycle composed of edges with
weights adding up to a negative number—but they guarantee that, for all vertices, the
current distances indicate the shortest path only after the processing of the graph is finished.
Most of the label-setting and label-correcting methods, however, can be subsumed to the
same form, which allows finding the shortest paths from one vertex to all other vertices
13
Collected by Bipin Timalsina
DSA with Java/Unit- 7
In Dijkstra’s algorithm, a number of paths p1, . . . , pn from a vertex v are tried, and each
time, the shortest path is chosen among them, which may mean that the same path pi can
be continued by adding one more edge to it.
But if pi turns out to be longer than any other path that can be tried, pi is abandoned and
this other path is tried by resuming from where it was left and by adding one more edge to
it.
14
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Because paths can lead to vertices with more than one outgoing edge, new paths for
possible exploration are added for each outgoing edge. Each vertex is tried once, all paths
leading from it are opened, and the vertex itself is put away and not used anymore. After
all vertices are visited, the algorithm is finished.
Dijkstra’s algorithm is obtained from the generic method by being more specific about which
vertex is to be taken from toBeChecked so that the line
v = a vertex in toBeChecked;
is replaced by the line
v = a vertex in toBeChecked with minimal currDist(v);
Dijkstra’s Algorithm is a very famous greedy algorithm. It is used for solving the single source
shortest path problem. It computes the shortest path from one particular source node to all other
remaining nodes of the graph.
Dijkstra’s algorithm is not general enough in that it may fail when negative weights are used in
graphs.
15
Collected by Bipin Timalsina
DSA with Java/Unit- 7
The list toBeChecked is initialized to {a b . . . j}; the current distances of all vertices are
initialized to a very large value, marked here as ∞; and in the first iteration, the current
distances of d’s neighbors are set to numbers equal to the weights of the edges from d.
16
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Now, there are two candidates for the next try, a and h, because d was excluded from
toBeChecked.
In the second iteration, h is chosen, because its current distance is minimal, and then the
two vertices accessible from h, namely, e and i, acquire the current distances 6 and 10.
Now, there are three candidates in toBeChecked for the next try, a, e, and i. a has the
smallest current distance, so it is chosen in the third iteration. Eventually, in the tenth
iteration, toBeChecked becomes empty and the execution of the algorithm completes
Ford Algorithm
One of the first label-correcting algorithms was devised by Lester Ford. Like Dijkstra’s
algorithm, it uses the same method of setting current distances, but Ford’s method does not
permanently determine the shortest distance for any vertex until it processes the entire
graph.
It is more powerful than Dijkstra’s method in that it can process graphs with negative
weights (but not graphs with negative cycles).
The graph includes edges with negative weights. The table indicates iterations of the while loop
and current distances updated in each iteration, where one iteration is defined as one pass through
the edges. Note that a vertex can change its current distance during the same iteration. However,
at the end, each vertex of the graph can be reached through the shortest path from the starting
vertex (vertex c in this example).
17
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Although the task of finding all shortest paths from any vertex to any other vertex seems to be
more complicated than the task of dealing with one source only, a method designed by Stephen
Warshall and implemented by Robert W. Floyd and P. Z. Ingerman does it in a surprisingly simple
way, provided an adjacency matrix is given that indicates all the edge weights of the graph (or
digraph). The algorithm is also known as Floyd Warshall algorithm or WFI algorithm.
Note: The graph can include negative weights.
18
Collected by Bipin Timalsina
DSA with Java/Unit- 7
The outermost loop refers to vertices that may be on a path between the vertex with index j and
the vertex with index k. For example, in the first iteration, when i = 1, all paths vj . . . v1 . . . vk are
considered, and if there is currently no path from vj to vk and vk is reachable from vj, the path is
established, with its weight equal to p = weight(path(vjv1)) + weight(path(v1vk)), or the current
weight of this path, weight(path(vjvk)), is changed to p if p is less than weight(path(vjvk)).
Follow the steps below to find the shortest path between all the pairs of vertices.
19
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Create a matrix A0 of dimension n×n where n is the number of vertices. The row and the
column are indexed as i and j respectively. i and j are the vertices of the graph.
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex.
If there is no path from ith vertex to jth vertex, the cell is left as infinity.
Now, create a matrix A1 using matrix A0. The elements in the first column and the first row
are left as they are. The remaining cells are filled in the following way.
Let k be the intermediate vertex in the shortest path from source to
destination. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).
20
Collected by Bipin Timalsina
DSA with Java/Unit- 7
In this step, k is vertex 1. We calculate the distance from source vertex to destination
vertex through this vertex k.
Calculate the distance from the source vertex to destination vertex through this vertex
k (here k is 1)
For A1[2, 4], the direct distance from vertex 2 to 4 is infinity and the sum of the distance
from vertex 2 to 4 through vertex (i.e. from vertex 2 to 1 and from vertex 1 to 4) is 15.
Since 15 < infinity , A1[2, 4] is filled with 15.
Similarly, A2 is created using A1. The elements in the second column and the second row are
left as they are.
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as
in step 2.
Calculate the distance from the source vertex to destination vertex through this vertex 2
21
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Calculate the distance from the source vertex to destination vertex through this vertex 4
22
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Cycle Detection
Floyd Warshall’s algorithm also allows us to detect cycles if the diagonal is initialized to
∞ and not to zero. If any of the diagonal values are changed, then the graph contains a
cycle.
Cycle in a graph can be detected using DFS
23
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Example:
24
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Visited List
A is popped and stored in visited list since A is not in Visited list. Here an adjacent node of A is
B so, B is pushed to stack
Stack
B
Visited List
A
B is now popped which is not in visited list so add B to visited list. C is adjacent node of B which
is now added to stack
Stack
C
Visited List
A B
C is now popped which is not in visited list so add C to visited list. E is adjacent node of C which
is now added to stack
Stack
E
Visited List
A B C
25
Collected by Bipin Timalsina
DSA with Java/Unit- 7
E is now popped which is not in visited list so add E to visited list. D and F are adjacent node of
which are now added to stack
Stack
F D
Visited List
A B C E
F is now popped which is not in visited list so add F to visited list. F does not have adjacent nodes
so no nodes are pushed to stack.
Stack
D
Visited List
A B C E F
D is now popped which is not in visited list so add D to visited list. B is adjacent nodes of D so B
is pushed to stack.
Stack
B
Visited List
A B C E F D
26
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Spanning Trees
The Spanning tree of a graph is a subgraph that contains all the vertices connected by
minimum number of edges and is also a tree.
A spanning tree does not have any cycle
A graph can have more than one spanning tree.
A spanning tree has n −1 edges. Where n is the number of vertices
From a complete graph, by removing maximum (e - n + 1) edges, we can construct a
spanning tree. Here e is number of edges and n is number of vertices in a graph.
A complete undirected graph can have nn-2 number of spanning trees where n is the number
of vertices in the graph
All possible spanning trees of a graph G, have the same number of edges and vertices.
Removing one edge from the spanning tree will make the graph disconnected
Adding one edge to the spanning tree will create a circuit or loop
If all the vertices are connected in a graph, then there exists at least one spanning tree.
Spanning trees are a subset of connected graph and disconnected graphs do not have
spanning tree
27
Collected by Bipin Timalsina
DSA with Java/Unit- 7
28
Collected by Bipin Timalsina
DSA with Java/Unit- 7
The graph contains 5 vertices and 7 edges. So, the minimum spanning tree formed will be having
(5– 1) = 4 edges.
The weights of the edges are:
Edges AE AD AC AB BC CD DE
Weights 5 10 7 1 3 4 2
Now adding DE to the MST; because adding this does not form a cycle
29
Collected by Bipin Timalsina
DSA with Java/Unit- 7
4
Now, number of edges in MST has become 4 (i.e. |tree| = |v|-1). Here, all the vertices are included
in MST So we stop here.
. Here the sum of all edge weights in this MST = 1+2+3+4 = 10
30
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Prim’s Algorithm
Like Kruskal’s algorithm, Prim’s algorithm is also a Greedy algorithm.
It starts with an empty spanning tree.
In contrast to Kruskal’s algorithm that starts with graph edges, Prim’s algorithm starts with
a vertex. We start with one vertex and keep on adding edges with the least weight till all
the vertices are covered.
The idea is to maintain two sets of vertices. The first set contains the vertices already
included in the MST, the other set contains the vertices not yet included.
At every step, it considers all the edges that connect the two sets, and picks the minimum
weight edge from these edges.
Prim’s algorithm works by maintaining two sets R and (V-R) where V is the set of vertices
and R is a sub set of V.
It starts with arbitrary vertex and checks all the edges from that vertex to other vertices
and minimum cost edge is taken at next item to be added in R.
Initially R is empty and starts with taking a node in R.
31
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Algorithm
PrimsMST(G){
T = ϕ;
R = {vi}; // vi is an arbitrary node from set of all vertices,V
While( R!=V){
choose edge e = (u,v) where u is from R , v is from (V-R) and e is of minimum weight;
add v to R i.e. R = R ∪ {v} and e to T i.e T = T ∪ e
}
return T;
}
32
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Step 2:
R V-R Possible edges with weights (From R Edge with
to V-R) that are not included in MST minimum weight
{B} { A, C, D, E} BC = 10 ; BD = 4 BD
Minimum cost edge BD is added to MST and vertex D is added to R . So, now R = {B, D} and
V-R = { A, C, E}
4
D
Step 3:
R V-R Possible edges with weights (From R to Edge with
V-R) that are not included in MST minimum weight
{ B, D} { A, C, E} BC = 10 ; CD = 2; DE = 1 DE
Minimum cost edge DE is added to MST and vertex E is added to R . So, now R = {B, D, E} and
V-R = { A, C }
33
Collected by Bipin Timalsina
DSA with Java/Unit- 7
B
E
4 1
D
Step 4:
R V-R Possible edges with weights (From R to Edge with
V-R) that are not included in MST minimum weight
{ B, D, E} { A, E} BC = 10 ; CD = 2; CE = 6 CD
Minimum cost edge CD is added to MST and vertex C is added to R . So, now R = {B, D, E, C}
and V-R = { A }
C
B
2 E
4 1
D
Step 5:
R V-R Possible edges with weights (From R to Edge with
V-R) that are not included in MST minimum weight
{ B, D, E, C} {A} BC = 10 ; CE = 6; CA= 3 CA
Minimum cost edge CA is added to MST and vertex A is added to R . So, now
R = {B, D, E, C, A} and V-R = { }
Here V = R so, we stop.
34
Collected by Bipin Timalsina
DSA with Java/Unit- 7
3
C
B 2
E
4 1
D
Connectivity
In many problems, we are interested in finding a path in the graph from one vertex to any
other vertex. For undirected graphs, this means that there are no separate pieces, or
subgraphs, of the graph; for a digraph, it means that there are some places in the graph to
which we can get from some directions but are not necessarily able to return to the starting
points.
In other words, if this vertex is removed from the graph (along with incident edges), then
there is no way to find a path from a to b, which means that the graph is split into two
separate subgraphs. Such vertices are called articulation points, or cut-vertices. Vertices
a and b in following figure are examples of articulation points.
If an edge causes a graph to be split into two subgraphs, it is called a bridge or cut-edge,
as for example, the edge (bc) in above figure.
Connected subgraphs with no articulation points or bridges are called blocks, or—when
they include at least two vertices—biconnected components
36
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Topological Sort
In many situations, there is a set of tasks to be performed. For some pairs of tasks, it matters
which task is performed first, whereas for other pairs, the order of execution is unimportant.
The dependencies between tasks can be shown in the form of a digraph. A topological sort
linearizes a digraph. The digraph must not include a cycle; otherwise, a topological sort is
impossible.
Topological Sort or Topological Ordering of a directed graph is a linear ordering of
its vertices such that for every directed edge uv from vertex u to vertex v, u comes
before v in the ordering.
A topological ordering is possible if and only if the graph has no directed cycles, that is,
if it is a directed acyclic graph (DAG)
DAG is a directed graph without any cycle.
The topological ordering of a DAG is ordering of its nodes in such a way that each node
comes after it’ s predecessor and no successor of any node comes before that node.
Algorithm (Source Removal Algorithm) for topological sorting
Initially, indegree is computed for all vertices, starting with the vertices which are having
indegree 0. To keep track of vertices with indegree zero we can use a queue. All vertices
of indegree 0 are placed on queue.
While the queue is not empty, a vertex v is removed, and all edges adjacent to v have their
indegrees decremented. A vertex is put on the queue as soon as its indegree falls to 0.
The topological ordering is the order in which the vertices DeQueue.
37
Collected by Bipin Timalsina
DSA with Java/Unit- 7
38
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Step-01:
Write in-degree of each vertex-
Step-02:
Vertex-A has the in-degree zero.
So, remove vertex-A and its associated edges.
Add A to Q
Now, update the in-degree of other vertices.
Q= A
39
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Step-03:
Vertex-B has in-degree Zero.
So, remove vertex-B and its associated edges.
Add B to Q
Now, update the in-degree of other vertices.
Q= A B
Step-04:
Vertex - C with indegree zero
So, remove vertex-B and its associated edges.
Add C to Q
Now, update the in-degree of other vertices.
40
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Q= A B C
Step-05:
Vertex - D with indegree zero
So, remove vertex-D and its associated edges.
Add D to Q
Now, update the in-degree of other vertices
Q= A B C D
Step-06:
Remove remaining vertex-E and add to Q
0
Q= A B C D E
All vertices of the graph are processed. Now the topological ordering is obtained by performing
dequeue operation in Q.
Hence A, B, C, D, E is a topological ordering
NOTE: DFS based algorithm can also be used to solve topological sorting problem.
Source Vertex: The vertex with no incoming edges.
Sink Vertex / Minimal Vertex: The vertex with no outgoing edges.
41
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Networks
Network is a digraph with one vertex s, called the source, and one vertex t, called the
sink.
Network or Flow Network is a directed graph that is used for modeling material flow.
There are two different vertices; one is a source which produces material at some
steady rate, and another one is sink which consumes the content at the same constant
speed. The flow of the material at any mark in the system is the rate at which the
element moves.
Some real-life problems like the flow of liquids through pipes, the current through
wires and delivery of goods can be modeled using flow networks.
A Flow Network is a directed graph G = (V, E) such that
For each edge (u, v) ∈ E, we associate a nonnegative weight capacity
c (u, v) ≥ 0. If (u, v) ∉ E, we assume that c (u, v) = 0.
There are two distinguishing points, the source s, and the sink t;
For every vertex v ∈ V, there is a path from s to t containing v.
Example:
A network of pipelines used to deliver water from one source to one destination. However,water
is not simply pumped through one pipe, but through many pipes with many pumping stations in
between. The pipes are of different diameter and the stations are of different power so that the
amount of water that can be pumped may differ from one pipeline to another. For example, the
network in following Figure has eight pipes and six pumping stations. The numbers shown in this
figure are the maximum capacities of each pipeline. For example, the pipe going northeast from
the source s, the pipe sa, has a capacity of 5 units (say, 5,000 gallons per hour).
42
Collected by Bipin Timalsina
DSA with Java/Unit- 7
The problem is to maximize the capacity of the entire network so that it can transfer the maximum
amount of water. It may not be obvious how to accomplish this goal. Notice that the pipe sa coming
from the source goes to a station that has only one outgoing pipe, ab, of capacity 4. This means
that we cannot put 5 units through pipe sa, because pipe ab cannot transfer it. Also, the amount of
water coming to station b has to be controlled as well because if both incoming pipes, ab and cb,
are used to full capacity, then the outgoing pipe, bt, cannot process it either. It is far from obvious,
especially for large networks, what the amounts of water put through each pipe should be to utilize
the network maximally.
Let G = (V, E) be a flow network. Let s be the source of the network, and let t be the sink. A flow
in G is a real-valued function f: E→R that assigns a number to each edge of the network and
meets following properties:
Capacity Constraint: The flow through an edge e cannot be greater than its
capacity i.e. 0 ≤ f (e) ≤ cap(e)
Flow Conservation: The total flow coming to a vertex v is the same as the total
flow coming from it.
i.e. ∑𝑢 𝑓 (𝑒𝑑𝑔𝑒(𝑢𝑣)) = ∑𝑣 𝑓 (𝑒𝑑𝑔𝑒(𝑣𝑤)))
where v is neither the source nor the sink
. In other words, the amount of flow into a v is the same as the amount of
flow out of v for every vertex v ∈ V - {s, t}
Maximum Flow
It is defined as the maximum amount of flow that the network would allow to flow from
source to sink.
In the maximum-flow problem, we are given a flow network G with source s and sink t,
and we wish to find a flow of maximum value from s to t. This is called a maximum-flow
(or maxflow) problem
Multiple algorithms exist in solving the maximum flow problem. Two major algorithms
to solve these kind of problems are
Ford-Fulkerson algorithm and
Dinic's Algorithm
43
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Cut
In graph theory, a cut is a partition of the vertices of a graph into two disjoint subsets. Any cut
determines a cut-set, the set of edges that have one endpoint in each subset of the partition.
In a flow network, an s–t cut is a cut that requires the source and the sink to be in different
subsets, and its cut-set only consists of edges going from the source's side to the sink's side.
The capacity of an s–t cut is defined as the sum of the capacity of each edge in the cut-set.
A cut separating s and t is a set of edges between vertices of set 𝑋 and vertices of set
𝑋̅; any vertex of the graph belongs to one of these sets, and source s is in X and sink t
is in 𝑋̅.
Example:
In above figure, if 𝑋 = {𝑠, 𝑎}, then 𝑋̅ = {𝑏, 𝑐, 𝑑, 𝑡}, and the cut is the set of
edges {(a,b),(s,c),(s,d)}.
This means that if all edges belonging to this set are cut, then there is no way to
get from s to t.
Let us define the capacity of the cut as the sum of capacities of all its edges
leading from a vertex in 𝑋 to a vertex in 𝑋̅; thus,
cap{(a,b),(s,c),(s,d)} = cap(a,b) + cap(s,c) + cap(s,d) = 19.
The flow through the network cannot be greater than the capacity of any cut.
44
Collected by Bipin Timalsina
DSA with Java/Unit- 7
45
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Algorithm
FordFulkersonAlgo(G,s,t)
flow = 0;
for each edge e in G
flow(e) = 0;
while there is a path, p, from s to t in residual network G_f
for each e in p
residual_capacity(e) = capacity(e) – flow(e) ;
bottleneck_capacity(p) = min(residual_capacity(e) for e in p)
flow = flow + bottleneck_capacity(p);
for each edge e in p
if e is a forward edge
flow(e) = flow(e) + bottleneck_capacity(p);
else
flow(e) = flow(e) - bottleneck_capacity(p);
return flow;
Example: Compute the max flow of following network using Ford Fulkerson Algorithm
4
A B
10 10
S 8 6 T
2
10 10
C D
9
46
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Note that if the capacity for any forward edge is full, then that edge cannot be used in other
augmented paths. Similarly if the capacity of backward edge is empty then that edge cannot be
used in other augmented paths.
Step 0:
The flow of all the edges is 0 at the beginning.
0/4
A B
0/10 0/10
S 0/8 0/6 T
0/2
0/10 0/10
C D
0/9
47
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Step 1:
Select any arbitrary path from S to T. In this step, we have selected path S-A-D-T
The minimum residual capacity among the three edges is 8 (A-D). Based on this, update
the flow/capacity for each edge.
0/4
A B
8/10 0/10
S 8/8 0/6 T
0/2
0/10 8/10
C D
0/9
Step 2:
Select another path S-C-D-T. The minimum residual capacity among these edges is 2 (D-T).
Update the flows according to this.
0/4
A B
8/10 0/10
S 8/8 0/6 T
0/2
2/10 10/10
C D
2/9
48
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Step 3:
Another augmenting path is S-C-D-A-B-T. Here, backward edge (reverse edge) DA is considered.
The minimum residual capacity among the edges is 4 (A-B). Update the flows according to this.
4/4
A B
8/10 4/10
S 4/8 0/6 T
0/2
6/10 10/10
C D
6/9
Step 4:
Now, next augmenting path is S-C-D-B-T. The minimum residual capacity among the edges is 3
(C-D). Update the flows according to this.
4/4
A B
8/10 7/10
S 4/8 3/6 T
0/2
9/10 10/10
C D
9/9
49
Collected by Bipin Timalsina
DSA with Java/Unit- 7
Step 5:
Next augmenting path is S-A-D-B-T. The minimum residual capacity among the edges is 2 (S-A).
Update the flows according to this.
4/4
A B
10/10 9/10
S 6/8 5/6 T
0/2
9/10 10/10
C D
9/9
Try Yourself:
50
Collected by Bipin Timalsina