Lec23 BFS 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Data Structures and Algorithms

(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)

Definition: 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.

sink

How many
sinks can there At most 1.
be in G ?

2
An interesting problem
(Finding a sink)

Problem: Given a directed graph G=(V,E) in an adjacency matrix representation,


design an O() time algorithm to determine if there is any sink in G.

We are allowed to look into only O()


entries of the Adjacency matrix M.

Sink

Question: Can we verify efficiently whether any given vertex is a sink ?


Answer: Yes, in O() time only 
Look at th row and th column of M.

3
Key idea

Can we eliminate non-sink vertices in O() look-up

𝒊
into the Adjacency Matrix M ? M

If M[] = 0, then ?? can not be sink


If M[] = 1 , then ??
can not be sink

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.

Find-Sink(M) // M is the adjacency matrix of the given directed graph.


s 0;
For(=1 to )
{
If (M[s,] = ? ) ….?...;
}
Verify if s is a sink and output accordingly.

(Fill in the details of this pseudo code as a Homework.)


5
What is Graph traversal ?

6
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.
7
Non-triviality 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.

8
Breadth First Search traversal

We shall introduce this traversal technique


through an interesting problem.
computing distances from a vertex.

9
Notations and Observations

Length of a path: the number of edges on the path.

x y

A path of length 6 between x and 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:

Question: What can be the maximum length of any path in a graph ?


Answer:

11
Notations and Observations
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.

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

While reporting , you have


(sub)consciously used an important
property of shortest paths.
Can you state this property ? 14
An important property of shortest paths
𝑷′
A shortest path
between x and y
x v y

𝑷 What can you say


Observation: about ?
If <, …,> is a shortest path from to ,
then <, …,> is also a shortest path.
Proof:
Suppose = <, …,> is not a shortest path between and .
Then let be a shortest path between and .
Length() < Length().
Question: What happens if we concatenate with edge (, ) ?
Answer: a path between and shorter than the shortest-path <, …,> .
 Contradiction.
15
An important question

𝐰
𝐰 𝐰
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

.
.

: Vertices at distance i+1 from x =


Those Neighbors of which …
do not belong to or

How to distinguish the neighbors of which belong to from


those which belong to , ≤ ?
17
How can we compute ?

Key idea: compute ’s in increasing order of .


Initialize Distance[v]  ∞ of each vertex v in the graph.
Initialize Distance[x]  0.

• 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] = ??

We can thus distinguish the neighbors of which


belong to from those which belong to .
18
A neat algorithm for
computing distances from x

we need an algorithm which traverses/visits the vertices in


non-decreasing order of distances from x

This traversal algorithm is called BFS (breadth first search) traversal

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

Question: What do we mean by correctness of BFS traversal from vertex x ?

Answer:

• All vertices reachable from x get visited.

• Vertices get visited in the non-decreasing order of their distances from x.

• At the end of the algorithm,


Distance(v) is the distance of vertex v from x.

We shall discuss this proof in the next


lecture. Please think over it for some
time before next lecture.
23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy