Dv Mount CMSC 45: Ltur Grph Bsis Thursy, Au, 07 Rin: Chpt. in KT (Klinr n Tros) n Chpt. in DBV (Dsupt, Ppimitriou, n Vzirni). Som o our trminoloy irs rom our txt. Grphs n Dirphs: A rph G = (V, E) is strutur tht rprsnts isrt st V ojts, ll vrtis or nos, n st o pirwis rltions E twn ths ojts, ll s. Es my irt rom on vrtx to nothr or my unirt. Th trm rph mns n unirt rph, n irt rphs r otn ll irphs (s Fi. ). Grphs n irphs provi lxil mthmtil mol or numrous pplition prolms involvin inry rltionships twn isrt olltion o ojt. Exmpls o rph pplitions inlu ommunition n trnsporttion ntworks, soil ntworks, loi iruits, sur mshs us or shp sription in omputr-i sin n orphi inormtion systms, prn onstrints in shulin systms. (Unirt) Grph Dirph Fi. : Grphs n irphs. Dinition: An unirt rph (or simply rph) G = (V, E) onsists o init st V n st E o unorr pirs o istint vrtis. Dinition: A irt rph (or irph) G = (V, E) onsists o init st V n st E o orr pirs o vrtis. Osrv tht multipl s twn th sm two vrtis r not llow, ut in irt rph, it is possil to hv two oppositly irt s twn th sm pir o vrtis. For unirt rphs, sl-loop s r not llow, ut thy r llow or irt rphs. Dirt rphs n unirt rphs r irnt ojts mthmtilly. Crtin notions (suh s pth) r in or oth, ut othr notions (suh s onntivity n spnnin trs) my in only or on. Grph n Dirph Trminoloy: Givn n = (u, v) in irph, w sy tht u is th oriin o n v is th stintion o. Givn n = {u, v in n unirt rph, u n v r ll th npoints o. Th is inint on (mnin tht it touhs) oth u n v. Givn two vrtis in rph or irph, w sy tht vrtx v is jnt to vrtx u i thr is n {u, v (or rphs) or (u, v) (or irphs). In irph, th numr o s omin out o v is ll its out-r, not out-(v), n th numr o s omin in is ll its in-r, not in-(v). In n unirt Ltur Fll 07
Dv Mount rph w just tlk out th r o vrtx s th numr o inint s, not (v). Whn isussin th siz o rph, w typilly onsir oth th numr o vrtis n th numr o s. Th numr o vrtis is typilly writtn s n, n th numr o s is writtn s m. (Bwr: Thr r mny irnt onvntions. Th numr o vrtis my xprss s n, v, V, or V, n th numr o s my xprss s m,, E, or E. Hr r som si omintoril ts out rphs n irphs. W will lv th proos to you. Givn rph with n vrtis n m s thn: In rph: Numr o s: 0 m ( n ) = n(n )/ = O(n ). Sum o rs: v V (v) = m. In irph: Numr o s: 0 m n. Sum o rs: v V in-(v) = v V out-(v) = m. Noti tht nrlly th numr o s in rph my s lr s qurti in th numr o vrtis. Howvr, th lr rphs tht ris in prti typilly hv muh wr s. A rph is si to sprs i m is O(n), n ns, othrwis. Whn ivin th runnin tims o lorithms, w will usully xprss it s untion o oth n n m, so tht th prormn on sprs n ns rphs will pprnt. Pths n Cyls: A pth in rph or irph is squn o vrtis v 0,..., v k suh tht (v i, v i ) is n or i =,..., k. Th lnth o th pth is th numr o s, k. A pth is simpl i ll vrtis n ll th s r istint. A yl is pth ontinin t lst on n or whih v 0 = v k. A yl is simpl i its vrtis (xpt v 0 n v k ) r istint, n ll its s r istint. A rph or irph is si to yli i it ontins no simpl yls. An yli onnt rph is ll r tr or simply tr or short (s Fi. ). (Th trm r is intn to mphsiz th t tht th tr hs no root, in ontrst to root tr, s is usully sn in t struturs.) An yli unirt rph (whih n not onnt) is olltion o r trs, n is ll orst. An yli irph is ll irt yli rph, or DAG or short (s Fi. ). (Fr) Tr DAG Biprtit rph Fi. : Illustrtion o ommon rph trms. Ltur Fll 07
Dv Mount A iprtit rph is on in whih th vrtis o rph n prtition into two isjoint susts, not V n V, suh tht ll th s hv on npoint in V n on in V (s Fi. ). Not tht vry yl in iprtit rph ontins n vn numr o s. W sy tht w is rhl rom u i thr is pth rom u to w. Not tht vry vrtx is rhl rom itsl y trivil pth tht uss zro s. An unirt rph is onnt i vry vrtx n rh vry othr vrtx. (Conntivity is it mssir or irphs, n w will in it ltr.) Th susts o mutully rhl vrtis prtition th vrtis o th rph into isjoint susts, ll th onnt omponnts o th rph. In irphs th notion o rhility is it irnt, us it is possil or u to rh w ut not vi vrs. A irph is si to stronly onnt i or h u n w, thr is pth rom u to w n pth rom w to u. Rprsnttions o Grphs n Dirphs: Thr r two ommon wys o rprsntin rphs n irphs. First w show how to rprsnt irphs. Lt G = (V, E) irph with n = V n lt m = E. W will ssum tht th vrtis o G r inx {,,..., n. Ajny Mtrix: An n n mtrix in or v, w n. { i (v, w) E A[v, w] = 0 othrwis. (S Fi..) I th irph hs wihts w n stor th wihts in th mtrix. For xmpl i (v, w) E thn A[v, w] = W (v, w) (th wiht on (v, w)). I (v, w) / E thn nrlly W (v, w) n not in, ut otn w st it to som spil vlu,.. A(v, w) =, or. (By w mn som numr whih is lrr thn ny llowl wiht.) It miht om s surpris, ut thr r numr o intrstin rltionships twn th us o mtris to rprsnt rphs n th mtris tht ris in linr lr to rprsnt linr trnsormtions. For xmpl, th invlus o th jny mtrix o rph provi lot o inormtion out th strutur o th rph. Ajny List: An rry Aj[... n] o pointrs whr or v n, Aj[v] points to list (.., sinly or ouly link list) ontinin th vrtis tht r jnt to v (i.., th vrtis tht n rh rom v y sinl ). I th s hv wihts thn ths wihts my lso stor in th link list lmnts (s Fi. ). Aj 0 0 0 0 Ajny mtrix Ajny list Fi. : Ajny mtrix n jny list or irphs. Ltur Fll 07
Dv Mount W n rprsnt unirt rphs usin xtly th sm rprsnttion, ut w will stor h twi. In prtiulr, w rprsntin th unirt {v, w y th two oppositly irt s (v, w) n (w, v) (s Fi. 4). Noti tht vn thouh w rprsnt unirt rphs in th sm wy tht w rprsnt irphs, it is importnt to rmmr tht ths two lsss o ojts r mthmtilly istint rom on nothr. This n us som omplitions. For xmpl, suppos you writ n lorithm tht oprts y mrkin s o rph. You n to rul whn you mrk (v, w) in th rprsnttion tht you lso mrk (w, v), sin thy r oth th sm in rlity. Whn lin with jny lists, it my not onvnint to wlk own th ntir link list, so it is ommon to inlu ross links twn orrsponin s. 4 4 4 0 0 0 0 0 0 0 0 Ajny mtrix 4 Aj 4 4 Ajny list (with rosslinks) Fi. 4: Ajny mtrix n jny list or rphs. An jny mtrix rquirs Θ(n ) stor, n n jny list rquirs Θ(n+m) stor. Th n riss us thr is on ntry or h vrtx in Aj. Sin h list hs out-(v) ntris, whn this is summ ovr ll vrtis, th totl numr o jny list rors is Θ(m). For most pplitions, th jny list rprsnttion is stnr. Dpth-First Srh: On o th most importnt si oprtions on rph is to systmtilly visit ll its vrtis. Ths trvrsls ntully impos typ o tr strutur (or nrlly orst) on th rph, n trs r usully muh sir to rson out thn nrl rphs. W r ivn rph G = (V, E), whih my irt or unirt. W mploy our uxiliry rrys. To voi rvisitin th sm vrtx, w mintin mrk or h vrtx: unisovr, isovr, inish. Aitionl inormtion n stor s prt o th trvrsl pross: Disovry tim: [u] inits th tim whn vrtx u ws isovr, whih oinis with th momnt tht th DFS pross is strt t this vrtx. Finish tim: [u] inits th tim whn vrtx u is inish prossin. At this point, ll o u s nihorin nos hv n visit, n in, vrythin rhl rom u hs n isovr n possily inish. Prssor pointr: p[u] inits th vrtx tht isovr u. Eh o th orm (p[u], u) is tr in th DFS rursion tr. DFS inus tr strutur. In orr to hnl instns whr not ll vrtis r rhl rom th strtin vrtx, w inlu min prorm tht invoks DFS whnvr n unisovr vrtx is nountr. Th min prorm is shown in o lok low n th Ltur 4 Fll 07
Dv Mount rursiv DFSvisit untion is shown in th nxt o lok. (Fi. 5 illustrts th xution on n unirt rph, n Fi. 6 shows n xmpl on irt rph.) DFS(G) { tim = 0 or h (u in V) mrk[u] = unisovr Dpth-First Srh (Min Prorm) // min prorm // initiliztion or h (u in V) i (mrk[u] == unisovr) DFSVisit(u) // unisovr vrtx? //...strt nw srh hr DFSVisit(u) { mrk[u] = isovr [u] = ++tim or h (v in Aj(u)) { i (mrk[v] == unisovr) { pr[v] = u DFSVisit(v) mrk[u] = inish [u] = ++tim DFS Visit (Pross sinl no) // prorm DFS srh t u // u hs n isovr // unisovr nihor? //...visit it // w r on with u Anlysis: Th runnin tim o DFS is O(n + m). W ll o th nlysis or unirt rphs. First osrv tht i w inor th tim spnt in th rursiv lls, th min DFS prour runs in O(n) tim. Eh vrtx is visit xtly on in th srh, n hn th ll DFSVisit() is m xtly on or h vrtx. W n just nlyz h on iniviully n up thir runnin tims. Inorin th tim spnt in th rursiv lls, w n s tht h vrtx u n pross in O( + (u)) tim (th + is n in s th r is 0). Thus th totl tim us in th prour is T (n) = n + ( ) ( + (u)) = n + (u) + n = n + m = O(n + m). u V u V A similr nlysis hols i w onsir DFS or irphs. Prnthsis Lmm n E Typs: DFS nturlly imposs tr strutur (tully olltion o trs, or orst) on th strutur o th rph. This is just th rursion tr, whr th (u, v) riss whn prossin vrtx u w ll DFSVisit(v) or som nihor v. Th hirrhil strutur nturlly imposs nstin strutur on th isovry-inish tim intrvls. This is sri in th ollowin lmm (n illustrt in Fi. 7()). Ltur 5 Fll 07
Dv Mount G : visit(s) s s /.. visit() visit() s /.. /.. /.. visit() visit() 4/.. 5/.. s /.. /.. /.. rturn rturn visit() s /.. 4/7 5/6 /.. /.. 8/.. visit() visit() 4/7 5/6 s /.. /.. /.. rturn rturn... rturn s 8/.. 9/.. 0/.. s /6 /5 4/7 8/ 5/6 9/ 0/ Fi. 5: Dpth-irst srh on n unirt rph. (Blu nos r isovr, n pink nos r inish. Eh no u is ll with th vlus [u]/[u].) G : visit() visit() visit() /.. /.. rturn rturn /.. /5 visit() visit() /.. /5 rturn rturn rturn 6/.. visit() visit() rturn /0 rturn /0 6/9 6/9 /5 /5 /4 / /.. 7/.. 7/8 7/8 Fi. 6: Dpth-irst srh on irt rph. (Blu nos r isovr, n pink nos r inish. Eh no u is ll with th vlus [u]/[u].) Ltur 6 Fll 07
Dv Mount Lmm: (Prnthsis Lmm) Givn rph G = (V, E) (irt or unirt), n ny DFS tr or G n ny two vrtis u, v V : u is snnt o v i [[u], [u]] [[v], [v]]. u is n nstor o v i [[u], [u]] [[v], [v]]. u n v r unrlt (in trms o nstor/snnt) i [[u], [u]] n [[v], [v]] r isjoint. /5 /0 6/9 7/8 /4 / tr orwr k ross 4 5 6 7 8 9 0 4 () () Fi. 7: () th Prnthsis Lmm n () th DFS typs. Th strutur o th rminin (non-tr) s o th rph pn on th typ o rph involv. For unirt rphs, th rminin s r ll k s. An importnt osrvtion is tht or h k (u, v), u is ithr propr nstor or propr snnt o v. To s why, onsir ny non-tr (u, v). Sin th rph is unirt, w my ssum without loss o nrlity tht u ws isovr or v. By th prnthsis lmm, this mns ithr tht u is n nstor o v (n w r on) or tht thir isovryinish intrvls r isjoint. I thy r isjoint, u must inish or v is isovr. Howvr, this is impossil, us s w r prossin u, w will s th (u, v) n thus isovr v. For irt rphs th non-tr s o th rph n lssii s ollows (S Fi. 7()): Bk s: (u, v) whr v is (not nssrily propr) nstor o u in th tr. (Thus, sl-loop is onsir to k.) Forwr s: (u, v) whr v is propr snnt o u in th tr. Cross s: (u, v) whr u n v r not nstors or snnts o on nothr (in t, th my o twn irnt trs o th orst). It is not iiult to lssiy th s o DFS tr on-th-ly y nlyzin th vrtx sttus (unisovr, isovr, inish) n/or onsirin th tim stmps. (This is lt s n xris.) B rul, howvr. Rmmr tht in n unirt rph, vry is rprsnt twi. Whn lssiyin k s, you shoul sur tht you r not sin th othr hl o tr. Ltur 7 Fll 07