Definitions G = (V, E) V = set of verties (vertex / noe) E = set of eges (v, w) (v, w in V) (v, w) orere => irete grph (igrph) (v, w) non-orere => unirete grph igrph: w is jent to v if there is n ege from v to w ege my e (v, w, ) where is ost omponent (e.g. istne) 05/12/2016 DFR - DSA - Grphs 1 1
Exmples 05/12/2016 DFR - DSA - Grphs 1 2
Mening & Use A grph is use to represent ritrry reltionships mong t ojets e.g. unirete grphs ommunitions network trnsport network (ro, ril, ir, se) with osts/istnes (trvelling slesmn prolem) e.g. irete grphs (igrph) flow of ontrol in omputer progrms University ourse plnning (epeneny grph) stte trnsition igrms 05/12/2016 DFR - DSA - Grphs 1 3
Other ADTs linke list irete yli grph (g) (g) tree 05/12/2016 DFR - DSA - Grphs 1 4
Terminology PATH: sequene of verties v 1, v 2, v n suh tht v 1 -> v 2, v 2 -> v 3, v n-1 -> v n re eges LENGTH: SIMPLE PATH: SIMPLE CYCLE: numer of eges in pth (v enotes pth length 0 from v to v) ll verties re istint (exept possily the first n the lst) simple pth of length >= 1 tht egins (irete grph) n ens t the sme vertex 05/12/2016 DFR - DSA - Grphs 1 5
Grphs & Cyles A yle is pth whih egins n ens t the sme vertex A grph with no yles is yli A irete grph with no yles is irete yli grph (DAG) DAG irete grphs unirete grphs 05/12/2016 DFR - DSA - Grphs 1 6
Unirete Grphs For yles in unirete grphs, the eges must e istint sine (u,v) n (v, u) re the sme ege onnete: if there exists pth from every vertex to every other vertex non-onnete: onnete: 05/12/2016 DFR - DSA - Grphs 1 7
Direte Grphs A onnete irete grph is lle strongly onnete i.e. there is pth from every vertex to every other vertex if the igrph is not strongly onnete BUT the unerlying grph, without istintion to the iretion, is onnete, then the grph is si to e wekly onnete strong: wek: not: 05/12/2016 DFR - DSA - Grphs 1 8
Complete Grph A grph is omplete if there is n ege etween every pir of verties 12 eges 6 eges n * (n - 1) n * (n - 1) / 2 05/12/2016 DFR - DSA - Grphs 1 9
Ajeny Mtrix For eh ege (u, v) set [u, v] = 1 storge => omeg(n 2 ) re in/ serh => O(n 2 ) 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 1 0 05/12/2016 DFR - DSA - Grphs 1
Ajeny List Use list of noes where eh noe points to list of jent noes (etter for sprse grphs) spe = O( V + E ) (for nme verties - use hsh tle) 05/12/2016 DFR - DSA - Grphs 1 11
Opertions insert remove fin vertex ege list opertions nvigte is_pth is_yle shortest pth spnning forest topologil sort 05/12/2016 DFR - DSA - Grphs 1 12
Shortest Pth 1 Dijkstr s lgorithm Single soure shortest pth (non-negtive osts) Determines the shortest pth from soure to every other vertex in the grph where the length of the pth is the sum of the osts of the eges S - set of verties; shortest istne from soure lrey known eh step s vertex v whose istne from S is s short s possile the visite verties (noes) speil pth: shortest pth from the soure to v pssing through u rry D: length of shortest speil pth to eh vertex C[i,j]: ost of v i to v j (no ege ost is infinite ) 05/12/2016 DFR - DSA - Grphs 1 13
Dijkstr s lgorithm priniples Given strt noe x, note the ege lengths from x to the remining noes in the grph. Choose the shortest ege from x to noe y. Mrk noes x n y s visite. S = {x,y} Chek to see if there is shorter pth to the remining (unvisite) noes, {V-S}, in the grph from x vi y. If so, upte the pth lengths so fr lulte. Repet the proess until ll noes hve een visite. y x {V-S} unvisite noes 05/12/2016 DFR - DSA - Grphs 1 14
Dijkstr - Exmple ( ) ( 30) ( e 0) ( 50) ( e ) ( 20) ( e 60) Strt visite {}, unvisite {,,, e}, shortest pth ( ) = infinity e Visite {, }, unvisite {,, e} D = [,, 30, 0] (-- 60) (-- ) (--e ) D = [, 60, 30, 0] Shortest pth (- 30) visite {,, }, unvisite {, e} (-- 50) (--e 90) D = [, 50, 30, 90] Shortest pth (- 50) visite {,,, }, unvisite {e} (--e 60) D = [, 50, 30, 60] Shortest pth (-e 60) visite {,,,, e}, unvisite { } No noes left! Finl nswer:- D = [, 50, 30, 60] 05/12/2016 DFR - DSA - Grphs 1 15
Dijkstr - Exmple Itertion S w D[] D[] D[] D[e] 0 initil {} - 30 0 1 {,} 60 30 0 2 {,,} 50 30 90 3 {,,,} 50 30 60 4 {,,,,e} e 50 30 60 See seprte notes on worke exmple:- (i) Revision notes (ii) stuy pln 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 16
Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [,, 30, 0 ] D [,, 30, 0 ] D [, 60, 30, 0 ] E [,,, ] E [,,, ] E [,,, ] L [,, 30, 0 ] L [,, 30, 0 ] L [, 50, 30, 0 ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 17
Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [, 60, 30, 0 ] D [, 50, 30, 90 ] D [, 50, 30, 60 ] E [,,, ] E [,,, ] E [,,, ] L [, 50, 30, 0 ] L [, 20, 30, 60 ] L [, 20, 30, ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 18
Dijkstr s Algorithm Grph (G) + Cost Mtrix (C) 0 30 e 50 60 20 e 30 0 50 20 60 e NB ount the numer of eges in the grph n the ost mtrix 05/12/2016 DFR - DSA - Grphs 1 19
Dijkstr s Algorithm Dijkstr () { S = {} // G = (V, E) for ( i in V-S) D[i] = C[, i] // initilistion while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum S = S + {w} foreh ( v in V-S) D[v] = min(d[v], D[w]+C[w,v]) } // proess } // D[i] = istne; C[i,j] = ost mtrix; S = {visite noes} 05/12/2016 DFR - DSA - Grphs 1 20
Dijkstr s Algorithm Dijkstr () -- is the strt noe { S = {} -- S represents the noes visite for ( i in V-S) D[i] = C[, i] -- initilise D (pth lengths) -- from the strt noe while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum -- unvisite noe with shortest pth from strt_noe S = S + {w} -- this noe to visite noes foreh ( v in V-S) D[v] = min(d[v], D[w]+C[w,v]) -- relulte pths vi w to unvisite noes } } 05/12/2016 DFR - DSA - Grphs 1 21
Dijkstr - Comment greey lgorithm - lol est solution is est overll hoose w in V-S suh tht D[w] is minimum (mening?) rell tht D[i] mens the length of the shortest pth to eh vertex note tht the lgorithm prtitions the noes into two spes S (initilly with the strt noe) n V-S (the remining noes) visite / unvisite foreh ( v in V-S) D[v] = min(d[v], D[w]+C[w,v]) (mening?) w is the noe with the minimum istne from the soure (not in S) D[v] mens the shortest (speil) pth length so fr lulte D[w] is the ost (so fr) to w (gin shortest (speil) pth length) C[w,v] is the ost from noe w to noe v (ege ost) 05/12/2016 DFR - DSA - Grphs 1 22
Dijkstr priniples revisite Choose the strt noe S = {} G = (V, E) S represents visite noes; V-S represents unvisite noes D represents the pth lengths from to the remining noes (V-) C[x,y] represents the ost mtrix the ost of the ege x y Algorithm Choose the shortest pth to n unvisite noe (my e n ege) A this noe (w) to the set of visite noes S Clulte n lterntive pth vi w to ll noes v in {V-S} hoose if istne D[w]+C[w,v] is shorter thn D[v] pth length - D[w] w C[w,v] - ege weight for eh v in {V-S} D[v] previous pth length 05/12/2016 DFR - DSA - Grphs 1 23
Dijkstr s Algorithm + Shortest Pth Tree Dijkstr () { S = {} for ( i in V-S) { D[i] = C[, i]; E[i] = ; L[i] = C[,i]; } while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum } S = S + {w} foreh ( v in V-S) if (D[w]+C[w,v])< D[v] ) { D[v] = D[w]+C[w,v]; E[v] = w; L[v] = C[w,v]; } } 05/12/2016 DFR - DSA - Grphs 1 24
Dijkstr s Algorithm + Shortest Pth Tree Dijkstr () -- is the strt noe { S = {} -- S represents the noes visite for ( i in V-S) { D[i] = C[, i]; E[i] = ; L[i] = C[,i]; } -- initilise D + SPT (E + L) while (!is_empty(v-s)) { hoose w in V-S suh tht D[w] is minimum -- unvisite noe with shortest pth from strt_noe S = S + {w} foreh ( v in V-S) if (D[w]+C[w,v])< D[v] ) { D[v] = D[w]+C[w,v]; E[v] = w; L[v] = C[w,v]; } -- relulte pths n SPT (E + L) } } 05/12/2016 DFR - DSA - Grphs 1 25
Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [,, 30, 0 ] D [,, 30, 0 ] D [, 60, 30, 0 ] E [,,, ] E [,,, ] E [,,, ] L [,, 30, 0 ] L [,, 30, 0 ] L [, 50, 30, 0 ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 26
Dijkstr Exmple - pitures 0 0 0 50 30 20 e 60 50 30 20 e e e D [, 60, 30, 0 ] D [, 50, 30, 90 ] D [, 50, 30, 60 ] E [,,, ] E [,,, ] E [,,, ] L [, 50, 30, 0 ] L [, 20, 30, 60 ] L [, 20, 30, ] e 60 50 30 20 e 60 05/12/2016 DFR - DSA - Grphs 1 27
Shortest Pth 2 All pirs shortest pth prolem (i.e. Shortest pth etween ny two verties) pply Dijkstr s lgorithm to eh noe in turn pply Floy s lgorithm Floy given G = (V,E), non-negtive osts C[v,w], for eh orere pir (v,w) fin the shortest pth note the initil onitions use n rry A[i,j] whih is initilise to C[i,j], i.e. the initil ege osts if no ege exists C[i,j]= (infinite ost) for n verties there re n itertions over the rry A Floy is thus O(n 3 ) 05/12/2016 DFR - DSA - Grphs 1 28
Floy s Algorithm Floy ( ) { A[i,k] A[i,j] A[k,j] for (i in 1..n) for (j in 1..n) if (i <> j) A[i, j] = C[i, j] for (i in 1..n) A[i, i] = 0 -- initilistion for (k in 1..n) for (i in 1..n) for (j in 1..n) } if ( A[i, k] + A[k, j] < A[i, j]) A[i, j] = A[i, k] + A[k, j] 05/12/2016 DFR - DSA - Grphs 1 29
Floy - Exmple A 0 [i,j] = 0 8 5 A 1 [i,j] = 0 8 5 3 0 3 0 8 2 0 2 0 A 2 [i,j] = 0 8 5 A 3 [i,j] = 0 7 5 3 0 8 3 0 8 5 2 0 5 2 0 = infinity 2 1 3 2 2 3 8 5 05/12/2016 DFR - DSA - Grphs 1 30
Floy - Comment Initilistion is the osts in C (i.e. Initil ege osts) with the igonl (i.e. v => v) set to 0 for eh noe (k = 1..n) go through the rry (i, j = 1..n) n ompute osts - i.e. hek if there is heper pth from noe i to noe j vi noe k - if so hnge A[i, j] if ( A[i, k] + A[k, j] < A[i, j]) A[i, j] = A[i, k] + A[k, j] 05/12/2016 DFR - DSA - Grphs 1 31
Trnsitive Closure & Wrshll s Algorithm Determine if pth exists from vertex i to vertex j C[i, j] = 1 if n ege exists (i <> j), otherwise = 0 ompute A[i, j], suh tht A[i, j] = 1 if there exists pth of length 1 or more from vertex i to vertex j A is lle the trnsitive losure of the jeny mtrix Note tht this is speil se of Floy s where we re not iretly intereste in the osts 05/12/2016 DFR - DSA - Grphs 1 32
Wrshll s Algorithm Wrshll ( ) { A[i,k] A[i,j] A[k,j] for (i in 1..n) for (j in 1..n) A[i, j] = C[i, j] for (i in 1..n) A[i, i] = 0 -- initilistion for (k in 1..n) for (i in 1..n) for (j in 1..n) if (A[i, j] = 0) A[i, j] = A[i, k] n A[k, j] } 05/12/2016 DFR - DSA - Grphs 1 33
Wrshll - Exmple A 0 [i,j] = 0 1 1 A 1 [i,j] = 0 1 1 1 0 0 1 1 1 0 1 0 0 1 0 A 2 [i,j] = 1 1 1 A 3 [i,j] = 1 1 1 1 1 1 1 1 1 2 1 3 2 2 8 5 1 1 1 1 1 1 3 05/12/2016 DFR - DSA - Grphs 1 34
Wrshll - Comment if (A[i, j] = 0) A[i, j] = A[i, k] n A[k, j] - (mening?) i.e. there is pth from noe i to noe j IF there is pth from noe i to noe k AND pth from noe k to noe j t vrious stges in the lultion for k, i, j, the ifferent pths re isovere (1, 2, 2) - A[2,2] =A[2,1] n A[1,2] - i.e. 2 to 1 to 2 => 2 to 2 (1, 2, 3) - A[2,3] =A[2,1] n A[1,3] - i.e. 2 to 1 to 3 => 2 to 3 (2, 3, 1) - A[3,1] =A[3,2] n A[2,1] - i.e. 3 to 2 to 1 => 3 to 1 (2, 3, 3) - A[3,3] =A[3,2] n A[2,3] - i.e. 3 to 2 to 3 => 3 to 3 05/12/2016 DFR - DSA - Grphs 1 35
Summry irete grphs Definitions & implementtions Algorithms Dijkstr single noe shortest pth Dijkstr-SPT + shortest pth tree Floy ll pirs shortest pth Wrshll trnsitive losure is there pth from to? 05/12/2016 DFR - DSA - Grphs 1 36