Outline CS38 Introution to Algorithms Leture 2 April 3, 2014 grph trversls (BFS, DFS) onnetivity topologil sort strongly onnete omponents heps n hepsort greey lgorithms April 3, 2014 CS38 Leture 2 2 Grphs Grph G = (V, E) irete or unirete nottion: n = V, m = E (note: m n 2 ) jeny list or jeny mtrix 0 1 1 0 0 0 0 1 0 Grphs Grphs moel mny things physil networks (e.g. ros) ommunition networks (e.g. internet) inormtion networks (e.g. the we) soil networks (e.g. riens) epeneny networks (e.g. topis in this ourse) so mny unmentl lgorithms operte on grphs April 3, 2014 CS38 Leture 2 3 April 3, 2014 CS38 Leture 2 4 Grphs Grph terminology: n unirete grph is onnete i there is pth etween eh pir o verties tree is onnete, unirete grph with no yles; orest is olletion o isjoint trees irete grph is strongly onnete i there is pth rom x to y n rom y to x, 8 x,y2v DAG is Direte Ayli Grph Grph trversls Grph trversl lgorithm: visit some or ll o the noes in grph, leling them with useul inormtion reth-irst: useul or unirete, yiels onnetivity n shortest-pths inormtion epth-irst: useul or irete, yiels numering use or topologil sort strongly-onnete omponent eomposition April 3, 2014 CS38 Leture 2 5 April 3, 2014 CS38 Leture 2 6 1
Breth irst serh BFS(unirete grph G, strting vertex s) 1. or eh vertex v, v.olor = white, v.ist = 1, v.pre = nil 2. s.olor = grey, s.ist = 0, s.pre = nil 3. Q = ;; ENQUEUE(Q, s) 4. WHILE Q is not empty u = DEQUEUE(Q) 5. or eh v jent to u 6. IF v.olor = white THEN 7. v.olor = grey, v.ist = u.ist + 1, v.pre = u 8. ENQUEUE(Q, v) 9. u.olor = lk BFS exmple rom CLRS Lemm: BFS runs in time O(m + n), when G is represente y n jeny list. April 3, 2014 CS38 Leture 2 7 8 Breth irst serh Lemm: or ll v 2 V, v.ist = istne(s, v), n shortest pth rom s to v is shortest pth rom s to v.pre ollowe y ege (v.pre,v) Proo: prtition V into levels L 0 = {s} L i = {v : 9 u 2 L i-1 suh tht (u,v) 2 E} Oserve: istne(s,v) = i, v 2 L i s Breth irst serh eges only within lyers or etween jent lyers L 0 L 1 L 2 L n Clim: t ny point in opertion o lgorithm: 1. lk/grey verties extly L 0, L 1,, L i n prt o L i+1 2. Q = (v 0, v 1, v 2, v 3,, v k ) n ll hve v.ist = level o v s eges only within lyers or etween jent lyers L 0 L 1 L 2 L n level i level i+1 hols initilly: s.olor = grey, s.ist = 0, Q = (s) April 3, 2014 CS38 Leture 2 10 s Breth irst serh L 0 L 1 L 2 L n Clim: t ny point in opertion o lgorithm: 1. lk/grey verties extly L 0, L 1,, L i n prt o L i+1 2. Q = (v 0, v 1, v 2, v 3,, v k ) n ll hve v.ist = level o v level i level i+1 eges only within lyers or etween jent lyers ) level i+1 ) level i+1 1 step: equeue v 0 ; white nrs o v 0 w/ ist = v 0.ist + 1 Depth irst serh DFS(irete grph G) 1. or eh vertex v, v.olor = white, v.pre = nil 2. time = 0 3. or eh vertex u, IF u.olor = white THEN DFS-VISIT(G, u) DFS-VISIT(irete grph G, strting vertex u) 1. time = time +1, u.isovere = time, u.olor = grey 2. or eh v jent to u, IF v.olor = white THEN 3. v.pre = u, DFS-VISIT(G, v) 4. u.olor = lk; time = time + 1; u.inishe = time Lemm: DFS runs in time O(m + n), when G is represente y n jeny list. Proo? April 3, 2014 CS38 Leture 2 11 April 3, 2014 CS38 Leture 2 12 2
Depth irst serh DFS(irete grph G) 1. or eh vertex v, v.olor = white, v.pre = nil 2. time = 0 3. or eh vertex u, IF u.olor = white THEN DFS-VISIT(G, u) DFS-VISIT(irete grph G, strting vertex u) 1. time = time +1, u.isovere = time, u.olor = grey 2. or eh v jent to u, IF v.olor = white THEN 3. v.pre = u, DFS-VISIT(G, v) 4. u.olor = lk; time = time + 1; u.inishe = time Lemm: DFS runs in time O(m + n), when G is represente y n jeny list. Proo: DFS-VISIT lle or eh vertex extly one; its j. list snne one; O(1) work April 3, 2014 CS38 Leture 2 13 Depth irst serh DFS yiels orest: the DFS orest eh vertex lele with isovery time n inishing time eges o G lssiie s tree eges k eges (point k to n nestor) orwr eges (point orwr to esennt) ross eges (ll others) April 3, 2014 CS38 Leture 2 14 DFS exmple rom CLRS DFS pplition: topologil sort Given DAG, list verties v 0, v 1,., v n so tht no eges rom v i to v j (j < i) exmple: e e April 3, 2014 CS38 Leture 2 16 DFS pplition: topologil sort Theorem: listing verties in reverse orer o DFS inishing times yiels topologil sort o DAG G (n implement in liner time; how?) Proo: lim or ll (u,v) 2 E, v.inish < u.inish when (u,v) explore, v not grey sine then G woul hve yle [k-ege] v white ) esenent o u so v inishes irst v lk ) lrey one, so v.inish is set n u.inish will e set with lter time Strongly onnete omponents sy tht x» y i there is irete pth rom x to y n rom y to x in G equivlene reltion, equivlene lsses re strongly onnete omponents o G lso, mximl strongly onnete susets SCC struture is DAG (why?) April 3, 2014 CS38 Leture 2 17 April 3, 2014 CS38 Leture 2 18 3
Strongly onnete omponents DFS tree rom v in G: ll noes rehle rom v DFS tree rom v in G T : ll noes tht n reh v v G with eges reverse Key: in sink SCC, this is extly the SCC April 3, 2014 CS38 Leture 2 19 Strongly onnete omponents v given v in sink SCC, run DFS strting there, then move to next in reverse topologil orer DFS orest woul give the SCCs Key #2: topologil orering onsistent with SCC DAG struture! (why?) April 3, 2014 CS38 Leture 2 20 Strongly onnete omponents Strongly onnete omponents SCC(irete grph G) 1. run DFS(G) 2. onstrut G T rom G 3. run DFS(G T ) ut in line 3, onsier verties in eresing orer o inishing times rom the irst DFS running time O(n + m) i G in j. list note: step 2 n e one in O(m + n) time trees in DFS orest o the seon DFS re the SCCs o G April 3, 2014 CS38 Leture 2 21 SCC(irete grph G) 1. run DFS(G) 2. onstrut G T rom G 3. run DFS(G T ) ut in line 3, onsier verties in eresing orer o inishing times rom the irst DFS Corretness (sketh): irst vertex is in sink SCC, DFS-VISIT olors lk, eetively removes next unvisite vertex is in sink ter removl n so on April 3, 2014 CS38 Leture 2 22 Summry O(m + n) time lgorithms or omputing BFS tree rom v in unirete G ining shortest pths rom v in unirete G omputing DFS orest in irete G omputing topologil orering o DAG ientiying the strongly onnete omponents o irete G (ll ssume G given in jeny list ormt) Heps A si t struture eyon stks n queues: hep rry o n elt/key pirs in speil orer min-hep or mx-hep opertions: INSERT(H, elt) INCREASE-KEY(H, i) EXTRACT-MAX(H) April 3, 2014 CS38 Leture 2 23 April 3, 2014 CS38 Leture 2 24 4
Heps A si t struture eyon stks n queues: hep rry o n elt/key pirs in speil orer min-hep or mx-hep opertions: time: INSERT(H, elt) O(log n) INCREASE-KEY(H, i) O(log n) EXTRACT-MAX(H) O(log n) Heps rry A represents inry tree tht is ull exept or possily lst row height = O(log n) prent(i) = i/2 let(i) = 2i right(i) = 2i+1 hep property: A[prent(i)] A[i] or ll i April 3, 2014 CS38 Leture 2 25 April 3, 2014 CS38 Leture 2 26 Heps key opertion: HEAPIFY-DOWN(H, i) Heps key opertion: HEAPIFY-UP(H, i) A[i] my violte hep property repetely swp with lrger hil running time? O(log n) or O(ht) A[i] my violte hep property repetely swp with lrger hil running time? O(log n) or O(ht) April 3, 2014 CS38 Leture 2 27 April 3, 2014 CS38 Leture 2 28 Heps How o you implement opertions: time: INSERT(H, elt) O(log n) INCREASE-KEY(H, i) O(log n) EXTRACT-MAX(H) O(log n) using HEAPIFY-UP n HEAPIFY-DOWN? BUILD-HEAP(A): re-orers rry A so tht it stisies hep property how o we o this? running time? Heps BUILD-HEAP(A): re-orers rry A so tht it stisies hep property ll HEAPIFY-DOWN(H, i) or i rom n ownto 1 running time O(n log n) more reul nlysis: O(n) April 3, 2014 CS38 Leture 2 29 April 3, 2014 CS38 Leture 2 30 5
Heps suies to show h 0 h/2 h = O(1) note: h 0 h = O(1) or < 1 oserve: (h+1)/2 h+1 = h/(2 h ) (1+1/h)/2 (1+1/h)/2 < 1 or h > 1 April 3, 2014 CS38 Leture 2 31 Hepsort Sorting n numers using hep BUILD-HEAP(A) O(n) repetely EXTRACT-MIN(H) n O(log n) totl O(n log n) Cn we o etter? O(n)? oserve tht only ever ompre vlues no eisions se on tul vlues o keys April 3, 2014 CS38 Leture 2 32 Sorting lower oun omprison-se sort: only inormtion out A use y lgorithm omes rom pirwise omprisons hepsort, mergesort, quiksort, visulize sequene o omprisons in tree: is A[i] A[j]? Greey Algorithms eh root-le pth onsistent with 1 perm. mximum pth length log(n!) = (n log n) April 3, 2014 CS38 Leture 2 33 April 3, 2014 CS38 Leture 2 34 Greey lgorithms Greey lgorithm prigm uil up solution inrementlly t eh step, mke the greey hoie Exmple: in unirete grph G = (V,E), vertex over is suset o V tht touhes every ege hr prolem: in the smllest vertex over e April 3, 2014 CS38 Leture 2 35 Dijkstr s lgorithm given irete grph G = (V,E) with non-negtive ege weights strting vertex s 2 V in shortest pths rom s to ll noes v note: unweighte se solve y BFS April 3, 2014 CS38 Leture 2 36 6
Dijkstr s lgorithm shortest pths exhiit optiml sustruture property optiml solution ontins within it optiml solutions to suprolems shortest pth rom x to y vi z ontins shortest pth rom x to z shortest pths rom s orm tree roote t s Min ie: mintin set S µ V with orret istnes nr u with smllest istne estimte April 3, 2014 CS38 Leture 2 37 7