Data Structures and Algorithms: (CS210/ESO207/ESO211)
Data Structures and Algorithms: (CS210/ESO207/ESO211)
(CS210/ESO207/ESO211)
Lecture 20
Finding a sink in a directed graph
Graph Traversal
Breadth First Search Traversal and its simple applications
1
An interesting problem
(Finding a sink)
A vertex x in a given directed graph is said to be a sink if
There is no edge emanating from (leaving) x
Every other vertex has an edge into x.
Given a directed graph G=(V,E) given in an adjacency matrix representation,
design an O(n) time algorithm to determine if there is any sink in G.
2
Sink
Adjacency Matrix
If M[i,j] = 0, then ??
If M[i,j] = 1 , then ??
3
M
i
j
j can not be sink
i can not be sink
Algorithm to compute a sink in a graph
A sketch
Key ideas:
Looking at a single entry in M allows us to discard one vertex from being a sink.
It takes O(n) time to verify if a vertex i is a sink
Find-Sink(M) // M is the adjacency matrix of the given directed graph.
s 0;
For(i=1 to n-1)
{
If (M[s,i] = ??) .??...;
}
Check if s is a sink and output accordingly.
(Fill in the details of this pseudo code as an exercise.)
4
What is Graph traversal ?
5
Graph traversal
Definition:
A vertex y is said to be reachable from x if there is a path from x to y.
Graph traversal from vertex x: Starting from a given vertex x, the aim is to
visit all vertices which are reachable from x.
6
x
y
Nontriviality of graph traversal
Avoiding loop:
How to avoid visiting a vertex multiple times ?
(keeping track of vertices already visited)
Finite number of steps :
The traversal must stop in finite number of steps.
Completeness :
We must visit all vertices reachable from the start vertex x.
7
The following points will convince you about
the non-triviality of a graph traversal algorithm
Breadth First Search traversal
8
We shall introduce this traversal technique
through a problem of computing distances from a
vertex.
Some preliminaries
Length of a path: Length of a path is defined as the number of edges on the path.
Question:If <, ,,> is a path of length k from to ,then what is the length of
the path <, ,> ?
Answer: k-1
Question: What can be the maximum length of any path in a graph ?
Answer: n-1
9
x y
A path of length 6 between x and y
Some preliminaries
Shortest Path from x to y: A path from x to y of least length
Distance from x to y: the length of the shortest path from x to y.
10
x
y
An interesting exercise
Exercise: How will you design an algorithm
to compute distance to all vertices reachable
from x in a given undirected graph ?
Spend a few minutes thinking over this
exercise before going to the next slide.
11
y
b
c
d
x
f
g
h
u
w
v
r
s
An interesting exercise
Compute distance of vertices from x:
0
: Vertices at distance 0 from x: ??
1
: Vertices at distance 1 from x: ??
2
: Vertices at distance 2from x: ??
3
: Vertices at distance 3from x: ??
12
y
b
c
d
x
f
g
h
u
w
v
r
s
{x}
{f,u,b}
{g,h,s,r,v,w}
{d,c,y}
After seeing the first two steps (computing
0
and
1
), you
could describe the remaining two steps on your own.
Do you realize that In doing so, you implicitly used a
very important property of shortest paths?
An important property of shortest paths
Observation:
If <, ,,> is a shortest path from to , then <, ,>is also a shortest path.
Proof: If P = <, ,>is not a shortest path between and , then let P be the
shortest path between and . Hence length of P is strictly less than P.
Concatenating P with the edge (v-y) gives a path between and which is shorter
than (the shortest path) <, ,,>, hence a contradiction.
In short, every subpath of a shortest path is also a shortest path.
you used this property of shortest paths implicitly in the example of previous slide ?
13
x y
A shortest path between x and y
v
What can we say about this path??
Let us see if you can establish some relationship
between vertices at different distances from x
0
: Vertices at distance 0 from x = {x}
1
: Vertices at distance 1 from x=
Neighbors of
0
2
: Vertices at distance 2 from x=
Those Neighbors of
1
which do not belong to
0
or
1
.
.
.
+1
: Vertices at distance i+1from x=
Those Neighbors of
,
14
How to distinguish the neighbors of
which belong to
+1
from those which belong to
, ?
How can we compute
+
?
Key idea: Initialize Distance[v] of each vertex vin the graph.
Initialize Distance[x] 0. Now compute
s in increasing order of .
First compute
0
.
Then compute
1
.
Once we have computed
,
Ifv is in
which
belong to
+1
from those which belong to
.
An algorithm for computing distances from x
It is straightforward now to design an iterative algorithm for computing
distance from x wherein during ith iteration you compute vertices (and their
distances from x) of set
+1
using
s.
(do it as an exercise)
We shall now design a very neat algorithm for computing distances from x.
This algorithm will be based on the same idea as the above algorithm, but it
wont compute
2
Remove x and for each neighbor of x that
was unvisited, mark it visited and put it
into queue.
Remove f and for each neighbor of f that
was unvisited, mark it visited and put it
into queue.
Remove u and for each neighbor of u that
was unvisited, mark it visited and put it
into queue.
Remove b and for each neighbor of b that
was unvisited, mark it visited and put it
into queue.
BFS traversal from a vertex
BFS(G, x)
CreateEmptyQueue(Q);
Distance(x) 0;
Enqueue(x,Q);
While( ?? )
{ v Dequeue(Q);
For each neighbor w of v
{ if (Distance(w) = )
{ Distance(w) ?? ;
?? ;
}
}
}
19
Not IsEmptyQueue(Q)
Distance(v) +1
Enqueue(w, Q);
Analysis of BFS(x)
Observations:
A vertex enters the queue at most once.
When a vertex is dequeued, we perform computation of the order of the number
of edges incident on it.
Running time of BFS(x) =
number of edges in the connected component of x.
Theorem: For each v in the connected component of x, Distance(v) stores true
distance from x.
Proof: By induction on distance from x. (Do it as homework exercise. The proof will
be discussed in the lecture on 28 September.)
It is a direct corollary of the above theorem that BFS(x) visits all vertices of connected
component of x. Hence BFS is indeed a graph traversal algorithm.
20
Computing connected components of a graph
21
Connected components of a Graph
How to use BFS traversal to compute connected
components of a graph ?
Sketch :
Initialize Distance[v] for all vertices v.
Pick any vertex x, execute BFS(x).
This will compute connected component containing vertex x.
We may label all vertices visited during BFS(x) with label x.
Pick any vertex v with Distance[v] = , execute BFS(v).
This will compute connected component containing vertex v.
We may label all vertices visited during BFS(v) with label v.
Pick any vertex y with Distance[y] = , execute BFS(y).
. and so on.
23
Computing connected components of a graph
BFS(x)
CreateEmptyQueue(Q);
Distance(x) 0;
Enqueue(x,Q);
While(Not IsEmptyQueue(Q))
{ v Dequeue(Q);
For each neighbor w of v
{ if (Distance(w) = )
{ Distance(w) Distance(v) +1 ;
Enqueue(w, Q);
}
}
}
Connected-components(G)
{ For each vertex x Distance(x) ; Create an array Label;
For each vertex v
If (Distance(v) = ) {Label[x] x; BFS(x); }
return Label;
}
24
Label[w] x ;
Analysis of the algorithm
Running time of the algorithm : O(n+m)
Output of the algorithm: Array Label[] of size O(n) such that
Label[x]=Label[y] if and only if x and y belong to same connected component.
Theorem: An undirected graph can be processed in O(n+m) time to build an
O(n) size data structure which can answer any connectivity query in O(1)
time.
25