Download presentation
Presentation is loading. Please wait.
Published byJasmina MiheliΔ Modified over 6 years ago
1
Dynamic Programming Shortest Paths with Negative Weights Yin Tat Lee
CSE 421 Dynamic Programming Shortest Paths with Negative Weights Yin Tat Lee
2
Shortest Paths with Neg Edge Weights
Given a weighted directed graph πΊ= π,πΈ and a source vertex π , where the weight of edge (u,v) is π π’,π£ (that can be negative) Goal: Find the shortest path from s to all vertices of G. Recall that Dikjstraβs Algorithm fails when weights are negative 1 1 -2 3 3 -2 source s 2 3 2 3 s 2 -1 -1 2 4 4
3
Impossibility on Graphs with Neg Cycles
Observation: No solution exists if G has a negative cycle. This is because we can minimize the length by going over the cycle again and again. So, suppose G does not have a negative cycle. 1 3 -2 2 3 s 2 -1 4
4
DP for Shortest Path (First Attempt)
Def: Let πππ(π£) be the length of the shortest π - π£ path πππ π£ = 0 if π£=π min π’: π’,π£ an edge πππ π’ + π π’,π£ The formula is correct. But it is not clear how to compute it.
5
DP for Shortest Path Def: Let πππ(π£,π) be the length of the shortest π - π£ path with at most π edges. Let us characterize πππ(π£,π). Case 1: πππ(π£,π) path has less than π edges. Then, πππ π£,π =πππ π£,πβ1 . Case 2: πππ(π£,π) path has exactly π edges. Let π , π£ 1 , π£ 2 ,β¦, π£ πβ1 ,π£ be the πππ(π£,π) path with π edges. Then, π , π£ 1 ,β¦, π£ πβ1 must be the shortest π - π£ πβ1 path with at most πβ1 edges. So, πππ π£,π =πππ π£ πβ1 ,πβ1 + π π£ πβ1 ,π£
6
DP for Shortest Path Def: Let πππ(π£,π) be the length of the shortest π - π£ path with at most π edges. πππ π£,π = 0 if π£=π β if π£β π ,π=0 min (πππ π£,πβ1 , min π’: π’,π£ an edge πππ π’,πβ1 + π π’,π£ ) So, for every v, πππ π£,? is the shortest path from s to v. But how long do we have to run? Since G has no negative cycle, it has at most πβ1 edges. So, πππ(π£,πβ1) is the answer.
7
Bellman Ford Algorithm
for v=1 to n if πβ π then M[v,0]=β M[s,0]=0. for i=1 to n-1 M[v,i]=M[v,i-1] for every edge (u,v) M[v,i]=min(M[v,i], M[u,i-1]+cu,v) Running Time: π ππ Can we test if G has negative cycles? Yes, run for i=1β¦3n and see if the M[v,n-1] is different from M[v,3n]
8
DP Techniques Summary Recipe: Dynamic programming techniques.
Follow the natural induction proof. Find out additional assumptions/variables/subproblems that you need to do the induction Strengthen the hypothesis and define w.r.t. new subproblems Dynamic programming techniques. Ordering is important: longest path in DAG Adding a new variable: knapsack. Dynamic programming over intervals: RNA secondary structure. Top-down vs. bottom-up: Different people have different intuitions Bottom-up is useful to optimize the memory
9
Divide and Conquer πΆ(π ππππ) time polynomial multiplication
CSE 421 Divide and Conquer πΆ(π ππππ) time polynomial multiplication
10
Multiplication Polynomials Integers NaΓ―ve: ο(n2) Karatsuba: ο(n1.59β¦)
Best known: ο(n log n) "Fast Fourier Transformβ FFT widely used for signal processing Integers Similar, but some ugly details re: carries, etc. due to Schonhage-Strassen in 1971 gives ο(n log n loglog n) Improvement in 2007 due to Furer gives ο(n log n 2log* n) Used in practice in symbolic manipulation systems like Maple
11
Given set of values at 5 points
Interpolation Given set of values at 5 points
12
Interpolation Given set of values at 5 points
Can find unique degree 4 polynomial going through these points
13
Multiplying Polynomials by Evaluation & Interpolation
Any degree n-1 polynomial R(y) is determined by R(y0), ... R(yn-1) for any n distinct y0,...,yn-1 To compute PQ (assume degree at most n/2-1) Evaluate P(y0),..., P(yn-1) Evaluate Q(y0),...,Q(yn-1) Multiply values P(yi)Q(yi) for i=0,...,n-1 Interpolate to recover PQ
14
Interpolation Given values of degree n-1 polynomial R at n distinct points y0,β¦,yn-1 R(y0),β¦,R(yn-1) Compute coefficients c0,β¦,cn-1 such that R(x)=c0+c1x+c2x2+β¦+cn-1xn-1 System of linear equations in c0,β¦,cn-1 c0 +c1y0+c2y02+β¦+cn-1y0n-1=R(y0) c0 +c1y1+c2y12+β¦+cn-1y1n-1=R(y1) β¦ c0 +c1yn-1+c2yn cn-1yn-1n-1=R(yn-1) known unknown
15
Interpolation: n equations in n unknowns
Matrix form of the linear system 1 y0 y02 β¦ y0n c R(y0) 1 y1 y12 β¦ y1n c R(y1) β¦ c2 = β¦ 1 yn-1 yn-12 β¦ yn-1n-1 cn R(yn-1) Fact: Determinant of the matrix is PiοΌj (yi-yj) which is not 0 since points are distinct System has a unique solution c0,β¦,cn-1
16
Evaluation & Interpolation
ordinary polynomial multiplication Q(n2) P: a0,a1,...,an/2-1 Q: b0,b1,...,bn/2-1 R:c0,c1,...,cn-1 evaluation at y0,...,yn-1 O(?) interpolation from y0,...,yn-1 O(?) point-wise multiplication of numbers O(n) P(y0),Q(y0) P(y1),Q(y1) ... P(yn-1),Q(yn-1) R(y0)ο¬P(y0)οQ(y0) R(y1)ο¬P(y1)οQ(y1) ... R(yn-1)ο¬P(yn-1)οQ(yn-1)
17
Karatsubaβs algorithm and evaluation and interpolation
Strassen gave a way of doing 2x2 matrix multiplies with fewer multiplications Karatsubaβs algorithm can be thought of as a way of multiplying degree 1 polynomials (which have 2 coefficients) using fewer multiplications PQ=(P0+P1z)(Q0+Q1z) = P0Q0 + (P1Q0+P0Q1)z + P1Q1z2 Evaluate at 0,1,-1 (Could also use other points) A = P(0)Q(0)= P0Q0 C = P(1)Q(1)=(P0+P1)(Q0+Q1) D = P(-1)Q(-1)=(P0 -P1)(Q0 -Q1) Interpolating, Karatsubaβs Mid=(C-D)/2 and B=(C+D)/2-A
18
Evaluation at Special Points
Evaluation of polynomial at 1 point takes O(n) time So 2n points (naively) takes O(n2)βno savings But the algorithm works no matter what the points areβ¦ Soβ¦choose points that are related to each other so that evaluation problems can share subproblems Own a modem or an MP3 player? You own this stuff.
19
The key idea: Evaluate at related points
P(x) = a0+a1x+a2x2+a3x3+a4x4+...+an-1xn = a0 +a2x2 +a4x an-2xn a1x+a3x3 +a5x an-1xn = Peven(x2) + x Podd(x2) P(-x)=a0 -a1x+a2x2 -a3x3+a4x an-1xn = a0 +a2x2 +a4x an-2xn (a1x+a3x3 +a5x an-1xn-1) = Peven(x2) - x Podd(x2) where Peven(z) = a0 +a2z +a4z an-2zn/2-1 and Podd(z) = a1+a3z +a5z an-1zn/2-1
20
The key idea: Evaluate at related points
Soβ¦ if we have half the points as negatives of the other half i.e., yn/2= -y0, yn/2+1= -y1,β¦,yn-1= -yn/2-1 then we can reduce the size n problem of evaluating degree n-1 polynomial P at n points to evaluating 2 degree n/2 - 1 polynomials Peven and Podd at n/2 points y02,β¦yn/2-12 and recombine answers with O(1) extra work per point But to use this idea recursively we need half of y02,β¦yn/2-12 to be negatives of the other half If yn/42 = -y02, say, then (yn/4/y0)2= -1 Motivates use of complex numbers as evaluation points
21
To multiply complex numbers:
c+di To multiply complex numbers: 1. add angles 2. multiply lengths (all length 1 here) a+bi e+fi q+j j -1 q 1 e+fi = (a+bi)(c+di) a+bi =cos q +i sin q = eiq c+di =cos j +i sin j = eij e+fi =cos (q+j) +i sin (q+j) = ei(q+j) -i e2pi = 1 epi = -1
22
Primitive nth root of 1 w=wn= ei 2p/n
w2=i w6= -i w4=-1 w3 w7 w w5 Let w = wn = ei 2p /n = cos (2p/n) +i sin (2p/n) i2 = -1 e2p i = 1
23
Facts about w=e2pi /n for even n
w = e2pi /n for i = βπ wn = 1 wn/2 = -1 wn/2+k = - wk for all values of k w2 = e2pi / m where m=n/2 wk = cos(2kp/n)+i sin(2kp/n) so can compute with powers of w wk is a root of xn-1= (x-1)(xn-1+xn-2+β¦+1) = but for kβ 0, wkβ 1 so wk(n-1)+wk(n-2) +β¦+1=0
24
The key idea for n even P(w) = a0+a1w+a2w2+a3w3+a4w4+...+an-1wn = a0 +a2w2 +a4w an-2wn a1w+a3w3 +a5w an-1wn = Peven(w2) + w Podd(w2) P(-w)=a0 -a1w+a2w2 -a3w3+a4w an-1wn = a0 +a2w2 +a4w an-2wn (a1w+a3w3 +a5w an-1wn-1) = Peven(w2) - w Podd(w2) where Peven(x) = a0 +a2x +a4x an-2xn/2-1 and Podd(x) = a1+a3x +a5x an-1xn/2-1
25
The recursive idea for n a power of 2
Goal: Evaluate P at 1,w,w2,w3,...,wn-1 Now Peven and Podd have degree n/2-1 where P(wk)=Peven(w2k)+wkPodd(w2k) P(-wk)=Peven(w2k)-wkPodd(w2k) Recursive Algorithm Evaluate Peven at 1,w2,w4,...,wn-2 Evaluate Podd at 1,w2,w4,...,wn-2 Combine to compute P at 1,w,w2,...,wn/2-1 Combine to compute P at -1,-w,-w2,...,-wn/ (i.e. at wn/2, wn/2+1 , wn/2+2,..., wn-1) w2 is e2pi /m where m=n/2 so problems are of same type but smaller size
26
Analysis and more Run-time T(n)=2οT(n/2)+cn so T(n)=O(n log n)
So much for evaluation ... what about interpolation? Given r0=R(1), r1=R(w), r2=R(w2),..., rn-1=R(wn-1) Compute c0, c1,...,cn-1 s.t. R(x)=c0+c1x+...+cn-1xn-1
27
Interpolation ο» Evaluation: strange but true
Non-obvious fact: If we define a new polynomial S(x) = r0 + r1x + r2x rn-1xn-1 where r0, r1, ... , rn are the evaluations of R at 1, w, ... , wn-1 Then ck=S(w-k)/n for k=0,...,n-1 Relies on the fact the interpolation (inverse) matrix has ij entry w-(i+j)/n instead of wi+j So... evaluate S at 1,w-1,w-2,...,w-(n-1) then divide each answer by n to get the c0,...,cn-1 w-1 behaves just like w did so the same O(n log n) evaluation algorithm applies !
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.