graphs –breadth first search - york university · 2019. 5. 9. · breadth-first search idea: send...

54
Graphs – Breadth First Search ORD DFW SFO LAX

Upload: others

Post on 29-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Graphs – Breadth First Search

ORD

DFW

SFO

LAX

Page 2: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Outline

BFS Algorithm

BFS Application: Shortest Path on an unweighted graph

Unweighted Shortest Path: Proof of Correctness

Page 3: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Outline

BFS Algorithm

BFS Application: Shortest Path on an unweighted graph

Unweighted Shortest Path: Proof of Correctness

Page 4: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Breadth-First Search Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G

Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G

BFS on a graph with |V| vertices and |E| edges takes O(|V|+|E|) time

BFS can be further extended to solve other graph problems Cycle detection

Find and report a path with the minimum number of edges between two given vertices

Page 5: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS Algorithm PatternBFS(G,s)Precondition: G is a graph, s is a vertex in GPostcondition: all vertices in G reachable from s have been visited

for each vertex uV [G] color[u] BLACK //initialize vertex

colour[s] REDQ.enqueue(s)while Q

uQ.dequeue()for each v Adj[u] //explore edge (u,v)

if color[v ] = BLACKcolour[v]REDQ.enqueue(v)

colour [u]GRAY

Page 6: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS is a Level-Order Traversal

Notice that in BFS exploration takes place on a wavefront consisting of nodes that are all the same distance from the source s.

We can label these successive wavefronts by their distance: L0, L1, …

Page 7: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS Example

CB

A

E

D

discovery edge

cross edge

A discovered (on Queue)A undiscovered

unexplored edge

L0

L1

F

CB

A

E

DL1

F

CB

A

E

D

L0

L1

F

A finished

Page 8: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS Example (cont.)

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 9: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS Example (cont.)

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 10: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

PropertiesNotation

Gs: connected component of sProperty 1

BFS(G, s) visits all the vertices and edges of Gs

Property 2The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs

Property 3For each vertex v in Li The path of Ts from s to v has i

edges Every path from s to v in Gs has at

least i edges

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

Page 11: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Analysis Setting/getting a vertex/edge label takes O(1) time

Each vertex is labeled three times once as BLACK (undiscovered)

once as RED (discovered, on queue)

once as GRAY (finished)

Each edge is considered twice (for an undirected graph)

Each vertex is placed on the queue once

Thus BFS runs in O(|V|+|E|) time provided the graph is represented by an adjacency list structure

Page 12: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Applications

BFS traversal can be specialized to solve the following problems in O(|V|+|E|) time:Compute the connected components of G

Compute a spanning forest of G

Find a simple cycle in G, or report that G is a forest

Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists

Page 13: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Outline

BFS Algorithm

BFS Application: Shortest Path on an unweightedgraph

Unweighted Shortest Path: Proof of Correctness

Page 14: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Application: Shortest Paths on an Unweighted Graph

Goal: To recover the shortest paths from a source node s to all other reachable nodes v in a graph. The length of each path and the paths themselves are returned.

Notes: There are an exponential number of possible paths

Analogous to level order traversal for trees

This problem is harder for general graphs than trees because of cycles!

s

?

Page 15: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Breadth-First Search

Idea: send out search ‘wave’ from s.

Keep track of progress by colouring vertices: Undiscovered vertices are coloured black

Just discovered vertices (on the wavefront) are coloured red.

Previously discovered vertices (behind wavefront) are coloured grey.

Graph ( , ) (directed or undirected) and sourceInput: vertex .G V E s V

[ ] shortest path distance ( , ) from to , . [ ] such that ( , ) is las

Outpu

t edg

t:

e on shortest path from a to

.d v s v s v v V

v u u v s v

Page 16: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS Algorithm with Distances and PredecessorsBFS(G,s)Precondition: G is a graph, s is a vertex in GPostcondition: d[u] shortest distance [u] and [u] = predecessor of u on shortest path from s to each vertex u in G

for each vertex uV [G]d[u] [u] null color[u] = BLACK //initialize vertex

colour[s] REDd[s] 0 Q.enqueue(s)while Q

uQ.dequeue()for each v Adj[u] //explore edge (u,v)

if color[v ] = BLACKcolour[v]REDd[v ] d[u]1 [v ] uQ.enqueue(v)

colour [u]GRAY

Page 17: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

First-In First-Out (FIFO) queuestores ‘just discovered’ vertices

Page 18: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

s

d=0

d=0

Page 19: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS FoundNot Handled

