Download presentation
Presentation is loading. Please wait.
1
Analysis of Algorithms
Shortest Paths - Finding Negative Cycles Uri Zwick Tel Aviv University November 2015 Last modified: December 5, 2017
2
Single Source Shortest Paths
Non-negative edge weights π(π+π log π ) [Dijkstra (1959)] [Fredman-Tarjan (1987)] Undirected positively weighted graphs (word-RAM model) π(π+π) [Thorup (1999)] Positive and negative edge weights π(ππ) [Bellman (1956)] [Ford-Fulkerson (1962)] Positive and negative integer edge weights π(π π log π ) [Goldberg (1995)] All edge weights are integral and β₯βπ. (πβ₯2.)
3
Potentials and modified weights
A function π:πββ is called a potential function. If π€:πΈββ is a weight (length) function, then the modified weight function π€ π :πΈββ is defined as follows: π€ π π’,π£ =π€ π’,π£ +π π’ βπ(π£) , π’,π£ βπΈ Lemma: (i) If π is a path from π’ to π£, then π€ π π =π€ π +π π’ βπ(π£). (ii) If πΆ is a directed cycle, then π€ π πΆ =π€(πΆ). Corollary: Shortest paths with respect to π€ π are also shortest paths with respect to π€.
4
Potentials and negative cycles
Theorem: A digraph πΊ= π,πΈ,π€ has no negative cycles if and only if there is a potential function π such that π€ π π’,π£ =π€ π’,π£ +π π’ βπ(π£)β₯0, for every π’,π£ βπΈ. If there is such an admissible potential function, then by part (ii) of the previous lemma, there are no negative cycles. Suppose that there are no negative cycles. Add a vertex π and an edge (π ,π£) of weight 0 for each π£βπ. As there are still no negative cycles, distances from π are well-defined and satisfy the triangle inequality. Let π π£ =πΏ π ,π£ = distance from π to π£. πΏ π ,π£ β€πΏ π ,π’ +π€(π’,π£) , π’,π£ βπΈ π€ π π’,π£ =π€ π’,π£ +πΏ π ,π’ βπΏ π ,π£ β₯0 , π’,π£ βπΈ
5
Scaling We want to solve a problem with a given integer weight function π€:πΈββ€ Define a βcoarserβ integer weight function π€ β² :πΈββ€ π€ β² π = π€(π) 2 , πβπΈ. Solve the problem (recursively) with respect to π€β². Convert the solution for π€β² into a solution for π€.
6
Scaling and negative cycles
Given a digraph πΊ= π,πΈ,π€ , where π€:πΈββ€, find a negative cycle, or find a potential function π such that π€ π π β₯0, for every πβπΈ. Let π€ β² π = π€(π) 2 , πβπΈ. A negative cycle with respect to π€β² is also a negative cycle with respect to π€. Otherwise, let π β² :πββ€ be such that π€ π β² β² π β₯0, πβπΈ. Let π π’ =2πβ²(π’) , π’βπ. π€ π π’,π£ =π€ π’,π£ +π π’ βπ π£ β₯ 2 π€ β² π’,π£ β1 +2 π β² π’ β2 π β² π£ β₯β1 We thus only need to consider the case π€ π β₯β1 , πβπΈ.
7
Goldbergβs algorithm Let πΊ= π,πΈ,π€ , where π€:πΈβ{β1,0,1,β¦}.
If a strongly connected component of πΊ β contains a negative edge, then a negative cycle in πΊ is easily found. Otherwise, contract each strongly connected component of πΊ β . The contracted graph is acyclic. Add a source vertex π to πΊ β and connect it with 0-edges to all vertices of πΊ β . Compute the distances πΏ β π ,π£ , for all π£βπ. Can be done in π π time as πΊ β is acyclic. Let πΏ π = π£βπ πΏ β π ,π£ =βπ}.
8
π€ π’,π£ =β1 , π’β πΏ π , π£β πΏ π βΉ π<π
Goldbergβs algorithm Layers: πΏ π = π£βπ πΏ β π ,π£ =βπ}. π€ π’,π£ =0 , π’β πΏ π , π£β πΏ π βΉ πβ€π π€ π’,π£ =β1 , π’β πΏ π , π£β πΏ π βΉ π<π s β1 2 πΏ 0 πΏ 1 πΏ 2 πΏ π
9
β Dilworthβs theorem β πΏ 0 πΏ 1 πΏ 2 πΏ π
Let π be the index of the last layer. Let π be the number of negative vertices, i.e., vertices with an incoming negative edge. If πβ€ π , there is a layer πΏ π with at least π negative vertices. If π> π , there is a path with at least π negative vertices. s β1 πΏ 0 πΏ 1 πΏ 2 πΏ π
10
β1 A layer with negative vertices πΏ 0 πΏ 1 πΏ π πΏ π
Suppose that πΏ π contains many (at least π ) negative vertices. Assign the vertices of πΏ 0 βͺ πΏ 1 βͺβ¦βͺ πΏ πβ1 a potential of 0. Assign the vertices of πΏ π βͺ πΏ π+1 βͺβ¦βͺ πΏ π a potential of β1. Vertices of πΏ π are no longer negative. No negative vertices introduced. There are new 0 edges. Need to recompute πΊ β . s β1 β1 1 πΏ 0 πΏ 1 πΏ π πΏ π
11
A path with negative vertices
1 2 π
3 β1 β1 β1 π£ 1 π£ 2 π£ 3 Let π
3 be the set of vertices reachable from π£ 3 by 0 and β1 edges. (As πΊ β is acyclic, π£ 3 is the only vertex on the path in π
3 .) (We cannot reach β1 edges if the path is βmaximalβ.) Decrease the potential of all vertices in π
3 by 1. π£ 3 is no longer negative. No new negative vertices. But, there are new 0 edges.
12
A path with negative vertices
π
1 π
3 1 π
2 β1 β1 π£ 1 π£ 2 π£ 3 Let π
2 be the set of vertices reachable from π£ 2 by 0 and β1 edges. If π
2 contains a vertex preceding π£ 2 on the path ο negative cycle. Decrease the potential of all vertices in π
2 by 1. (Vertices in π
3 have their potential decreased by 2.)
13
A path with negative vertices
Let π£ 1 , π£ 2 ,β¦, π£ π be the negative vertices on the path. For π=π,πβ1,β¦,1 do: Let π
π be the vertices reachable from π£ π by 0 and β1 edges. If π
π contains a vertex preceding π£ π on the path, or if a path to π£ π containing at least one β1 edge is discovered ο negative cycle. Decrease the potential of all the vertices in π
π by 1. (This may create new 0 edges emanating from vertices of π
π .) Claim: If no negative cycles are discovered, then: (i) π£ 1 , π£ 2 ,β¦, π£ π are no longer negative. (ii) No new negative vertices introduced. (iii) No edge weight drops below β1. Exercise 1: Prove the above claim. Exercise 2: Implement the process above in π π time. (We will essentially solve these exercise below. Try to do it directly.)
14
A path with negative vertices
A clever way of implementing the previous process. Let π€ + π’,π£ = max 0,π€(π’,π£) . Add a source vertex π , and edges (π ,π£), for every π£βπ. Let π€ + π , π£ π =πβπ, for 1β€πβ€π. Let π€ + π ,π£ =π, for π£βπβ π£ 1 , π£ 2 ,β¦, π£ π . Compute the distances πΏ + π£ = πΏ + (π ,π£) from π with respect to π€ + . Can be done in π(π+π) time using Dialβs algorithm. Let π π£ = πΏ + (π£) , π£βπ. If the graph contains no negative cycles then: (i) π£ 1 , π£ 2 ,β¦, π£ π are no longer negative. (ii) No new negative vertices introduced. (iii) No edge weight drops below β1.
15
A path with negative vertices
π πβ1 πβ2 πβπ π β1 β1 β1 β1 π£ 1 π£ 2 π π£ π π£ π (ii) π€ π’,π£ β₯ βΉ π€ + π’,π£ =π€(π’,π£) πΏ + π£ β€ πΏ + π’ +π€(π’,π£) π€ π π’,π£ β₯0 (iii) π€ π’,π£ =β βΉ π€ + π’,π£ =0 πΏ + π£ β€ πΏ + π’ π€ π π’,π£ β₯β1
16
A path with negative vertices
π πβ1 πβπ πβπ β1 β1 β1 β1 π£ 1 β1 π£ π π£ π π£ π πΆ π’ π Suppose that π€ π’, π£ π =β1. If πΏ + π’ > πΏ + ( π£ π ), then π€ π π’, π£ π =π€ π’, π£ π + πΏ + π’ β πΏ + π£ π β₯0. Otherwise, πΏ + π’ β€ πΏ + π£ π β€πβπ. A shortest path from π to π’ starts with (π , π£ π ), where πβ₯π, and then a path π from π£ π to π’. By the triangle inequality, π€ π β€ π€ + π =πΏ + ( π£ π ,π’)β€πβπ. Let π be the portion of the path from π£ π to π£ π . Clearly π€ π =πβπ. π,π,(π’, π£ π ) is a negative cycle. π€ πΆ β€ πβπ + πβπ β1β€β1.
17
β¦ Dialβs algorithm [Dial (1969)]
Let πΊ=(π,πΈ,π€), where π€:πΈβ β€ + . Let π βπ. Let π·= max π£βπ πΏ π€ (π ,π£) be the maximum distance from π . The SSSP from π can be solved in π(π+π·) time. Use a bucket-based priority queue to implement Dijkstra. The priority queue used by Dijkstra is monotone. (Keys of extracted items cannot decrease.) β¦ 1 π· β₯π·
18
Complexity of Goldbergβs algorithm
Number of recursive calls is log π . After each recursive call, we adjust the potential, or find a negative cycle, by the process above. If there are π negative vertices, we get rid of π of them, without creating new ones, in π(π) time. How many such iterations, πβπβ π , do we need? If πβ€ 1 4 π΄ 2 , then πβ π < π΄β (πβ π is increasing.) As π 0 β€πβ€ π 2 , number of iterations is at most 4π . Thus, the total runtime is π(π π log π ).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.