Algorithm Deign nd Anli Vitor Admhik C - pring Leture Feb, Crnegie Mellon Univerit The hortet Pth Problem Grph Algorithm - The hortet Pth Problem Given poitivel weighted grph G with oure verte, find the hortet pth from to ll other vertie in the grph. 9 8 9 7 Greed pproh When lgorithm proeed ll vertie re divided into two group - vertie whoe hortet pth from the oure i known - vertie whoe hortet pth from the oure i NOT known Move vertie (hortet ditne) one t time from the unknown et to the known et. Mintin PQ of ditne from the oure to verte. The hortet Pth Problem 9 8 9 9 7 Compleit O (V Log V + E log V) Let D(v) denote length from the oure to verte v. We tore ditne D(v) in PQ. O(V) INIT: D() = ; D(v)= PQ h V vertie LOOP: O(log V) Delete node v from PQ uing deletemin() Updte D(w) for ll w in dj(v) uing dereeke() O(log V) We do O(E) updte D(w) = min[d(w), D(v) + (v, w)]
PQ i liner rr Aume tht unorted rr i ued inted of priorit queue. Wht would the lgorithm' running time in thi e? findmin tke O(V) - for one verte findmin tke O(V ) - for ll vertie Updting tke O() - for one edge totl edge djutment O(E) the lgorithm running time O(E + V ) Wh Dijktr lgorithm doe not work on grph with negtive weight? The ellmn-ford lgorithm (98) A repet V - time: for ll e in E: updte(e) -9 The ellmn-ford Algorithm for (k = ; k < V; k++) dit[k] = INFINITY; Queue q = new Queue(); dit[] = ; q.enqueue(); while (!q.iempt()) { v = q.dequeue(); for eh w in dj(v) do if (dit[w] > dit[v] + weight[v,w]) { dit[w] = dit[v] + weight[v,w]; if (!q.iinqueue(w)) q.enqueue(w); }} Wht i the wort-e ompleit of the ellmn-ford lgorithm? for (k = ; k < V; k++) dit[k] = INFINITY; Queue q = new Queue(); V dit[] = ; q.enqueue(); while (!q.iempt()) { v = q.dequeue(); E for eh w in dj(v) do if (dit[w] > dit[v] + weight[v,w]) { dit[w] = dit[v] + weight[v,w]; if (!q.iinqueue(w)) q.enqueue(w); }} O(V E)
Grph with negtive le? A How would ou ppl the ellmn-ford lgorithm to find out if grph h negtive le? -9 How would ou ppl the ellmn- Ford lgorithm to find out if grph h negtive le? Do not top fter V- itertion, perform one more round. If there i uh le, then ome ditne will be redued ellmn-ford Dnmi progrmming pproh We will be ounting the number of edge in the hortet pth Dnmi progrmming pproh For eh node, find the length of the hortet pth to t tht ue t mot edge, or write down if there i no uh pth. uppoe for ll v we hve olved for length of the hortet pth to t tht ue k or fewer edge. How n we ue thi to olve for the hortet pth tht ue k or fewer edge? We go to ome neighbor of v, nd then tke the hortet pth from to t tht ue k or fewer edge. All-Pir hortet Pth (APP) Given weighted grph, find hortet pth from n verte to n other verte. Note, no ditinguihed verte All-Pir hortet Pth One pproh: run Dijktr' lgorithm uing ever verte oure. Compleit: O(V E Log V) pre: O(V Log V) dene: O(V Log V) ut wht bout negtive weight
APP: ellmn-ford APP : Dnmi progrmming pproh Compleit : O(V E) Flod-Wrhll, O(V ) Note, for dene grph we hve O(V ). We won t diu it APP: Johnon lgorithm Compleit: O(V E + V E log V) for dene grph -- O(V log V). Johnon Algorithm It improve the runtime onl when grph h negtive weight. A bird view: - Reweight the grph, o ll weight re nonnegtive (b running ellmn-ford ) - Run Dijktr on ll vertie for pre grph -- O(V log V). Compleit: O(V E + V E log V) Johnon Algorithm: intuition Wrong reweighting (dding the fi mount) The w to improve the runtime i to run Dijktr from eh verte. A ut Dijktr doe not work on negtive edge. o wht bout if we hnge the edge weight to be nonnegtive? We hve to be reful on hnging the edge weight to preerve the hortet pth - C The tul hortet pth to i --C- Let u dd to ll edge
Wrong reweighting (dding the fi mount) A Adding the fi mount doe not work, ine ever hortet pth h different number of edge C hortet pth to i -A- Johnon Algorithm: reweighting Ever edge (v, u) with the ot w(v, u) i repled b where p(v) will be deided lter. w * (v, u) = + (-) = - v p(v)=- w(v, u) = u p(u)= Johnon Algorithm: reweighting Theorem. All pth between the me two vertie re reweighted b the me mount. Proof. Conider pth v = v v v n = u Then we hve w * (v,u) = w * (v, v ) + + w * (v n-, v n ) = w(v, v ) + p(v ) p(v ) + w(v, v ) + p(v ) p(v ) + Teleoping um Johnon reweighting hnge n pth between u nd v b the me mount nd therefore preerve the hortet pth unhnged Find verte lbeling P(v) Firt we need to rete new verte nd onnet it to ll other vertie with zero weight. Note thi hnge in the grph won t hnge the hortet ditne between vertie. - - b - z -
Running P Net we run ellmn-ford trting t verte. hortet Pth - i -b i - - i - nd o on - - - - b - z - Now we define p(v) the hortet ditne -v. - - Johnon Reweighting Here we redrw the emple b uing Edge (,b): -+-(-) = Edge (b,): -+(-)-(-) = Edge (z,): +-(-) = - - - - - b - z - - New grph After Johnon reweighting we get new grph with non-negtive weight. Remember, Johnon reweighting preerve the hortet pth. Now we n ue Dijktr z b Johnon Algorithm: reweighting Theorem. After reweighting ever edge h nonnegtive ot. Proof. Conider edge (v, u) p(v) i the hortet ditne from to v p(u) i the hortet ditne from to u p(u) p(v) + w(v, u) v u ine the hortet pth -u nnot be longer then p(v) + (v, u). QED Johnon Algorithm. Add new verte nd onnet it with ll other vertie.. Run ellmn-ford lgorithm from to ompute p(v). Note tht ellmn-ford lgorithm will orretl report if the originl grph h negtive ot le.. Reweight ll edge: w * (v,u) =w(v,u)+p(v) p(u). Run Dijktr lgorithm from ll vertie. Compute the tul ditne b ubtrting p(v) p(u) Compleit. Add new verte nd onnet it with ll other vertie.. Run ellmn-ford lgorithm from to ompute p(v). O(V) O(V E). Reweight ll edge: w * (v,u) =w(v,u)+p(v) p(u) O(E). Run Dijktr lgorithm from ll vertie O(V E log V). Compute the tul ditne b ubtrting p(v) p(u) O(E) Totl: O(V E log V)
Johnon Algorithm It hine for pre grph with negtive edge O(V log V) etter thn Flod-Wrhll, whih i O(V ) 7