25
Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 20 Finding a sink in a directed graph Graph Traversal Breadth First Search Traversal and its simple applications 1

Lecture-20-CS210-2012.pptx

Embed Size (px)

Citation preview

Page 1: Lecture-20-CS210-2012.pptx

Data Structures and Algorithms(CS210/ESO207/ESO211)

Lecture 20• Finding a sink in a directed graph• Graph Traversal

• Breadth First Search Traversal and its simple applications

1

Page 2: Lecture-20-CS210-2012.pptx

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

Page 3: Lecture-20-CS210-2012.pptx

Adjacency Matrix

If M[i,j] = 0, then ?? If M[i,j] = 1 , then ??

3

Mi

j

j can not be sink

i can not be sink

Page 4: Lecture-20-CS210-2012.pptx

Algorithm to compute a sink in a graphA 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

Page 5: Lecture-20-CS210-2012.pptx

What is Graph traversal ?

5

Page 6: Lecture-20-CS210-2012.pptx

Graph traversalDefinition: 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

Page 7: Lecture-20-CS210-2012.pptx

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

Page 8: Lecture-20-CS210-2012.pptx

Breadth First Search traversal

8

We shall introduce this traversal technique through a problem of computing distances from a

vertex.

Page 9: Lecture-20-CS210-2012.pptx

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

Page 10: Lecture-20-CS210-2012.pptx

Some preliminaries

Shortest Path from x to y: A path from x to y of least lengthDistance from x to y: the length of the shortest path from x to y.

10

x

y

Page 11: Lecture-20-CS210-2012.pptx

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

uw

v

r

s

Page 12: Lecture-20-CS210-2012.pptx

An interesting exercise

Compute distance of vertices from x:

: Vertices at distance 0 from x: ??

: Vertices at distance 1 from x: ??

: Vertices at distance 2from x: ??

: Vertices at distance 3from x: ??

12

y

b

c

d

x

f

g

h

uw

v

r

s

{x}

{f,u,b}

{g,h,s,r,v,w}

{d,c,y}

After seeing the first two steps (computing and ), 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?

Page 13: Lecture-20-CS210-2012.pptx

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

Page 14: Lecture-20-CS210-2012.pptx

Let us see if you can establish some relationship between 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+1from x= Those Neighbors of which do not belong to , ≤

14

How to distinguish the neighbors of which belong to from those which belong to , ≤ ?

Page 15: Lecture-20-CS210-2012.pptx

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 .• Then compute.• …• Once we have computed , for every neighbor v of a vertex in , Ifv is in for some ≤, then Distance[v] = ?? Ifv is in ,Distance[v] = ??

15

a number ≤∞

We can thus distinguish the neighbors of which belong to from those which belong to .

Page 16: Lecture-20-CS210-2012.pptx

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 using . Of course, you will use temporary arrays and lists for ’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 won’t compute ’s explicitly and hence no extra arrays or lists will be needed.

Note: Many of you would have noticed a similarity of this problem and its solution with the problem of distances to cells in a grid in presence of obstacles (Lecture 11). That was indeed a special case of this graph problem.

16

Page 17: Lecture-20-CS210-2012.pptx

A neat algorithm for computing distances from x

17

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

Page 18: Lecture-20-CS210-2012.pptx

Using a queue for traversing vertices in non-decreasing order of distances

Compute distance of vertices from x:

18

y

b

c

d

x

f

g

h

uw

v

r

s

x

u b g h

b g h s r v w

g h s r v w

f u b 𝑉 1

𝑉 0

𝑉 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.

Page 19: Lecture-20-CS210-2012.pptx

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

Page 20: Lecture-20-CS210-2012.pptx

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

Page 21: Lecture-20-CS210-2012.pptx

Computing connected components of a graph

21

Page 22: Lecture-20-CS210-2012.pptx

Connected components of a Graph

Page 23: Lecture-20-CS210-2012.pptx

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

Page 24: Lecture-20-CS210-2012.pptx

Computing connected components of a graphBFS(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 ;

Page 25: Lecture-20-CS210-2012.pptx

Analysis of the algorithm

Running time of the algorithm : O(n+m)

Output of the algorithm: Array Label[] of size O(n) such thatLabel[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