Presentation is loading. Please wait.

Presentation is loading. Please wait.

Honors Track: Competitive Programming & Problem Solving Avoiding negative edges Steven Ge.

Similar presentations


Presentation on theme: "Honors Track: Competitive Programming & Problem Solving Avoiding negative edges Steven Ge."— Presentation transcript:

1 Honors Track: Competitive Programming & Problem Solving Avoiding negative edges
Steven Ge

2 Content Negative Cycles Dijkstra’s Algorithm with negative edges
Adding constant Bellman-Ford Algorithm Single source shortest path Johnson’s Algorithm All pair shortest path Suurballe’s Aglorithm Cheapest multiple, non-overlapping paths

3 Negative Cycles Graph 1 1 2 S D -10 Shortest Path?

4 Negative Cycles Graph 1 1 2 S D -10 Shortest Path? No Shortest Path!

5 Dijkstra’s with negative edges
Graph A 10 -100 1 S B -2 D

6 Dijkstra’s with negative edges
Shortest Path A 10 -100 1 S B 1 D Total length: -89

7 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: D B A A 10 -100 1 S B 1 D

8 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: B A A 10 -100 1 S B 1 D

9 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: B A A 10 -100 1 S B 1 D

10 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: A A 10 -100 1 S B 1 D

11 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: A A 10 -100 1 1 S B 1 D

12 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: D A A 10 -100 1 1 S B 1 D

13 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: A A 10 -100 1 1 S B 1 D

14 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: A A 10 -100 1 1 S B 1 D No expansion on D

15 Dijkstra’s with negative edges
Dijkstra’s with closed set PQ: A 10 -100 1 1 S B 1 D

16 Dijkstra’s with negative edges
Dijkstra’s with closed set 10 PQ: A 10 -100 1 1 S B 1 D

17 Dijkstra’s with negative edges
Dijkstra’s with closed set 10 PQ: B A 10 -100 1 1 S B 1 D

18 Dijkstra’s with negative edges
Dijkstra’s with closed set 10 PQ: A 10 -100 1 1 S B 1 D

19 Dijkstra’s with negative edges
Dijkstra’s with closed set 10 PQ: A 10 -100 1 1 S B 1 D B is in closed set, so no more expansions 0 > -89 Incorrect output, so don’t bother

20 Dijkstra’s with negative edges
Dijkstra’s without closed set A 10 -100 1 S B 1 Gigantic graph with Node D T

21 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: T B A A 10 -100 1 S B 1 Gigantic graph with Node D T

22 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: B A A 10 -100 1 S B 1 Gigantic graph with Node D T

23 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: GgwND B A A 10 -100 1 S B 1 Gigantic graph with Node D T

24 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: B A A 10 -100 1 S B 1 Gigantic graph with Node D T

25 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: B A A 10 -100 1 S B 1 Gigantic graph with Node D T

26 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: A A 10 -100 1 S B 1 Gigantic graph with Node D T

27 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: A A 10 -100 1 1 S B 1 Gigantic graph with Node D T

28 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: T A A 10 -100 1 1 S B 1 Gigantic graph with Node D T

29 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: A A 10 -100 1 1 S B 1 Gigantic graph with Node D T

30 Dijkstra’s with negative edges
Dijkstra’s without closed set PQ: A 10 -100 1 1 S B 1 Gigantic graph with Node D T

31 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: A 10 -100 1 1 S B 1 Gigantic graph with Node D T

32 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: B A 10 -100 1 1 S B 1 Gigantic graph with Node D T

33 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: A 10 -100 1 1 S B 1 Gigantic graph with Node D T

34 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T

35 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: T A 10 -100 1 -90 S B 1 Gigantic graph with Node D T

36 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T

37 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T -89

38 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: GgwND A 10 -100 1 -90 S B 1 Gigantic graph with Node D T -89

39 Dijkstra’s with negative edges
Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T -89 Correct output Takes very long

40 Dijkstra’s with negative cycles
Graph PQ: A 1 1 2 S A B D -10

41 Dijkstra’s with negative cycles
Graph PQ: 1 1 2 S A B D -10

42 Dijkstra’s with negative cycles
Graph PQ: 1 1 2 S A B D 1 -10

43 Dijkstra’s with negative cycles
Graph PQ: B 1 1 2 S A B D 1 -10

44 Dijkstra’s with negative cycles
Graph PQ: 1 1 2 S A B D 1 -10

45 Dijkstra’s with negative cycles
Graph PQ: 1 1 2 S A B D 1 2 -10

46 Dijkstra’s with negative cycles
Graph PQ: A D 1 1 2 S A B D 1 2 -10

47 Dijkstra’s with negative cycles
Graph PQ: D 1 1 2 S A B D 1 2 -10

48 Dijkstra’s with negative cycles
Graph PQ: D 1 1 2 S A B D -8 2 -10

49 Dijkstra’s with negative cycles
Graph PQ: B D 1 1 2 S A B D -8 2 -10

50 Dijkstra’s with negative cycles
Graph PQ: D 1 1 2 S A B D -8 2 -10

51 Dijkstra’s with negative cycles
Graph PQ: D 1 1 2 S A B D -8 -7 -10

52 Dijkstra’s with negative cycles
Graph PQ: A D 1 1 2 S A B D -8 -7 -10 Etc.

53 Adding Constant Graph 1 1 1 -100 10 10

54 Adding Constant Shortest Path Add constant 100 and run Dijkstra’s? 1 1
-100 10 10 Add constant 100 and run Dijkstra’s?

55 Adding Constant Shortest Path Add constant 100 and run Dijkstra’s?
101 101 101 110 110 Add constant 100 and run Dijkstra’s? Difference in amount of edges

56 Bellman-Ford Similar to Dijkstra’s No priority queue
Slower than Dijkstra’s O(V*E) More versatile than Dijkstra’s (can detect negative cycles) Used in Johnson’s Algorithm

57 Bellman-Ford Steps: 1) Initialize graph
2) Relax all edges v-1 times (v = amount vertices) In other words: For each vertex, check if the distance to current vertex + edge distance < distance to child vertex 3) Check for negative cycles For each edge, if the distance to current vertex + edge distance < distance to child vertex, return error

58 Bellman-Ford Graph A 100 50 -100 10 20 S B D 20 1 20 C

59 Bellman-Ford ∞ ∞ ∞ ∞ Step 1 ∞ S A B C D A 100 50 -100 10 20 S B D 20 1
S A B C D A 100 50 -100 10 20 S B D 20 1 20 C

60 Bellman-Ford ∞ ∞ ∞ ∞ Step 2 ∞ S A B C D A 100 50 -100 10 20 S B D 20 1
S A B C D A 100 50 -100 10 20 S B D 20 1 20 C

61 Bellman-Ford ∞ ∞ ∞ Step 2 100 ∞ S A B C D 100 A 100 50 -100 10 20 S B
100 S A B C D 100 A 100 50 -100 10 20 S B D 20 1 20 C

62 Bellman-Ford ∞ ∞ ∞ Step 2 100 ∞ S A B C D 100 A 100 50 -100 10 20 S B
100 S A B C D 100 A 100 50 -100 10 20 S B D 20 1 20 C

63 Bellman-Ford ∞ ∞ Step 2 100 10 ∞ S A B C D 100 A 100 50 -100 10 10 20
100 10 S A B C D 100 A 100 50 -100 10 10 20 S B D 20 1 20 C

64 Bellman-Ford ∞ ∞ Step 2 100 10 ∞ S A B C D 100 A 100 50 -100 10 10 20
100 10 S A B C D 100 A 100 50 -100 10 10 20 S B D 20 1 20 C

65 Bellman-Ford ∞ Step 2 100 10 1 ∞ S A B C D 100 A 100 50 -100 10 10 20
100 10 1 S A B C D 100 A 100 50 -100 10 10 20 S B D 20 1 20 C 1

66 Bellman-Ford ∞ Step 2 100 10 1 ∞ S A B C D 100 A 100 50 -100 10 10 20
100 10 1 S A B C D 100 A 100 50 -100 10 10 20 S B D 20 1 20 C 1

67 Bellman-Ford Step 2 100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20
100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20 S B D 150 20 1 20 C 1

68 Bellman-Ford Step 2 100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20
100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20 S B D 150 20 1 20 C 1

69 Bellman-Ford Step 2 100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D
100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D 150 20 1 20 C 1

70 Bellman-Ford Step 2 100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D
100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D 150 20 1 20 C 1

71 Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D
100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1

72 Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D
100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1

73 Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D
100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1

74 Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D
100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1

75 Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1 After the second iteration, the distances don’t change This also means no negative cycles

76 Bellman-Ford Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2

77 Bellman-Ford ∞ ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D

78 Bellman-Ford ∞ ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D

79 Bellman-Ford ∞ ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D

80 Bellman-Ford ∞ ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D

81 Bellman-Ford ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D 1

82 Bellman-Ford ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D 1

83 Bellman-Ford ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D 1

84 Bellman-Ford ∞ ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D 1

85 Bellman-Ford ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D 1 2

86 Bellman-Ford ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D 1 2

87 Bellman-Ford ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2

88 Bellman-Ford ∞ Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2

89 Bellman-Ford Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3

90 Bellman-Ford Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3

91 Bellman-Ford Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3

92 Bellman-Ford Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3

93 Bellman-Ford Justification:
Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3 Contains cycle, because optimal path contains at most all the nodes All pairs, O(V2 * E)

94 Johnson’s Algorithm Reweight all the edges effectively
Makes use of both Dijkstra’s and Bellman-Ford

95 Johnson’s Algorithm Steps: 1) Add new node q to graph
2) Connect node q to all other nodes in graph (directed towards other nodes) 3) Initialize graph with q as source 4) Run Bellman-Ford 5) Remove q and reweight the edges from u to v with the following formula Wnew(u, v) = Wold(u, v) + h(u) – h(v) 6) Run Dijkstra’s 7) Use the formula to get the original weight Wold(u, v) = Wnew(u, v) - h(u) + h(v)

96 Johnson’s Algorithm Graph A 100 50 -100 10 20 S B D 20 1 20 C

97 Johnson’s Algorithm Step 1 A 100 50 -100 10 20 S B D 20 1 20 C q

98 Johnson’s Algorithm Step 2 A 100 50 -100 10 20 S B D 20 1 20 C q

99 Johnson’s Algorithm Step 3 + 4 A 100 50 -100 10 -100 20 -80 S B D 20 1
A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C q

100 Johnson’s Algorithm Step 3 + 4 A 100 50 -100 10 -100 20 -80 S B D 20 1
A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C q

101 Johnson’s Algorithm Step 5 A 100 50 -100 10 -100 20 -80 S B D 20 1 20
A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C q

102 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100
A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v)

103 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100
A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v)

104 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – 0 = 100

105 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -100 = 110

106 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 -100 110 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -100 = 110

107 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 -100 110 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 1 + 0 – -80 = 81

108 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 -100 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 1 + 0 – -80 = 81

109 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 -100 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -100 = 0

110 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -100 = 0

111 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -80 = 0

112 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -80 = 0

113 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 50 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -80 = 130

114 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 130 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -80 = 130

115 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 130 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -80 = 0

116 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 130 110 -100 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -80 = 0

117 Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v)
A 100 130 110 -100 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) – -80 = 20

118 Johnson’s Algorithm Step 6 A 100 130 110 -100 -80 S B D 20 81 -80 C

119 Johnson’s Algorithm Step 6, shortest path to A
A 100 130 110 -100 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 100 – = 100

120 Johnson’s Algorithm Step 6, shortest path to A
100 A 100 130 110 -100 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 100 – = 100

121 Johnson’s Algorithm Step 6, shortest path to B
100 A 100 130 110 -100 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) ( ) – = 0

122 Johnson’s Algorithm Step 6, shortest path to B
100 A 100 130 110 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) ( ) – = 0

123 Johnson’s Algorithm Step 6, shortest path to C
100 A 100 130 110 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 81 – = 1

124 Johnson’s Algorithm Step 6, shortest path to C
100 A 100 130 110 -80 S B D 20 81 1 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 81 – = 1

125 Johnson’s Algorithm Step 6, shortest path to D
100 A 100 130 110 -80 S B D 20 81 1 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) ( ) – = 20

126 Johnson’s Algorithm Step 6, shortest path to D
100 A 100 130 110 20 S B D 20 81 1 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) ( ) – = 20

127 Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D
100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1

128 Johnson’s Algorithm Using node q guaranties that all edges and nodes are checked Node q does not affect any properties of the graph Single source: Dijkstra: O((V + E) * log V) Bellman-Ford: O(V * E) Johnson: O(V * E) All pairs: Dijkstra: O((V2 + E) log V) Bellman-Ford: O(V2 * E) Johnson: O((V2 + E) log V)

129 Johnson’s Algorithm Correctness reweighting path:
Claim: Every path with the same source and destination will be shifted equally Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

130 Johnson’s Algorithm Correctness reweighting:
Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

131 Johnson’s Algorithm Correctness reweighting:
Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

132 Johnson’s Algorithm Correctness reweighting:
Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) + h(Vi) and – h(Vi) cancel each other out. Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

