Warm Up Rn Breadh Fir Search on hi graph aring from. Wha order are erice placed on he qee? When proceing a erex iner neighbor in alphabeical order. In a direced graph, BFS only follow an edge in he direcion i poin. w y x Correc order:,,,y,x,w, bf(graph) ovii.enqee(fir erex) mark fir erex a iied while(ovii i no empy) crren = ovii.deqee() for (V : crren.oneighbor()) if ( i no iied) ovii.enqee() mark a iied finihed.add(crren) CSE 373 SP 8 - KASEY CHAMPION
Shore Pah How doe Google Map figre o hi i he fae way o ge o office hor from Kane? CSE 373 SP 8 - KASEY CHAMPION
Repreening Map a Graph How do we repreen a map a a graph? Wha are he erice and edge? CSE 373 SP 8 - KASEY CHAMPION 3
Repreening Map a Graph K R S H 4 D 4 5 3 P CSE 373 SP 8 - KASEY CHAMPION 4
Shore Pah The lengh of a pah i he m of he edge weigh on ha pah. Shore Pah Problem Gien: a direced graph G and erice and Find: he hore pah from o y 4 5 4 w 3 6 x 5 CSE 373 SP 8 - KASEY CHAMPION 5
Unweighed graph Le ar wih a impler erion: he edge are all he ame weigh (nweighed) If he graph i nweighed, how do we find a hore pah? CSE 373 SP 8 - KASEY CHAMPION 6
Unweighed Graph If he graph i nweighed, how do we find a hore pah? w y x Wha he hore pah from o? - Well.we re already here. Wha he hore pah from o or? - J go on he edge from From o w,x, or y? - Can ge here direcly from, if we wan a lengh pah, hae o go hrogh or. CSE 373 SP 8 - KASEY CHAMPION 7
Unweighed Graph: Key Idea To find he e of erice a diance k, j find he e of erice a diance k-, and ee if any of hem hae an ogoing edge o an ndicoered erex. Do we already know an algorihm ha doe omehing like ha? Ye! BFS! bfshorepah(graph G, erex orce) ovii.enqee(orce) orce.di = 0 mark orce a iied while(ovii i no empy){ crren = ovii.deqee() for ( : crren.oneighbor()){ if ( i no ye iied){.diance = crren.diance +.predeceor = crren ovii.enqee() mark a iied } } } CSE 373 SP 8 - KASEY CHAMPION 8
Unweighed Graph If he graph i nweighed, how do we find a hore pah? bfshorepah(graph G, erex orce) ovii.enqee(orce) orce.di = 0 mark orce a iied while(ovii i no empy){ crren = ovii.deqee() for ( : crren.oneighbor()){ if ( i no ye iied){.diance = crren.diance +.predeceor = crren ovii.enqee() mark a iied } } } w x y 3 CSE 373 SP 8 - KASEY CHAMPION 9
Wha abo he arge erex? Shore Pah Problem Gien: a direced graph G and erice, Find: he hore pah from o. BFS didn menion a arge erex I acally find he hore pah from o eery oher erex. If yo know yor arge, yo can op he algorihm early, when he arge i remoed from he qee. CSE 373 SP 8 - KASEY CHAMPION 0
Weighed Graph Each edge hold repreen he ime or diance from one erex o anoher. Someime hoe aren niform, o we p a weigh on each edge o record ha nmber. The lengh of a pah in a weighed graph i he m of he weigh along ha pah. We ll ame all of he weigh are poiie - For GoogleMap ha definiely make ene. - Someime negaie weigh make ene. Today algorihm doen work for hoe graph - There are oher algorihm ha do work. CSE 373 SP 8 - KASEY CHAMPION
Weighed Graph: Take BFS work if he graph i nweighed. Maybe i j work for weighed graph oo? w x 0 0 0 3 Wha wen wrong? When we fond a horer pah from o, we needed o pdae he diance o (and anyhing whoe hore pah wen hrogh ) b BFS doen do ha. CSE 373 SP 8 - KASEY CHAMPION
Weighed Graph: Take Redcion (informally) Uing an algorihm for Problem B o ole Problem A. Yo already do hi all he ime. In a preio projec, yo redced implemening a hahe o implemening a hahmap. Any ime yo e a library, yo re redcing yor problem o he one he library ole. Can we redce finding hore pah on weighed graph o finding hem on nweighed graph? CSE 373 SP 8 - KASEY CHAMPION 3
Weighed Graph: A Redcion Gien a weighed graph, how do we rn i ino an nweighed one wiho meing p he edge lengh? Tranform Inp Unweighed Shore Pah Tranform Op CSE 373 SP 8 - KASEY CHAMPION 4
Weighed Graph: A Redcion Wha i he rnning ime of or redcion on hi graph? Doe or redcion een work on hi graph? 5000 00 50 5000 0.5 π 3 5000 O( V + E ) of he modified graph, which i low. Ummm. Tl;dr: If yor graph weigh are all mall poiie ineger, hi redcion migh work grea. Oherwie we probably need a new idea. CSE 373 SP 8 - KASEY CHAMPION 5
Weighed Graph: Take 3 So we can j do a redcion. Inead le ry o figre o why BFS worked in he nweighed cae, and ry o make he ame hing happen in he weighed cae. Why did BFS work on nweighed graph? How did we aoid hi problem: w x 0 0 3 When we ed a erex o pdae hore pah we already knew he exac hore pah o. So we neer ran ino he pdae problem So if we proce he erice in order of diance from, we hae a chance. CSE 373 SP 8 - KASEY CHAMPION 6
Weighed Graph: Take 3 Goal: Proce he erice in order of diance from Idea: Hae a e of erice ha are known - (we know a lea one pah from o hem). Record an eimaed diance - (he be way we know o ge o each erex). If we proce only he erex cloe in eimaed diance, we won eer find a horer pah o a proceed erex. CSE 373 SP 8 - KASEY CHAMPION 7