Objecive Wrap up: Weighed, direced graph hore pah Minimum Spanning Tree eb, 1 SI - Sprenkle 1 Review Wha are greedy algorihm? Wha i our emplae for olving hem? Review he la problem we were working on: Single-ource, weighed-graph hore pah Ø Wha wa our approach o olving he problem? eb, 1 SI - Sprenkle 1
Review: Shore Pah Problem Given Ø ireced graph G = (V, ) Ø Source, deinaion Ø Lengh! e = lengh of edge e (non-negaive) Shore pah problem: find hore direced pah from o www.wlu.edu 3 o of pah ---- = + 3 + + = 48 www.cnn.com eb, 1 SI - Sprenkle 3 Wha wa our raegy? 1 co of pah = um of edge co in pah Review: ijkra lgorihm 1. Mainain a e of explored node S Ø Keep he hore pah diance d(u) from o u. Iniialize S={}, d()=, u, d(u)= 3. Repeaedly chooe unexplored node v which imize π ( v) = d( u) + e, Ø dd v o S and e d(v) = p(v) S d(u) u! e e = ( u, v) : u S v hore pah o (ome u in explored par followed by a ingle edge (u, v)) Implemenaion Idea Wha o repreen? How o repreen? eb, 1 SI - Sprenkle 4
ijkra Shore Pah lgorihm ind hore pah from o 4 1 eb, 1 SI - Sprenkle ijkra Shore Pah lgorihm S = { } PQ = {,,,,,,, } Iniialize diance o all node o infiniy 4 1 diance label eb, 1 SI - Sprenkle 3
ijkra Shore Pah lgorihm S = { } PQ = {,,,,,,, } elee 4 1 diance label eb, 1 SI - Sprenkle 7 ijkra Shore Pah lgorihm S = { } PQ = {,,,,,, } ecreae key xplored Se dd node o explored e X Updae diance o node i poin o 4 X 1 diance label X eb, 1 SI - Sprenkle 8 4
ijkra Shore Pah lgorihm S = { } PQ = {,,,,,, } ecreae key Updae PQ baed on he diance xplored Se X 4 1 diance label X eb, 1 SI - Sprenkle ijkra Shore Pah lgorihm S = { } PQ = {,,,,,, } elee xplored Se Selec node wih imum lengh from explored e (from PQ) 4 X 1 diance label X eb, 1 SI - Sprenkle 1
ijkra Shore Pah lgorihm S = {, } PQ = {,,,,, } elee xplored Se dd node o explored e 4 X 1 diance label X eb, 1 SI - Sprenkle ijkra Shore Pah lgorihm xplored Se S = {, } PQ = {,,,,, } Updae diance o node ha poin o, if maller 4 ecreae key X 33 1 eb, 1 SI - Sprenkle 1
ijkra Shore Pah lgorihm xplored Se S = {, } PQ = {,,,,, } elee he imum node from PQ, dd i o S elee 4 X 33 1 eb, 1 SI - Sprenkle 13 ijkra Shore Pah lgorihm S = {,, } PQ = {,,,, } dd o S 33 elee X 4 1 eb, 1 SI - Sprenkle 7
ijkra Shore Pah lgorihm S = {,, } PQ = {,,,, } Updae diance o node poin o, if maller X 4 3 33 X 1 eb, 1 SI - Sprenkle ijkra Shore Pah lgorihm S = {,, } PQ = {,,,, } Remove from PQ 3 4 1 elee eb, 1 SI - Sprenkle 8
ijkra Shore Pah lgorihm S = {,,, } PQ = {,,, } dd o S, Updae diance o node X 3 4 3 1 elee X eb, 1 SI - Sprenkle 17 ijkra Shore Pah lgorihm S = {,,, } PQ = {,,, } elee from PQ 4 X 3 elee 3 1 X eb, 1 SI - Sprenkle
ijkra Shore Pah lgorihm S = {,,,, } PQ = {,, } dd o S, Updae diance o node 3 X 34 4 elee 3 1 1 X eb, 1 SI - Sprenkle 1 ijkra Shore Pah lgorihm S = {,,,, } PQ = {,, } elee 4 34 1 elee 1 eb, 1 SI - Sprenkle 3 1
ijkra Shore Pah lgorihm S = {,,,,, } PQ = {, } dd o S, Updae diance o node 4 4 X 34 1 elee 1 X eb, 1 SI - Sprenkle 1 3 ijkra Shore Pah lgorihm S = {,,,,, } PQ = {, } elee 3 34 4 elee 4 1 1 X eb, 1 SI - Sprenkle
ijkra Shore Pah lgorihm S = {,,,,,, } PQ = { } dd o S, Updae diance o node 34 4 4 3 1 1 X eb, 1 SI - Sprenkle 3 ijkra Shore Pah lgorihm S = {,,,,,, } PQ = { } 3 34 4 4 1 elee eb, 1 SI - Sprenkle 4 1
ijkra Shore Pah lgorihm S = {,,,,,,, } PQ = { } dd o S, Updae diance o node 34 4 4 3 1 elee eb, 1 SI - Sprenkle ijkra Shore Pah lgorihm S = {,,,,,,, } PQ = { } 3 34 4 4 1 eb, 1 SI - Sprenkle 13
ijkra' lgorihm: Proof of orrecne Invarian. or each node u Î S, d(u) i he lengh of he hore -u pah Pf. (by inducion on S ) ae cae: S =1 Inducive hypohei? Nex ep? eb, 1 SI - Sprenkle 7 ijkra' lgorihm: Proof of orrecne Prove: or each node u Î S, d(u) i he lengh of he hore -u pah Pf. (by inducion on S ) ae cae: or S = 1, S={}; d() = Inducive hypohei: ume rue for S = k, k ³ 1 Proof: Ø Grow S o k+1 Ø Greedy: dd node v by u v Ø Wha do we know abou u? Ø Why didn Greedy pick y a he nex node? Ø Wha can we ay abou all oher v pah? S P' u x Greedy P v y eb, 1 SI - Sprenkle 8
ijkra' lgorihm: Proof of orrecne Prove: or each node u Î S, d(u) i he lengh of he hore -u pah Pf. (by inducion on S ) Inducive hypohei: ume rue for S = k, k ³ 1 Proof: Ø Le v be he nex node added o S by Greedy, and le uàv be he choen edge Ø The hore àu pah plu uàv i an àv pah of lengh p(v) Ø onider any àv pah P. I' no horer han p(v). Ø Le xày be he fir edge in P ha leave S, and le P' be he ubpah o x. Ø P i already oo long a oon a i leave S. In erm of inequaliie:! (P) ³! (P') +! (x,y) = d(x) +! (x, y) ³ p(y) ³ p(v) nonnegaive weigh inducive hypohei defn of p(y) ijkra choe v inead of y eb, 1 SI - Sprenkle S P' u x Greedy P v y icuion: ijra lgorihm Why doe he algorihm break down if we allow negaive weigh/co on edge? 4 3 1 7 eb, 1 SI - Sprenkle
ijkra lgorihm: nalyi 1. Mainain a e of explored node S Ø Know he hore pah diance d(u) from o u. Iniialize S={}, d()=, u, d(u)= 3. Repeaedly chooe unexplored node v which imize π ( v) = d( u) + e, Ø dd v o S and e d(v) = p(v) S d(u) u! e e = ( u, v) : u S v hore pah o ome u in explored par, followed by a ingle edge (u, v) Running ime? Implemenaion? aa rucure? eb, 1 SI - Sprenkle 31 ijkra lgorihm: nalyi 1. Mainain a e of explored node S Ø Keep he hore pah diance d(u) from o u. Iniialize S={}, d()=, u, d(u)= 3. Repeaedly chooe unexplored node v which imize π ( v) = d( u) + e, Ø dd v o S and e d(v) = p(v) PQ Operaion RT of Op # in ijkra Iner xracmin hangekey Impy e = ( u, v) : u S hore pah o ome u in explored par, followed by a ingle edge (u, v) How long doe each operaion ake? How many of each operaion? Toal eb, 1 SI - Sprenkle 3
ijkra lgorihm: Implemenaion or each unexplored node, explicily mainain π(v) = d(u) + e. e = (u,v) : u S Ø Nex node o explore = node wih imum p(v). Ø When exploring v, for each inciden edge e = (v, w), updae π(w) = { π(w), π(v)+ e }. fficien implemenaion. Mainain a prioriy queue of unexplored node, prioriized by p(v) PQ Operaion RT of Op # Prioriy in ijkra Queue Iner log n n xracmin log n n hangekey log n m Impy 1 n Toal eb, 1 SI - Sprenkle 33 ijkra lgorihm: Implemenaion or each unexplored node, explicily mainain π(v) = d(u) + e. e = (u,v) : u S Ø Nex node o explore = node wih imum p(v). Ø When exploring v, for each inciden edge e = (v, w), updae π(w) = { π(w), π(v)+ e }. fficien implemenaion. Mainain a prioriy queue of unexplored node, prioriized by p(v) PQ Operaion RT of Op # Prioriy in ijkra Queue Iner log n n xracmin log n n hangekey log n m Impy 1 n O(m log n) Toal m log n eb, 1 SI - Sprenkle 34 17
Laying able omca wan o lay cable in a neighborhood Ø Reach all houe Ø Lea co Neighborhood Layou o of laying cable bw houe depend on amoun of cable, landcaping, obacle, ec. 3 1 1 7 8 13 8 4 Wha ype of graph? eb, 1 SI - Sprenkle 3 Minimum Spanning Tree (MST) Spanning ree: pan all node in graph Given a conneced graph G = (V, ) wih poiive edge weigh c e, an MST i a ube of he edge T Í uch ha T i a panning ree whoe um of edge weigh i imized 4 4 3 8 1 1 4 8 7 7 T, S G = (V, ) eît c e = eb, 1 SI - Sprenkle 3
Looking ahead Wiki oday: haper 4 (fron maer), 4.1, 4., 4.4 PS due riday eb, 1 SI - Sprenkle 37 1