133 Johnson’s Algorithm Correctness reweighting:
Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) + h(Vi) and – h(Vi) cancel each other out. (Wold (S, V1) + Wold (V1, V2) + … + Wold (Vn, D) + h(S) – h(D)) = Wnew(S, D) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

134 Johnson’s Algorithm Correctness reweighting:
Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) + h(Vi) and – h(Vi) cancel each other out. (Wold (S, V1) + Wold (V1, V2) + … + Wold (Vn, D) + h(S) – h(D)) = Wnew(S, D) Each path from S to T gets shifted a fixed amount, unaffected by the amount of edges Each path from S to T gets shifted the same amount. (Only with h(S) – h(D)) So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

135 Johnson’s Algorithm Correctness reweighting edge:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

136 Johnson’s Algorithm Correctness reweighting:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

137 Johnson’s Algorithm Correctness reweighting:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

138 Johnson’s Algorithm Correctness reweighting:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

139 Johnson’s Algorithm Correctness reweighting:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

140 Johnson’s Algorithm Correctness reweighting:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Length of a path from q to u to v is: Wold(u, v) + h(u) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

141 Johnson’s Algorithm Correctness reweighting:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Length of a path from q to u to v is: Wold(u, v) + h(u) Length of a path ≥ length of a shortest path Wold(u, v) + h(u) ≥ h(v) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

142 Johnson’s Algorithm Correctness reweighting:
Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Length of a path from q to u to v is: Wold(u, v) + h(u) Length of a path ≥ length of a shortest path Wold(u, v) + h(u) ≥ h(v) Rewrite Wold(u, v) + h(u) – h(v) ≥ 0 Wnew ≥ 0 Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.

143 Suurballe’s Algorithm
Finding disjoint paths in a network The paths have the shortest length compared to other set of paths with the same amount.

144 Suurballe’s Algorithm
Steps: 1) Find the shortest path with Dijksta’s 2) Create a shortest path tree T from source S with Dijkstra’s 3) Use: Wnew(u, v) = Wold(u, v) + h(u) – h(v) to reweight the edges and reverse the edges of the shortest path 4) Rerun Dijkstra’s to find the second path 5) Remove the overlapping edges from the first and second path 6) connect the ends of the graphs together

145 Suurballe’s Algorithm
Graph: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C

146 Suurballe’s Algorithm
Step 1: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C

147 Suurballe’s Algorithm
Step 2: 23 20 A E 8 23 5 15 S B D 28 12 3 C 12

148 Suurballe’s Algorithm
Step 3: 99 23 20 A E 73 8 17 23 5 33 15 S B D 20 28 12 3 21 C 12

149 Suurballe’s Algorithm
Step 3: 79 23 20 A E 68 25 25 15 S B D 33 28 5 C 12

150 Suurballe’s Algorithm
Step 2 for comparison: Notice that all the edges in this graph becomes 0 Because of the formula 23 20 A E 8 23 5 15 S B D 28 12 3 C 12

151 Suurballe’s Algorithm
Step 3: 79 A E 68 25 25 S B D 33 5 C

152 Suurballe’s Algorithm
Step 4: 79 A E 68 25 25 S B D 33 5 C

153 Suurballe’s Algorithm
Step 5: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C

154 Suurballe’s Algorithm
Step 5: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C

155 Suurballe’s Algorithm
Step 6: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C

156 Suurballe’s Algorithm
Extra constraint: Paths cannot go through the same node

157 Suurballe’s Algorithm
Extra constraint: Paths cannot go through the same node

158 Suurballe’s Algorithm
Extra constraint: Paths cannot go through the same node Split the node in out

159 Suurballe’s Algorithm
Justification: Reweighting technique proven at Johnson’s Reversed edges of the original graph used as backward edges Similar to finding a minimum flow from S to D

160 Conclusion Single source:
Graph without negative edges: Dijkstra’s algorithm Graph with negative edges: Bellman-Ford All pairs: Graph with negative edges: Johnson’s algorithm Finding multiple, non-overlapping shortest paths in a graph Suurballe’s algorithm

161 References Bellman-Ford: https://www.youtube.com/watch?v=W2fKGISUAtM
Johnson’s: Suurballe’s: Incorrect naming, but maybe nice to look at: NWERC 2012 A - Admiral


Download ppt "Honors Track: Competitive Programming & Problem Solving Avoiding negative edges Steven Ge."

Similar presentations


Ads by Google