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 17 Shortest Path Agenda 1.


  1. CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡ Winter ¡2017 ¡ ¡ Tim ¡Pierson ¡ 260 ¡(255) ¡Sudikoff ¡ Day ¡17 ¡– ¡Shortest ¡Path ¡

  2. Agenda ¡ 1. Shortest-­‑path ¡simulaLon ¡ 2. Dijkstra’s ¡algorithm ¡ 3. A* ¡search ¡ 4. Implicit ¡graphs ¡ 2 ¡

  3. Previously ¡we ¡looked ¡at ¡finding ¡the ¡ minimum ¡number ¡of ¡steps ¡between ¡nodes ¡ Breadth ¡First ¡Search ¡ B ¡ F ¡ H ¡ BFS ¡is ¡a ¡good ¡choice ¡ ¡ Can ¡find ¡shortest ¡number ¡ C ¡ of ¡steps ¡between ¡source ¡ A ¡ and ¡any ¡other ¡node ¡ ¡ I ¡ D ¡ G ¡ Could ¡use ¡BFS ¡on ¡a ¡map ¡to ¡ Start ¡ plan ¡driving ¡routes ¡ between ¡ciLes ¡ E ¡ Goal ¡ 3 ¡

  4. Previously ¡we ¡looked ¡at ¡finding ¡the ¡ minimum ¡number ¡of ¡steps ¡between ¡nodes ¡ Breadth ¡First ¡Search ¡ B ¡ F ¡ H ¡ BFS ¡is ¡a ¡good ¡choice ¡ 2 ¡ 3 ¡ ¡ 6 8 Can ¡find ¡shortest ¡number ¡ 1 ¡ C ¡ of ¡steps ¡between ¡source ¡ 4 A ¡ and ¡any ¡other ¡node ¡ 7 9 ¡ I ¡ D ¡ G ¡ Could ¡use ¡BFS ¡on ¡a ¡map ¡to ¡ 5 Start ¡ plan ¡driving ¡routes ¡ between ¡ciLes ¡ E ¡ ¡ Search ¡adjacent ¡ciLes ¡first ¡ Goal ¡ 4 ¡

  5. BFS ¡considers ¡the ¡number ¡of ¡steps, ¡but ¡not ¡ how ¡long ¡each ¡step ¡could ¡take ¡ Fastest ¡driving ¡route ¡to ¡Sea3le ¡from ¡Hanover ¡ 50 ¡hours? ¡ Could ¡try ¡to ¡take ¡the ¡ Hanover ¡ most ¡direct ¡route ¡ Sea_le ¡ 29 ¡hours ¡ • Take ¡local ¡roads ¡ • Try ¡to ¡keep ¡on ¡a ¡line ¡ 4 ¡hours ¡ 12 ¡hours ¡ between ¡Start ¡and ¡ Total ¡<me: ¡45 ¡hours ¡ ¡ Goal ¡ ¡ Could ¡try ¡to ¡take ¡major ¡ highways: ¡ • New ¡York ¡ • Chicago ¡ • Sea_le ¡ 5 ¡ Drive ¡Lme ¡esLmates ¡from ¡travelmath.com ¡ ¡

  6. Now ¡we ¡consider ¡the ¡idea ¡that ¡not ¡all ¡steps ¡ are ¡the ¡same ¡ Fastest ¡driving ¡route ¡to ¡Sea3le ¡from ¡Hanover ¡ BFS ¡would ¡choose ¡the ¡ 50 ¡hours? ¡ direct ¡route ¡(one ¡leg) ¡ ¡ 29 ¡hours ¡ Highway ¡travel ¡makes ¡ larger ¡number ¡of ¡steps ¡ 4 ¡hours ¡ 12 ¡hours ¡ more ¡a_racLve ¡ Total ¡<me: ¡45 ¡hours ¡ ¡ ¡ Note: ¡our ¡metric ¡now ¡is ¡ driving ¡Lme, ¡however ¡total ¡ distance ¡is ¡longer! ¡ ¡ Need ¡a ¡way ¡to ¡account ¡for ¡ the ¡idea ¡that ¡each ¡step ¡ might ¡have ¡different ¡ “weight” ¡(drive ¡Lme ¡here) ¡ 6 ¡ Drive ¡Lme ¡esLmates ¡from ¡travelmath.com ¡ ¡

  7. With ¡no ¡negaLve ¡edge ¡weights, ¡we ¡can ¡use ¡ Dijkstra’s ¡algorithm ¡to ¡find ¡short ¡paths ¡ Goal: ¡find ¡shortest ¡path ¡to ¡all ¡nodes ¡considering ¡edge ¡weights ¡ Start ¡at ¡node ¡ s (single ¡source) ¡ ¡ Find ¡path ¡with ¡smallest ¡sum ¡of ¡ weights ¡to ¡all ¡other ¡nodes ¡ ¡ Store ¡shortest ¡path ¡weights ¡in ¡ v.dist ¡instance ¡variable ¡ ¡ Keep ¡back ¡pointer ¡to ¡previous ¡ node ¡in ¡ v.pred ¡ Updated ¡ v.dist and v.pred ¡if ¡ find ¡shorter ¡path ¡later ¡found ¡ 7 ¡

  8. To ¡get ¡intuiLon, ¡imagine ¡sending ¡runners ¡ from ¡the ¡start ¡to ¡all ¡adjacent ¡nodes ¡ Time ¡0 ¡ Simula<on ¡ s.dist = 0 ¡ Runners ¡take ¡edge ¡weight ¡ minutes ¡to ¡arrive ¡at ¡adjacent ¡ nodes ¡ ¡ s.dist ¡= ¡0 ¡ ¡ When ¡runners ¡arrive ¡at ¡node: ¡ • Record ¡arrival ¡Lme ¡in ¡ v.dist • Record ¡prior ¡node ¡in ¡ v.pred Runners ¡immediately ¡leave ¡for ¡ an ¡adjacent ¡node ¡ ¡ Here ¡runners ¡leave ¡for ¡ y ¡and ¡ t ¡ 8 ¡

  9. Imagine ¡we ¡send ¡runners ¡from ¡the ¡start ¡to ¡ all ¡adjacent ¡nodes ¡ Time ¡4 ¡ Runner ¡arrives ¡at ¡ y ¡in ¡4 ¡ minutes ¡ • Record ¡ y.dist = 4 • Record ¡ y.pred = s s.dist ¡= ¡0 ¡ ¡ Runners ¡leave ¡ y ¡for ¡adjacent ¡ nodes ¡ t, x, and ¡ z ¡ Runner ¡from ¡ s ¡has ¡not ¡ reached ¡ t ¡yet ¡ y.dist = 4 y.pred = s 9 ¡

  10. Imagine ¡we ¡send ¡runners ¡from ¡the ¡start ¡to ¡ all ¡adjacent ¡nodes ¡ Time ¡5 ¡ t.dist = 5 t.pred = y Runner ¡from ¡ y ¡arrives ¡at ¡ t at ¡Lme ¡5 ¡ • t.dist = 5 s.dist ¡= ¡0 ¡ • t.pred = y ¡ Runners ¡from ¡ s sLll ¡hasn’t ¡ made ¡it ¡to ¡ t ¡ Runners ¡leave ¡ t ¡for ¡ adjacent ¡nodes ¡ x ¡and ¡ y y.dist = 4 ¡ y.pred = s 10 ¡

  11. Imagine ¡we ¡send ¡runners ¡from ¡the ¡start ¡to ¡ all ¡adjacent ¡nodes ¡ Time ¡6 ¡ t.dist = 5 t.pred = y Runner ¡from ¡ s ¡arrives ¡at ¡ t ¡at ¡ Lme ¡6 ¡ ¡ Runner ¡from ¡ y ¡has ¡already ¡ s.dist ¡= ¡0 ¡ arrived, ¡so ¡best ¡route ¡is ¡from ¡ y , ¡ not ¡direct ¡from ¡ s ¡ Do ¡not ¡update ¡ t.dist ¡and ¡ t.pred NOTE: ¡BFS ¡would ¡have ¡chosen ¡ the ¡direct ¡route ¡to t y.dist = 4 y.pred = s ¡ 11 ¡

  12. Imagine ¡we ¡send ¡runners ¡from ¡the ¡start ¡to ¡ all ¡adjacent ¡nodes ¡ Time ¡7 ¡ t.dist = 5 t.pred = y Runner ¡from ¡ y ¡arrives ¡at z at ¡ Lme ¡7 ¡ s.dist ¡= ¡0 ¡ ¡ Record ¡ z.dist = 7 and ¡ z.pred = y Runners ¡leave ¡ z ¡for ¡ s and x ¡ y.dist = 4 z.dist = 7 y.pred = s z.pred = y 12 ¡

  13. Imagine ¡we ¡send ¡runners ¡from ¡the ¡start ¡to ¡ all ¡adjacent ¡nodes ¡ Time ¡8 ¡ t.dist = 5 x.dist = 8 Runner ¡from t arrives ¡at ¡ x at ¡ t.pred = y x.pred = t Lme ¡8 ¡ ¡ x.dist = 8, x.pred = t ¡ s.dist ¡= ¡0 ¡ All ¡nodes ¡explored ¡ ¡ Now ¡have ¡shortest ¡path ¡from ¡ s ¡to ¡all ¡other ¡nodes ¡ ¡ Shaded ¡lines ¡indicate ¡best ¡ path ¡to ¡each ¡node ¡ y.dist = 4 z.dist = 7 y.pred = s z.pred = y ¡ Path ¡forms ¡a ¡tree ¡on ¡graph ¡ 13 ¡ ¡

  14. Agenda ¡ 1. Shortest-­‑path ¡simulaLon ¡ 2. Dijkstra’s ¡algorithm ¡ 3. A* ¡search ¡ 4. Implicit ¡graphs ¡ 14 ¡

  15. Dijkstra’s ¡algorithms ¡works ¡similarly ¡but ¡ doesn’t ¡rely ¡on ¡waiLng ¡for ¡runners ¡ Dijkstra’s ¡algorithm ¡ Overview ¡ Start ¡at ¡ s ¡ Process ¡all ¡out ¡edges ¡at ¡the ¡ same ¡Lme ¡ ¡ Compare ¡distance ¡to ¡adjacent ¡ nodes ¡with ¡best ¡so ¡far ¡ ¡ If ¡current ¡path ¡< ¡best, ¡update ¡ best ¡distance ¡and ¡predecessor ¡ node ¡ ¡ Example: ¡one ¡hop ¡from s set t.dist = 6, t.pred = s ¡ 15 ¡

  16. Dijkstra’s ¡algorithms ¡works ¡similarly ¡but ¡ doesn’t ¡rely ¡on ¡waiLng ¡for ¡runners ¡ Dijkstra’s ¡algorithm ¡ Overview ¡ Start ¡at ¡ s ¡ Process ¡all ¡out ¡edges ¡at ¡the ¡ same ¡Lme ¡ ¡ Compare ¡distance ¡to ¡adjacent ¡ nodes ¡with ¡best ¡so ¡far ¡ ¡ If ¡current ¡path ¡< ¡best, ¡update ¡ best ¡distance ¡and ¡predecessor ¡ node ¡ ¡ Example: ¡one ¡hop ¡from s set t.dist = 6, t.pred = s , ¡then ¡ update ¡ t.dist = 5, t.pred = y on ¡second ¡hop ¡ 16 ¡ ¡

  17. Dijkstra ¡uses ¡a ¡Min ¡Priority ¡Queue ¡with ¡ dist ¡values ¡as ¡keys ¡to ¡get ¡closest ¡vertex ¡ Dijkstra’s ¡algorithm ¡star<ng ¡from ¡s ¡ Set ¡up ¡Min ¡Priority ¡ void dijkstra(s) { Queue ¡ queue = new PriorityQueue<Vertex>(); ¡ for (each vertex v) { ¡IniLalize ¡ dist ¡and ¡ pred v.dist = infinity; ¡ v.pred = null; Use ¡ dist ¡as ¡key ¡for ¡Min ¡ Priority ¡Queue ¡(iniLally ¡ queue.enqueue(v); infinite) ¡ } IniLalize ¡ s ¡distance ¡ s.dist = 0; While ¡not ¡all ¡nodes ¡ while (!queue.isEmpty()) { have ¡been ¡explored ¡ u = queue.extractMin(); ¡ for (each vertex v adjacent to u) Get ¡closest ¡node ¡based ¡ relax(u, v); on ¡distance ¡(iniLally ¡ s ) ¡ } ¡ } Examine ¡adjacent ¡and ¡ relax ¡ 17 ¡

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