You are on page 1of 37

Algorithms - I

CSC 302

SK Hafizul Islam

Department of CSE, IIIT Kalyani

October 26, 2022

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 1 / 34
Agenda I

1 Graph Traversal Algorithms


Breadth-first search (BFS)
Depth-first search

2 Cycle Detection in a Directed Graph


White-path theorem
Cycle detection in Directed Graph

3 Topological Sort
Idea for Topological Sort
Algorithm for Topological Sort
Topological Sort: An Example

4 Suggested Readings

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 2 / 34
Graph Traversal Algorithms

Breadth-first search (BFS): BFS uses a queue as an auxiliary data structure to store
nodes for further processing.
Depth-first search (DFS): DFS uses a stack as an auxiliary data structure to store nodes
for further processing.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 3 / 34
Breadth-first search (BFS) Traversal

BFS algorithm begins at the any node of a graph/root node of a tree, and explores all the
neighboring nodes. Then for each of those nearest nodes, the BFS algorithm explores their
unexplored neighbor nodes, and so on, until it finds the goal.
BFS algorithm search systematically explores the edges of G to “discover” every vertex
that is reachable from s.
BFS algorithm computes the distance (smallest number of edges) from s to each reachable
vertex v . It also produces a “breadth-first tree” with root s that contains all reachable
vertices.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 4 / 34
Breadth-first search (BFS) Traversal

Algorithm: BFS(G, s)
1 V is the set of vertices of G 11 Q := ϕ
2 E is the set of edges of G 12 ENQUEUE(Q,s)
3 for each vertex u ∈ V − {s} do 13 while (Q ̸= ϕ) do
4 color(u):=WHITE ▷ Color of 14 u :=DEQUEUE(Q)
each vertex u ∈ V − {s} as 15 for each vertex v adjacent to u
WHITE do
5 dis(u):=∞ ▷ Compute the 16 if (color(v )) = WHITE
distance from the source then
s to u 17 color(v ):=GRAY
6 pred(u):=NIL ▷ Select the 18 dis(v ):=dis(v )+1
predecessor of u 19 pred(v ):=u
7 end 20 ENQUEUE(Q,v )
8 color(s):=GRAY ▷ s is just 21 end
discovered 22 end
9 dis(s):=0 23 end
10 pred(s):=NIL
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 5 / 34
Breadth-first search (BFS) Traversal1

Space complexity: In the BFS algorithm, all the nodes at a particular level must be saved
until their child nodes in the next level have been generated. The space complexity is
therefore proportional to the number of nodes at the deepest level of the graph. Given
a graph with branching factor b (number of children at each node) and depth d, the
asymptotic space complexity is the number of nodes at the deepest level O(b d+1 ).
Time complexity: The time complexity is O(|E | + |V |), since every vertex and every edge
will be explored in the worst case.

1
https://en.wikipedia.org/wiki/Breadth-first_search
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 6 / 34
Breadth-first search (BFS) Traversal

Chapter 22 Elementary Graph Algorithms

r s t u r s t u
∞ 0 ∞ ∞ 1 0 ∞ ∞
(a) Q s (b) Q w r
∞ ∞ ∞ ∞ 0 ∞ 1 ∞ ∞ 1 1
v w x y v w x y
r s t u r s t u
1 0 2 ∞ 1 0 2 ∞
(c) Q r t x (d) Q t x v
∞ 1 2 ∞ 1 2 2 2 1 2 ∞ 2 2 2
v w x y v w x y
r s t u r s t u
1 0 2 3 1 0 2 3
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 7 / 34
1 0 2 ∞ 1 0 2 ∞
Breadth-first search (BFS) Traversal
(c) Q r t x (d) Q t x v
∞ 1 2 ∞ 1 2 2 2 1 2 ∞ 2 2 2
v w x y v w x y
r s t u r s t u
1 0 2 3 1 0 2 3
(e) Q x v u (f) Q v u y
2 1 2 ∞ 2 2 3 2 1 2 3 2 3 3
v w x y v w x y
r s t u r s t u
1 0 2 3 1 0 2 3
(g) Q u y (h) Q y
2 1 2 3 3 3 2 1 2 3 3
v w x y v w x y
r s t u
1 0 2 3
(i) Q ;
2 1 2 3
v w x y
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 8 / 34
Breadth-first search (BFS) Traversal

