Lec23 BFS 1
Lec23 BFS 1
Lec23 BFS 1
(ESO207)
Lecture 23
• Finding a sink in a directed graph
• Graph Traversal
• Breadth First Search Traversal and its simple applications
1
An interesting problem
(Finding a sink)
sink
How many
sinks can there At most 1.
be in G ?
2
An interesting problem
(Finding a sink)
Sink
3
Key idea
𝒊
into the Adjacency Matrix M ? M
4
Algorithm to find a sink in a graph
Key ideas:
• Looking at a single entry in M allows us to discard one vertex from being a sink.
• It takes O() time to verify if a vertex is a sink.
6
Graph traversal
Definition:
A vertex y is said to be reachable from x if there is a path from x to y.
• Avoiding loop:
How to avoid visiting a vertex multiple times ?
(keeping track of vertices already visited)
• Completeness :
We must visit all vertices reachable from the start vertex x.
8
Breadth First Search traversal
9
Notations and Observations
x y
10
Notations and Observations
x 𝐯 y
𝒌 −𝟏
Observation:
If <, …,> is a path of length from to ,
then what is the length of the path <, …,> ?
Answer:
11
Notations and Observations
Shortest Path from x to y: A path from x to y of least length
12
Shortest Paths
in Undirected Graphs
Problem:
x b How to compute distance to all vertices
reachable from x in a given undirected graph ?
w
f u
h v d
g r
y
s c
13
Shortest Paths
in Undirected Graphs
x b
: Vertices at distance 0 from x: ?? {x}
w
f u
: Vertices at distance 1 from x: ?? {f,u,b}
h v d
: Vertices at distance 2 from x: ?? {g,h,s,r,v,w}
g r
y
s Why ?
c
𝐰
𝐰 𝐰
A shortest path
between x and v
x 𝒌 v
Question:
Let (,) be an edge. If Distance(,) is , 𝒌 −𝟏 𝒌 𝒌+ 𝟏
then what can be Distance(,) ?
Answer: an element from the set {, , } only.
16
Relationship among vertices
at different distances from x
: Vertices at distance 0 from x = {x}
: Vertices at distance 1 from x =
Neighbors of
: Vertices at distance 2 from x =
.
Those Neighbors of which … do not belong to or
.
.
• First compute .
• Then compute .
• …
• Once we have computed , for every neighbor v of a vertex in ,
If v is in for some , then Distance[v] = ??
a number ≤
If v is in , Distance[v] = ??
∞
19
Using a queue for traversing vertices in
non-decreasing order of distances
Compute distance of vertices from x:
x b
x 𝑉0
w
f u
fub 𝑉1
h v d
ubgh
g r
y
bghsrvw
s c
Remove x and for each neighbor of x that
was unvisited, mark it visited and put it ghsrvw 𝑉2
Remove f and for
intoeach neighbor of f that
queue.
Remove u and for
was unvisited, each
mark neighbor
itneighbor of uput
visited and that
it
Remove b and for each
was unvisited, mark it visited of
and b
putthat
it
into queue.
was unvisited,into
mark it visited and put it
queue.
into queue.
20
BFS traversal from a vertex
BFS(G, 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) ??+1
Distance(v) ;
Enqueue(w,??
Q); ;
}
}
}
21
Running time of BFS traversal
BFS(G, x)
CreateEmptyQueue(Q);
A vertex can enter queue
Distance(x) 0;
at most once.
Enqueue(x,Q); Prove this claim first.
While( ??
Not IsEmptyQueue(Q) )
{ v Dequeue(Q);
For each neighbor w of v
{
if (Distance(w) = ∞)
{ Distance(w) ??+1
Distance(v) ; O(deg(v))
Enqueue(w,??
Q); ;
}
}
}
Running time of BFS(x) = no. of edges in the connected component of x. 22
Correctness of BFS traversal
Answer: