Graph Traversals: Breadth-First and Depth-First Search Eric - - PowerPoint PPT Presentation

graph traversals
SMART_READER_LITE
LIVE PREVIEW

Graph Traversals: Breadth-First and Depth-First Search Eric - - PowerPoint PPT Presentation

Graph Traversals: Breadth-First and Depth-First Search Eric Eaton Bryn Mawr College Computer Science Department What is a Graph? Graphs are collections of vertex vertices joined by edges


slide-1
SLIDE 1

Graph ¡Traversals: ¡

Breadth-­‑First ¡and ¡Depth-­‑First ¡Search ¡

Eric Eaton

Bryn Mawr College Computer Science Department

slide-2
SLIDE 2

What ¡is ¡a ¡Graph? ¡

Graphs are collections of vertices joined by edges “Graph” ¡≡ ¡“Network” ¡

Vertices Edges vertices edges, arcs math nodes links, relations computer science sites bonds physics actors ties, relations sociology

vertex edge

2

slide-3
SLIDE 3

3

Example ¡Networks ¡

Airline Network

(Source: Northwest Airlines)

Terrorist Network

(by Valdis Krebs, Orgnet.com)

School Friendship Network

(from Moody 2001)

Protein-Protein Interactions

(by Peter Uetz)

slide-4
SLIDE 4

Other ¡Applica8ons ¡

  • Intersections and streets within a city
  • Computer networks
  • Electronic circuits
  • Food webs
  • Gene regulatory networks
  • Steps to solve a puzzle
  • many more...

4

slide-5
SLIDE 5

Outline ¡

  • Introduc8on ¡
  • Graph ¡Basics ¡
  • Graph ¡Search ¡Problem ¡

– Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡

  • Complexity ¡Analysis ¡

5

slide-6
SLIDE 6

Outline ¡

  • IntroducEon ¡
  • Graph ¡Basics ¡
  • Graph ¡Search ¡Problem ¡

– Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡

  • Complexity ¡Analysis ¡

6

slide-7
SLIDE 7

Basic ¡Graph ¡Defini8ons ¡

§ A graph G = (V,E) consists of a finite set of vertices V and a finite set of edges E § Each edge is a pair (u,v) where u,v ∈ V

§ V and E are sets, so each vertex u ∈ V is unique, and each edge e ∈ E is unique § v is adjacent to u

§ We will focus on two types:

§ Undirected graphs § Directed graphs

7

u v (u,v)

slide-8
SLIDE 8

Undirected ¡Graph ¡

8

2 1 3 4 5

All ¡edges ¡are ¡ two-­‑way ¡

n V ¡= ¡{ ¡1, ¡2, ¡3, ¡4, ¡5 ¡} ¡ n ¡Edges ¡are ¡unordered ¡pairs: ¡

E ¡= ¡{ ¡{1,2}, ¡{2,3}, ¡{3,4}, ¡{2,4}, ¡{4,5}, ¡{5,1} ¡} ¡

slide-9
SLIDE 9

Directed ¡Graph ¡

9

2 1 3 4 5

All ¡edges ¡are ¡ “one-­‑way” ¡as ¡ indicated ¡by ¡ the ¡arrows ¡

n V ¡= ¡{ ¡1, ¡2, ¡3, ¡4, ¡5 ¡} ¡ n ¡Edges ¡are ¡ordered ¡pairs: ¡

E ¡= ¡{ ¡(1,2), ¡(2,4), ¡(3,2), ¡(4,3), ¡(4,5), ¡(5,1), ¡(5,4) ¡} ¡

slide-10
SLIDE 10

Degree ¡

Undirected ¡Graphs ¡ degree(u): ¡ ¡the ¡number ¡of ¡edges ¡{u,v} ¡for ¡all ¡v ∈ V

10

2 1 3 4 5

slide-11
SLIDE 11

Degree ¡

Undirected ¡Graphs ¡ degree(u): ¡ ¡the ¡number ¡of ¡edges ¡{u,v} ¡for ¡all ¡v ∈ V Directed ¡Graphs ¡ in-­‑degree(u): ¡ ¡the ¡number ¡of ¡edges ¡(v,u) ¡for ¡all ¡v ∈ V

  • ut-­‑degree(u): ¡ ¡the ¡number ¡of ¡edges ¡(u,v) ¡for ¡all ¡v ∈ V

¡

11

2 1 3 4 5 2 1 3 4 5

slide-12
SLIDE 12

12

Paths ¡in ¡Graphs ¡

§ A ¡path ¡in ¡a ¡graph ¡is ¡a ¡sequence ¡of ¡verEces ¡ ¡ s.t. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡1 ¡≤ ¡i ¡< ¡n ¡ § The ¡path’s ¡length ¡is ¡the ¡number ¡of ¡edges ¡on ¡the ¡path ¡

– The ¡length ¡of ¡the ¡path ¡from ¡a ¡vertex ¡to ¡itself ¡is ¡0 ¡

§ In ¡a ¡simple ¡path, ¡all ¡verEces ¡are ¡disEnct ¡

– The ¡first ¡and ¡last ¡verEces ¡may ¡be ¡the ¡same ¡

w1, w2, . . . , wn (wi, wi+1) ∈ E

2 1 3 4 5

path length = 4

slide-13
SLIDE 13

Paths ¡in ¡Graphs ¡

§ How ¡many ¡simple ¡paths ¡are ¡there ¡from ¡1 ¡to ¡4 ¡ and ¡what ¡are ¡their ¡lengths? ¡

13

2 1 3 4 5 2 1 3 4 5

slide-14
SLIDE 14

Outline ¡

  • IntroducEon ¡
  • Graph ¡Basics ¡
  • Graph ¡Search ¡Problem ¡

– Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡

  • Complexity ¡Analysis ¡

14

slide-15
SLIDE 15

Outline ¡

  • IntroducEon ¡
  • Graph ¡Basics ¡
  • Graph ¡Search ¡Problem ¡

– Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡

  • Complexity ¡Analysis ¡

15

slide-16
SLIDE 16

Graph ¡Search ¡Problem ¡

§ Goal: ¡ ¡Find ¡a ¡simple ¡path ¡from ¡a ¡starEng ¡ vertex ¡to ¡a ¡goal ¡vertex ¡ § What ¡applicaEons ¡can ¡be ¡framed ¡as ¡ instances ¡of ¡this ¡problem? ¡

17

v1 v7 v2 v8 v4 v6 v3 v9

v10

v5 start goal

slide-17
SLIDE 17

Intui8on ¡

§ From ¡starEng ¡vertex, ¡keep ¡expanding ¡verEces ¡ ¡ unEl ¡we ¡find ¡the ¡goal ¡ ¡ ¡ ¡ ¡

¡

¡

18

slide-18
SLIDE 18

Intui8on ¡

§ From ¡starEng ¡vertex, ¡keep ¡expanding ¡verEces ¡ ¡ unEl ¡we ¡find ¡the ¡goal ¡ ¡ ¡ ¡ ¡

¡

¡

19

v1 v7 v2 v8 v4 v6 v3 v9

v10

v5 start goal

slide-19
SLIDE 19

Intui8on ¡

§ From ¡starEng ¡vertex, ¡keep ¡expanding ¡verEces ¡ ¡ unEl ¡we ¡find ¡the ¡goal ¡ ¡ ¡ ¡ ¡

¡

¡

20

v1 v7 v2 v8 v4 v6 v3 v9

v10

v5 start goal

slide-20
SLIDE 20

Intui8on ¡

§ From ¡starEng ¡vertex, ¡keep ¡expanding ¡verEces ¡ ¡ unEl ¡we ¡find ¡the ¡goal ¡ ¡ ¡ ¡ ¡

¡

¡

21

v1 v7 v2 v8 v4 v6 v3 v9

v10

v5 start goal

slide-21
SLIDE 21

Intui8on ¡

§ From ¡starEng ¡vertex, ¡keep ¡expanding ¡verEces ¡ ¡ unEl ¡we ¡find ¡the ¡goal ¡ ¡ ¡ ¡ ¡

¡

¡

§ Breadth-­‑First: ¡ ¡expand ¡shallowest ¡unexpanded ¡vertex ¡ § Depth-­‑First: ¡ ¡expand ¡deepest ¡unexpanded ¡vertex ¡

22

v1 v7 v2 v8 v4 v6 v3 v9

v10

v5 start goal

slide-22
SLIDE 22

Queuing ¡Func8on ¡

§ Used ¡to ¡maintain ¡a ¡ranked ¡list ¡of ¡nodes ¡that ¡are ¡ candidates ¡for ¡expansion ¡

§ Called ¡the ¡“fringe” ¡

§ SubsEtuEng ¡different ¡queuing ¡funcEons ¡yields ¡ different ¡searches ¡

23

v1 v7 v2 v8 v4 v6 v3 v9

v10

v5 start goal

slide-23
SLIDE 23

Protec8on ¡Against ¡Cycles ¡

§ We ¡need ¡to ¡guard ¡against ¡cycles ¡

§ Mark ¡each ¡vertex ¡as ¡“closed” ¡when ¡we ¡encounter ¡it ¡ § Do ¡not ¡consider ¡closed ¡verEces ¡again ¡

25

v1 v7 v2 v8 v4 v6 v3 v9

v10

v5 start goal

slide-24
SLIDE 24

Bookkeeping ¡Structures ¡

§ Node: ¡

– vertex ¡ID ¡ – predecessor ¡node ¡ – path ¡length ¡ ¡

§ Problem: ¡

– graph ¡ – starEng ¡vertex ¡ – goalTest(Vertex ¡v) ¡– ¡tests ¡if ¡vertex ¡is ¡a ¡goal ¡state ¡

26

slide-25
SLIDE 25

General ¡Graph ¡Search ¡

// problem describes the graph, start vertex, and goal test // queueingfn is a comparator function that ranks two states // graphSearch returns either a goal node or failure graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

//empty lists queuingFn(open, new Node(problem.startvertex)) //init loop { if empty(open) then return FAILURE //no nodes remain c = removeFront(open) //get current node if problem.goalTest(c.vertex) //goal test return c if c.vertex is not in closed { //avoid duplicates add c.vertex to closed for each Vertex w adjacent to c.vertex //expand node if w is not in closed queuingFn(open, new Node(w,c)); } } }

27

slide-26
SLIDE 26

Applica8on: ¡ ¡Route ¡Finding ¡

28

A G H D F C I J E B start goal

slide-27
SLIDE 27

Breadth-­‑First ¡Search ¡

Expands ¡the ¡“shallowest” ¡vertex ¡

slide-28
SLIDE 28

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

30

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

A G H D F C I J E B start goal

  • pen ¡list ¡

closed ¡list ¡

slide-29
SLIDE 29

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

31

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

A G H D F C I J E B start goal

  • pen ¡list ¡

(A,0,null) ¡ closed ¡list ¡

slide-30
SLIDE 30

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

32

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

A G H D F C I J E B start goal

  • pen ¡list ¡

(A,0,null) ¡ closed ¡list ¡

slide-31
SLIDE 31

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

33

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(A,0,null) ¡ closed ¡list ¡ A G H D F C I J E B start goal

slide-32
SLIDE 32

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

34

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

closed ¡list ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-33
SLIDE 33

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

35

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

closed ¡list ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-34
SLIDE 34

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

36

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

closed ¡list ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-35
SLIDE 35

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

37

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

closed ¡list ¡ A ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-36
SLIDE 36

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

38

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

closed ¡list ¡ A ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-37
SLIDE 37

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

39

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(B,1,A) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-38
SLIDE 38

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

40

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(B,1,A) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ A G H D F C I J E B start goal

slide-39
SLIDE 39

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

41

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-40
SLIDE 40

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

42

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-41
SLIDE 41

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

43

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-42
SLIDE 42

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

44

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-43
SLIDE 43

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

45

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-44
SLIDE 44

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

46

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

Need ¡to ¡add ¡(C,2,B) ¡and ¡(F,2,B) ¡to ¡open ¡ ¡ Since ¡BFS ¡expands ¡the ¡shallowest ¡node, ¡ what ¡must ¡we ¡insure ¡about ¡the ¡open ¡list? ¡ ¡ What ¡queuing ¡funcEon ¡should ¡we ¡use? ¡

slide-45
SLIDE 45

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

47

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ (C,2,B) ¡ (F,2,B) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal BFS ¡uses ¡a ¡ ¡ FIFO ¡Queue! ¡

slide-46
SLIDE 46

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

48

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(D,1,A) ¡ (C,2,B) ¡ (F,2,B) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (C,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-47
SLIDE 47

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

49

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(D,1,A) ¡ (C,2,B) ¡ (F,2,B) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ node ¡c ¡ (C,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-48
SLIDE 48

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

50

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(D,1,A) ¡ (C,2,B) ¡ (F,2,B) ¡ (F,2,C) ¡ (E,2,C) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ node ¡c ¡ (C,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-49
SLIDE 49

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

51

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,2,B) ¡ (F,2,B) ¡ (F,2,C) ¡ (E,2,C) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ node ¡c ¡ (D,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-50
SLIDE 50

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

52

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,2,B) ¡ (F,2,B) ¡ (F,2,C) ¡ (E,2,C) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ node ¡c ¡ (D,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-51
SLIDE 51

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

53

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,2,B) ¡ (F,2,B) ¡ (F,2,C) ¡ (E,2,C) ¡ (E,2,D) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ node ¡c ¡ (D,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-52
SLIDE 52

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

54

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(F,2,B) ¡ (F,2,C) ¡ (E,2,C) ¡ (E,2,D) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ node ¡c ¡ (C,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-53
SLIDE 53

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

55

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(F,2,B) ¡ (F,2,C) ¡ (E,2,C) ¡ (E,2,D) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ node ¡c ¡ (C,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-54
SLIDE 54

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

56

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(F,2,C) ¡ (E,2,C) ¡ (E,2,D) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-55
SLIDE 55

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

57

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(F,2,C) ¡ (E,2,C) ¡ (E,2,D) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ F ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-56
SLIDE 56

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

58

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(F,2,C) ¡ (E,2,C) ¡ (E,2,D) ¡ (G,3,F) ¡ (H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ F ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-57
SLIDE 57

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

59

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(E,2,C) ¡ (E,2,D) ¡ (G,3,F) ¡ (H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ F ¡ node ¡c ¡ (F,2,C) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-58
SLIDE 58

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

60

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(E,2,C) ¡ (E,2,D) ¡ (G,3,F) ¡ (H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ F ¡ node ¡c ¡ (F,2,C) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-59
SLIDE 59

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

61

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(E,2,D) ¡ (G,3,F) ¡ (H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ F ¡ node ¡c ¡ (E,2,C) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-60
SLIDE 60

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

62

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(E,2,D) ¡ (G,3,F) ¡ (H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ E ¡ F ¡ node ¡c ¡ (E,2,C) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-61
SLIDE 61

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

63

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(G,3,F) ¡ (H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ E ¡ F ¡ node ¡c ¡ (E,2,D) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-62
SLIDE 62

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

64

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ E ¡ F ¡ node ¡c ¡ (G,3,F) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-63
SLIDE 63

Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡

65

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(H,3,F) ¡ ¡ closed ¡list ¡ A ¡ B ¡ C ¡ D ¡ E ¡ F ¡ node ¡c ¡ (G,3,F) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-64
SLIDE 64

Breadth-­‑First ¡Search ¡

66

A G H D F C I J E B start

slide-65
SLIDE 65

Breadth-­‑First ¡Search ¡

67

A G H D F C I J E B start 1

slide-66
SLIDE 66

Breadth-­‑First ¡Search ¡

68

A G H D F C I J E B start 1 2

slide-67
SLIDE 67

Breadth-­‑First ¡Search ¡

69

A G H D F C I J E B start 1 2 3

slide-68
SLIDE 68

Breadth-­‑First ¡Search ¡

70

A G H D F C I J E B start 1 2 3 4

slide-69
SLIDE 69

Depth-­‑First ¡Search ¡

Expands ¡the ¡“deepest” ¡vertex ¡

slide-70
SLIDE 70

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

72

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

closed ¡list ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-71
SLIDE 71

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

73

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(B,1,A) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ node ¡c ¡ (A,0,null) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-72
SLIDE 72

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

74

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-73
SLIDE 73

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

75

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-74
SLIDE 74

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

76

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal

Need ¡to ¡add ¡(C,2,B) ¡and ¡(F,2,B) ¡to ¡open ¡ ¡ Since ¡DFS ¡expands ¡the ¡deepest ¡node, ¡what ¡ must ¡we ¡insure ¡about ¡the ¡open ¡list? ¡ ¡ What ¡queuing ¡funcEon ¡should ¡we ¡use? ¡

slide-75
SLIDE 75

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

77

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(F,2,B) ¡ (C,2,B) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ A G H D F C I J E B start goal DFS ¡uses ¡a ¡ ¡ LIFO ¡Stack! ¡

slide-76
SLIDE 76

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

78

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,2,B) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-77
SLIDE 77

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

79

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(C,2,B) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ F ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-78
SLIDE 78

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

80

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(H,3,F) ¡ (G,3,F) ¡ (C,2,B) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ F ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ A G H D F C I J E B start goal

slide-79
SLIDE 79

Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡

81

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); }}}

  • pen ¡list ¡

(H,3,F) ¡ (G,3,F) ¡ (C,2,B) ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ closed ¡list ¡ A ¡ B ¡ F ¡ A G H D F C I J E B start goal

slide-80
SLIDE 80

What ¡we’ve ¡found ¡so ¡far... ¡

Breadth-­‑First ¡Search ¡ ¡(FIFO ¡Queue) ¡

§ Solves ¡unweighted ¡shortest ¡path ¡problem: ¡ ¡ ¡ Finds ¡the ¡shortest ¡path ¡between ¡verEces ¡if ¡edges ¡ are ¡unweighted ¡(or ¡equal ¡cost) ¡ ¡

Depth-­‑First ¡Search ¡(LIFO ¡Stack) ¡

§ Finds ¡nearby ¡goals ¡quickly ¡if ¡lucky ¡ § If ¡unlucky, ¡finds ¡nearby ¡goals ¡very ¡slowly ¡

82

slide-81
SLIDE 81

Applica8on: ¡ ¡8-­‑Puzzle ¡

Given ¡an ¡iniEal ¡configuraEon ¡of ¡8 ¡numbered ¡ Eles ¡on ¡a ¡3 ¡x ¡3 ¡board, ¡move ¡the ¡Eles ¡as ¡to ¡ produce ¡a ¡desired ¡goal ¡configuraEon ¡ ¡ ¡

83

Slide adapted from materials by Stuart Russell

slide-82
SLIDE 82
  • What are the vertices? 3 x 3 array

configuration of the tiles on the board.

  • What are the edges?
  • Starting vertex?
  • of the board.
  • Goal vertex / vertices? A particular

configuration of the board.

Applica8on: ¡ ¡8-­‑Puzzle ¡

84

Slide adapted from materials by Stuart Russell

slide-83
SLIDE 83
  • What ¡are ¡the ¡ver8ces? ¡ ¡Each ¡vertex ¡corresponds ¡

to ¡a ¡parEcular ¡Ele ¡configuraEon ¡

  • What ¡are ¡the ¡edges? ¡ ¡Consider ¡four ¡operators: ¡ ¡

Move ¡Blank ¡Square ¡Lei, ¡Right, ¡Up ¡or ¡Down ¡ ¡ – This ¡is ¡a ¡more ¡efficient ¡encoding ¡than ¡ considering ¡each ¡of ¡4 ¡moves ¡for ¡each ¡Ele ¡

¡The ¡edges ¡signify ¡applying ¡an ¡operator ¡to ¡a ¡board ¡

configuraEon ¡

  • Ini8al ¡state? ¡ ¡A ¡parEcular ¡board ¡configuraEon ¡
  • Goal ¡vertex? ¡ ¡A ¡parEcular ¡board ¡configuraEon ¡

Applica8on: ¡ ¡8-­‑Puzzle ¡

85

Slide adapted from materials by Stuart Russell

slide-84
SLIDE 84

Outline ¡

  • IntroducEon ¡
  • Graph ¡Basics ¡
  • Graph ¡Search ¡Problem ¡

– Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡

  • Complexity ¡Analysis ¡

86

slide-85
SLIDE 85

Outline ¡

  • IntroducEon ¡
  • Graph ¡Basics ¡
  • Graph ¡Search ¡Problem ¡

– Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡

  • Complexity ¡Analysis ¡

87

slide-86
SLIDE 86

88

n In ¡the ¡worst ¡case, ¡the ¡goal ¡vertex ¡won’t ¡be ¡found ¡

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) return c if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); } }

}

slide-87
SLIDE 87

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) return c if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); } }

} 89

n In ¡the ¡worst ¡case, ¡the ¡goal ¡vertex ¡won’t ¡be ¡found ¡

Each ¡vertex ¡is ¡in ¡ the ¡queue ¡at ¡most ¡

  • nce, ¡so ¡the ¡outer ¡

loop ¡runs ¡at ¡most ¡ |V | ¡iteraEons ¡ ¡

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

slide-88
SLIDE 88

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) return c if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); } }

} 90

n In ¡the ¡worst ¡case, ¡the ¡goal ¡vertex ¡won’t ¡be ¡found ¡

Each ¡vertex ¡is ¡in ¡ the ¡queue ¡at ¡most ¡

  • nce, ¡so ¡the ¡outer ¡

loop ¡runs ¡at ¡most ¡ |V | ¡iteraEons ¡ ¡ Performance ¡will ¡ depend ¡on ¡the ¡Eme ¡ for ¡getAdjacent() ¡ ¡

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

slide-89
SLIDE 89

91

Graph ¡Representa8on: ¡ ¡Adjacency ¡Matrix ¡

2 1 3 4 5

1 2 3 4 5 1 0 1 0 0 0 2 0 0 0 1 0 3 0 1 0 0 0 4 0 0 1 0 1 5 1 0 0 1 0

2 1 3 4 5

1 2 3 4 5 1 0 1 0 0 1 2 1 0 1 1 0 3 0 1 0 1 0 4 0 1 1 0 1 5 1 0 0 1 0

slide-90
SLIDE 90

92

Graph ¡Representa8on: ¡ ¡Adjacency ¡Matrix ¡

2 1 3 4 5

1 2 3 4 5 1 0 1 0 0 0 2 0 0 0 1 0 3 0 1 0 0 0 4 0 0 1 0 1 5 1 0 0 1 0

2 1 3 4 5

1 2 3 4 5 1 0 1 0 0 1 2 1 0 1 1 0 3 0 1 0 1 0 4 0 1 1 0 1 5 1 0 0 1 0

What ¡is ¡the ¡ performance ¡of ¡ getAdjacent(u)? ¡

slide-91
SLIDE 91

93

Graph ¡Representa8on: ¡ ¡Adjacency ¡List ¡

2 1 3 4 5

What ¡is ¡the ¡ performance ¡of ¡ getAdjacent(u)? ¡

2 4 3 5 1 2 3 4 5 1 4 2

slide-92
SLIDE 92

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) return c if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); } }

} 94

n Using ¡an ¡adjacency ¡matrix: ¡

|V | ¡iteraEons ¡ ¡ O(|V |) ¡ ¡

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

slide-93
SLIDE 93

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) return c if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); } }

} 95

n Using ¡an ¡adjacency ¡matrix: ¡ ¡O(|V |2)

|V | ¡iteraEons ¡ ¡ O(|V |) ¡ ¡

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

slide-94
SLIDE 94

graphSearch(problem, queuingFn) {

  • pen = {}, closed = {}

queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) if problem.goalTest(c.vertex) return c if c.vertex is not in closed { add c.vertex to closed for each Vertex w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); } }

} 96

n Using ¡an ¡adjacency ¡list: ¡

|V | ¡iteraEons ¡ ¡ O(out-­‑degree(c.vertex)) ¡ ¡

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

slide-95
SLIDE 95

97

n For ¡an ¡adjacency ¡list, ¡looping ¡over ¡all ¡adjacent ¡verEces ¡

  • f ¡u ¡will ¡be ¡O(out-­‑degree(u)) ¡

n Therefore, ¡the ¡traversal ¡performance ¡is ¡ ¡

¡ since ¡the ¡inner ¡loop ¡is ¡repeated ¡O( ¡|V | ¡) ¡Emes ¡

n However, ¡in ¡a ¡disconnected ¡graph, ¡we ¡must ¡sEll ¡look ¡at ¡

every ¡vertex, ¡so ¡the ¡performance ¡is ¡ ¡O( ¡|V | ¡+ ¡|E| ¡) ¡

O @

|V |

X

i=1

  • ut-degree(vi)

1 A = O(|E|)

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

How do these terms compare?

slide-96
SLIDE 96

Sparse ¡vs ¡Dense ¡Graphs ¡

§ A ¡sparse ¡graph ¡is ¡one ¡with ¡“few” ¡edges. ¡ That ¡is ¡ ¡ § A ¡dense ¡graph ¡is ¡one ¡with ¡“many” ¡edges. ¡ That ¡is ¡

98

|E| = O

  • |V |2

|E| = O (|V |)

2 1 3 4 5 2 1 3 4 5

slide-97
SLIDE 97

99

n For ¡an ¡adjacency ¡list, ¡getAdjacent(u) ¡will ¡be ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

O(out-­‑degree(u)) ¡

n Therefore, ¡the ¡traversal ¡performance ¡is ¡ ¡

¡ since ¡getAdjacent ¡is ¡done ¡O( ¡|V | ¡) ¡Emes ¡

n However, ¡in ¡a ¡disconnected ¡graph, ¡we ¡must ¡sEll ¡look ¡at ¡

every ¡vertex, ¡so ¡the ¡performance ¡is ¡ ¡O( ¡|V | ¡+ ¡|E| ¡) ¡

O @

|V |

X

i=1

  • ut-degree(vi)

1 A = O(|E|)

What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

Ranges ¡from ¡O( ¡|V | ¡) ¡to ¡O( ¡|V |2), ¡ depending ¡on ¡density ¡

slide-98
SLIDE 98

§ Really ¡depends ¡on ¡the ¡graph ¡representaEon ¡

100

What ¡is ¡the ¡Space ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

2 4 3 5 1 2 3 4 5 1 4 2 Adjacency List

1 2 3 4 5 1 0 1 0 0 0 2 0 0 0 1 0 3 0 1 0 0 0 4 0 0 1 0 1 5 1 0 0 1 0

Adjacency Matrix

slide-99
SLIDE 99

§ Really ¡depends ¡on ¡the ¡graph ¡representaEon ¡

101

What ¡is ¡the ¡Space ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡

2 4 3 5 1 2 3 4 5 1 4 2 Adjacency List

1 2 3 4 5 1 0 1 0 0 0 2 0 0 0 1 0 3 0 1 0 0 0 4 0 0 1 0 1 5 1 0 0 1 0

Adjacency Matrix Space ¡Complexity ¡ O( |V | + |E| ) Space ¡Complexity ¡ O( |V |2)

slide-100
SLIDE 100

Does ¡BFS ¡find ¡Shortest ¡Paths ¡in ¡ Weighted ¡Graphs? ¡

102

v1 v7 v2 v8 v4 v6 v3 v9 v10 v5 1 3 4 3 1 1 2 7 3 4 1 2 5 6

slide-101
SLIDE 101

Summary ¡

  • Breadth-­‑First ¡Search ¡

– Solves ¡unweighted ¡shortest ¡path ¡problem ¡ – Uses ¡FIFO ¡queue ¡ – Traverses ¡the ¡graph ¡in ¡level-­‑order ¡

  • Depth-­‑First ¡Search ¡

– Uses ¡LIFO ¡stack ¡ – Takes ¡a ¡“deep-­‑dive” ¡into ¡the ¡graph ¡

  • Time/Space ¡Complexity: ¡ ¡O( ¡|V | ¡+ ¡|E| ¡) ¡

103