PEMP CSN2501
Algorithms on Graphs
Lecture delivered by:
N. D. Gangadhar
N D Gangadhar MSRSAS
Professor & Head, Dept. of Computer Engineering,
MSRSAS-Bangalore
M.S. Ramaiah School of Advanced Studies - Bangalore
11
PEMP CSN2501
Session Objectives
N D Gangadhar MSRSAS
• To introduce Topological Sort and its characteristics
• To introduce the import class of Shortest Path algorithms on
Graphs and discuss their applications
• To study the main shortest path algorithms, namely Dijkstra’s
Algorithm, Bellman-Ford Algorithm and DAG-based
Algorithm
• To introduce the idea of a Minimum Spanning Tree (MST) of
a Graph discuss its uses
• To study Kruskal, Prim-Jarnik and Baruka MST Algorithms
• To study the maximum flow algorithms, namely Augmenting
path, Maximum flow and minimum cut and Ford-Fulkerson’s
algorithm
M.S. Ramaiah School of Advanced Studies - Bangalore
2
PEMP CSN2501
Topological Sort
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
3
Topological Sort
PEMP CSN2501
• There are many problems
involving a set of tasks in
which some of the tasks
must be done before others.
• For example, consider the
problem of taking a course
only after taking its
prerequisites.
N D Gangadhar MSRSAS
• Is there any systematic way
of linearly arranging the
courses in the order that
they should be taken?
.Yes! - Topological sort
M.S. Ramaiah School of Advanced Studies - Bangalore
4
Definition of Topological Sort
PEMP CSN2501
• Topological sort is a method of arranging the vertices in a directed acyclic
graph (DAG), as a sequence, such that no vertex appear in the sequence
before its predecessor.
• The graph in (a) can be topologically sorted as in (b)
N D Gangadhar MSRSAS
(a)
(b)
M.S. Ramaiah School of Advanced Studies - Bangalore
5
PEMP CSN2501
Topological Sort
• Topological sort is not unique.
• Main use is to Indicate order of events, what should happen first
• The following are all topological sort of the graph below:
s1 = {a, b, c, d, e, f, g, h, i}
s2 = {a, c, b, f, e, d, h, g, i}
s3 = {a, b, d, c, e, g, f, h, i}
N D Gangadhar MSRSAS
s4 = {a, c, f, b, e, h, d, g, i}
etc.
Time complexity of a topological sort is O (|V| + |E|)
M.S. Ramaiah School of Advanced Studies - Bangalore
6
Topological Sort Algorithm
PEMP CSN2501
• One way to find a topological sort is to consider in-degrees of the vertices.
• The first vertex must have in-degree zero – every DAG must have at least
one vertex with in-degree zero.
• The Topological sort algorithm can be expressed as:
N D Gangadhar MSRSAS
int topologicalOrderTraversal( ){
int numVisitedVertices = 0;
while(there are more vertices to be visited){
if(there is no vertex with in-degree 0)
break;
else{
select a vertex v that has in-degree 0;
visit v;
numVisitedVertices++;
delete v and all its emanating edges;
}
}
return numVisitedVertices;
}
M.S. Ramaiah School of Advanced Studies - Bangalore
7
PEMP CSN2501
Topological Sort Example
1
A
2
B
3
C
0
D
2
E
F
G
H
I
J
1
0
2
2
0
D
G
A
B
F
H
J
E
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
I
C
8
PEMP CSN2501
Shortest Paths
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
9
PEMP CSN2501
Recall – Weighted Graphs
• In a weighted graph, each edge has an associated numerical
value, called the weight of the edge
• Edge weights may represent, distances, costs, etc.
• Example:
– In a flight route graph, the weight of an edge represents the distance in
miles between the endpoint airports
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
10
PEMP CSN2501
Shortest Paths
• Given a weighted graph and two vertices u and v, we want to
find a path of minimum total weight between u and v.
– Length of a path is the sum of the weights of its edges.
• Example:
– Shortest path between PVD and HNL
• Applications
– Internet packet routing
– Flight reservations/Transportations
– Driving directions
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
11
PEMP CSN2501
Dijkstra’s Algorithm - 1
• The distance of a vertex v from a vertex s is the length of a
shortest path between s and v
• Dijkstra’s algorithm computes the distances of all the
vertices from a given start vertex s
• Assumptions:
– the graph is connected
– the edges are undirected
– the edge weights are nonnegative
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
12
PEMP CSN2501
Dijkstra’s Algorithm - 2
• We grow a “cloud” of vertices
– beginning with s and eventually covering all the vertices
• We store with each vertex v a label d(v)
– representing the distance of v from s in the subgraph consisting of the
cloud and its adjacent vertices
• At each step
– We add to the cloud the vertex u outside the cloud with the smallest
distance label, d(u)
– We update the labels of the vertices adjacent to u
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
13
PEMP CSN2501
Edge Relaxation
• Consider an edge e = (u,z)
such that
– u is the vertex most
recentlyadded to the cloud
– z is not in the cloud
• The relaxation of edge e
updates distance d(z) as
follows:
– d(z) ← min{d(z),d(u)
weight(e)}
+
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
14
PEMP CSN2501
Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
15
PEMP CSN2501
Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
16
PEMP CSN2501
Dijkstra’s Algorithm Development
• A priority queue stores the vertices outside the cloud
– Key: distance
– Element: vertex
• Locator-based methods
– insert(k, e) returns a locator
– replaceKey(l, k) changes the key of an item
• We store two labels with each vertex:
– Distance (d(v) label)
– Locator in priority queue
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
17
PEMP CSN2501
Dijkstra’s Algorithm
N D Gangadhar MSRSAS
Algorithm DijkstraDistances(G, s)
Q ← new heap-based priority queue
for all v ∈ G.vertices()
if v = s
setDistance(v, 0)
else
setDistance(v, ∞)
l ← Q.insert(getDistance(v), v)
setLocator(v,l)
while ¬Q.isEmpty()
u ← Q.removeMin()
for all e ∈ G.incidentEdges(u)
{ relax edge e }
z ← G.opposite(u,e)
r ← getDistance(u) + weight(e)
if r < getDistance(z)
setDistance(z,r)
Q.replaceKey(getLocator(z),r)
M.S. Ramaiah School of Advanced Studies - Bangalore
18
PEMP CSN2501
Analysis of Dijkstra’s Algorithm
• Graph operations
– Method incidentEdges is called once for each vertex
• Label operations
– We set/get the distance and locator labels of vertex z O(deg(z)) times
– Setting/getting a label takes O(1) time
• Priority queue operations
– Each vertex is inserted once into and removed once from the priority queue,
where each insertion or removal takes O(log n) time
– The key of a vertex in the priority queue is modified at most deg(w) times,
where each key change takes O(log n) time
• Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is
represented by the adjacency list structure
N D Gangadhar MSRSAS
– Recall that Σv deg(v) = 2m
• The running time can also be expressed as O(m log n) since the graph is
connected
M.S. Ramaiah School of Advanced Studies - Bangalore
19
PEMP CSN2501
Shortest Paths Tree
• Using the template method pattern
– We can extend Dijkstra’s algorithm
– To return a tree of shortest paths
– From the start vertex to all other vertices
• We store with each vertex a third label:
– Parent edge in the shortest path tree
• In the edge relaxation step, we update the parent label
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
20
PEMP CSN2501
Shortest Paths Tree Algorithm
Algorithm DijkstraShortestPathsTree(G, s)
…
for all v in G.vertices()
setParent(v, ∅)
…
for all e in G.incidentEdges(u)
{ relax edge e }
z ← G.opposite(u,e)
r ← getDistance(u) + weight(e)
if r < getDistance(z)
setDistance(z,r)
setParent(z,e)
N D Gangadhar MSRSAS
Q.replaceKey(getLocator(z),r)
M.S. Ramaiah School of Advanced Studies - Bangalore
21
PEMP CSN2501
Why Dijkstra’s Algorithm Works
• Dijkstra’s algorithm is based on the greedy method. It adds
vertices by increasing distance.
– Suppose it didn’t find all shortest distances. Let F be the first wrong
vertex the algorithm processed.
– When the previous node, D, on the true shortest path was considered,
its distance was correct.
– But the edge (D,F) was relaxed at that time!
– Thus, so long as d(F)>d(D), F’s distance cannot be wrong. That is,
there is no wrong vertex.
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
22
PEMP CSN2501
Why It Doesn’t Work for Negative-Weight Edges
• Dijkstra’s algorithm is based on the greedy method. It adds
vertices by increasing distance.
• If a node with a negative incident edge were to be added late
to the cloud, it could mess up distances for vertices already in
the cloud.
C’s true distance is 1, but
it is already in the cloud
with d(C)=5!
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
23
PEMP CSN2501
Bellman-Ford Algorithm
N D Gangadhar MSRSAS
• Works even with Negative
weight edges
• Must assume directed edges
(for otherwise we would
have
negative-weight
cycles)
• Iteration i finds all shortest
paths that use i edges.
• Running time: O(nm).
• Can be extended to detect a
negative-weight cycle if it
exists
– How?
Algorithm BellmanFord(G,
s)
for all v in G.vertices()
if v = s
setDistance(v, 0)
else
setDistance(v, ∞)
for i ← 1 to n-1 do
for each e in G.edges()
{ relax edge e }
u ← G.origin(e)
z ← G.opposite(u,e)
r ← getDistance(u) +
weight(e)
if r < getDistance(z)
setDistance(z,r)
M.S. Ramaiah School of Advanced Studies - Bangalore
24
PEMP CSN2501
Bellman-Ford Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
25
PEMP CSN2501
DAG-based Algorithm
• Works even with negative-weight edges
• Uses topological order
• Doesn’t use any fancy data structures
• Is much faster than Dijkstra’s algorithm
N D Gangadhar MSRSAS
• Running time: O(n+m).
M.S. Ramaiah School of Advanced Studies - Bangalore
26
PEMP CSN2501
DAG-based Algorithm
N D Gangadhar MSRSAS
Algorithm DagDistances(G, s)
for all v in G.vertices()
if v = s
setDistance(v, 0)
else
setDistance(v, ∞)
Perform a topological sort of the vertices
for u ← 1 to n do {in topological order}
for each e in G.outEdges(u)
{ relax edge e }
z ← G.opposite(u,e)
r ← getDistance(u) + weight(e)
if r < getDistance(z)
setDistance(z,r)
M.S. Ramaiah School of Advanced Studies - Bangalore
27
PEMP CSN2501
DAG Example
• Nodes are labeled with their d(v) values
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
28
PEMP CSN2501
Minimum Spanning Trees
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
29
PEMP CSN2501
Minimum Spanning Trees
• Spanning subgraph
– Subgraph of a graph G
containing all the vertices of G
• Spanning tree
– Spanning subgraph that is itself
a (free) tree
• Minimum spanning tree
(MST)
– Spanning tree of a weighted
graph with minimum total edge
weight
• Applications
N D Gangadhar MSRSAS
– Communications networks
– Transportation networks
M.S. Ramaiah School of Advanced Studies - Bangalore
30
PEMP CSN2501
Cycle Property
• Cycle Property:
– Let T be a minimum spanning
tree of a weighted graph G
– Let e be an edge of G that is
not in T and C let be the cycle
formed by e with T
– For every edge f of C,
weight(f) ≤ weight(e)
• Proof:
N D Gangadhar MSRSAS
– By contradiction
– If weight(f) > weight(e) we
can get a spanning tree of
smaller weight by replacing e
with f
M.S. Ramaiah School of Advanced Studies - Bangalore
31
PEMP CSN2501
Partition Property
•
Partition Property:
–
Consider a partition of the vertices of
G into subsets U and V
– Let e be an edge of minimum weight
across the partition
– There is a minimum spanning tree of
G containing edge e
•
Proof:
N D Gangadhar MSRSAS
– Let T be an MST of G
– If T does not contain e, consider the
cycle C formed by e with T and let
fbe an edge of C across the partition
– By the cycle property,
weight(f) ≤ weight(e)
– Thus, weight(f) = weight(e)
– We obtain another MST by replacing
f with e
M.S. Ramaiah School of Advanced Studies - Bangalore
32
PEMP CSN2501
Kruskal’s Algorithm
• A priority queue stores the
edges outside the cloud
– Key: weight
– Element: edge
• At the end of the algorithm
– We are left with one cloud
that encompasses the MST
– A tree T which is our MST
N D Gangadhar MSRSAS
Algorithm KruskalMST(G)
for each vertex V in G do
define a Cloud(v) of �{v}
let Q be a priority queue.
Insert all edges into Q using their
weights as the key
T NULL
while T has fewer than n-1 edges do
edge e = T.removeMin()
Let u, v be the endpoints of e
if Cloud(v) ≠ Cloud(u) then
Add edge e to T
Merge Cloud(v) and Cloud(u)
return T
M.S. Ramaiah School of Advanced Studies - Bangalore
33
PEMP CSN2501
Data Structure for Kruskal Algortihm
• The algorithm maintains a forest of trees
• An edge is accepted it if connects distinct trees
• We need a data structure that maintains a partition,
– i.e., a collection of disjoint sets, with the operations:
– find(u): return the set storing u
– union(u,v): replace the sets storing u and v with their union
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
34
PEMP CSN2501
Partition-Based Implementation
• A partition-based version of Kruskal’s Algorithm performs
cloud merges as unions and tests as finds
Algorithm Kruskal(G):
N D Gangadhar MSRSAS
Input: A weighted graph G.
Output: An MST T for G.
Let P be a partition of the vertices of G, where each vertex forms a separate
set.
Let Q be a priority queue storing the edges of G, sorted by their weights
Let T be an initially-empty tree
while Q is not empty do
(u,v) ← Q.removeMinElement()
if P.find(u) != P.find(v) then
Add (u,v) to T
P.union(u,v)
return T
• Running time: O( (n+m) log n)
M.S. Ramaiah School of Advanced Studies - Bangalore
35
PEMP CSN2501
Kruskal Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
36
PEMP CSN2501
Kruskal Example (2)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
37
PEMP CSN2501
Kruskal Example (3)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
38
PEMP CSN2501
Kruskal Example (4)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
39
PEMP CSN2501
Kruskal Example (5)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
40
PEMP CSN2501
Kruskal Example (6)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
41
PEMP CSN2501
Kruskal Example (7)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
42
PEMP CSN2501
Kruskal Example (8)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
43
PEMP CSN2501
Kruskal Example (9)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
44
PEMP CSN2501
Kruskal Example (10)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
45
PEMP CSN2501
Kruskal Example (11)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
46
PEMP CSN2501
Kruskal Example (12)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
47
PEMP CSN2501
Kruskal Example (13)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
48
PEMP CSN2501
Kruskal Example (end)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
49
PEMP CSN2501
Prim-Jarnik’s Algorithm
• Similar to Dijkstra’s algorithm (for a connected graph)
• We pick an arbitrary vertex s and we grow the MST as a cloud
of vertices, starting from s
• We store with each vertex v a label d(v) = the smallest weight
of an edge connecting v to a vertex in the cloud
• At each step:
– We add to the cloud the vertex u outside the cloud with the smallest
distance label
– We update the labels of the vertices adjacent to u
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
50
PEMP CSN2501
Prim-Jarnik’s Algorithm
Algorithm PrimJarnikMST(G)
Q ← new heap-based priority
queue
s ← a vertex of G
– Key: distance
for all v in G.vertices()
– Element: vertex
if v = s
setDistance(v, 0)
Locator-based methods
else
– insert(k,e) returns a locator
setDistance(v, ∞)
setParent(v, ∅)
– replaceKey(l,k) changes
l ← Q.insert(getDistance(v),
the key of an item
v)
setLocator(v,l)
We store three labels with
while ¬Q.isEmpty()
each vertex:
u ← Q.removeMin()
for all e in
– Distance
G.incidentEdges(u)
– Parent edge in MST
z ← G.opposite(u,e)
– Locator in priority queue
r ← weight(e)
if r < getDistance(z)
setDistance(z,r)
M.S. Ramaiah School of Advanced Studies - Bangalore
setParent(z,e)
• A priority queue stores the
vertices outside the cloud
•
•
N D Gangadhar MSRSAS
51
PEMP CSN2501
Prim-Jarnik’s Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
52
PEMP CSN2501
Prim-Jarnik’s Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
53
PEMP CSN2501
Analysis
• Graph operations
– Method incidentEdges is called once for each vertex
• Label operations
– We set/get the distance, parent and locator labels of vertex z O(deg(z)) times
– Setting/getting a label takes O(1) time
• Priority queue operations
–
Each vertex is inserted once into and removed once from the priority queue,
where each insertion or removal takes O(log n) time
– The key of a vertex w in the priority queue is modified at most deg(w) times,
where each key change takes O(log n) time
• Prim-Jarnik’s algorithm runs in O((n + m) log n) time provided the graph
is represented by the adjacency list structure
N D Gangadhar MSRSAS
– Recall that Σv deg(v) = 2m
• The running time is O(m log n) since the graph is connected
M.S. Ramaiah School of Advanced Studies - Bangalore
54
PEMP CSN2501
Baruvka Algorithm
• Like Kruskal’s Algorithm, Baruvka’s algorithm grows many
“clouds” at once.
• Algorithm BaruvkaMST(G)
T ← V {just the vertices of G}
while T has fewer than n-1 edges do
for each connected component C in T do
Let edge e be the smallest-weight edge from C to another
component in T.
if e is not already in T then
Add edge e to T
return T
N D Gangadhar MSRSAS
• Each iteration of the while-loop halves the number of
connected components in T.
– The running time is O(m log n).
M.S. Ramaiah School of Advanced Studies - Bangalore
55
PEMP CSN2501
Baruvka Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
56
PEMP CSN2501
Baruvka Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
57
PEMP CSN2501
Baruvka Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
58
PEMP CSN2501
Maximum Flow Algorithms
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
59
PEMP CSN2501
Maximum Flow Algorithms
Flow networks
– Flow
– Cut
Maximum flow
– Augmenting path
– Ford-Fulkerson’s algorithm
– Maximum flow and minimum cut
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
60
PEMP CSN2501
Flow Network
• A flow network (or just network) N consists of
• A weighted diagraph G with nonnegative integer edge weights, where
the weight of an edge e is called the capacity c(e) of e
• Two distinguished vertices, s and t of G, called the source and sink
respectively, such that s has no incoming edges and t has no outgoing
edges
• Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
61
Flow
PEMP CSN2501
• A flow f for a network N is an assignment of an integer value
f(e) to each other e that satisfies the following properties:
Capacity Rule: For each edge e, 0<=f(e)<=c(e)
where E-(v) and E+(v) are the incoming and outgoing edges of v
• The value of a flow f, denoted |f|, is the total flow from the
source, which is the same as the total flow into the sink
• Example
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
62
PEMP CSN2501
Maximum Flow
• A flow for a network N is
said to be maximum if its
value is the largest of all
flows for N
• The maximum flow problem
consists of finding a
maximum flow for a given
network N
• Applications
N D Gangadhar MSRSAS
–
–
–
–
Flow of value 8=2+3+3=1+3+4
Hydraulic systems
Electrical circuits
Traffic movements
Freight transportation
Maximum flow of value 10=4+3+3=3+3+4
M.S. Ramaiah School of Advanced Studies - Bangalore
63
PEMP CSN2501
Cut
• A cut of a network N with source s
and sink t is a partition x=(Vs,Vt) of
the vertices of N such that s € Vs and
t € Vt
– Forward edge of cut x: origin in Vs
and destination in Vt
– Backward edge of cut x: origin in
Vt and destination in Vs
N D Gangadhar MSRSAS
• Flow f(x) across a cut x: total flow
of forward edges minus total flow of
backward edges
• Capacity c(x) of a cut x: total
capacity of forward edges
• Example
– C(x)=24
– F(x)=8
M.S. Ramaiah School of Advanced Studies - Bangalore
64
PEMP CSN2501
Flow and Cut
N D Gangadhar MSRSAS
Lemma:
The flow f(x) across any cut x
is equal to the flow value |f|
Lemma:
The flow f(x) across a cut x is
less than or equal to the
capacity c(x) of the cut
Theorem
The value of any flow is less
than or equal to the capacity of
any cut, i.e. for any flow f and
any cut x, we have
|f|<=c(x)
M.S. Ramaiah School of Advanced Studies - Bangalore
65
PEMP CSN2501
Augmenting Path
• Consider a f for a network N
• Let e be an edge from u to v:
– Residual capacity of e from u to
v: ∆f (u, v)=c (e) –f (e)
– Residual capacity of e from v to
u: ∆f (v, u)=f (e)
• Let π be a path from s to t
– The residual capacity ∆f(π) of π
is the smallest of the residual
capacities of the edges of π in
the direction from s to t
N D Gangadhar MSRSAS
• A path π from s to t is an
augmenting path if ∆f (π)>0
M.S. Ramaiah School of Advanced Studies - Bangalore
66
PEMP CSN2501
Flow Augmentation
Lemma:
Let π be an augmenting path for
flow f in network N. There exists a
flow f’ for N of value
|f’|=|f| + ∆f(π)
Proof:
N D Gangadhar MSRSAS
We compute flow f’ by modifying
the flow on the edges of π
• Forward edge:
f’ (e)= f (e) + ∆f(π)
• Backward edge:
f’ (e)= f (e) - ∆f (π)
M.S. Ramaiah School of Advanced Studies - Bangalore
67
PEMP CSN2501
Ford-Fulkerson’s Algorithm
• Initially, f (e) =0 for each
edge e
• Repeatedly
– Search for an augmenting path π
– Augment by ∆f(π) the flow
along the edges of π
• A specialization of DFS (or
BFS)
searches
for
an
augmenting path
– An edge e is traversed from u to
v provided ∆f (u, v)>0
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
68
PEMP CSN2501
Max-Flow and Min-Cut
• Termination of Ford-Fulkerson’s
algorithm
– There is no augmenting path from s
to t with respect to the current flow
f
Theorem:
The value of a maximum flow
is equal to the capacity of a
minimum cut
• Define
– Vs set of vertices reachable form s
by augmenting paths
– Vt set of remaining vertices
• Cut x=(Vs , Vt) has capacity c(x)=|f|
– Forward edge: f (e)=c (e)
– Backward edge: f (e)=0
N D Gangadhar MSRSAS
• Thus, flow f has maximum value
and cut x has minimum capacity
M.S. Ramaiah School of Advanced Studies - Bangalore
69
PEMP CSN2501
Example (1)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
70
PEMP CSN2501
Example (2)
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
71
PEMP CSN2501
Analysis
• In the worst case, FordFulkerson’s
algorithm
performs
|f*|
flow
augmentations, where f* is a
maximum flow
• Example
– The augmenting paths found
alternate between π1and π2
– The algorithm performs 100
augmentations
N D Gangadhar MSRSAS
• Finding an augmenting path
and augmenting the flow takes
O (n + m) time
• The running time of FordFulkerson’s algorithm is 0(|f*|
(n + m))
M.S. Ramaiah School of Advanced Studies - Bangalore
72
PEMP CSN2501
Summary
N D Gangadhar MSRSAS
• Topological sort is a method of arranging the vertices in a
directed acyclic graph (DAG), as a sequence, such that no
vertex appear in the sequence before its predecessor. Main use
is to indicate order of events, what should happen first
• The Graph data structure can be used to map a wide variety of
problems to algorithms on Graphs
• Shortest Path and Minimum Spanning Tree algorithms are the
most import algorithms on graphs
• Dijkstra’s Algorithm is greedy algorithm and finds the shortest
path to all vertices of a graph from a given vertex of it
• Bellman-Ford Algorithm overcomes the limitation of
Dijkstra’s Algorithm and works with negative edge weights
M.S. Ramaiah School of Advanced Studies - Bangalore
73
PEMP CSN2501
Summary
N D Gangadhar MSRSAS
• A large number of algorithms for computing the Minimum
Spanning Tree (MST) of a Graph have been developed
• Kruskal MST algorithm builds the MST by removing the
minimum weight edges in order using a Priority Queue
• The partition based Kruskal Algorithm computes the MST by
growing it as a Forest based on the partition property of the
MST
• Prim-Jarnik Algorithm is similar to Dijkstra’s Algorithm and
grows the MST by adding one vertex at a time
• Baruvka Algorithm grows multiple clouds of vertices at a time
to build a MST
M.S. Ramaiah School of Advanced Studies - Bangalore
74
PEMP CSN2501
Summary
The maximum flow algorithm consists of finding a
maximum flow for a given network N
The max-flow min-cut theorem shows that maximum flow is
the same as the capacity of a minimum cut
N D Gangadhar MSRSAS
M.S. Ramaiah School of Advanced Studies - Bangalore
75
© Copyright 2026 Paperzz