Data Stctes Week #0 Gaphs & Gaph Algoithms
Otline Motiation fo Gaphs Definitions Repesentation of Gaphs Topological Sot Beadth-Fist Seach (BFS) Depth-Fist Seach (DFS) Single-Soce Shotest Path Poblem (SSSP) Dijksta s Algoithm Minimm Spanning Tees Pim s Algoithm Kskal s Algoithm Octobe 5, 05 Boahan Tüme, Ph.D.
Gaphs & Gaph Algoithms Octobe 5, 05 Boahan Tüme, Ph.D. 3
Motiation Gaphs ae sefl stctes fo soling man poblems compte science is inteested in inclding bt not limited to Compte and telephon netoks Game theo Implementation of atomata Octobe 5, 05 Boahan Tüme, Ph.D.
Gaph Definitions A gaph G=(V,E) consists a set of etices V and a set of edges E. An edge (,) E has a stating ete and and ending ete. An edge sometimes is called an ac. If the pai is odeed, then the gaph is diected. Diected gaphs ae also called digaphs. Gaphs hich hae a thid component called a eight o cost associated ith each edge ae called eighted gaphs. Octobe 5, 05 Boahan Tüme, Ph.D. 5
Adjacenc Set and Being Adjacent Vete is adjacent to iff (,) E. In an ndiected gaph ith e=(,), and ae adjacent to each othe. In Fig.., the etices, and fom the adjacenc set of o Adj()={,,}. Fige.. Adjacenc set of Octobe 5, 05 Boahan Tüme, Ph.D.
Moe Definitions A ccle is a path sch that the ete at the destination of the last edge is the soce of the fist edge. A digaph is acclic iff it has no ccles in it. In-degee of a ete is the nmbe of edges aiing at that ete. Ot-degee of a ete is the nmbe of edges leaing that ete. Octobe 5, 05 Boahan Tüme, Ph.D.
Path Definitions A path in a gaph is a seqence of etices,,, n hee each edge ( i, i+ ) E fo i<n. The length of a path is the nmbe of edges on the path, (i.e., n- fo the aboe path). A path fom a ete to itself, containing no edges has a length 0. An edge (,) is called a loop. A simple path is one in hich all etices, ecept possibl the fist and the last, ae distinct. Octobe 5, 05 Boahan Tüme, Ph.D.
Connectedness An ndiected gaph is connected if thee eists a path fom ee ete to ee othe ete. A digaph ith the same popet is called stongl connected. If a digaph is not stongl connected, bt the ndeling gaph (i.e., the ndiected gaph ith the same topolog) is connected, then the digaph is said to be eakl connected. A gaph is complete if thee is an edge beteen ee pai of etices. Octobe 5, 05 Boahan Tüme, Ph.D.
Repesentation of Gaphs To as to epesent gaphs: Adjacenc mati epesentation Adjacenc list epesentation Octobe 5, 05 Boahan Tüme, Ph.D. 0
Adjacenc Mati Repesentation Assme o hae n etices. In a boolean aa ith n elements, hee each element epesents the connection of a pai of etices, o assign te to those elements that ae connected b an edge and false to othes. Good fo dense gaphs! Not e efficient fo spase (i.e., not dense) gaphs. Space eqiement: O( V ). Octobe 5, 05 Boahan Tüme, Ph.D.
Adjacenc mati epesentation 5 3 3 5 5 3 3 3 (AMR) 3 5 3 5 3 3 5 5 3 3 Disadantage: Waste of space fo spase gaphs Adantage: Fast access Octobe 5, 05 Boahan Tüme, Ph.D.
Adjacenc List Repesentation Assme o hae n etices. We emplo an aa ith n elements, hee i th element epesents ete i in the gaph. Hence, element i is a heade to a list of etices adjacent to the ete i. Good fo spase gaphs Space eqiement: O( E + V ). Octobe 5, 05 Boahan Tüme, Ph.D. 3
Adjacenc list epesentation (ALR) aa inde: soce ete; fist nmbe: destination ete; second nmbe: cost of the coesponding edge 5 3 3 3, 3,5,,, 5,,3,,3 3 5 5 3 5,5,, 3,,,3, 3,,,3, Disadantage: Seqential seach among edges of a node Adantage: Minimm space eqiement Octobe 5, 05 Boahan Tüme, Ph.D.
Topological Sot Topological sot is an odeing of etices in an acclic digaph sch that if thee is a path fom i to j, then j appeas afte i in the odeing. Eample: cose peeqisite eqiements. Octobe 5, 05 Boahan Tüme, Ph.D. 5
Algoithm fo Topological Sot * Void Toposot () { Qee Q; int ct=0; Vete,; Q=ceateQee(NmVete); fo each ete if (indegee[] == 0) enqee(,q); hile (!IsEmpt(Q)) { =deqee(q); topnm[]=++ct; fo each adjacent to if (--indegee[] == 0) enqee(,q); } if (ct!= NmVete) epot eo ( gaph cclic! ) fee qee; } *Fom [] Octobe 5, 05 Boahan Tüme, Ph.D.
An Eample to Topological Sot s t Q is indicated b the ed sqaes. TN keeps tack of the ode in hich the etices ae pocessed. Q TN Octobe 5, 05 Boahan Tüme, Ph.D.
An Eample to Topological Sot s t Q TN 0 Octobe 5, 05 Boahan Tüme, Ph.D.
An Eample to Topological Sot s t Q TN 0 s Octobe 5, 05 Boahan Tüme, Ph.D.
An Eample to Topological Sot s t Q TN 0 s t Octobe 5, 05 Boahan Tüme, Ph.D. 0
An Eample to Topological Sot s t Q TN 0 s 3 t Octobe 5, 05 Boahan Tüme, Ph.D.
An Eample to Topological Sot s t Q TN 0 s t 3 Octobe 5, 05 Boahan Tüme, Ph.D.
An Eample to Topological Sot s t Q TN 0 s t 3 5 Octobe 5, 05 Boahan Tüme, Ph.D. 3
An Eample to Topological Sot s t Q TN s t 0 3 5 Octobe 5, 05 Boahan Tüme, Ph.D.
An Eample to Topological Sot s t Q TN 0 s t 3 5 Octobe 5, 05 Boahan Tüme, Ph.D. 5
An Eample to Topological Sot s t Q TN 0 s t 3 5 Octobe 5, 05 Boahan Tüme, Ph.D.
Beadth-Fist Seach (BFS) Gien a gaph, G, and a soce ete, s, beadth-fist seach (BFS) checks to discoe ee ete eachable fom s. BFS discoes etices eachable fom s in a beadth-fist manne. That is, etices a distance of k aa fom s ae sstematicall discoeed befoe etices eachable fom s thogh a path of length k+. Octobe 5, 05 Boahan Tüme, Ph.D.
Beadth-Fist Seach (BFS) To follo ho the algoithm poceeds, BFS colos each ete hite, ga o black. Unpocessed nodes ae coloed hite hile etices discoeed (enconteed ding seach) tn to ga. Vetices pocessed (i.e., etices ith all neighbos discoeed) become black. Algoithm teminates hen all etices ae isited. Octobe 5, 05 Boahan Tüme, Ph.D.
Algoithm fo Beadth-Fist Seach * BFS(Gaph G, Vete s) { // initialie all etices fo each ete V[G]-{s} { colo []=hite; dist[]= ; fom[]=null; } colo[s]=ga; dist[s]=0; fom[s]=null; Q={}; enqee(q,s); } hile (!isempt(q)) { =deqee(q); fo each Adj[] if (colo []==hite) { colo[]=ga; dist[]=dist[]+; fom[]=; enqee(q,); } colo[]=black; } * Fom [] Octobe 5, 05 Boahan Tüme, Ph.D.
Octobe 5, 05 Boahan Tüme, Ph.D. 30 An Eample to BFS 0 Q s s t s t 0 Q t s t 0 Q t s t 0 Q t s t 0 Q s t 0 Q
Octobe 5, 05 Boahan Tüme, Ph.D. 3 Rest of Eample s t 0 Q s t 0 Q s t 0 Q s t 0 Q
Depth-Fist Seach (DFS) Unlike in BFS, depth-fist seach (DFS), pefoms a seach going deepe in the gaph. The seach poceeds discoeing etices that ae deepe on a path and looks fo an left edges of the most ecentl discoeed ete. If all edges of ae fond, DFS backtacks to the ete t hich as discoeed fom to find the emaining edges. Octobe 5, 05 Boahan Tüme, Ph.D. 3
Algoithm fo Depth-Fist Seach * DFS(Gaph G, Vete s) { // initialie all etices fo each ete V[G] { colo []=hite; fom[]=null; } time=0; fo each ete V[G] if (colo []==hite) DFS-isit(); } DFS-isit() { colo[]=ga; // jst discoeed time++; d[]=time; fo each Adj[] //check edge (,) if (colo[] == hite) { fom[]=; DFS-isit(); //ecsie call } colo[]=black; // is done pocessing f[] = time++; } *Fom [] Octobe 5, 05 Boahan Tüme, Ph.D. 33
Depth-Fist Seach The fnction DFS() is a manage fnction calling the ecsie fnction DFS-isit() fo each ete in the gaph. DFS-isit() stats b gaing the ete jst discoeed. Then it ecsiel isits and discoes (and hence gas) all those nodes in the adjacenc set of, Adj[]. At the end, is finished pocessing and tns to black. time in DFS-isit() time-stamps each ete hen is discoeed sing d[] is done pocessing sing f[]. Octobe 5, 05 Boahan Tüme, Ph.D. 3
An eample to DFS s t / s / t / 3/ s / t / s / t / s / t / s / t / 3/ 3/ 3/ /5 / / Octobe 5, 05 Boahan Tüme, Ph.D. 35
Octobe 5, 05 Boahan Tüme, Ph.D. 3 Eample cont d... s t 3/ / / /5 s t 3/ / / /5 s t / 3/ / / /5 s t / 3/ / / /5 s t / 3/ / /0 /5 s t / / 3/ / /0 /5
Octobe 5, 05 Boahan Tüme, Ph.D. 3 Eample cont d... s t / / / 3/ / /0 /5 s t / / / 3/ / /0 /5 s t / / / 3/ / 3/ /0 /5 s t / / / 3/ / 3/ /0 /5 s t / / / 3/ / 3/ / /0 /5 s t / / / 3/ / 3/ / /0 /5
End of Eample / s /0 t / / s /0 t / / s /0 t / 3/ / / 3/ / / 3/ / / 3/ /5 /5 3/ /5 /5 3/ /5 /5 s t / /0 / / s /0 t / 3/ / / 3/ / / 3/ /5 /5 3/ /5 /5 Octobe 5, 05 Boahan Tüme, Ph.D. 3
Single-Soce Shotest Paths (SSSP) SSSP Poblem: Gien a eighted digaph G (V,E), e need to efficientl find the shotest path p * =( i, i+,..., j,..., k-, k ) beteen to etices i and k. The shotest path p * is the path ith the minimm eight among all paths p l =( i,..., k ), o * ( p ) min l ( p l ) Octobe 5, 05 Boahan Tüme, Ph.D. 3
Dijksta s Algoithm Dijksta s algoithm soles the SSSP poblem on a eighted digaph G=(V,E) assming no negatie eights eist in G. Inpt paametes fo Dijksta s algoithm the gaph G, the eights, a soce ete s. It ses a set V F holding etices ith final shotest paths fom the soce ete s. fom[] and dist[] fo each ete V as in BFS. A min-heap Q Octobe 5, 05 Boahan Tüme, Ph.D. 0
Dijksta s Algoithm Dijksta(Gaph G, Weights, Vete s) { fo each ete V[G] { dist []= ; fom[]=null; } dist [s]=0; V F = ø; Q = all etices V ; hile (!IsEmpt(Q)) { =deletemin(q); add to V F ; fo each ete Adj() if (dist[]>dist[]+(,)){ dist[]=dist[]+(,)); fom[]=; } } // end of hile } //end of fnction Octobe 5, 05 Boahan Tüme, Ph.D.
Dijksta s Algoithm An Eample t 5 3 s 0 3 5 3 3 t 5 3 s 0 3 5 3 3 3 t 5 3 s 0 3 5 3 3 3 t 5 3 s 0 3 5 3 3 3 t 5 3 s 0 3 5 3 3 3 t 5 3 s 0 3 5 3 3 3 Octobe 5, 05 Boahan Tüme, Ph.D.
Dijksta s Algoithm An Eample t 5 3 s 0 3 5 3 3 t 5 3 s 3 0 3 5 3 3 3 3 t 5 3 s 3 0 3 5 3 3 3 t 5 3 s 3 0 3 5 3 3 3 t 5 3 s 3 0 3 5 3 3 3 t 5 3 s 0 3 5 3 3 3 3 Octobe 5, 05 Boahan Tüme, Ph.D. 3
Reslting Shotest Paths 3 3 t 5 0 3 3 5 3 s 3 Note that is not eachable fom s! Octobe 5, 05 Boahan Tüme, Ph.D.
Minimm Spanning Tees (MSTs) Poblem: Gien a connected eighted ndiected gaph G=(V,E), find an acclic sbset S E, sch that S connects all etices in G and the sm of the eights of the edges in S ae minimm. The soltion to the poblem is poided b a minimm spanning tee. Octobe 5, 05 Boahan Tüme, Ph.D. 5
Minimm Spanning Tees (MSTs) MST is a tee since it connects all etices b an acclic sbset of S E, spanning since it spans the gaph (connects all its etices) minimm since its eights ae minimied. Octobe 5, 05 Boahan Tüme, Ph.D.
Pim s Algoithm Pim s algoithm opeates simila to Dijksta s algoithm to find shotest paths. Pim s algoithm poceeds alas ith a single tee. It stats ith an abita ete t. It pogessiel connects an isolated ete to the eisting tee b adding the edge ith the minimm possible eight to the tee. Octobe 5, 05 Boahan Tüme, Ph.D.
Pim s Algoithm Pim(Gaph G, Weights, Vete t) { fo each ete V[G] { dist []= ; fom[]=null; } dist [t]=0; V F = ø; Q = all etices V ; hile (!IsEmpt(Q)) { =deletemin(q); O(VlgV) add to V F ; fo each ete Adj() O(E) if ( Q and (,)<dist[]){ dist[]=(,); O(lgV) fom[]=; } } // end of hile } //end of fnction Rnning Time: O(V lgv + E lgv)=o(e lgv) Octobe 5, 05 Boahan Tüme, Ph.D.
Pim s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D.
Pim s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D. 50
Pim s Algoithm - Eample a b c d i e h g f 0 a b c d i e h g f 0 Octobe 5, 05 Boahan Tüme, Ph.D. 5
Pim s Algoithm - Eample a b c d i e h g f 0 a b c d i e h g f 0 Octobe 5, 05 Boahan Tüme, Ph.D. 5
Pim s Algoithm - Eample a b c d i e h g f 0 a b c d i e h g f 0 Octobe 5, 05 Boahan Tüme, Ph.D. 53
Pim s Algoithm - Eample a b c d i e h g f 0 a b c d i e h g f 0 Octobe 5, 05 Boahan Tüme, Ph.D. 5
Pim s Algoithm - Eample a b c d i e h g f 0 a b c d i e h g f 0 Octobe 5, 05 Boahan Tüme, Ph.D. 55
Pim s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D. 5
Pim s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D. 5
Kskal s Algoithm Kskal s Algoithm is anothe geed algoithm. It is abot finding the least eight and connecting ith that to tees in the foest. Initiall, thee eists a foest of man singlenode tees. Octobe 5, 05 Boahan Tüme, Ph.D. 5
Kskal s Algoithm Kskal(Gaph G, Weights ) { fo each ete V[G] { make each ete to a single-element tee; } sot edges in ascending ode b thei eight ; O(E lge) fo each edge (,) E O(E) if ( and ae in to diffeent tees) { lge=o(lgv) add (,) to the MST; since E < V combine both tees; } dist []=0; etn; Rnning Time: O(E lge + E)=O(E lge)=o(e lgv) Octobe 5, 05 Boahan Tüme, Ph.D. 5
Kskal s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D. 0
Kskal s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D.
Kskal s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D.
Kskal s Algoithm Eample To Altenaties a b c d i e h g f a 0 b c d i e h g f 0 Octobe 5, 05 Boahan Tüme, Ph.D. 3
Kskal s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D.
Kskal s Algoithm Eample To Altenaties a b c d i e h g f a 0 b c d i e h g f 0 Octobe 5, 05 Boahan Tüme, Ph.D. 5
Kskal s Algoithm - Eample b c d a i e 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D.
Kskal s Algoithm - Eample b c d a i e 0 h g f Edge not accepted! It bilds a ccle! Octobe 5, 05 Boahan Tüme, Ph.D.
Kskal s Algoithm Eample To Altenaties b c d a i e 0 h g f Edge not accepted! It bilds a ccle! b c d a i 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D. e
Kskal s Algoithm Eample To Altenaties a b c d i e h g f 0 b c d a i 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D. e
Kskal s Algoithm Eample To Altenaties a b c d i e h g f 0 b c d a i 0 h g f Octobe 5, 05 Boahan Tüme, Ph.D. 0 e
Refeences [] T.H. Comen, C.E. Leiseson, R.L. Riest, C. Stein, Intodction to Algoithms, nd Edition, 003, MIT Pess [] M.A. Weiss, Data Stctes and Algoithm Analsis in C, nd Edition,, Addison Wesle Octobe 5, 05 Boahan Tüme, Ph.D.