Define the shortest-path distance δ(s, v ) from s to v as the minimum number of edges
in any path from s to v .
δ(s, v ) = ∞ means there is no path from s to v .
Lemma 1
Let G = (V , E ) be a directed or undirected graph, and let s ∈ V be an arbitrary vertex. Then,
for any edge (u, v ) ∈ E
δ(s, v ) ≤ δ(s, u) + 1

Lemma 2
Let G = (V , E ) be a directed or undirected graph, and suppose that BFS is run on G from a
given source vertex s ∈ V . Then upon termination, for each vertex v ∈ V , the value dis(v )
computed by BFS satisfies
dis(v ) ≥ δ(s, v )
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 9 / 34
Breadth-first search (BFS) Traversal

BFS algorithm constructs a breadth-first tree.


T (n) = O(|V |).
The following procedure prints out the vertices on a shortest path from s to v .
Algorithm: PRINT-PATH(G, s, v )
1 if (v = s) then
2 Print s
3 end
4 else
5 if (pred(v )=NIL) then
6 Print “No path from” s “to” v “exists”
7 end
8 else
9 PRINT-PATH(G, s, pred(v ))
10 Print v
11 end
12 end
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 10 / 34
Depth-first search (DFS) Traversal

DFS algorithm progresses by expanding the starting node of G and then going deeper and
deeper until the goal node is found, or until a node that has no children is encountered.
When a dead-end is reached, the algorithm backtracks, returning to the most recent node
that has not been completely explored.
If any undiscovered vertices remain, then DFS algorithm selects one of them as a new
source, and it repeats the search from that source. The algorithm repeats this entire
process until it has discovered every vertex.
DFS algorithm may output a depth-first forest, because the search may repeat from
multiple sources.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 11 / 34
Depth-first search (DFS) Traversal

Algorithm: DFS(G)
1 V is the set of vertices of G
2 E is the set of edges of G
3 for each vertex u ∈ V do
4 color(u):=WHITE ▷ Color of each vertex u ∈ V as WHITE
5 pred(u):=NIL ▷ Select the predecessor of u
6 end
7 time := 0 ▷ Global timestamp
8 for each vertex u ∈ V do
9 if (color(u) = WHITE ) then
10 DFS-VISIT(G, u)
11 end
12 end

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 12 / 34
Depth-first search (DFS) Traversal

Algorithm: DFS-VISIT(G, u)
1 time := time + 1
2 dt (u) := time ▷ discovery time of u
3 color(u):=GRAY ▷ u is just discovered
4 for each vertex v adjacent to u do
5 if (color(v ) = WHITE ) then
6 pred(v ):=u
7 DFS-VISIT(G, v )
8 end
9 end
10 color(u):=BLACK ▷ Make it “black” if it is finished
11 time := time + 1
12 ft (u) := time ▷ finish time of u

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 13 / 34
Depth-first search (DFS) Traversal2

Space complexity: The space complexity of a depth-first search is lower than that of a
BFS algorithm.
Time complexity: The time complexity of a DFS algorithm is proportional to the number
of vertices plus the number of edges in the graphs that are traversed. The time complexity
is O(|E | + |V |).

2
https://en.wikipedia.org/wiki/Depth-first_search
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 14 / 34
Depth-first search (DFS) Traversal

22.3 Depth-first search 605

u v w u v w u v w u v w
1/ 1/ 2/ 1/ 2/ 1/ 2/

3/ 4/ 3/
x y z x y z x y z x y z
(a) (b) (c) (d)

u v w u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/ 1/ 2/7
B B B B

4/ 3/ 4/5 3/ 4/5 3/6 4/5 3/6


x y z x y z x y z x y z
(e)
SK Hafizul Islam (Department of CSE, IIIT Kalyani)
(f) Algorithms - I
(g) (h)
October 26, 2022 15 / 34
22.3 Depth-first search 605
Depth-first search (DFS) Traversal
u v w u v w u v w u v w
1/ 1/ 2/ 1/ 2/ 1/ 2/

3/ 4/ 3/
x y z x y z x y z x y z
(a) (b) (c) (d)

u v w u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/ 1/ 2/7
B B B B

4/ 3/ 4/5 3/ 4/5 3/6 4/5 3/6


x y z x y z x y z x y z
(e) (f) (g) (h)

u v w u v w u v w u v w
1/ 2/7 1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/
F B F B F B F B C

4/5 3/6 4/5 3/6 4/5 3/6 4/5 3/6


x y z x y z x y z x y z
(i)
SK Hafizul Islam (Department of CSE, IIIT Kalyani) (j) Algorithms - I (k) (l)
October 26, 2022 16 / 34
x y z x y z x y z x y z
Depth-first search (DFS) Traversal
(a) (b) (c) (d)

u v w u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/ 1/ 2/7
B B B B

4/ 3/ 4/5 3/ 4/5 3/6 4/5 3/6


x y z x y z x y z x y z
(e) (f) (g) (h)

u v w u v w u v w u v w
1/ 2/7 1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/
F B F B F B F B C

4/5 3/6 4/5 3/6 4/5 3/6 4/5 3/6


x y z x y z x y z x y z
(i) (j) (k) (l)

u v w u v w u v w u v w
1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/12
F B C F B C F B C F B C
B B B
4/5 3/6 10/ 4/5 3/6 10/ 4/5 3/6 10/11 4/5 3/6 10/11
x y z x y z x y z x y z
SK Hafizul Islam (m)
(Department of CSE, IIIT Kalyani) (n) Algorithms - I (o) (p)
October 26, 2022 17 / 34
x y z x y z x y z x y z
Depth-first search (DFS) Traversal
(e) (f) (g) (h)

u v w u v w u v w u v w
1/ 2/7 1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/
F B F B F B F B C

4/5 3/6 4/5 3/6 4/5 3/6 4/5 3/6


x y z x y z x y z x y z
(i) (j) (k) (l)

u v w u v w u v w u v w
1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/12
F B C F B C F B C F B C
B B B
4/5 3/6 10/ 4/5 3/6 10/ 4/5 3/6 10/11 4/5 3/6 10/11
x y z x y z x y z x y z
(m) (n) (o) (p)

Figure 22.4 The progress of the depth-first-search algorithm DFS on a directed graph. As edges
are explored by the algorithm, they are shown as either shaded (if they are tree edges) or dashed
(otherwise). Nontree edges are labeled B, C, or F according to whether they are back, cross, or
forward edges. Timestamps within vertices indicate discovery time/finishing times.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 18 / 34
Depth-first search (DFS) Traversal

Tree Edge: It is an edge which is present in the tree obtained after applying DFS on the
graph.
T := (u, v )
dt (u) < dt (v ) < df (v ) < df (u)
Forward Edge: It is an edge (u, v ) such that v is descendant but not part of the DFS tree.

F := (u, x )
dt (u) < dt (x ) < df (x ) < df (u)
Back edge: It is an edge (u, v ) such that v is ancestor of node u but not part of DFS tree.

B := (x , v )
dt (v ) < dt (x ) < df (x ) < df (v )
Cross Edge: It is a edge which connects two node such that they do not have any ancestor
and a descendant relationship between them.
C := (w , y )
dt (y ) < dt (y ) < df (w ) < df (w )
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 19 / 34
Properties of DFS

Theorem 1 (Parenthesis theorem)


In any DFS of a (directed or undirected) graph G = (V , E ), for any two vertices u and v ,
exactly one of the following three conditions holds:
the intervals [dt (u), df (u)] and [dt (v ), df (v )] are entirely disjoint, and neither u nor v is a
descendant of the other in the depth-first forest, [cross edge]
the interval [dt (u), df (u)] is contained entirely within the interval [dt (v ), df (v )], and u is
a descendant of v in a depth-first tree, [back edge] or
the interval [dt (v ), df (v )] is contained entirely within the interval [dt (u), df (u)], and v is
a descendant of u in adepth-first tree [forward edge].

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 20 / 34
x

Properties of DFS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
22.3 Depth-first search
(s (z (y607
(x x) y) (w w) z) s) (t (v v) (u u) t)

y z s t
s t
3/6 2/9 1/10 11/16 B
C
B F C
(a) C B z F v u
B C
(c)
4/5 7/8 12/13 14/15
x C w C v C u y w
C

x
s t

z v u Figure 22.5 Properties of depth-first search. (a) The result of a depth-fi


graph. Vertices are timestamped and edge types are indicated as in Figure
(b) y w the discovery time and finishing time of each vertex correspond to the parent
rectangle spans the interval given by the discovery and finishing times of th
x Only tree edges are shown. If two intervals overlap, then one is nested w
vertex corresponding to the smaller interval is a descendant of the vertex corr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (c) The graph of part (a) redrawn with all tree and forward edges going down
(s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t) and all back edges going up from a descendant to an ancestor.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 21 / 34
Properties of DFS

(a) The result of a DFS of a directed graph. Vertices are timestamped and edge types are
indicated as in previous Figure.
(b) Intervals for the discovery time and finishing time of each vertex correspond to the
parenthesization shown. Each rectangle spans the interval given by the discovery and
finishing times of the corresponding vertex. Only tree edges are shown. If two intervals
overlap, then one is nested within the other, and the vertex corresponding to the smaller
interval is a descendant of the vertex corresponding to the larger.
(c) The graph of part (a) redrawn with all tree and forward edges going down within a
depth-first tree and all back edges going up from a descendant to an ancestor.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 22 / 34
DFS

Given a digraph G = (V , E ), DFS traverses all vertices of G and


constructs a forest, together with a set of source vertices; and outputs two time unit arrays,
dt (u) and df (u).
DFS Forest: DFS constructs a forest F = (V , Ef ), a collection of trees, where

Ef = {(pred(v ), v ) : where DFS calls are made}

the forest obtained with the DFS is not unique.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 23 / 34
DFS

Theorem 2 (White-path theorem)


In a depth-first forest of a (directed or undirected) graph G, vertex v is a descendant of vertex
u iff at the time dt (u) that the search discovers u, there is a path from u to v consisting entirely
of white vertices.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 24 / 34
DFS

Theorem 2 (White-path theorem)


In a depth-first forest of a (directed or undirected) graph G, vertex v is a descendant of vertex
u iff at the time dt (u) that the search discovers u, there is a path from u to v consisting entirely
of white vertices.

Proof.
If part
If v = u, then the path from u to v contains just vertex u, which is still white when we set
the value of dt (u).
Suppose that v is a proper descendant of u in the depth-first forest. Therefore, dt (u) < dt (v ),
and so v is white at time dt (u). Since v can be any descendant of u, all vertices on the unique
simple path from u to v in the depth-first three are white at time dt (u).

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 24 / 34
DFS

Proof.
Only if part
Assume that there is a path of white vertices from u to v at time dt (u), but v does not become
a descendant of u in the depth-first tree. We also assume that every vertex other than v along
the path becomes a descendant of u.
Let w be the predecessor of v in the path, so that w is a descendant of u.
Therefore, df (w ) < df (u).
Also dt (u) < dt (v ) < df (w ) ≤ df (u) because v must be discovered after u is discovered,
but before w is finished. Accordingly, [dt (v ), df (v )] is contained entirely within the interval
[dt (u), df (u)]. Therefore, v must after all be a descendant of u.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 25 / 34
Cycles in digraphs: Applications of DFS

Lemma 3
Given a digraph G, and any DFS tree of G, tree edges, forward edges, and cross edges all
go from a vertex of higher finish time to a vertex of lower finish time. Back edges go from a
vertex of lower finish time to a vertex of higher finish time.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 26 / 34
Cycles in digraphs: Applications of DFS
Cycles in digraphs: Applications of DFS

Claim: Given a digraph, DFS can be used to determine whether


Lemma 3 it contains any cycles.

Lemma 2: Given a digraph , and any DFS tree of , tree


Given a digraph G, and edges,
any DFSforwardtree of and
edges, G, tree edges,
cross edges forward
all go edges,
from a vertex of and cross edges all
higher finish time to a vertex of lower finish time. Back edges go
go from a vertex of higher
fromfinish
a vertextime tofinish
of lower a vertex
time to aofvertex
lower finishfinish
of higher time.
time.Back edges go from a
vertex of lower finish time to a vertex of higher finish
Proof: The conclusions follow from Lemma 1.
time.
11/14
d e d e 12/13
root
2/5 C C C
b a b 1/10
6/9
f a f
root
F B
7/8
g C g
c c 3/4
original graph
C: cross, F: forward,
B: back edge
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 26 / 34
Cycles in digraphs: Applications of DFS

