Algorithms and Data Structures Lecture 10 Graph Algorithms III: - - PowerPoint PPT Presentation

β–Ά
algorithms and data structures
SMART_READER_LITE
LIVE PREVIEW

Algorithms and Data Structures Lecture 10 Graph Algorithms III: - - PowerPoint PPT Presentation

Algorithms and Data Structures Lecture 10 Graph Algorithms III: Shortest Paths Fabian Kuhn Algorithms and Complexity Fabian Kuhn Algorithms and Complexity Shortest Paths Single Sourse Shortest Paths Problem Given: weighted graph =


slide-1
SLIDE 1

Algorithms and Complexity Fabian Kuhn

Lecture 10 Graph Algorithms III: Shortest Paths

Algorithms and Data Structures

Fabian Kuhn Algorithms and Complexity

slide-2
SLIDE 2

Algorithms and Complexity Fabian Kuhn

Single Sourse Shortest Paths Problem

  • Given: weighted graph 𝐻 = π‘Š, 𝐹, π‘₯ , start node 𝑑 ∈ π‘Š

– We denote the weight of an edge 𝑣, 𝑀 by π‘₯ 𝑣, 𝑀 – Assumption for now: βˆ€π‘“ ∈ 𝐹: π‘₯ 𝑓 β‰₯ 0

  • Goal: Find shortest paths / distances from 𝑑 to all nodes

– Distance from 𝑑 to 𝑀: 𝑒𝐻 𝑑, 𝑀 (length of a shortest path)

2

Shortest Paths

1 2 3 4 5 6 8 7 1 3 6 8 9 1 15 2 9 7 6 3 1

Distance from node 1 to node 7 : 10

𝒕

slide-3
SLIDE 3

Algorithms and Complexity Fabian Kuhn

Lemma: If 𝑀0, 𝑀1, … , 𝑀𝑙 is a shortest path from 𝑀0 to 𝑀𝑙, then it holds for all 0 ≀ 𝑗 ≀ π‘˜ ≀ 𝑙 that the subpath 𝑀𝑗, 𝑀𝑗+1, … , π‘€π‘˜ is also a shortest path from 𝑀𝑗 to π‘€π‘˜. Shortest path from π’˜πŸ to π’˜π’:

  • Subpath from 𝑀𝑗 to π‘€π‘˜ is also a shortest path.

– Otherwise, one could replace the path from 𝑀𝑗 to π‘€π‘˜ by the shortest path from 𝑀𝑗 to π‘€π‘˜. – If by doing this, nodes are visited multiple time, one can cut out cycles and

  • btains an even shorter path.
  • Lemma also holds for negative edge weights,

– as long as the graph does not contain negative cycles.

3

Optimality of Subpaths

π’˜πŸ

π’˜πŸ

π’˜π’‹ π’˜π’Œ π’˜π’

slide-4
SLIDE 4

Algorithms and Complexity Fabian Kuhn

  • Spanning tree that is rooted at node 𝑑 and that contains

shortest paths from 𝑑 to all other nodes.

– Such a tree always exists (follows from the optimality of subpaths)

  • For unweighted graphs: BFS spanning tree
  • Goal: Find a shortest path tree

4

Shortest-Path Tree

1 2 3 4 5 6 8 7 1 3 6 8 9 1 15 2 9 7 6 3

𝒕

slide-5
SLIDE 5

Algorithms and Complexity Fabian Kuhn

  • Algorithm by Edsger W. Dijkstra (published in 1959)

Idea:

  • We start at 𝑑 and build the spanning tree in a step-by-step manner.
  • Goal: In each step of the algorithm, add one node

– Initially: subtree only consists of 𝑑 (trivially satisfies invariant...) – 1st step: Because of the optimality of subpaths, there must be a shortest path consisting of a single edge... – Always add the remaining node at the smallest distance from 𝑑.

5

Dijkstra’s Algorithm: Idea

Invariant: Algorithm always has a tree rooted at 𝑑, which is a subtree

  • f a shortest path tree.
slide-6
SLIDE 6

Algorithms and Complexity Fabian Kuhn

Given: A tree π‘ˆ that is rooted in 𝑑, such that π‘ˆ is a subtree of a shortest paths tree for node 𝑑 in 𝐻. (nodes of π‘ˆ : 𝑇) How can we extend π‘ˆ by a single node?

6

Dijkstra’s Algorithm : One Step

𝒕 1 4 3 2 2 5 𝟏 𝟐 πŸ” πŸ‘ πŸ“ πŸ• πŸ—

𝑻 𝑢(𝑻)

10 5 3 1 7 3 2 5 6 𝟐𝟏 𝟐𝟐 πŸ— πŸ— 𝟐𝟏

𝑇 : nodes in the tree π‘ˆ 𝑂 𝑇 : nodes that can be added to the tree directly. To add 𝑀 ∈ 𝑂 𝑇 it most hold that 𝑒𝐻 𝑑, 𝑀 = min

π‘£βˆˆπ‘‡ 𝑒𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀

We will see that this always holds for 𝑀 ∈ 𝑂 𝑇 with minimum distance 𝑒𝐻 𝑑, 𝑀 from 𝑑.

slide-7
SLIDE 7

Algorithms and Complexity Fabian Kuhn

Given: π‘ˆ is subtree of a shortest path tree for 𝑑 in 𝐻. Lemma: For a node 𝑀 ∈ 𝑂 𝑇 and an edge 𝑣, 𝑀 with 𝑣 ∈ 𝑇 such that 𝑒𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 is minimized, it holds that 𝒆𝑯 𝒕, π’˜ = 𝒆𝑯 𝒕, 𝒗 + 𝒙 𝒗, π’˜ Consider the 𝑑-𝑀 path that we obtain in this way: Assume that there is a shorter path:

– Because there are no negative edge weights, we therefore have 𝑒𝐻 𝑑, 𝑦 + π‘₯ 𝑦, 𝑧 ≀ 𝑒𝐻(𝑑, 𝑀) < 𝑒𝐻 𝑑, 𝑣 + π‘₯(𝑣, 𝑀)

7

Dijkstra’s Algorithm : One Step

𝒕 π’˜ 𝒗

𝑻 𝑢(𝑻)

𝒕 π’š 𝒛

𝑻 𝑢(𝑻)

π’˜

slide-8
SLIDE 8

Algorithms and Complexity Fabian Kuhn

  • At the beginning, we have π‘ˆ =

𝑑 , βˆ…

  • For each node 𝑀 βˆ‰ 𝑇, one at all times computes

πœ€ 𝑑, 𝑀 ≔ min

π‘£βˆˆπ‘‡βˆ©π‘‚in 𝑀 𝑒𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀

– as well as the incoming neighbor 𝑣 =: 𝛽 𝑀 that minimized the expression...

  • πœ€ 𝑑, 𝑀 corresponds to an 𝑑-𝑀 path ⟹ πœ€ 𝑑, 𝑀 β‰₯ 𝑒𝐻 𝑑, 𝑀
  • Lemma on last slide:

For minimum 𝜺 𝒕, π’˜ , we have: 𝜺 𝒕, π’˜ = 𝒆𝑯 𝒕, π’˜

8

Dijkstra’s Algorithm

Invariant: Algorithm always has a tree π‘ˆ = (𝑇, 𝐡) rooted at 𝑑, which is a subtree of a shortest path tree of 𝐻.

slide-9
SLIDE 9

Algorithms and Complexity Fabian Kuhn

Initialization 𝑼 = βˆ…, βˆ…

  • πœ€ 𝑑, 𝑑 = 0, and πœ€ 𝑑, 𝑀 = ∞ for all 𝑀 β‰  𝑑
  • 𝛽 𝑀 = NULL for all 𝑀 ∈ π‘Š

Iteration Step

  • Choose a node 𝑀 with smallest

πœ€ 𝑑, 𝑀 ≔ min

π‘£βˆˆπ‘‡βˆ©π‘‚in 𝑀 𝑒𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀

  • Go through all out-neighbors 𝑦 ∈ π‘Š βˆ– 𝑇 and set

πœ€ 𝑑, 𝑦 ≔ min πœ€ 𝑑, 𝑦 , πœ€ 𝑑, 𝑀 + π‘₯ 𝑀, 𝑦

– If πœ€ 𝑑, 𝑦 is decreased, set 𝛽 𝑦 = 𝑀

  • Add node 𝑀 and edge 𝛽 𝑀 , 𝑀 to the tree π‘ˆ.

9

Dijkstra’s Algorithm

𝒕 π’˜ π’š

update πœ€ 𝑑, 𝑦

slide-10
SLIDE 10

Algorithms and Complexity Fabian Kuhn 10

Dijkstra’s Algorithm: Example

∞ 𝟏 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-11
SLIDE 11

Algorithms and Complexity Fabian Kuhn 11

Dijkstra’s Algorithm: Example

𝟐 𝟏 ∞ ∞ ∞ 𝟐𝟘 ∞ πŸπŸ– πŸ‘πŸ ∞ πŸπŸ— 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-12
SLIDE 12

Algorithms and Complexity Fabian Kuhn 12

Dijkstra’s Algorithm: Example

𝟐 𝟏 πŸπŸ“ ∞ πŸ“ 𝟐𝟘 ∞ πŸ– πŸ‘πŸ ∞ πŸπŸ— 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-13
SLIDE 13

Algorithms and Complexity Fabian Kuhn 13

Dijkstra’s Algorithm: Example

𝟐 𝟏 πŸπŸ’ ∞ πŸ“ 𝟐𝟘 πŸ” πŸ– πŸ‘πŸ ∞ πŸπŸ— 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-14
SLIDE 14

Algorithms and Complexity Fabian Kuhn 14

Dijkstra’s Algorithm: Example

𝟐 𝟏 πŸπŸ’ ∞ πŸ“ 𝟐𝟘 πŸ” πŸ– πŸ‘πŸ πŸ’πŸ– πŸπŸ— 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-15
SLIDE 15

Algorithms and Complexity Fabian Kuhn 15

Dijkstra’s Algorithm: Example

𝟐 𝟏 𝟘 ∞ πŸ“ 𝟐𝟘 πŸ” πŸ– πŸ‘πŸ πŸ’πŸ– πŸπŸ” 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-16
SLIDE 16

Algorithms and Complexity Fabian Kuhn 16

Dijkstra’s Algorithm: Example

𝟐 𝟏 𝟘 πŸπŸ‘ πŸ“ 𝟐𝟐 πŸ” πŸ– πŸ‘πŸ 𝟐𝟘 πŸπŸ” 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-17
SLIDE 17

Algorithms and Complexity Fabian Kuhn 17

Dijkstra’s Algorithm: Example

𝟐 𝟏 𝟘 πŸπŸ‘ πŸ“ 𝟐𝟐 πŸ” πŸ– πŸπŸ’ 𝟐𝟘 πŸπŸ‘ 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-18
SLIDE 18

Algorithms and Complexity Fabian Kuhn

Initialization 𝑼 = βˆ…, βˆ…

  • πœ€ 𝑑, 𝑑 = 0, and πœ€ 𝑑, 𝑀 = ∞ for all 𝑀 β‰  𝑑
  • 𝛽 𝑀 = NULL for all 𝑀 ∈ π‘Š

Iteration Step

  • Choose a node 𝑀 with smallest

πœ€ 𝑑, 𝑀 ≔ min

π‘£βˆˆπ‘‡βˆ©π‘‚in 𝑀 𝑒𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀

  • Go through all out-neighbors 𝑦 ∈ π‘Š βˆ– 𝑇 and set

πœ€ 𝑑, 𝑦 ≔ min πœ€ 𝑑, 𝑦 , πœ€ 𝑑, 𝑀 + π‘₯ 𝑀, 𝑦

– If πœ€ 𝑑, 𝑦 is decreased, set 𝛽 𝑦 = 𝑀

  • Add node 𝑀 and edge 𝛽 𝑀 , 𝑀 to the tree π‘ˆ.

18

Dijkstra’s Algorithm

𝒕 π’˜ π’š

update πœ€ 𝑑, 𝑦

Similar to the MST algorithm

  • f Prim!
slide-19
SLIDE 19

Algorithms and Complexity Fabian Kuhn

𝐼 = new priority queue; 𝐡 = βˆ… for all 𝑣 ∈ π‘Š βˆ– {𝑑} do 𝐼.insert(𝑣, ∞); 𝛽(𝑣) = NULL 𝐼.insert(𝑑, 0) while 𝐼 is not empty do 𝑣 = H.deleteMin() for all unmarked neighbors 𝑀 of 𝑣 do if π‘₯ 𝑣, 𝑀 < 𝑒(𝑀) then 𝐼.decreaseKey(𝑀, π‘₯ 𝑣, 𝑀 ) 𝛽 𝑀 = 𝑣 𝑣.marked = true if 𝑣 β‰  𝑑 then 𝐡 = 𝐡 βˆͺ 𝑣, 𝛽 𝑣

19

Reminder : Prim’s MST Algorithm

slide-20
SLIDE 20

Algorithms and Complexity Fabian Kuhn

𝐼 = new priority queue; 𝐡 = βˆ… for all 𝑣 ∈ π‘Š βˆ– {𝑑} do 𝐼.insert(𝑣, ∞); πœ€ 𝑑, 𝑣 = ∞; 𝛽(𝑣) = NULL 𝐼.insert(𝑑, 0) while 𝐼 is not empty do 𝑣 = H.deleteMin() for all unmarked out-neighbors 𝑀 of 𝑣 do if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 then πœ€(𝑑, 𝑀) = πœ€ 𝑑, 𝑣 + π‘₯(𝑣, 𝑀) 𝐼.decreaseKey(𝑀, πœ€ 𝑑, 𝑀 ) 𝛽 𝑀 = 𝑣 𝑣.marked = true if 𝑣 β‰  𝑑 then 𝐡 = 𝐡 βˆͺ 𝛽 𝑣 , 𝑣

20

Dijkstra’s Algorithm : Implementation

slide-21
SLIDE 21

Algorithms and Complexity Fabian Kuhn

  • Algorithm implementation is almost identical to the

implementation of Prim’s MST algorithm.

  • Number of heap operations:

create: 1, insert: π‘œ, deleteMin: π‘œ, decreaseKey: ≀ 𝑛

– Or alternatively without decrease-key: 𝑃 𝑛 insert and deleteMin Op.

  • Running time with binary heap:

𝑷 𝒏 𝐦𝐩𝐑 𝒐

  • Running time with Fibonacci heap:

𝑷 𝒏 + 𝒐 𝐦𝐩𝐑 𝒐

21

Dijkstra’s Algorithm: Running Time

slide-22
SLIDE 22

Algorithms and Complexity Fabian Kuhn

  • Shortest paths can also be defined for graphs with

negative edge weights.

– Shortest path is defined if there no shorter way, even if nodes can be visited multiple times.

Example

22

Negative Edge Weights

𝑑

3 5 2

βˆ’4 βˆ’3 βˆ’6 3 6 4 8 7

βˆ’

slide-23
SLIDE 23

Algorithms and Complexity Fabian Kuhn

Lemma: In a directed, weighted graph 𝐻, there is a shortest path from 𝑑 to 𝑀 if and only if there is no there is no negative cycle that is reachable from 𝑑 and from which one can reach 𝑀.

  • Also holds for undirected graphs if edges 𝑣, 𝑀 are considered as 2

directed edges 𝑣, 𝑀 and 𝑀, 𝑣 .

Negative Edge Weights

𝑑 𝑀

βˆ’

no shortest path from 𝑣 to 𝑀 no reachable negative cycle We can restrict our attention to simple path. There are only finitely many such path.

Nodes are not visited multiple times.

slide-24
SLIDE 24

Algorithms and Complexity Fabian Kuhn

Does Dijkstra’s algorithm work with negative edge weights?

  • Answer: no

𝑑

2 1

2 βˆ’2 1

1

𝑀

24

Dijkstra’s Algorithm and Negative Weights

Shortest path has length 2. Dijkstra path has length 3.

slide-25
SLIDE 25

Algorithms and Complexity Fabian Kuhn

  • To simplify, we only compute the distances 𝑒𝐻 𝑑, 𝑀

Assumption:

  • For all nodes 𝑀: algorithm has dist. estimate 𝜺 𝒕, π’˜ β‰₯ 𝒆𝑯 𝒕, π’˜
  • Initialization: πœ€ 𝑑, 𝑑 = 0, πœ€ 𝑑, 𝑀 = ∞ for 𝑀 β‰  𝑑

Observation:

  • If 𝑣, 𝑀 ∈ 𝐹 such that πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€(𝑑, 𝑀), then we can

decrease (and thus improve) πœ€ 𝑑, 𝑀 because 𝒆𝑯 𝒕, π’˜ ≀ 𝑒𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 ≀ 𝜺 𝒕, 𝒗 + 𝒙 𝒗, π’˜

25

Bellman-Ford Algorithm

slide-26
SLIDE 26

Algorithms and Complexity Fabian Kuhn

  • Consider all edges 𝑣, 𝑀 and try to improve πœ€ 𝑑, 𝑀 ,

– until all distances are correct (βˆ€π‘€ ∈ π‘Š: πœ€ 𝑑, 𝑀 = 𝑒𝐻 𝑑, 𝑀 )

πœ€ 𝑑, 𝑑 ≔ 0; βˆ€π‘€ ∈ π‘Š βˆ– 𝑑 ∢ πœ€ 𝑑, 𝑀 ≔ ∞ repeat for all 𝑣, 𝑀 ∈ 𝐹 do if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 then πœ€ 𝑑, 𝑀 ≔ πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 until βˆ€π‘€ ∈ π‘Š: πœ€ 𝑑, 𝑀 = 𝑒𝐻 𝑑, 𝑀

  • How many repetitions are necessary?

– Shortest paths consisting of one edge ⟹ 1 repetitions – Shortest paths consisting of two edges ⟹ 2 repetitions – … – Shortest paths consisting of 𝑙 edges ⟹ 𝑙 repetitions

26

Bellman-Ford Algorithm

slide-27
SLIDE 27

Algorithms and Complexity Fabian Kuhn

πœ€ 𝑑, 𝑑 ≔ 0; βˆ€π‘€ ∈ π‘Š βˆ– 𝑑 ∢ πœ€ 𝑑, 𝑀 ≔ ∞ for i := 1 to n-1 do for all 𝑣, 𝑀 ∈ 𝐹 do if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 then πœ€ 𝑑, 𝑀 ≔ πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 After 𝑗 repetitions, we have 𝜺 𝒕, π’˜ ≀ 𝒆𝑯

𝒋 𝒕, π’˜ , where 𝑒𝐻 𝑗 𝑑, 𝑀 is

the length of a shortest path consisting of at most 𝑗 edges.

  • Follows by induction on 𝒋:

– 𝑗 = 0: πœ€ 𝑑, 𝑑 = 𝑒𝐻

0 𝑑, 𝑑 = 0, 𝑀 β‰  𝑑 ⟹ πœ€ 𝑑, 𝑀 = 𝑒𝐻 0 𝑑, 𝑀 = ∞

– 𝑗 > 0: 𝑒𝐻

𝑗 𝑑, 𝑀 = min 𝑒𝐻 π‘—βˆ’1 𝑑, 𝑀 ,

min

π‘£βˆˆπ‘‚π‘—π‘œ 𝑀 𝑒𝐻 π‘—βˆ’1 𝑑, 𝑣 + π‘₯(𝑣, 𝑀)

(shortest path consists of ≀ 𝑗 βˆ’ 1 edges or of exactly 𝑗 edges)

27

Bellman-Ford Algorithm

slide-28
SLIDE 28

Algorithms and Complexity Fabian Kuhn

πœ€ 𝑑, 𝑑 ≔ 0; βˆ€π‘€ ∈ π‘Š βˆ– 𝑑 ∢ πœ€ 𝑑, 𝑀 ≔ ∞ for i := 1 to n-1 do for all 𝑣, 𝑀 ∈ 𝐹 do if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 then πœ€ 𝑑, 𝑀 ≔ πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 Theorem: If the graph has no negative cycles that are reachable from 𝑑, at the end all distances are computed correctly.

  • At the end, we have for all 𝑀 ∈ π‘Š:

πœ€ 𝑑, 𝑀 ≀ 𝑒𝐻

π‘œβˆ’1 (𝑑, 𝑀)

  • Because every path consists of ≀ π‘œ βˆ’ 1 edges, we also have

𝑒𝐻

π‘œβˆ’1 𝑑, 𝑀 = 𝑒𝐻(𝑑, 𝑀)

28

Bellman-Ford Algorithm

slide-29
SLIDE 29

Algorithms and Complexity Fabian Kuhn

  • We will see: If there is a (from 𝑑 reachable) negative cycle, then

there is an improvement for some edge: βˆƒ 𝑣, 𝑀 ∈ 𝐹 ∢ πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 Bellman-Ford Algorithm

for i := 1 to n-1 do for all 𝑣, 𝑀 ∈ 𝐹 do if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 then πœ€ 𝑑, 𝑀 ≔ πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 for all 𝑣, 𝑀 ∈ 𝐹 do if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€(𝑑, 𝑀) then return false return true

29

Detecting Negative Cycles

slide-30
SLIDE 30

Algorithms and Complexity Fabian Kuhn 30

Detecting Negative Cycles

βˆ’

𝑑 𝑀1 𝑀2 𝑀3 𝑀0 = 𝑀𝑙

  • neg. cycle ⟹ ෍

𝑗=1 𝑙

π‘₯ π‘€π‘—βˆ’1, 𝑀𝑗 < 0 !

= < 0

