Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis of Algorithms

Similar presentations


Presentation on theme: "Analysis of Algorithms"β€” Presentation transcript:

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 𝑁 ).


Download ppt "Analysis of Algorithms"

Similar presentations


Ads by Google