CSE 373: Mor on grphs; DFS n BFS Mihl L Wnsy, F 14, 2018 1
Wrmup Wrmup: Disuss with your nighor: Rmin your nighor: wht is simpl grph? Suppos w hv simpl, irt grph with x nos. Wht is th mximum numr of gs it n hv, in trms of x? Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv, in trms of y? 2
Wrmup Wrmup: Disuss with your nighor: Rmin your nighor: wht is simpl grph? A simpl grph is grph tht hs no slf-loops n no prlll gs. Suppos w hv simpl, irt grph with x nos. Wht is th mximum numr of gs it n hv, in trms of x? Eh vrtx n onnt to x 1 othr vrtis, so x(x 1). Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv, in trms of y? Infinit: just kp ing nos with no gs tth. 2
Wrmup: Som follow-up qustions: Suppos w hv simpl, unirt grph with x nos. Wht is th mximum numr of gs it n hv? Wht if th grph is not simpl? Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv? Wht if th grph is not simpl? 3
Wrmup: Som follow-up qustions: Suppos w hv simpl, unirt grph with x nos. Wht is th mximum numr of gs it n hv? Wht if th grph is not simpl? If th grph is simpl, th mx numr of gs is xtly hlf x(x 1) of wht it woul if th grph wr irt. So,. 2 If th grph is not simpl, it s infinit: ssuming x > 0, w n just kp ing mor n mor slf-loops. Not tht if x = 0, thr n t ny gs t ll. Now, suppos w hv iffrnt simpl, unirt grph with y gs. Wht is th mximum numr of vrtis it n hv? Wht if th grph is not simpl? Eithr wy, it s still infinit, for th sm rsons givn prviously. 3
Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. 4
Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. In simpl grphs, if w know V is som fix vlu, w lso know E O ( V 2), for oth irt n unirt grphs. 4
Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. In simpl grphs, if w know V is som fix vlu, w lso know E O ( V 2), for oth irt n unirt grphs. Dns grph If E Θ ( V 2), w sy th grph is ns. To put it nothr wy, ns grphs hv lots of gs 4
Summry Wht i w lrn? In grphs with no rstritions, numr of gs n numr of vrtis r inpnnt. In simpl grphs, if w know V is som fix vlu, w lso know E O ( V 2), for oth irt n unirt grphs. Dns grph If E Θ ( V 2), w sy th grph is ns. To put it nothr wy, ns grphs hv lots of gs Sprs grph If E O ( V ), w su th grph is sprs. To put it nothr wy, sprs grphs hv fw gs. 4
How o w rprsnt grphs in o? So, how o w tully rprsnt grphs in o? 5
How o w rprsnt grphs in o? So, how o w tully rprsnt grphs in o? Two ommon pprohs, with iffrnt troffs: Ajny mtrix Ajny list 5
Ajny mtrix Cor i: Assign h no numr from 0 to V 1 Crt V V nst rry of oolns or ints If (x, y) E, thn nstarry[x][y] == tru 6
Ajny mtrix Cor i: Assign h no numr from 0 to V 1 Crt V V nst rry of oolns or ints If (x, y) E, thn nstarry[x][y] == tru 6
Ajny list Wht is th worst-s runtim to: Gt out-gs: Gt in-gs: Di if n g xists: Insrt n g: Dlt n g: How muh sp o w us? Is this ttr for sprs or ns grphs? Cn w hnl slf-loops n prlll gs? 7
Ajny list Wht is th worst-s runtim to: Gt out-gs: O ( V ) Gt in-gs: O ( V ) Di if n g xists: O (1) Insrt n g: O (1) Dlt n g: O (1) How muh sp o w us? O ( V 2) Is this ttr for sprs or ns grphs? Dns ons Cn w hnl slf-loops n prlll gs? Slf-loops ys, prlll gs, not sily 7
Ajny list Cor i: Assign h no numr from 0 to V 1 Crt n rry of siz V Eh lmnt in th rry stors its out gs in list or st 8
Ajny list Cor i: Assign h no numr from 0 to V 1 Crt n rry of siz V Eh lmnt in th rry stors its out gs in list or st On highr lvl: rprsnt s IDitionry<Vrtx, Egs>. 8
Ajny list Cor i: Assign h no numr from 0 to V 1 Crt n rry of siz V Eh lmnt in th rry stors its out gs in list or st On highr lvl: rprsnt s IDitionry<Vrtx, Egs>. 8
Ajny list W n stor gs using ithr sts or lists. Answr ths qustions for oth. Wht is th worst-s runtim to: Gt out-gs: Gt in-gs: Di if n g xists: Insrt n g: Dlt n g: How muh sp o w us? Is this ttr for sprs or ns grphs? Cn w hnl slf-loops n prlll gs? 9
Whih o w pik? So whih o w pik? 10
Whih o w pik? So whih o w pik? Osrvtions: Most grphs r sprs If w implmnt jny lists using sts, w n gt omprl worst-s prformn 10
Whih o w pik? So whih o w pik? Osrvtions: Most grphs r sprs If w implmnt jny lists using sts, w n gt omprl worst-s prformn So y fult, pik jny lists. 10
Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. 11
Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. Pth A pth is wlk tht nvr visits th sm vrtx twi. 11
Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. Pth A pth is wlk tht nvr visits th sm vrtx twi. Pth or wlk? Pth or wlk? 11
Wlks n pths Wlk A wlk is list of vrtis v 0, v 1, v 2,..., v n whr if i is som int whr 0 i < v n, vry pir (v i, v i+1 ) E is tru. Mor intuitivly, wlk is on ontinous lin following th gs. Pth A pth is wlk tht nvr visits th sm vrtx twi. Wlk Pth 11
Connt omponnts Connt grph A grph is onnt if vry vrtx is onnt to vry othr vrtx vi som pth. E.g.: if w pik up th grph n shk it, nothing flis off. 12
Connt omponnts Connt grph A grph is onnt if vry vrtx is onnt to vry othr vrtx vi som pth. E.g.: if w pik up th grph n shk it, nothing flis off. Connt or not onnt? Connt or not onnt? 12
Connt omponnts Connt grph A grph is onnt if vry vrtx is onnt to vry othr vrtx vi som pth. E.g.: if w pik up th grph n shk it, nothing flis off. Connt Not onnt Connt omponnt A onnt omponnt of grph is ny sugrph (prt of grph) whr ll vrtis r onnt to h othr. Not: A onnt grph hs only on onnt omponnt. 12
Trs vs grphs Is this grph or tr? f g 13
Trs vs grphs Is this grph or tr? f g Both! 13
Trs vs grphs Is this grph or tr? f g Both! Tr A tr is onnt n yli grph. 13
Trs vs grphs Is this grph or tr? Is this th sm thing? f Both! g g f Tr A tr is onnt n yli grph. 13
Trs vs grphs Is this grph or tr? Is this th sm thing? f g g f Both! Ys! (If is th root...) Tr A tr is onnt n yli grph. 13
Trs vs grphs Is this grph or tr? Is this th sm thing? f g g f Both! Ys! (If is th root...) Tr A tr is onnt n yli grph. Root tr A root tr is tr whr w ll on spil no th root. 13
Dtting if grph is onnt Qustion: How n w tll if grph is onnt or not? 14
Dtting if grph is onnt Qustion: How n w tll if grph is onnt or not? I: Lt s just fin out! Pik no n s if thr s pth to vry othr no! 14
Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 15
Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 15
Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 3. Dquu som no from th t strutur. Go to stp 1. 15
Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 3. Dquu som no from th t strutur. Go to stp 1. 4. Kp going until th t strutur is mpty. 15
Brth-first srh (BFS) Brth-first trvrsl, or i: 1. Pik som no n mrk it (or sv it in st, t...) 2. Exmin h nighor n visit h on (not: sv th ons w hvn t visit yt in som t strutur, lik quu?) 3. Dquu som no from th t strutur. Go to stp 1. 4. Kp going until th t strutur is mpty. Psuoo, vrsion 1: srh(v): visit = mpty st quu.nquu(v) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) 15
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:, 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu: 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,, 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:, 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,,,, 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,,, 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,,, f, g, 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:,, f, g, 16
Brth-first srh (BFS) xmpl srh(v): quu.nquu(v) f whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): quu.nquu(w) i g j h Currnt no: Quu:, f, g, Wht wnt wrong? 16
A rokn trvrsl Prolm: W r r-visiting nos w lry visit! 17
A rokn trvrsl Prolm: W r r-visiting nos w lry visit! A fix: Kp trk of nos w v lry visit in st! 17
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:, Visit:, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu: Visit:, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,, Visit:,,, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:, Visit:,,, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,,, Visit:,,,,, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,, Visit:,,,,, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:,, f, g, Visit:,,,,, f, g, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu:, f, g, Visit:,,,,, f, g, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu: f, g, Visit:,,,,, f, g, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: Quu: f, g, h, Visit:,,,,, f, g, h, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: f Quu: g, h, Visit:,,,,, f, g, h, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: g Quu: h, Visit:,,,,, f, g, h, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: g Quu: h, i, Visit:,,,,, f, g, h, i, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: h Quu: i, Visit:,,,,, f, g, h, i, 18
Brth-first srh (BFS) xmpl srh(v): visit = mpty st f quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) i g h j Currnt no: i Quu: Visit:,,,,, f, g, h, i, 18
An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19
An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19
An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19
An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19
An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19
An intrsting proprty... Not: W visit th nos in rings mintin grully growing frontir of nos. f i g j h 19
BFS Psuoo srh(v): visit = mpty st quu.nquu(v) visit.(urr) whil (quu is not mpty): urr = quu.quu() for (w : v.nighors()): if (w not in visit): quu.nquu(w) visit.(urr) 20
BFS nlysis Qustions: Wht is th worst-s runtim? (Lt V th numr of vrtis, lt E th numr of gs) Wht is th worst-s mount of mmory us? 21
BFS nlysis Qustions: Wht is th worst-s runtim? (Lt V th numr of vrtis, lt E th numr of gs) W visit h vrtx on, n h g on, so O ( V + E ). Wht is th worst-s mount of mmory us? Whtvr th lrgst horizon siz is. In th worst s, th horizon will ontin V 1 nos, so O ( V ). Not: O ( V + E ) is lso ll grph linr. 21
Othr pplitions of BFS Dsri how you woul us or moify BFS to solv th following: Dtrmin if som grph is lso tr. Print out ll th lmnts in tr lvl y lvl. Fin th shortst pth from on no to nothr. 22