Lemma: If 𝐻 contains a negative cycles that is reachable from 𝑑, then the Bellman-Ford algorithm returns false. Proof by contradiction:

  • Assumption : βˆ€π‘— ∈ 1, … , 𝑙 ∢ πœ€ 𝑑, π‘€π‘—βˆ’1 + π‘₯ π‘€π‘—βˆ’1, 𝑀𝑗 β‰₯ πœ€ 𝑑, 𝑀𝑗

෍

𝑗=1 𝑙

πœ€ 𝑑, 𝑀𝑗 ≀ ෍

𝑗=1 𝑙

πœ€ 𝑑, π‘€π‘—βˆ’1 + π‘₯ π‘€π‘—βˆ’1, 𝑀𝑗 = ෍

𝑗=1 𝑙

πœ€ 𝑑, π‘€π‘—βˆ’1 + ෍

𝑗=1 𝑙

π‘₯ π‘€π‘—βˆ’1, 𝑀𝑗

≀

reachable from 𝑑 ⟹ πœ€ 𝑑, 𝑀𝑗 β‰  ∞

slide-31
SLIDE 31

Algorithms and Complexity Fabian Kuhn

A shortest path tree can be computed in the usual way. Initialization:

  • πœ€ 𝑑, 𝑑 = 0, fΓΌr 𝑀 β‰  𝑑 ∢ πœ€ 𝑑, 𝑀 = NULL
  • 𝛽 𝑑 = NULL, for 𝑀 β‰  𝑑 ∢ 𝛽 𝑀 = NULL

In every loop iteration: ... if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 then πœ€ 𝑑, 𝑀 ≔ πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 𝛽 𝑀 ≔ 𝑣

  • At the end, 𝛽 𝑀 points to a parent in the shortest path tree

– if there are no negative cycles...

31

Bellman-Ford Algorithm : Shortest Paths

slide-32
SLIDE 32

Algorithms and Complexity Fabian Kuhn

Theorem: If there is a negative cycle that is reachable from 𝑑, the Bellman-Ford algorithm detects this. If no such cycle exists, the Bellman-Ford algorithm computes a shortest path tree in time 𝑃 π‘Š β‹… 𝐹 .

  • Correctness: already proven
  • Running time:

– π‘œ βˆ’ 1 + 1 loop iterations – In every loop iteration, we go once through all the edges.

  • Remark: One can adapt the algorithm such that it computes a

shortest path for all 𝑀, for shich such a path from 𝑑 existsts (and it detects if no shortest path exists).

– in the same asymptotic running time

32

Bellman-Ford Algorithm : Summary

slide-33
SLIDE 33

Algorithms and Complexity Fabian Kuhn

Goal: Optimal routing paths for some destination 𝑒

  • For every node, we want to know to which neighbor one has to

send a message destined at node 𝑒.

  • This corresponds to computing a shortest path tree if all edges are

reversed (transpose graph) Algorithm:

  • Nodes remember tha current distance πœ€ 𝑣, 𝑒 and the currently

best neighbor.

  • All nodes in parallel check if there is an improvement for some

neighbor: βˆƒ 𝑣, 𝑀 ∈ 𝐹 ∢ π‘₯ 𝑣, 𝑀 + πœ€ 𝑀, 𝑒 < πœ€ 𝑣, 𝑒

  • Corresponds to a parallel variant of the Bellman-Ford algorithm

33

Routing Paths in Networks

slide-34
SLIDE 34

Algorithms and Complexity Fabian Kuhn

  • all pairs shortest paths problem

Compute single-source shortest paths for all nodes

  • Dijkstra algorithm with all nodes:

Running time: π‘œ β‹… 𝑃 Running time Dijkstra ∈ 𝑃 π‘›π‘œ + π‘œ2 log π‘œ

– Problem: only works for non-negative edge weights

  • Bellman-Ford algorithm with all nodes:

Running time: π‘œ β‹… 𝑃 Running time BF ∈ 𝑃 π‘›π‘œ2 ∈ 𝑃 π‘œ4

– Problem: slow... – If the Bellman-Ford algorithm is carried out for all nodes, the running time can be improved to 𝑃 π‘œ3 β‹… log π‘œ . – If all 𝑒𝐻

𝑗 𝑣, 𝑀 -distances are known, one can directly compute the

𝑒𝐻

2𝑗 𝑣, 𝑀 -distances in one iteration.

  • Further details and discussion of other algorithms in various text books.

34

Shortest Paths Between All Node Pairs