lulu jwtt pnlton sin towr ounrs hpl lpp lu Our irst non-linr t strutur! rph G onsists o two sts G = {V, E} st o V vrtis, or nos st o E s, rltionships twn nos surph G onsists o sust o th vrtis n s o G jnt vrtis r two vrtis join y n i h rin o-workr tr Mry lrt rothrs rin lrt rotin 1 rotin 2 rotin 5 Movi 1 tor 1 Movi 2 tor 3 tor 2 Movi 3 tor 4 rotin 9 N=4 L=4 Ntwork in: Grph hory 2012
i pth twn two vrtis squn o s tht ins t th irst vrtx n ns t th othr vrtx simpl pth pth tht psss throuh vrtx t most on yl pth tht ins n ns t th sm vrtx h omplt rph rph tht hs n twn h pir o istint vrtis How mny s os omplt rph with n nos hv? simpl yl yl tht os not pss throuh vrtx mor thn on onnt rph rph tht hs pth twn h pir o istint vrtis isonnt rph rph tht hs t lst on pir o vrtis without pth twn thm i h irt rph Eh is irt, or n r, or link My hv two rs twn ivn pir o vrtis, on in h irtion Vrtx y is jnt to vrtx x i (i n only i) thr is irt rom x to y irt pth squn o irt s twn two vrtis irt yli Grph (G) irt rph with no yls oston n rniso Nw ork How w rs n you rmov to mk th rph G? nt
iht rph rph whos s hv wihts iht is th ost or mnitu o th rltionship rprsnt y th oston 170 n rniso Nw ork 2600 850 nt puli intr Grph<> // prtil puli ooln isempty() // rturns tru i rph is mpty puli int n() // rturns th numr o vrtis in rph puli int m() // rturns th numr o s in rph puli voi Vrtx( v) // Insrt vrtx in rph puli voi rmovvrtx( v) // lts vrtx rom rph lon with ny s twn th vrtx n othr vrtis puli voi E( v1, v2) // Insrt n twn two ivn vrtis in rph puli voi rmove( v1, v2) // lts th twn two ivn vrtis in rph puli invrtx(trin ky) // trivs n rturns th vrtx tht ontins ivn srh ky puli ooln ise( v1, v2) // rturns tru i n xists twn two ivn vrtis puli LinkList<> tnihors ( v) // O UNIECE GH // trivs n rturns list o th vrtis jnt to vrtx v jny mtrix or rph with n vrtis numr 0, 1,, n 1 ooln n n rry mtrix rs suh tht rs[i][j] = 1 (tru) i thr is n r rom vrtx i to vrtx j 0 (ls) i thr is no r rom vrtx i to vrtx j Vrtis 0 1 2 3 rs 0 1 2 3 0 0 0 1 1 1 2 3 0 0 1 0 1 1 0 0 0 0 1 0 ht proprty os th mtrix o n unirt rph hv?
import jv.util.*; import jv.io.*; puli lss jmtgrph<> implmnts Grph<> { privt inl int NO_OUN = -1; privt inl int EUL_CCI = 10; privt int n; // numr o vrtis in th rph privt ooln[][] rs; // jny mtrix o rs privt [] vrtis; // vlus o vrtis n jny list or rph with n vrtis numr 0, 1,, n 1 Consists o n link lists h i th link list hs list ntry or vrtx j i th rph ontins n r rom vrtx i to vrtx j puli jmtgrph(){ n = 0; this.rs = nw ooln[eul_cci][eul_cci]; this.vrtis = ([])(nw Ojt[EUL_CCI]); } ont rs / / / / hih rprsnttion supports ttr ths two rqunt oprtions on rphs? ise(v, w) trmin whthr thr is n rom vrtx v to vrtx w tnihors(v) turn list o ll vrtis link to rom ivn vrtx v jny mtrix or wiht rph with n vrtis numr 0, 1,, n 1 n n n rry mtrix E suh tht E[i][j] = h wiht o th r rom vrtx i to vrtx j i thr is n rom i to j i thr is no rom vrtx i to vrtx j 5 4 2 8 E 0 4 5 0 8 4 8 0 2 5 2 0
Multipl Es jny list or wiht unirt rph Eh list ntry ontins th ll n wiht rts h s i it wr two rs in opposit irtions Multirph Not rph llows multipl s twn vrtis Multipl s init multipl rltions twn vrtis 5 4 2 8 E 8 / 4 4 5 5 / 8 2 / 2 / i r: pil Grph tr is onnt rph in whih thr is xtly on simpl pth onn:n ny two nos How mny s os tr with n nos hv? ye: rt n simpl rph visuliztion ownlo it rom http://www.yworks.om/n/prouts_y_out.html ou n rt ny rph y likin (or vrtis) n likin-n-rin (or s) Lots o rph ormts support. Us.t G ormt: txt il listin lins o: vrtxi vrtxnm (or vrtis) # vrtxi pirs (or rs) On you uplo il, hoos Lyout > Cirulr to s it li out nily.
rph-trvrsl lorithm Visits ll th vrtis tht it n rh strtin t som vrtx Visits ll vrtis o th rph i th rph is onnt (tivly omputin Connt Componnts) Must not loop orvr, i rph ontins yl Must nvr visit vrtx mor thn on Connt omponnt (or unirt rphs) = h sust o vrtis visit urin trvrsl tht ins t ivn vrtx tronly onnt omponnt (or irt rphs) = h sust o vrtis visit urin trvrsl tht ins t ny o its mmrs Hih lns irlin Compny (Hir) rolm or h ustomr rqust, init whthr squn o Hir lihts xists rom th oriin ity to th stintion ity h liht mp or Hir is irt rph r twn vrtis mns hr is liht twn itis irt pth mns hr is squn o liht onntions h solu:on prorms n xhus'v srh innin t th oriin ity, tris vry possil squn o lihts un:l ithr ins squn tht ts to th s:n:on ity trmins tht no suh squn xists h tk is usul in ornizin n xhus:v srh It hlps you rmmr how you ot th th urrnt point ktrkin n us to rovr rom wron hoi o ity
(oriincity): rhin th liht Mp stk = nw tk<e>(); stk.push(oriincity); whil ( squn o lihts rom oriincity to stincity hs not n oun) { } i (you nnot o nywhr rom th ity on top o stk) stk.pop(); // ktrk ls slt nihor, nothrcity, rom th ity on top o stk; stk.push(nothrcity);... n rmmr whr you v n stk = nw tk<e>(); Clr Mrks; stk.push(oriincity); Mrk(oriinCity) s visit; whil ( squn o lihts rom oriincity to stincity hs not n oun) { } i (you nnot in n unvisit ity rom th ity on top o stk) stk.pop(); // ktrk ls slt n unvisit nihor, nothrcity, rom th ity on top o stk; stk.push(nothrcity); Mrk(nothrCity) s visit; pth-irst-rh Exmpl: rom ->Z oul (oc) work or unirt rphs? List visit (mrk) tk stk Z Z stk = nw tk<e>(); Clr Mrks; stk.push(oriincity); Mrk(oriinCity) s visit; whil ( squn o lihts rom oriincity to stincity } hs not n oun) { i (you nnot in n unvisit ity rom th ity on top o stk) stk.pop(); // ktrk ls slt n unvisit nihor, nothrcity, rom th ity on top o stk; stk.push(nothrcity); Mrk(nothrCity) s visit; Z Q
Lyrinth: Umrto Eo viss hsus Mzs s Grphs o in th wy out o lyrinth thr is only on mns. t vry nw juntion, nvr sn or, th pth w hv tkn will mrk with thr sins. I you s tht th juntion hs lry n visit, you will mk only on mrk on th pth you hv tkn. I ll th prturs hv lry n mrk, thn you must rtr your stps. ut i on or two prturs o th juntion r still without sins, you will hoos ny on, mkin two sins on it. roin throuh n prtur tht rs only on sin, you will mk two mor, so tht now th prtur rs thr. ll th prts o th lyrinth must hv n visit i, rrivin t juntion, you nvr tk pss with thr sins, unlss non o th othr psss is now without sins. C E C EI H G E C E G H G H G stin or Conntivity usin (oc) Connt: n unirt rph or whih thr is pth rom ny no to ny othr no Z Is this rph onnt? Connt omponnt: onnt su-rph Cn w us to in ll onnt omponnts? Q