CS 10: Problem solving via Object Oriented Programming - - PowerPoint PPT Presentation

cs 10 problem solving via object oriented programming
SMART_READER_LITE
LIVE PREVIEW

CS 10: Problem solving via Object Oriented Programming - - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 16 Graph Traversals Agenda 1. Depth


slide-1
SLIDE 1

CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡

Winter ¡2017 ¡

¡

Tim ¡Pierson ¡

260 ¡(255) ¡Sudikoff ¡

Day ¡16 ¡– ¡Graph ¡Traversals ¡

slide-2
SLIDE 2

2 ¡

Agenda ¡

  • 1. Depth ¡first ¡search ¡
  • 2. Breadth ¡first ¡search ¡
slide-3
SLIDE 3

3 ¡

Graph ¡traversals ¡are ¡useful ¡to ¡answer ¡ quesRons ¡about ¡vertex ¡relaRonships ¡

Some ¡Graph ¡traversals ¡uses ¡

  • Uses ¡are ¡typically ¡around ¡reachability ¡

¡

  • CompuRng ¡path ¡from ¡vertex ¡u ¡to ¡vertex ¡v

¡

  • Given ¡start ¡vertex ¡s ¡of ¡Graph ¡G, ¡compute ¡a ¡path ¡with ¡the ¡

minimum ¡number ¡of ¡edges ¡between ¡s ¡and ¡all ¡other ¡verRces ¡(or ¡ report ¡no ¡such ¡path ¡exists) ¡ ¡

  • TesRng ¡whether ¡G ¡is ¡fully ¡connected ¡(e.g., ¡all ¡verRces ¡reachable) ¡

¡

  • IdenRfying ¡cycles ¡in ¡G ¡(or ¡reporRng ¡no ¡cycle ¡exists) ¡
  • Today’s ¡examples ¡have ¡no ¡cycles ¡(next ¡class ¡will ¡consider ¡them) ¡
slide-4
SLIDE 4

4 ¡

Depth ¡First ¡Search ¡(DFS) ¡uses ¡a ¡stack ¡to ¡ explore ¡as ¡if ¡in ¡a ¡maze ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡

DFS ¡basic ¡idea ¡

  • Keep ¡going ¡unRl ¡you ¡

can’t ¡go ¡any ¡further, ¡ then ¡back ¡track ¡

  • Relies ¡on ¡a ¡stack ¡(implicit ¡
  • r ¡explicit) ¡to ¡keep ¡track ¡
  • f ¡where ¡you’ve ¡been ¡

Goal ¡ E ¡ H ¡ G ¡ I ¡

Graph ¡structure ¡from ¡h_p://stackoverflow.com/quesRons/687731/breadth-­‑first-­‑vs-­‑depth-­‑first ¡

slide-5
SLIDE 5

5 ¡

Some ¡of ¡you ¡did ¡Depth ¡First ¡Search ¡on ¡ Problem ¡Set ¡1 ¡

RegionFinder ¡

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

¡

slide-6
SLIDE 6

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

¡

6 ¡

Some ¡of ¡you ¡did ¡Depth ¡First ¡Search ¡on ¡ Problem ¡Set ¡1 ¡

RegionFinder ¡ If ¡you ¡added ¡to ¡end ¡of ¡list… ¡

slide-7
SLIDE 7

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

¡

7 ¡

Some ¡of ¡you ¡did ¡Depth ¡First ¡Search ¡on ¡ Problem ¡Set ¡1 ¡

RegionFinder ¡ And ¡if ¡you ¡get ¡pixel ¡from ¡end ¡of ¡ list, ¡you ¡implemented ¡a ¡stack ¡ ¡ If ¡you ¡added ¡to ¡end ¡of ¡list… ¡

slide-8
SLIDE 8

8 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

slide-9
SLIDE 9

9 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

A ¡

Push(A) ¡

slide-10
SLIDE 10

10 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡ Pop(A), ¡mark ¡visited ¡

1 ¡

slide-11
SLIDE 11

11 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡ C ¡ B ¡

Push ¡unvisited ¡adjacent ¡

1 ¡

slide-12
SLIDE 12

12 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡ C ¡

Pop(B), ¡mark ¡visited ¡

2 ¡ 1 ¡

slide-13
SLIDE 13

13 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡ C ¡

Push ¡unvisited ¡adjacent ¡(F, ¡but ¡not ¡A) ¡

2 ¡ F ¡ 1 ¡

slide-14
SLIDE 14

14 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡ C ¡

Pop(F), ¡mark ¡visited ¡

2 ¡ 3 ¡ 1 ¡

slide-15
SLIDE 15

15 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡ C ¡

Push ¡unvisited ¡adjacent ¡(H, ¡but ¡not ¡B) ¡

2 ¡ 3 ¡ 1 ¡ H ¡

slide-16
SLIDE 16

16 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡ C ¡

Pop(H), ¡mark ¡visited ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡

slide-17
SLIDE 17

17 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡ C ¡

Nothing ¡to ¡push, ¡back ¡up ¡by ¡popping ¡C ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡

slide-18
SLIDE 18

18 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡

Pop(C), ¡mark ¡visited ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡

slide-19
SLIDE 19

19 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡ D ¡

Nothing ¡to ¡push, ¡back ¡up ¡by ¡popping ¡D ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡

slide-20
SLIDE 20

20 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡

Pop(D), ¡mark ¡visited ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡

slide-21
SLIDE 21

21 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡

Push ¡unvisited ¡adjacent ¡(G, ¡but ¡not ¡A) ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡ G ¡

slide-22
SLIDE 22

22 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡

Pop(G), ¡mark ¡visited ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡

slide-23
SLIDE 23

23 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡

Push ¡unvisited ¡adjacent ¡(I, ¡but ¡not ¡D) ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ I ¡

slide-24
SLIDE 24

24 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡

Pop(I), ¡mark ¡visited ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡

slide-25
SLIDE 25

25 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡

E ¡

Nothing ¡to ¡push, ¡back ¡up ¡by ¡popping ¡E ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡

slide-26
SLIDE 26

26 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡ Pop(E), ¡mark ¡visited ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡

slide-27
SLIDE 27

27 ¡

Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡

DFS ¡algorithm ¡

stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent if !v.visited stack.push(v)

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

Stack ¡ Found ¡goal ¡

2 ¡ 3 ¡ 1 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡

slide-28
SLIDE 28

28 ¡

Node ¡discovery ¡tells ¡us ¡something ¡about ¡ the ¡graph ¡

Discovery ¡edges ¡

  • Edges ¡that ¡lead ¡to ¡unvisited ¡nodes ¡
  • Discovery ¡edges ¡form ¡a ¡tree ¡on ¡the ¡graph ¡
  • Can ¡traverse ¡from ¡start ¡to ¡goal ¡on ¡tree ¡(if ¡goal ¡reachable) ¡
  • Can ¡tell ¡us ¡which ¡nodes ¡are ¡not ¡reachable ¡(not ¡on ¡path ¡

formed ¡by ¡discovery ¡nodes) ¡

  • Not ¡guaranteed ¡to ¡be ¡shortest ¡path! ¡

¡ Back ¡edges ¡

  • Edges ¡that ¡lead ¡to ¡previously ¡discovered ¡nodes ¡
  • Lead ¡to ¡ancestor ¡nodes ¡in ¡tree ¡
  • Indicate ¡presence ¡of ¡a ¡cycle ¡in ¡the ¡graph ¡
slide-29
SLIDE 29

29 ¡

Run ¡Rme ¡is ¡O(n+m) ¡

Run ¡>me ¡

  • Assume ¡graph ¡with ¡n ¡nodes ¡and ¡m ¡edges ¡
  • Visit ¡each ¡node ¡at ¡most ¡one ¡Rme ¡(due ¡to ¡visited ¡

indicator) ¡

  • Visit ¡each ¡edge ¡at ¡most ¡one ¡Rme ¡
  • Run ¡Rme ¡is ¡O(n+m) ¡
slide-30
SLIDE 30

30 ¡

Agenda ¡

  • 1. Depth ¡first ¡search ¡
  • 2. Breadth ¡first ¡search ¡
slide-31
SLIDE 31

31 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡

BFS ¡basic ¡idea ¡

  • Explore ¡outward ¡in ¡

“ripples” ¡

  • Look ¡at ¡all ¡nodes ¡1 ¡step ¡

away, ¡then ¡all ¡nodes ¡2 ¡ steps ¡away… ¡

  • Relies ¡on ¡a ¡queue ¡

(implicit ¡or ¡explicit) ¡ implementaRon ¡

  • Path ¡found ¡from ¡s ¡to ¡any ¡
  • ther ¡vertex ¡is ¡shortest ¡

Goal ¡ E ¡ H ¡ G ¡ I ¡

slide-32
SLIDE 32

32 ¡

Some ¡of ¡you ¡did ¡Breadth ¡First ¡Search ¡on ¡ Problem ¡Set ¡1 ¡

RegionFinder ¡

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

¡

slide-33
SLIDE 33

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

¡

33 ¡

Some ¡of ¡you ¡did ¡Breadth ¡First ¡Search ¡on ¡ Problem ¡Set ¡1 ¡

RegionFinder ¡ If ¡you ¡added ¡to ¡end ¡of ¡list… ¡

slide-34
SLIDE 34

Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so

¡

34 ¡

Some ¡of ¡you ¡did ¡Breadth ¡First ¡Search ¡on ¡ Problem ¡Set ¡1 ¡

RegionFinder ¡ And ¡if ¡you ¡get ¡pixel ¡from ¡front ¡of ¡ list, ¡you ¡implemented ¡a ¡queue ¡ If ¡you ¡added ¡to ¡end ¡of ¡list… ¡

slide-35
SLIDE 35

35 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

slide-36
SLIDE 36

36 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ enqueue(A) ¡

slide-37
SLIDE 37

37 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ enqueue(A) ¡

A ¡ 1 ¡

slide-38
SLIDE 38

38 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(A), ¡unvisited ¡enqueue ¡adjacent ¡

B ¡ C ¡ D ¡ E ¡ 1 ¡

slide-39
SLIDE 39

39 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(B), ¡enqueue ¡unvisited ¡adjacent ¡F ¡

C ¡ D ¡ E ¡ F ¡ 2 ¡ 1 ¡

slide-40
SLIDE 40

40 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(C), ¡enqueue ¡unvisited ¡adjacent ¡(none) ¡ ¡

D ¡ E ¡ F ¡ 2 ¡ 1 ¡ 3 ¡

slide-41
SLIDE 41

41 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(D), ¡enqueue ¡unvisited ¡adjacent ¡G ¡

E ¡ F ¡ G ¡ 2 ¡ 1 ¡ 3 ¡ 4

slide-42
SLIDE 42

42 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(E), ¡enqueue ¡unvisited ¡adjacent ¡

F ¡ G ¡ 2 ¡ 1 ¡ 3 ¡ 4 5

slide-43
SLIDE 43

43 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ Found ¡goal! ¡

F ¡ G ¡ 2 ¡ 1 ¡ 3 ¡ 4 5

slide-44
SLIDE 44

44 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ Keep ¡going ¡for ¡fun ¡

F ¡ G ¡ 2 ¡ 1 ¡ 3 ¡ 4 5

slide-45
SLIDE 45

45 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(F), ¡enqueue ¡unvisited ¡adjacent ¡H ¡ ¡

G ¡ H ¡ 2 ¡ 1 ¡ 3 ¡ 4 5 6

slide-46
SLIDE 46

46 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(G), ¡enqueue ¡unvisited ¡adjacent ¡I ¡ ¡

H ¡ I ¡ 2 ¡ 1 ¡ 3 ¡ 4 5 6 7

slide-47
SLIDE 47

47 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(H), ¡enqueue ¡unvisited ¡adjacent ¡(none) ¡ ¡

I ¡ 2 ¡ 1 ¡ 3 ¡ 4 5 6 7 8

slide-48
SLIDE 48

48 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ dequeue(I), ¡enqueue ¡unvisited ¡adjacent ¡(none) ¡ ¡

2 ¡ 1 ¡ 3 ¡ 4 5 6 7 8 9

slide-49
SLIDE 49

49 ¡

Breadth ¡First ¡Search ¡(BFS) ¡can ¡find ¡the ¡ shortest ¡path ¡between ¡nodes ¡

A ¡ B ¡ C ¡ D ¡ F ¡ Start ¡ Goal ¡ E ¡ H ¡ G ¡ I ¡

BFS ¡algorithm ¡

enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty: u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)

Queue ¡ All ¡nodes ¡explored ¡

2 ¡ 1 ¡ 3 ¡ 4 5 6 7 8 9

slide-50
SLIDE 50

50 ¡

Node ¡discovery ¡tells ¡us ¡something ¡about ¡ the ¡graph ¡

Discovery ¡edges ¡

  • Lead ¡to ¡unvisited ¡nodes ¡
  • Form ¡a ¡tree ¡on ¡the ¡graph ¡
  • Can ¡traverse ¡from ¡start ¡to ¡goal ¡(or ¡any ¡node) ¡
  • Can ¡tell ¡us ¡which ¡nodes ¡are ¡not ¡reachable ¡(not ¡on ¡path ¡

formed ¡by ¡discovery ¡nodes) ¡

  • Path ¡guaranteed ¡to ¡have ¡smallest ¡number ¡of ¡edges ¡

¡ Can ¡track ¡how ¡we ¡got ¡to ¡node ¡to ¡find ¡shortest ¡path ¡

  • Build ¡vertex ¡tree ¡
  • Parent ¡of ¡each ¡vertex ¡is ¡vertex ¡that ¡discovered ¡it ¡
  • Parent ¡is ¡unique ¡because ¡we ¡don’t ¡visit ¡verRces ¡twice ¡
slide-51
SLIDE 51

51 ¡

Run ¡Rme ¡is ¡O(n+m) ¡

Run ¡>me ¡

  • Assume ¡graph ¡with ¡n ¡nodes ¡and ¡m ¡edges ¡

¡

  • Visit ¡each ¡node ¡at ¡most ¡one ¡Rme ¡(due ¡to ¡visited ¡

indicator ¡ ¡

  • Visit ¡each ¡edge ¡at ¡most ¡one ¡Rme ¡
  • Run ¡Rme ¡O(n+m) ¡
  • Useful ¡for ¡the ¡Kevin ¡Bacon ¡game! ¡