Queue

d=0a

bgd

d=1

s

a

c

h

k

f

i

l

m

j

e

b

gd

d=0d=1

Page 20: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

a

bgd

d=0d=1

d=1

Page 21: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

bgd

cf

d=0d=1

d=2

d=1

d=2

Page 22: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

bg

cfme

d=0d=1

d=2

d=1

d=2

Page 23: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queued=0

d=1

d=2

b

j

cfme

d=1

d=2

Page 24: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queued=0

d=1

d=2

j

cfme

d=1

d=2

Page 25: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

cfmej

d=0d=1

d=2

d=2

Page 26: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

fmejhi

d=0d=1

d=2

d=3

d=2

d=3

Page 27: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

mejhi

d=0d=1

d=2

d=3

d=2

d=3

Page 28: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

ejhil

d=0d=1

d=2

d=3

d=2

d=3

Page 29: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

jhil

d=0d=1

d=2

d=3

d=2

d=3

Page 30: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

hil

d=0d=1

d=2

d=3

d=2

d=3

Page 31: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

h

d=0d=1

d=2

d=3

il

d=3

Page 32: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

ilk

d=0d=1

d=2

d=3d=4

d=3

d=4

Page 33: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

lk

d=0d=1

d=2

d=3d=4

d=3

d=4

Page 34: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

k

d=0d=1

d=2

d=3d=4

d=3

d=4

Page 35: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

k

d=0d=1

d=2

d=3d=4

d=4

Page 36: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queued=0

d=1

d=2

d=3d=4

d=4d=5

Page 37: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Breadth-First Search Algorithm: Properties

Q is a FIFO queue.

Each vertex assigned finite dvalue at most once.

Q contains vertices with dvalues {i, …, i, i+1, …, i+1}

d values assigned are monotonically increasing over time.

BFS(G,s)Precondition: G is a graph, s is a vertex in GPostcondition: d[u] shortest distance [u] and [u] = predecessor of u on shortest paths from s to each vertex u in G

for each vertex uV [G]d[u] [u] null color[u] = BLACK //initialize vertex

colour[s] REDd[s] 0 Q.enqueue(s)while Q

uQ.dequeue()for each v Adj[u] //explore edge (u,v)

if color[v ] = BLACKcolour[v]REDd[v ] d[u]1 [v ] uQ.enqueue(v)

colour [u]GRAY

Page 38: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Breadth-First-Search is Greedy

Vertices are handled (and finished): in order of their discovery (FIFO queue)

Smallest d values first

Page 39: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Outline

BFS Algorithm

BFS Application: Shortest Path on an unweighted graph

Unweighted Shortest Path: Proof of Correctness

Page 40: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Basic Steps:

su

The shortest path to uhas length d

v

& there is an edge from u to v

There is a path to v with length d+1.

Correctness

d

Page 41: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Correctness: Basic Intuition

When we discover v, how do we know there is not a shorter path to v? Because if there was, we would already have discovered it!

su vd

Page 42: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Correctness: More Complete Explanation

Vertices are discovered in order of their distance from the source vertex s.

Suppose that at time t1 we have discovered the set Vd of all vertices that are a distance of d from s.

Each vertex in the set Vd+1 of all vertices a distance of d+1 from s must be adjacent to a vertex in Vd

Thus we can correctly label these vertices by visiting all vertices in the adjacency lists of vertices in Vd.

su vd

Page 43: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Inductive Proof of BFS

Suppose at step i that the set of nodes Si with distance (v) di have been discovered and their distance values d[v ] have been correctly assigned.

Any node v with (v) di 1 must be adjacent to Si .

Any node v adjacent to Si but not in Si must have (v) di 1.

At step i 1, all nodes on the queue with d values of di are dequeued and processed.

Thus after step i 1, all nodes v with distance (v) di 1 have been discoveredand their distance values d[v ] have been correctly assigned.

Further suppose that the queue contains only nodes in Si with d values of di .

In so doing, all nodes adjacent to Si are discovered and assigned d values of di 1.

Furthermore, the queue contains only nodes in Si with d values of di 1.

Page 44: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Correctness: Formal Proof

Graph ( , ) (directed or undirected) and sourceInput: vertex .G V E s V

Output: d[v] distance (v) from s to v, v V . [v] u such that (u,v ) is last edge on shortest path from s to v .

1. [ ] ( , )d v s v v V

2. [ ] ( , ) d v s v v V

Two-step proof:

On exit:

Page 45: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Claim 1. is never too small: [ ] ( , )d d v s v v VProof: There exists a path from s to v of length d[v ].

By Induction:Suppose it is true for all vertices thus far discovered ( an grre d d ey).

is discovered from some adjacent vertex being handled.uv

[ ] [ ] 1d v d u ( , ) 1us ( , )s v u v

s

since each vertex is assigned a value exactly once, it follows that o [ ]n exit, ( ., )d v s v

vv V

d

Page 46: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS(G,s)Precondition: G is a graph, s is a vertex in GPostcondition: d[u] shortest distance [u] and [u] = predecessor of u on shortest paths from s to each vertex u in G

for each vertex uV [G]d[u] [u] null color[u] = BLACK //initialize vertex

colour[s] REDd[s] 0 Q.enqueue(s)while Q

uQ.dequeue()for each v Adj[u] //explore edge (u,v)

if color[v ] = BLACKcolour[v]REDd[v ] d[u]1 [v ] uQ.enqueue(v)

colour [u]GRAY

<LI>: [ ] ( , ) 'disco rvered' ( o gr )eyred d v s v v V

( , ) 1s u ( , )s v

Claim 1. is never too small: [ ] ( , )d d v s v v VProof: There exists a path from s to v of length d[v ].

su v

Page 47: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Claim 2. is never too big: [ ] ( , ) d d v s v v VProof by contradiction:

Suppose one or more vertices receive a value greater than .d

Let be the vertex with minimum ( , ) that receives such a value.s dv v

Let be 's predecessor on a shortest path from to .u sv v

su v

Suppose that is discovered and assigned this d value when vertex is dequeued.v x

[ ] [ ] 1d x d v

[ ] ( , ) 1d s vu

( , ) [ ]vs d v

vertices are dequeued in increasing order of Reca v .ll: alued

u was dequeued before x. [ ] [ ] 1 ( , )dvd u s v

x ( , ) 1 [ ] 1v d vs

[ ] [ ]d u d x

Then

Contradiction!

Page 48: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Correctness

Claim 1. is never too small: [ ] ( , )d d v s v v V

Claim 2. is never too big: [ ] ( , ) d d v s v v V

is just right: [ ] ( , ) d d v s v v V

Page 49: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Progress? On every iteration one vertex is processed (turns gray).BFS(G,s)Precondition: G is a graph, s is a vertex in GPostcondition: d[u] shortest distance [u] and [u] = predecessor of u on shortest paths from s to each vertex u in G

for each vertex uV [G]d[u] [u] null color[u] = BLACK //initialize vertex

colour[s] REDd[s] 0 Q.enqueue(s)while Q

uQ.dequeue()for each v Adj[u] //explore edge (u,v)

if color[v ] = BLACKcolour[v]REDd[v ] d[u]1 [v ] uQ.enqueue(v)

colour [u]GRAY

Page 50: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

The shortest path problem has the optimal substructure property: Every subpath of a shortest path is a shortest path.

The optimal substructure property is a hallmark of both greedy and dynamic programming algorithms.

allows us to compute both shortest path distance and the shortest paths themselves by storing only one d value and one predecessor value per vertex.

Optimal Substructure Property

u vs

shortest path

shortest path shortest path

How would we prove this?

Page 51: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Recovering the Shortest PathFor each node v, store predecessor of v in (v).

su v

Predecessor of v is

(v)

(v) = u.

Page 52: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Recovering the Shortest Path

Precondition: and are vertices of graph Postcondition: the vertices on the shortest path from to have been prin

P

if then

RINT-PATH( , , )

pr

print

ted in o

else

intif

rd

then [ ] I "

e

L

r

N

s v Gs v

s

v

v

ss

G

v

elseno path from" "to" "exists"

PRINT-PATH( , , [ ])print

s v

G s vv

Page 53: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

BFS Algorithm without ColoursBFS(G,s)Precondition: G is a graph, s is a vertex in GPostcondition: predecessors [u] and shortest distance d[u] from s to each vertex u in G has been computed

for each vertex uV [G]d[u] [u] null

d[s] 0 Q.enqueue(s)while Q

uQ.dequeue()for each v Adj[u] //explore edge (u,v)

if d[v ] = d[v ] d[u]1 [v ] uQ.enqueue(v)

Page 54: Graphs –Breadth First Search - York University · 2019. 5. 9. · Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouringvertices: Undiscovered

Outline

BFS Algorithm

BFS Application: Shortest Path on an unweighted graph

Unweighted Shortest Path: Proof of Correctness