Unirt Grphs An unirt grph G = (V, E) V st o vrtis E st o unorr gs (v,w) whr v, w in V USE: to mol symmtri rltionships twn ntitis vrtis v n w r jnt i thr is n g (v,w) [or (w,v)] th g (v,w) is inint upon vrtis v n w n g my (v,w,) whr is ost omponnt (.g. istn) /0/0 DFR - DSA - Grphs
Exmpls /0/0 DFR - DSA - Grphs
Trminology PATH: squn o vrtis v, v, v n suh tht (v,v ), (v,v ), (v n-,v n ) r gs LENGTH: SIMPLE PATH: SIMPLE CYCLE: (unirt grph) numr o gs in pth (v nots pth lngth 0 rom v to v) ll vrtis r istint (xpt possily th irst n th lst) simpl pth o lngth or mor tht onnts vrtx to itsl /0/0 DFR - DSA - Grphs
Su-grph G = (V, E) su-grph o G is grph G = (V, E ) whr V is sust o V E onsists o gs (v,w) suh tht oth v n w r in V i E onsists o ll gs (v,w) in E suh tht oth v, w in V thn G is n INDUCED SUB-GRAPH o G onnt omponnt o grph G is mximl onnt inu su-grph tht is not itsl propr su-grph o ny othr onnt su-grph o G /0/0 DFR - DSA - Grphs
Su-grph: xmpl grph G su-grph G (n) inu su-grph On onnt omponnt - nmly G itsl /0/0 DFR - DSA - Grphs
An Unonnt Grph two onnt omponnts (h r tr) onnt yli grph is FREE TREE vry r tr with n >= vrtis ontins xtly (n-) gs ny g to r tr givs yl /0/0 DFR - DSA - Grphs
Grph Rprsnttion Ajny Mtrix Ajny List 0 0 0 0 0 0 0 0 /0/0 DFR - DSA - Grphs 7
Oprtions insrt rmov in vrtx g list oprtions nvigt is_pth is_yl shortst pth spnning orst /0/0 DFR - DSA - Grphs 8
Minimum-ost Spnning Trs G = (V,E) whr h g (v,w) hs n ssoit ost SPANNING TREE or G is r tr tht onnts ll th vrtis in G (n nos n (n-) gs; no yls) th ost o th spnning tr is th sum o th osts o th gs in th tr pplition rs: ommunition ntworks (trnsport/omputr) /0/0 DFR - DSA - Grphs 9
MST Proprty G = (V,E) onnt grph with ost untion on th gs lt U propr sust o V i (u,v) is n g o lowst ost suh tht u in U n v in V-U thn thr is MST tht inlus (u,v) s n g /0/0 DFR - DSA - Grphs 0
/0/0 DFR - DSA - Grphs Builing n MST: rtiv guss stp stp stp stp stp
Kruskl s prinipls Buil priority quu (PQ) with th gs, shortst gs irst Eh no in th grph oms omponnt Choos n g rom th PQ suh tht th g onnts istint omponnts until thr is only on omponnt this is th MST /0/0 DFR - DSA - Grphs
Kruskl s prinipls - xmpl PQ: ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ) Componnts: [], [], [], [], [], [] - omponnts ( ) [-], [], [], [], [] - omponnts ( ) [-], [], [-], [] - omponnts ( ) [-], [-], [, ] - omponnts ( ) [-, -, -], [-] - omponnts ( ) not hosn - & in sm omponnt ( ) [-, -, -, -, -] - omponnt (MST) /0/0 DFR - DSA - Grphs
MST xplntion (Kruskl) priority quu Commnts Th gs r stor in PQ (lowst vlus irst) Eh no oms omponnt Eh g shoul onnt omponnts NB: os not onnt omponnts n r in th sm omponnt (stp ov) ing woul lso rt yl An MST is r tr n thror hs no yls omplts th MST An MST with n nos hs (n-) gs An MST is Fr Tr (no yls) /0/0 DFR - DSA - Grphs
Kruskl s Algorithm (rtiv guss ) On mtho o onstruting n MST is Kruskl s strt with grph T = (V, ) i.. only th vrtis o G = (V, E) h vrtx is onnt omponnt (in th grph T) to onstrut th MST, T xmin th gs in E in orr o inrsing ost (implmnttion - priority quu) i th g onnts two vrtis in two onnt omponnts thn th g to T (othrwis isr th g) whn ll th gs r in on omponnt, T is MST or G /0/0 DFR - DSA - Grphs
Kruskl s Algorithm S = st o onnt omponnts (V rom G=(V,E)) mrg(a, B, S) -- mrg omponnts A & B in S - rnm A in(v, S) -- rturn nm o omponnt X in S : v in X initil( A, v, S) -- mk A th nm o omponnt in S ontining only vrtx v initilly insrt(, S) -- givn g to S rmov_pq() -- rmov n g rom th PQ (x, y, ) -- g (x, y) in PQ with ost /0/0 DFR - DSA - Grphs
Kruskl s Algorithm or h v in S initil ( nxt(nm), v, S) -- initilis whil (siz(s) > { -- siz = numr o omponnts gt_pq ( ); -- gt (x, y, ) rom PQ i ( in(x, S)!= in(y, S) ) { -- x, y in irnt omponnts mrg ( in (x, S), in (y, S), S ); insrt (gt_pq ( ), S); } rmov_pq( ); } /0/0 DFR - DSA - Grphs 7
Kruskl: xmpl PQ B E A C S D F /0/0 DFR - DSA - Grphs 8
Kruskl: xmpl PQ B E A C S D F B E A S D F /0/0 DFR - DSA - Grphs 9
Kruskl: xmpl PQ B E A C S D F B E A S D /0/0 DFR - DSA - Grphs 0
Kruskl: xmpl PQ B E A C S D F B A S D /0/0 DFR - DSA - Grphs
Kruskl: xmpl PQ B E A C S D F B A S /0/0 DFR - DSA - Grphs
Kruskl: xmpl PQ B E A C S D F B A S /0/0 DFR - DSA - Grphs
Kruskl: xmpl PQ B E A C S D F A S /0/0 DFR - DSA - Grphs
Kruskl: Commnt Using th PQ, th lgorithm is rsonly sy to unrstn in prinipl (th pitoril rprsnttion is sy to ollow) In gnrl it is worth looking t th prolm n its solution or going through ny lgorithm in til look t h lin o th psuo o n sur tht you n rlt th o to th tion rquir i.. tht you n intrprt th o /0/0 DFR - DSA - Grphs
/0/0 DFR - DSA - Grphs Builing n MST: rtiv guss stp stp stp stp stp
Prim s prinipls givn strt no x mrk s visit; not th g vlus rom x to th rmining nos; this uss rrys L or th g lngths n C or th no nm; in th shortst g rom x to y; mrk y s visit; uil COMPONENT (x y) i.. y is thn to th omponnt (i.. th visit nos); now xmin th g osts rom y to th rmining nos; i this g is hpr, rpl th urrnt g with this g. Th nw no is to th omponnt. Rpt or th unvisit nos. Th omponnt grows no y no n hpr gs rpl thos gs prviously oun s hpr. /0/0 DFR - DSA - Grphs 7
Prim s prinipls xmpl ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ) Strt no visit {} unvisit {,,,, } L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {, } unvisit {,,, } ( ) is hpr L = [,,,, ] C = [,,,, ] ( ) not hpr no hng ( ) is hpr L = [,,,, ] C = [,,,, ] ( ) is hpr L = [,,,, ] C = [,,,, ] /0/0 DFR - DSA - Grphs 8
Prim s prinipls xmpl ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ) L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {,, } unvisit {,, } ( ) not hpr no hng ( ) is hpr L = [,,,, ] C = [,,,, ] ( ) not hpr no hng Shortst g ( ) visit {,,, } unvisit {, } ( ) not hpr no hng ( ) not hpr no hng L = [,,,, ] C = [,,,, ] /0/0 DFR - DSA - Grphs 9
Prim s prinipls xmpl ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ), ( ) L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {,,,, } unvisit {} ( ) is hpr L = [,,,, ] C = [,,,, ] Shortst g ( ) visit {,,,,, } unvisit {} mpty STOP Rsult L = [,,,, ] C = [,,,, ] /0/0 DFR - DSA - Grphs 0
Prim s prinipls - piturs L = [,,,, ] C = [,,,, ] L = [,,,, ] C = [,,,, ] /0/0 DFR - DSA - Grphs
Prim s prinipls - piturs L = [,,,, ] C = [,,,, ] L = [,,,, ] C = [,,,, ] /0/0 DFR - DSA - Grphs
Prim s prinipls - piturs L = [,,,, ] C = [,,,, ] L = [,,,, ] C = [,,,, ] /0/0 DFR - DSA - Grphs
Prim s prinipls - piturs L = [,,,, ] C = [,,,, ] L = [,,,, ] C = [,,,, ] /0/0 DFR - DSA - Grphs
MST xplntion (Prim) Prim s lgorithm is gry lgorithm gry = tks th lolly st solution Th MST grows th MST s on omponnt (similr to Dijkstr) Pross Choos th hpst g rom th omponnt to n unvisit no, g to th MST n mrk th no s visit (U) Strt t no hoos th hpst g mrk Now hoos th hpst g U = {,} mrk Now hoos th hpst g U = {,,} mrk Now hoos th hpst g U = {,,,} mrk Now hoos th hpst g U = {,,,,} mrk All nos hv now n visit U = {,,,,,} stop. /0/0 DFR - DSA - Grphs
Prim s Algorithm (rtiv guss ) V = {,,,, } initilis U to {} th spnning tr grows on g t tim h stp: in th shortst g (u,v) tht onnts U n V-U v to U until U = V i.. V-U = ost mtrix C givs th osts o h g /0/0 DFR - DSA - Grphs
Prim s Algorithm Prim ( no v) -- v is th strt no { U = {v}; or i in (V-U) { low-ost[i] = C[v,i]; losst[i] = v; } whil (!is_mpty (V-U) ) { -- in th losst vrtx in V-U i = irst(v-u); min = low-ost[i]; k = i; -- minimum ost g or j in (V-U-k) i (low-ost[j] < min) {min = low-ost[j]; k = j; } isply(k, losst[k]); -- isply g U = U + k; -- k to U or j in (V-U) i ( C[k,j] < low-ost[j] ) ) -- rjust osts {low-ost[j] = C[k,j]; losst[j] = k; } } } S http://www.s.ku.s/s/ution/ourss/vg0/rvision/inx.php?primex= /0/0 DFR - DSA - Grphs 7
/0/0 DFR - DSA - Grphs 8 Prim: xmpl ininity
Prim: xmpl Init: U V-U low-ost losst k / min {} {,,,,} (-,,,,, ) (-,,,,,) / isply ((,)) {,} {,,,} (-,,,,, ) (-,,,,,) / isply ((,)) {,,} {,,} (-,,,,, ) (-,,,,,) / isply ((,)) {,,,} {,} (-,,,,, ) (-,,,,,) / isply ((,)) {,,,,} {} (-,,,,, ) (-,,,,,) / isply ((,)) {,,,,,} { } (-,,,,, ) (-,,,,,) S http://www.s.ku.s/s/ution/ourss/vg0/rvision/inx.php?primex= /0/0 DFR - DSA - Grphs 9
Prim: Commnt Sin th MST is r tr, thr r n- gs, hn n- itrtions th ollowing o ins th lst ost g twn U n V-U (min, k) min = low-ost[i]; k = i; or j in (V-U-k) i (low-ost[j] < min) {min = low-ost[j]; k = j; } th ollowing o my rpl y or.g. A_MST(u,v) isply(k, losst[k]); -- isply g th r-justmnt o th osts is prhps th trikist to unrstn ut is in t similr to Dijkstr - ining th hpst (i,j) or (i,k) (k,j) or j in (V-U) i ( C[k,j] < low-ost[j] ) ) {low-ost[j] = C[k,j]; losst[j] = k; } /0/0 DFR - DSA - Grphs 0