Lecture 10 - Breadth First Search (BFS) Sanjoy Dasgupta Russell - - PowerPoint PPT Presentation

lecture 10 breadth first search bfs
SMART_READER_LITE
LIVE PREVIEW

Lecture 10 - Breadth First Search (BFS) Sanjoy Dasgupta Russell - - PowerPoint PPT Presentation

Lecture 10 - Breadth First Search (BFS) Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal CSE101, Spring 2020, Week-03 Paths in graphs The classic 15-puzzle explore(G,a): a b c d e Graph G = (V,E) V = {configurations of puzzle} f h


slide-1
SLIDE 1

Lecture 10 - Breadth First Search (BFS)

Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal CSE101, Spring 2020, Week-03

slide-2
SLIDE 2

Paths in graphs

The classic 15-puzzle

Graph G = (V,E) V = {configurations of puzzle} E: edges between neighboring configurations

c a g e b f j d i h a b c d e f h i explore(G,a): Finds a path from a to i. But this isn’t the shortest possible path!

slide-3
SLIDE 3

Distances in graphs

Distance between two nodes = length of shortest path between them c a g e b f j d i h a b c d e f h i

distance 0 distance 1 distance 2 distance 3

Physical model: Vertex – ping-pong ball Edge – piece of string dist(a,e) = ? dist(d,g) = ? Suppose we want to compute distances from some starting node s to all other nodes in G. Strategy: layer-by-layer first, nodes at distance 0 then, nodes at distance 1 then, nodes at distance 2, etc.

slide-4
SLIDE 4

Breadth-first search

Suppose we have seen all nodes at distance · d. How to get the next layer? Solution: A node is at distance d+1 if: it is adjacent to some node at distance d it hasn’t been seen yet

procedure bfs(G,s) input: graph G = (V,E); node s in V

  • utput: for each node u, dist[u] is

set to its distance from s for u in V: dist[u] = 1 dist[s] = 0 Q = [s] // queue containing just s while Q is not empty: u = eject(Q) for each edge (u,v) in E: if dist[v] = 1: inject(Q,v) dist[v] = dist[u]+1

slide-5
SLIDE 5

a b c d e f

BFS example

procedure bfs(G,s) for u in V: dist[u] = 1 prev[u] = nil dist[s] = 0 Q = [s] // queue containing just s while Q is not empty: u = eject(Q) for each edge (u,v) in E: if dist[v] = 1: inject(Q,v) dist[v] = dist[u]+1 prev[v] = u

c a e b f d Queue Distances [a] 1 1 1 1 1 [bcd] 1 1 1 1 1 [cd] 1 1 1 1 1 [de] 1 1 1 2 1 [e] 1 1 1 2 1 [f] [] 1 1 1 2 3 1 1 1 2 3 c a e b f d

Shortest path tree

slide-6
SLIDE 6

Why does BFS work?

procedure bfs(G,s) for u in V: dist[u] = 1 dist[s] = 0 Q = [s] while Q is not empty: u = eject(Q) for each edge (u,v) in E: if dist[v] = 1: inject(Q,v) dist[v] = dist[u]+1

Claim For any distance d = 0,1,2,..., there is a point in time at which: (i) all nodes at distance · d have their dist[] values correctly set (ii) all other nodes have dist[] = 1 (iii) the queue Q contains exactly the nodes at distance d

Running time: O(V + E), like DFS

slide-7
SLIDE 7

Two search strategies

c a e b f d c a e b f d c a e b f d

Depth-first Breadth-first

slide-8
SLIDE 8

Edge lengths

BFS treats all edges as having the same length. This is rarely true in applications. Denote the length of edge e = (u,v) by l(e) or le or l(u,v)