graph traversals
play

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


  1. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (A,0,null) ¡ D C F H E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 33 }}}

  2. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 34 }}}

  3. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 35 }}}

  4. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 36 }}}

  5. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G A ¡ D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 37 }}}

  6. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G A ¡ D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 38 }}}

  7. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (B,1,A) ¡ A ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 39 }}}

  8. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (B,1,A) ¡ A ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ D C F H E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 40 }}}

  9. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 41 }}}

  10. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 42 }}}

  11. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 43 }}}

  12. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ B ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 44 }}}

  13. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ B ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 45 }}}

  14. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ B ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, closed = {} queuingFn(open, new Node(problem.startvertex)) Need ¡to ¡add ¡(C,2,B) ¡and ¡(F,2,B) ¡to ¡open ¡ loop { ¡ if empty(open) then return FAILURE Since ¡BFS ¡expands ¡the ¡shallowest ¡node, ¡ c = removeFront(open) what ¡must ¡we ¡insure ¡about ¡the ¡open ¡list? ¡ if problem.goalTest(c.vertex) then return c ¡ if c.vertex is not in closed { What ¡queuing ¡funcEon ¡should ¡we ¡use? ¡ add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); 46 }}}

  15. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ B ¡ (C,2,B) ¡ (F,2,B) ¡ D C F H ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, closed = {} queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) BFS ¡uses ¡a ¡ ¡ if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { FIFO ¡Queue! ¡ add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); 47 }}}

  16. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (D,1,A) ¡ A ¡ (C,2,B) ¡ B ¡ (F,2,B) ¡ ¡ D C F H node ¡c ¡ (C,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 48 }}}

  17. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (D,1,A) ¡ A ¡ (C,2,B) ¡ B ¡ (F,2,B) ¡ C ¡ ¡ D C F H node ¡c ¡ (C,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 49 }}}

  18. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (D,1,A) ¡ A ¡ (C,2,B) ¡ B ¡ (F,2,B) ¡ C ¡ (F,2,C) ¡ D C F H (E,2,C) ¡ ¡ node ¡c ¡ (C,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 50 }}}

  19. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,2,B) ¡ A ¡ (F,2,B) ¡ B ¡ (F,2,C) ¡ C ¡ (E,2,C) ¡ D C F H ¡ node ¡c ¡ (D,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 51 }}}

  20. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,2,B) ¡ A ¡ (F,2,B) ¡ B ¡ (F,2,C) ¡ C ¡ (E,2,C) ¡ D ¡ D C F H ¡ node ¡c ¡ (D,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 52 }}}

  21. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,2,B) ¡ A ¡ (F,2,B) ¡ B ¡ (F,2,C) ¡ C ¡ (E,2,C) ¡ D ¡ D C F H (E,2,D) ¡ ¡ node ¡c ¡ (D,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 53 }}}

  22. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (F,2,B) ¡ A ¡ (F,2,C) ¡ B ¡ (E,2,C) ¡ C ¡ (E,2,D) ¡ D ¡ D C F H ¡ node ¡c ¡ (C,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 54 }}}

  23. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (F,2,B) ¡ A ¡ (F,2,C) ¡ B ¡ (E,2,C) ¡ C ¡ (E,2,D) ¡ D ¡ D C F H ¡ node ¡c ¡ (C,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 55 }}}

  24. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (F,2,C) ¡ A ¡ (E,2,C) ¡ B ¡ (E,2,D) ¡ C ¡ ¡ D ¡ D C F H node ¡c ¡ (F,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 56 }}}

  25. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (F,2,C) ¡ A ¡ (E,2,C) ¡ B ¡ (E,2,D) ¡ C ¡ ¡ D ¡ D C F H F ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 57 }}}

  26. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (F,2,C) ¡ A ¡ (E,2,C) ¡ B ¡ (E,2,D) ¡ C ¡ (G,3,F) ¡ D ¡ D C F H (H,3,F) ¡ F ¡ ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 58 }}}

  27. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (E,2,C) ¡ A ¡ (E,2,D) ¡ B ¡ (G,3,F) ¡ C ¡ (H,3,F) ¡ D ¡ D C F H ¡ F ¡ node ¡c ¡ (F,2,C) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 59 }}}

  28. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (E,2,C) ¡ A ¡ (E,2,D) ¡ B ¡ (G,3,F) ¡ C ¡ (H,3,F) ¡ D ¡ D C F H ¡ F ¡ node ¡c ¡ (F,2,C) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 60 }}}

  29. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (E,2,D) ¡ A ¡ (G,3,F) ¡ B ¡ (H,3,F) ¡ C ¡ ¡ D ¡ D C F H F ¡ node ¡c ¡ (E,2,C) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 61 }}}

  30. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A A ¡ B G (E,2,D) ¡ B ¡ (G,3,F) ¡ C ¡ (H,3,F) ¡ D ¡ ¡ E ¡ D C F H F ¡ node ¡c ¡ (E,2,C) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 62 }}}

  31. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A A ¡ B G (G,3,F) ¡ B ¡ (H,3,F) ¡ C ¡ ¡ D ¡ E ¡ D C F H F ¡ node ¡c ¡ (E,2,D) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 63 }}}

  32. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A A ¡ B G (H,3,F) ¡ B ¡ ¡ C ¡ D ¡ E ¡ D C F H F ¡ node ¡c ¡ (G,3,F) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 64 }}}

  33. Applica8on: ¡ ¡Route ¡Finding ¡(BFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A A ¡ B G (H,3,F) ¡ B ¡ ¡ C ¡ D ¡ E ¡ D C F H F ¡ node ¡c ¡ (G,3,F) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 65 }}}

  34. Breadth-­‑First ¡Search ¡ 0 start A B G D C F H E J I 66

  35. Breadth-­‑First ¡Search ¡ 0 1 start A B G D C F H E J I 67

  36. Breadth-­‑First ¡Search ¡ 0 1 2 start A B G D C F H E J I 68

  37. Breadth-­‑First ¡Search ¡ 0 1 2 3 start A B G D C F H E J I 69

  38. Breadth-­‑First ¡Search ¡ 0 1 2 3 4 start A B G D C F H E J I 70

  39. Depth-­‑First ¡Search ¡ Expands ¡the ¡“deepest” ¡vertex ¡

  40. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 72 }}}

  41. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (B,1,A) ¡ A ¡ (C,1,A) ¡ (D,1,A) ¡ ¡ D C F H node ¡c ¡ (A,0,null) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 73 }}}

  42. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 74 }}}

  43. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ B ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 75 }}}

  44. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,1,A) ¡ A ¡ (D,1,A) ¡ B ¡ ¡ D C F H node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, closed = {} queuingFn(open, new Node(problem.startvertex)) Need ¡to ¡add ¡(C,2,B) ¡and ¡(F,2,B) ¡to ¡open ¡ loop { ¡ if empty(open) then return FAILURE Since ¡DFS ¡expands ¡the ¡deepest ¡node, ¡what ¡ c = removeFront(open) must ¡we ¡insure ¡about ¡the ¡open ¡list? ¡ if problem.goalTest(c.vertex) then return c ¡ if c.vertex is not in closed { What ¡queuing ¡funcEon ¡should ¡we ¡use? ¡ add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); 76 }}}

  45. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (F,2,B) ¡ A ¡ (C,2,B) ¡ B ¡ (C,1,A) ¡ (D,1,A) ¡ D C F H ¡ node ¡c ¡ (B,1,A) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, closed = {} queuingFn(open, new Node(problem.startvertex)) loop { if empty(open) then return FAILURE c = removeFront(open) DFS ¡uses ¡a ¡ ¡ if problem.goalTest(c.vertex) then return c if c.vertex is not in closed { LIFO ¡Stack! ¡ add c.vertex to closed for each w adjacent to c.vertex if w is not in closed queuingFn(open, new Node(w,c)); 77 }}}

  46. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,2,B) ¡ A ¡ (C,1,A) ¡ B ¡ (D,1,A) ¡ ¡ D C F H node ¡c ¡ (F,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 78 }}}

  47. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (C,2,B) ¡ A ¡ (C,1,A) ¡ B ¡ (D,1,A) ¡ F ¡ ¡ D C F H node ¡c ¡ (F,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 79 }}}

  48. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (H,3,F) ¡ A ¡ (G,3,F) ¡ B ¡ (C,2,B) ¡ F ¡ (C,1,A) ¡ D C F H (D,1,A) ¡ ¡ node ¡c ¡ (F,2,B) ¡ ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 80 }}}

  49. Applica8on: ¡ ¡Route ¡Finding ¡(DFS) ¡ open ¡list ¡ closed ¡list ¡ start goal A B G (H,3,F) ¡ A ¡ (G,3,F) ¡ B ¡ (C,2,B) ¡ F ¡ (C,1,A) ¡ D C F H (D,1,A) ¡ ¡ E J I graphSearch( problem , queuingFn ) { open = {}, 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)); 81 }}}

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

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

  52. Applica8on: ¡ ¡8-­‑Puzzle ¡ • 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. 84 Slide adapted from materials by Stuart Russell

  53. Applica8on: ¡ ¡8-­‑Puzzle ¡ • 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 ¡ 85 Slide adapted from materials by Stuart Russell

  54. Outline ¡ • IntroducEon ¡ • Graph ¡Basics ¡ • Graph ¡Search ¡Problem ¡ – Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡ • Complexity ¡Analysis ¡ 86

  55. Outline ¡ • IntroducEon ¡ • Graph ¡Basics ¡ • Graph ¡Search ¡Problem ¡ – Breadth-­‑First ¡Search ¡ – Depth-­‑First ¡Search ¡ • Complexity ¡Analysis ¡ 87

  56. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n In ¡the ¡worst ¡case, ¡the ¡goal ¡vertex ¡won’t ¡be ¡found ¡ graphSearch( problem , queuingFn ) { open = {}, 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)); } } } 88

  57. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n In ¡the ¡worst ¡case, ¡the ¡goal ¡vertex ¡won’t ¡be ¡found ¡ graphSearch( problem , queuingFn ) { open = {}, closed = {} Each ¡vertex ¡is ¡in ¡ queuingFn(open, new Node(problem.startvertex)) the ¡queue ¡at ¡most ¡ loop { once, ¡so ¡the ¡outer ¡ if empty(open) then return FAILURE loop ¡runs ¡at ¡most ¡ c = removeFront(open) | V | ¡iteraEons ¡ 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

  58. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n In ¡the ¡worst ¡case, ¡the ¡goal ¡vertex ¡won’t ¡be ¡found ¡ graphSearch( problem , queuingFn ) { open = {}, closed = {} Each ¡vertex ¡is ¡in ¡ queuingFn(open, new Node(problem.startvertex)) the ¡queue ¡at ¡most ¡ loop { once, ¡so ¡the ¡outer ¡ if empty(open) then return FAILURE loop ¡runs ¡at ¡most ¡ c = removeFront(open) | V | ¡iteraEons ¡ if problem.goalTest(c.vertex) return c if c.vertex is not in closed { ¡ add c.vertex to closed Performance ¡will ¡ for each Vertex w adjacent to c.vertex if w is not in closed depend ¡on ¡the ¡Eme ¡ queuingFn(open, new Node(w,c)); } for ¡getAdjacent() ¡ } } ¡ 90

  59. Graph ¡Representa8on: ¡ ¡Adjacency ¡Matrix ¡ 1 1 2 2 5 5 3 4 3 4 1 2 3 4 5 1 2 3 4 5 1 0 1 0 0 1 1 0 1 0 0 0 2 1 0 1 1 0 2 0 0 0 1 0 3 0 1 0 1 0 3 0 1 0 0 0 4 0 1 1 0 1 4 0 0 1 0 1 5 1 0 0 1 0 5 1 0 0 1 0 91

  60. Graph ¡Representa8on: ¡ ¡Adjacency ¡Matrix ¡ 1 1 2 2 5 5 3 4 3 4 1 2 3 4 5 1 2 3 4 5 What ¡is ¡the ¡ 1 0 1 0 0 1 1 0 1 0 0 0 performance ¡of ¡ 2 1 0 1 1 0 2 0 0 0 1 0 getAdjacent(u)? ¡ 3 0 1 0 1 0 3 0 1 0 0 0 4 0 1 1 0 1 4 0 0 1 0 1 5 1 0 0 1 0 5 1 0 0 1 0 92

  61. Graph ¡Representa8on: ¡ ¡Adjacency ¡List ¡ 1 2 5 3 4 What ¡is ¡the ¡ 2 1 performance ¡of ¡ 4 2 getAdjacent(u)? ¡ 3 2 4 3 5 5 1 4 93

  62. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n Using ¡an ¡adjacency ¡matrix: ¡ graphSearch( problem , queuingFn ) { open = {}, closed = {} | V | ¡iteraEons ¡ 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 O(| V |) ¡ for each Vertex w adjacent to c.vertex if w is not in closed ¡ queuingFn(open, new Node(w,c)); } } } 94

  63. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n Using ¡an ¡adjacency ¡matrix: ¡ ¡ O(| V | 2 ) graphSearch( problem , queuingFn ) { open = {}, closed = {} | V | ¡iteraEons ¡ 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 O(| V |) ¡ for each Vertex w adjacent to c.vertex if w is not in closed ¡ queuingFn(open, new Node(w,c)); } } } 95

  64. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n Using ¡an ¡adjacency ¡list: ¡ graphSearch( problem , queuingFn ) { open = {}, closed = {} | V | ¡iteraEons ¡ 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 O(out-­‑degree(c.vertex)) ¡ for each Vertex w adjacent to c.vertex if w is not in closed ¡ queuingFn(open, new Node(w,c)); } } } 96

  65. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n For ¡an ¡adjacency ¡list, ¡looping ¡over ¡all ¡adjacent ¡verEces ¡ of ¡ u ¡will ¡be ¡O(out-­‑degree( u )) ¡ n Therefore, ¡the ¡traversal ¡performance ¡is ¡ ¡ ¡ 0 1 | V | X A = O ( | E | ) out-degree( v i ) O @ i =1 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 | ¡) ¡ How do these terms compare? 97

  66. Sparse ¡vs ¡Dense ¡Graphs ¡ § A ¡ sparse ¡graph ¡is ¡one ¡with ¡ “ few ” ¡edges. ¡ That ¡is ¡ ¡ | E | = O ( | V | ) § A ¡ dense ¡graph ¡is ¡one ¡with ¡ “ many ” ¡edges. ¡ That ¡is ¡ � | V | 2 � | E | = O 1 1 2 2 5 5 3 4 3 4 98

  67. What ¡is ¡the ¡Time ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ n For ¡an ¡adjacency ¡list, ¡getAdjacent( u ) ¡will ¡be ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ O(out-­‑degree( u )) ¡ n Therefore, ¡the ¡traversal ¡performance ¡is ¡ ¡ ¡ 0 1 | V | X A = O ( | E | ) out-degree( v i ) O @ i =1 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 | ¡) ¡ Ranges ¡from ¡O( ¡| V | ¡) ¡to ¡O( ¡| V | 2 ), ¡ depending ¡on ¡density ¡ 99

  68. What ¡is ¡the ¡Space ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ § Really ¡depends ¡on ¡the ¡graph ¡representaEon ¡ Adjacency Matrix Adjacency List 1 2 3 4 5 2 1 1 0 1 0 0 0 4 2 2 0 0 0 1 0 3 2 3 0 1 0 0 0 4 3 5 4 0 0 1 0 1 5 1 4 5 1 0 0 1 0 100

  69. What ¡is ¡the ¡Space ¡Complexity ¡of ¡BFS ¡& ¡DFS? ¡ § Really ¡depends ¡on ¡the ¡graph ¡representaEon ¡ Adjacency Matrix Adjacency List 1 2 3 4 5 2 1 1 0 1 0 0 0 4 2 2 0 0 0 1 0 3 2 3 0 1 0 0 0 4 3 5 4 0 0 1 0 1 5 1 4 5 1 0 0 1 0 Space ¡Complexity ¡ Space ¡Complexity ¡ O( | V | 2 ) O( | V | + | E | ) 101

  70. Does ¡BFS ¡find ¡Shortest ¡Paths ¡in ¡ Weighted ¡Graphs? ¡ 3 v1 v2 v7 1 3 1 2 7 5 3 v4 v3 v6 v8 1 6 4 4 2 v5 v10 v9 1 102

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend