Shores Pah Algorihms Background Seing: Lecure I: Shores Pah Algorihms Dr Kieran T. Herle Deparmen of Compuer Science Universi College Cork Ocober 201 direced graph, real edge weighs Le he lengh of a pah be he sum of is edge weighs and le δ(s, u) = lengh of shores pah from s o u Reduces o edge disance if all weighs are 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 1 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 2 / 1 Eample Background Graphs and Marices Single source shores pah problem Given a weighed graph G and a designaed node s G, deermine δ(s, u) for each u G. u δ(s, u) s 0 a 8 b c d 5 All pairs shores pah problem Given a weighed graph G, deermine δ(, ) for each, G. A Graph and is mari represenaion (nodes lised alpaheicall a = 1 ec.) 0 8 0 1 0 2 5 0 0 0 if i = j w = weigh(i, j) if i j and (i, j) E if i j and (i, j) E No negaive ccles allowed oherwise shores pah noion no well defined KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 / 1
Some Terminolog k-pah pah all of whose inermediae nodes are numbered less han or equal o k; (he sar/end nodes ma have numbers greaer han k). Consider shores k-pah from i o j : Bes pah is π is beer of α or β k γ eiher i passes hrough node k (once): 2- i.e. consiss of β = shores (k 1)-pah from i o k node k γ = shores (k 1)-pah from k o j or i does no pass hrough node k: α = shores (k 1)-pah from i o j KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 5 / 1 A useful ideni Definiion l (k) = lengh of shores k-pah joining i o j. l (k) = { w if k = 0 min{l (k 1), l (k 1) i,k + l (k 1) k,j } if k > 0 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 / 1 Observaion Visualie he se of l (k) quaniies as hree-dimensional grid wih poin l (k) having co-ordinaes (i, j, k). l (k) = { w if k = 0 min{l (k 1), l (k 1) i,k + l (k 1) k,j } if k > 0 Noice: each horional slice represens values l (k) for some fied k he quaniies in he boom slice (k = 0) depend onl on edge weighs each quani l (k) on slice k depends onl on quaniies on he slice below KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 / 1 Algorihm FLOYD WARSHALL(G): W = mari of edge weighs for i 1 o n do for j 1 o n do L[ i, j, 0] W[i, j ] for k 1 o n do for i 1 o n do for j 1 o n do if L[ i, j, k 1] < L[i, k, k 1] + L[k, j, k 1] L[ i, j, k] L[i, j, k 1] else L[ i, j, k] L[i, k, k 1] + L[k, j, k 1] KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 8 / 1
Noes Transiive Closure Lengh of shores i-o-j pah in L[i, j, n]. Negaive weigh OK, bu no negaive ccles. Running ime: O(n ) Can ge b wih 2D arras (wo for alernae slices) Can be modified o produce pahs (no jus lenghs) The ransiive closure of G = (V, E) he he graph G = (v, E ) such ha here is an edge in G from u i v if and onl if here is a pah in G from u o v. Algorihm: Give weigh 1 o each edge in G; Run Flod-Warshall algorihm; Each L[i, j, n] denoes pah in G, so add corresponding edge (i, j) o E. KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 10 / 1 Single-Source Shores Pah Problem Single-source shores pah problem (SSSP) Single source shores pah problem: Given a weighed graph G and a designaed node s G, deermine δ(s, u) for each u G. Disallow negaive edges Could use Flod-Warshall, bu is here a more efficien wa? s Problem 1 2 5 Pah sequence of edge-conneced verices Pah lengh sum of edge lenghs Shores pah pah beween endpoins wih minimum oal lengh (SSSP) Given graph G and source vere s, calculae lengh of shores pah from source s o each vere in G. KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 11 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 12 / 1
Idea s Inpus (lef): 1 2 5 s 0 5 1 2 5 G: graph wih nonnegaive (imporan!) edges s: sar node Oupus (righ): Compue for each node v, δ(s, v) = lengh shores pah from s o v ( if none). Shown inside nodes. 8 Begin wih a crude esimae d[u] for δ(s, u): { 0 if u = s d[u] = oherwise Refine esimaes using (carefull chosen) sequence of he following edge operaions (i.e. (u, v) mus be an edge): Ulimael (we hope) d[u] = δ(s, u), for all u. KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 1 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 1 / 1 Noes Observaion Through an sequence of Rela(u, v) seps d[] is non-increasing, and If d[], hen here is a pah of lengh d[] in G from s o. Algorihm Dijksra(G, s ): Iniialie d(s) o 0 Iniialie d() = INFTY for all s Creae priori queue Q and place each node in Q wih ke value d() while Q is no emp do u Q.remove min elemen() for each v in G.neighbours(u) do Rela(u, v) KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 15 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 1 / 1
Priori queue refresher D s algorihm in acion Wha Conainer absracion holding ke, value pairs Operaions inser Add new ke value iem o conainer min Reurn he value wih he smalles ke (ies broken arb.) remove min Reurn and remove he value wih he smalles ke Implemenaion Heap-ordered ree sored in arra; inser, remove min O(log n) ime. s 0 5 1 2 5 Algorihm Dijksra(G, s ):... while Q is no emp do u Q.remove min elemen() for each v in G.neighbours(u) do Rela(u, v) 1 8 Q vere non-q vere u vere KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 1 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 18 / 1 Aside Running ime of D s algorihm Rela involves changes o d[] i.e. priori queue kes. New priori queue operaion: reduce ke(, k) Replace ke of iem wih smaller ke k Implemenaion noe: Effecivel helper mehod heap decrease ke used in inser operaion Running ime: O(log(queue sie)) Algorihm Dijksra(G, s ): / Iniialiaion suff / while Q is no emp do u Q.remove min elemen() for each v in G.neighbours(u) do Rela(u, v) n, m = num. nodes, edges Priori queue operaions: O((n + m) log n) (over) Non-priori queue suff: O(n + m) Toal running ime: O((n + m) log n) KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 1 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 20 / 1
Noes Priori queue operaions op no. cos per suboal inser n O(log n) O(n log n) isemp n O(1) O(n) remove min elemen n O(log n) O(n log n) replaceke m O(log n) O(m log n) Non-priori queue suff n ieraions in all for fied u, for-loop akes O(#edges leaving u) ime Toal O(n) + O(#edges leaving u) = O(n + m) }{{} u V while } {{ } for O((n + m) log n) + O(n + m) = O((n + m) log n) KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 21 / 1 1 Definiion We sa is seled once d() = δ(s, ). If u is seled and s u v is a shores pah o v, hen following Rela(u, v) v is also seled Before: Rela(u, v), d(u) = δ(s, u) Afer: Rela(u, v), d(v) d(u) + w(u, v) This mus equal δ(s, v): shores pah assumpion KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 22 / 1 2 Node u is seled before being removed from Q. Proof. Suppose his were no rue Le u be he firs node removed from Q ha was no seled Le be firs node along shores pah from s o u ha is in Q and be is predecessor KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 2 / 1 2 con d Node u is seled before being removed from Q. d(u) d() b remove min elemens choice of u = δ(s, ) no in Q so d() = δ(s, ); Rela(, ) operaion ensures ha d() = δ(s, ) δ(s, u) since s-o- a prefi of s-o-u and edge-weighs are non-negaive d(u) KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 2 / 1 Hence d(u) = δ(s, u)
Consrucing he Pahs SP Algorihms and Nework Rouing Minor weak o Rela o record shores-pah predecessors so algorihms consrucs pahs no jus heir lengh. p[v] u A conclusion, p() indicaes paren of in a (shores-pah) ree rooed a s. The roo o leaf pah o is a shores pah. KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 25 / 1 nework of hoss/swiches(nodes) and links (edges) edge weigh desirabili of using edge Deermine for/a each node, he lowes cos roue o each desinaion Would like approach o be disribued resilien o failures/changes KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 2 / 1 Idea 1 Idea 2 Each node deermines is neighbours/edges and coss and injecs ino ino nework. (Re-injecs following changes) Nodes echange his informaion among hemselves Each node ges complee map of he nework Changes ge refleced in maps as new info replaces old Each node runs Dijksra locall o esablish bes pahs from o oher nework nodes Mainains able of (des., cos, ne-hop) enries for rouing decisions Informaion re-compued when nework map changes KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 2 / 1 KH (21/10/1) Lecure I: Shores Pah Algorihms Ocober 201 28 / 1