Lemma 4
A digraph G is acyclic (DAG) if and only if any DFS forest of G yields no back edges.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 27 / 34
Claim: Given a digraph, DFS can be used to determine whether
Cycles in digraphs:
it containsApplications
any cycles. of DFS
Lemma 2: Given a digraph , and any DFS tree of , tree
edges, forward edges, and cross edges all go from a vertex of
Lemma 4 higher finish time to a vertex of lower finish time. Back edges go
from a vertex of lower finish time to a vertex of higher finish time.
A digraph G is acyclic (DAG)
Proof: if and only
The conclusions if any
follow fromDFS forest
Lemma 1. of G yields no back edges.
11/14
d e d e 12/13
root
2/5 C C C
b a b 1/10
6/9
f a f
root
F B
7/8
g C g
c c 3/4
original graph
C: cross, F: forward,
B: back edge
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 27 / 34
Cycles in digraphs: Applications of DFS

Proof.
If part: Suppose there are no back edges. By Lemma 3, all edges go from a vertex of
higher finish time to a vertex of lower finish time. Hence there can be no cycles.
Assume that G has no cycles, but has atleast one back edge (u, v ).
Then v is an ancestor of u in a rooted DFS tree, i.e., there is a path v → · · · → u in the DFS
tree.
Therefore, the back edge and the path gives a cycle, which is a contradiction!
Only if part: Suppose that G contains a cycle c, but no back edges.
Let v be the first vertex to be discovered in c, and let (u, v ) be the preceding edge in c.
At time dt (u), the vertices of c form a path of white vertices from v to u. By the White-path
theorem (2), u becomes a descendant of v in the depth-first forest. Therefore, (u, v ) is a back
edge.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 28 / 34
Cycle Detection with the DFS

Cycle Detection in a DAG G=⇒Back edge detection in the DFS tree of G [Lemma 4].
Run the DFS algorithm and assigning the dt (u) and df (u) values.
Check whether an edge (u, v ) go from a vertex of lower finish time to a vertex of higher
finish time (Back edge).
This would take O(|V | + |E |) time for the DFS and O(|E |) time for the scan through all
of the edges. In total, this uses O(|V | + |E |) time.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 29 / 34
Topological Sort

If G is a DAG then a topological sorting of G is a linear ordering of V such that for each
edge (u, v ) in the DAG, u appears before v in the linear ordering.
Idea of Topological Sorting: Run the DFS on the DAG G and output the vertices in
reverse order of finishing time.
Correctness of the Idea: By Lemma 3, for every edge (u, v ) in a DAG G, the finishing
time of u is greater than that of v , as there are no back edges and the remaining three
classes of edges have this property.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 30 / 34
Algorithm for Topological Sort

Run DFS(G), computing finish time df (v ) for each vertex v ∈ V .


As each vertex is finished, insert it onto the front of a list.
Output the list.
Running time: Θ(|V | + |E |).

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 31 / 34
Topological Sort: An Example
Topological Sort: Example


  
  
a   b f 11/14 b 15/16
...
...
...
...
...
a 1/10
 
  
...
...

 
...


c 
 c 2/9 
...



...
...

 d
...
...
..

g 12/13
...
...

    
...
...


...


e 
 
e 
...
...
...

d 7/8
...
...
...

f
...
...
...


...

 
...

 3/6
 

...
...
...
...

g
...
...
...
...
...
... h 4/5
 
  
...
...
...
...
...
...
...
...

h ...
...
...
..

Original graph DFS forest

       "


  
Final order: 
.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 32 / 34
Suggested Readings

Chapter 22-25, Thomas H. Cormen, Charles E. Lieserson, Ronald L. Rivest and Clifford
Stein, Introduction to Algorithms, 3rd Edition, MIT Press/McGraw-Hill, 2009.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 33 / 34
Thank You

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 26, 2022 34 / 34

You might also like