cs 10 problem solving via object oriented programming
play

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


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

  2. Agenda ¡ 1. Depth ¡first ¡search ¡ 2. Breadth ¡first ¡search ¡ 2 ¡

  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) ¡ 3 ¡

  4. Depth ¡First ¡Search ¡(DFS) ¡uses ¡a ¡stack ¡to ¡ explore ¡as ¡if ¡in ¡a ¡maze ¡ B ¡ F ¡ H ¡ DFS ¡basic ¡idea ¡ • Keep ¡going ¡unRl ¡you ¡ C ¡ can’t ¡go ¡any ¡further, ¡ then ¡back ¡track ¡ A ¡ I ¡ D ¡ G ¡ • Relies ¡on ¡a ¡stack ¡(implicit ¡ Start ¡ or ¡explicit) ¡to ¡keep ¡track ¡ of ¡where ¡you’ve ¡been ¡ E ¡ Goal ¡ 4 ¡ Graph ¡structure ¡from ¡h_p://stackoverflow.com/quesRons/687731/breadth-­‑first-­‑vs-­‑depth-­‑first ¡

  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 ¡ 5 ¡

  6. 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 ¡ If ¡you ¡added ¡to ¡end ¡of ¡list… ¡ 6 ¡

  7. 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 And ¡if ¡you ¡get ¡pixel ¡from ¡end ¡of ¡ Add it to the region list, ¡you ¡implemented ¡a ¡stack ¡ ¡ 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 ¡ If ¡you ¡added ¡to ¡end ¡of ¡list… ¡ 7 ¡

  8. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ E ¡ Goal ¡ 8 ¡

  9. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Push(A) ¡ E ¡ Stack ¡ A ¡ Goal ¡ 9 ¡

  10. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Pop(A), ¡mark ¡visited ¡ E ¡ Stack ¡ Goal ¡ 10 ¡

  11. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Push ¡unvisited ¡adjacent ¡ E ¡ Stack ¡ E ¡ D ¡ C ¡ B ¡ Goal ¡ 11 ¡

  12. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node 2 ¡ repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Pop(B), ¡mark ¡visited ¡ E ¡ Stack ¡ E ¡ D ¡ C ¡ Goal ¡ 12 ¡

  13. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node 2 ¡ repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Push ¡unvisited ¡adjacent ¡(F, ¡but ¡not ¡A) ¡ E ¡ Stack ¡ E ¡ D ¡ C ¡ F ¡ Goal ¡ 13 ¡

  14. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ 3 ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node 2 ¡ repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Pop(F), ¡mark ¡visited ¡ E ¡ Stack ¡ E ¡ D ¡ C ¡ Goal ¡ 14 ¡

  15. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ 3 ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node 2 ¡ repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Push ¡unvisited ¡adjacent ¡(H, ¡but ¡not ¡B) ¡ E ¡ Stack ¡ E ¡ D ¡ C ¡ H ¡ Goal ¡ 15 ¡

  16. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ 4 ¡ 3 ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node 2 ¡ repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Pop(H), ¡mark ¡visited ¡ E ¡ Stack ¡ E ¡ D ¡ C ¡ Goal ¡ 16 ¡

  17. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ 4 ¡ 3 ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node 2 ¡ repeat until find goal vertex or stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Nothing ¡to ¡push, ¡back ¡up ¡by ¡popping ¡C ¡ E ¡ Stack ¡ E ¡ D ¡ C ¡ Goal ¡ 17 ¡

  18. Depth ¡First ¡Search ¡(DFS) ¡is ¡like ¡exploring ¡a ¡ maze ¡ 4 ¡ 3 ¡ DFS ¡algorithm ¡ B ¡ F ¡ H ¡ stack.push(s) //start node 2 ¡ repeat until find goal vertex or 5 ¡ stack empty: u = stack.pop() 1 ¡ C ¡ if !u.visited u.visited = true A ¡ (maybe do something while here) for v ∈ u.adjacent I ¡ D ¡ G ¡ if !v.visited stack.push(v) Start ¡ Pop(C), ¡mark ¡visited ¡ E ¡ Stack ¡ E ¡ D ¡ Goal ¡ 18 ¡

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