Dijkstra's Algorithm for Finding Shortest Paths, Slides of Data Representation and Algorithm Design

Dijkstra's algorithm, a popular algorithm for finding the shortest path between nodes in a graph. The algorithm maintains a set of explored vertices and repeatedly chooses the unexplored vertex with the shortest path from the source node. It then updates the shortest path to neighboring vertices and repeats the process until all vertices have been explored. The document also discusses the importance of the priority queue choice in dijkstra's implementation and its time complexity.

Typology: Slides

2011/2012

Uploaded on 07/15/2012

saandeep
saandeep 🇮🇳

4.5

(6)

99 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Shortest Paths
2
Shortest Path Problem
Shortest path problem. Given a weighted digraph, find the shortest
directed path from s to t.
Versions.
!Point-to-point, single source, all pairs.
!Nonnegative edge weights, arbitrary weights, Euclidean weights.
Path: s!6!3!5!t
Cost: 14 + 18 + 2 + 16 = 50
cost of path = sum of edge costs in path
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
3
Brief History
Shimbel (1955). Information networks.
Ford (1956). RAND, economics of transportation.
Leyzorek, Gray, Johnson, Ladew, Meaker, Petry, Seitz (1957).
Combat Development Dept. of the Army Electronic Proving Ground.
Dantzig (1958). Simplex method for linear programming.
Bellman (1958). Dynamic programming.
Moore (1959). Routing long-distance telephone calls for Bell Labs.
Dijkstra (1959). Simpler and faster version of Ford's algorithm.
4
Reference:
Network Flows: Theory, Algorithms, and Applications
, R. K. Ahuja, T. L. Magnanti, and J. B. Orlin, Prentice Hall, 1993.
Applications
More applications.
!Robot navigation.
!Texture mapping.
!Typesetting in TeX.
!Urban traffic planning.
!Optimal pipelining of VLSI chip.
!Telemarketer operator scheduling.
!Subroutine in higher level algorithms.
!Routing of telecommunications messages.
!Approximating piecewise linear functions.
!Network routing protocols (OSPF, BGP, RIP).
!Exploiting arbitrage opportunities in currency exchange.
!Optimal truck routing through given traffic congestion pattern.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Dijkstra's Algorithm for Finding Shortest Paths and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

Shortest Paths

2

Shortest Path Problem

Shortest path problem. Given a weighted digraph, find the shortest

directed path from s to t.

Versions.

! Point-to-point, single source, all pairs.

! Nonnegative edge weights, arbitrary weights, Euclidean weights.

Path: s! 6! 3! 5! t Cost: 14 + 18 + 2 + 16 = 50 cost of path = sum of edge costs in path s 3 t 2 6 7 4 5 24 18 2 9 14 (^15 ) 30 20 44 16 11 6 19 6 3

Brief History

Shimbel (1955). Information networks.

Ford (1956). RAND, economics of transportation.

Leyzorek, Gray, Johnson, Ladew, Meaker, Petry, Seitz (1957).

Combat Development Dept. of the Army Electronic Proving Ground.

Dantzig (1958). Simplex method for linear programming.

Bellman (1958). Dynamic programming.

Moore (1959). Routing long-distance telephone calls for Bell Labs.

Dijkstra (1959). Simpler and faster version of Ford's algorithm.

4 Reference: Network Flows: Theory, Algorithms, and Applications, R. K. Ahuja, T. L. Magnanti, and J. B. Orlin, Prentice Hall, 1993.

Applications

More applications.

! Robot navigation.

! Texture mapping.

! Typesetting in TeX.

! Urban traffic planning.

! Optimal pipelining of VLSI chip.

! Telemarketer operator scheduling.

! Subroutine in higher level algorithms.

! Routing of telecommunications messages.

! Approximating piecewise linear functions.

! Network routing protocols (OSPF, BGP, RIP).

! Exploiting arbitrage opportunities in currency exchange.

! Optimal truck routing through given traffic congestion pattern.

5

Dijkstra's Algorithm

6

Single Source Shortest Path

Assumptions.

! Digraph G.

! Single source s.

! Edge weights c(v, w) are nonnegative.

Goal. Find shortest path from s to every other vertex.

s 3 t 2 6 7 4 5 24 18 2 9 14 (^15 ) 30 20 44 16 11 6 19 6 0 9 32 14 15 50 34 45 shortest path tree (parent-link representation) 7

Edge Relaxation

Valid weights. For all vertices v, "(v) is length of some path from s to v.

Edge relaxation.

! Consider edge e = v !w.

! If current path from s to v plus edge v!w is shorter than

current path to w, then update current path to w.

s w v 33 (^0 ) 44 11 if (pi[w] > pi[v] + e.weight) { pi[w] = pi[v] + e.weight); pred[w] = v; } 8

Dijkstra's Algorithm

Dijkstra's algorithm. Maintain set of weights "(v) and a set of

explored vertices S for which "(v) is the length shortest s- v path.

! Initialize: S = { s }, "(s) = 0.

! Repeatedly choose unexplored node w which minimizes:

  • set pred[w] = v
  • add w to S, and set "(w) = "(v) + c(v, w) s w v "(v) S c(v, w)

" ( w ) = (^) ( v ,min w ) : v # S " ( v ) + c ( v , w ) shortest path to some v in explored part, followed by a single edge e = (v, w)

13

Dijkstra's Algorithm: Implementation

Critical step. Choose unexplored node w which minimizes:

Brute force implementation. Test all edges # O(EV) time.

" ( w ) = ( v ,min w ) : v # S " ( v ) + c ( v , w )

14

Dijkstra's Algorithm: Implementation

Critical step. Choose unexplored node w which minimizes:

Brute force implementation. Test all edges # O(EV) time.

Efficient implementation. Maintain a priority queue of unexplored

vertices, prioritized by "(w).

Q. How to maintain "?

A. When exploring v, for each edge v->w leaving v, update

" ( w ) = min { " ( w ), " ( v ) + c ( v , w ) }.

" ( w ) = ( v ,min w ) : v # S " ( v ) + c ( v , w )

15

Weighted Edge

public class Edge { public final int source; public final int target; public final double weight; public Edge(int v, int w, double weight) { this.source = v; this.target = w; this.weight = weight; } public String toString() { return source + "->" + target + " (" + weight + ") "; } } 16

Weighted Digraph

public class WeightedDigraph { private int V; private Sequence[] adj; public WeightedDigraph(int V) { this.V = V; adj = (Sequence[]) new Sequence[V]; for (int v = 0 ; v < V; v++) adj[v] = new Sequence(); } public int V() { return V; } public void addEdge(Edge e) { adj[e.source].add(e); } public Iterable adj(int v) { return adj[v]; } }

17

Dijkstra's Algorithm: Java Implementation

public Dijkstra(WeightedDigraph G, int s) { pi = new double[G.V()]; pred = new Edge[G.V()]; for (int v = 0 ; v < G.V(); v++) pi[v] = INFINITY; IndexMinPQ pq = new IndexMinPQ(G.V()); pi[s] = 0. 0 ; pq.insert(s, pi[s]); while (!pq.isEmpty()) { int v = pq.delMin(); for (Edge e : G.adj(v)) { int w = e.target; if (pi[w] > pi[v] + e.weight) { pi[w] = pi[v] + e.weight; pred[w] = e; if (pq.contains(w)) pq.decrease(w, pi[w]); else pq.contains(w) pq.insert(w, pi[w]); } } } } relax v-w 18

Indexed Priority Queue

Indexed PQ.

! Assume items are named 0 to N- 1.

! Insert, delete min, test if empty. [PQ ops]

! Decrease key, contains. [ST-like ops]

IndexMinPQ(int N) create an empty PQ on N elements public class IndexMinPQ (indexed priority queue) void insert(int i, Key key) add element i with given key void decrease(int i, Key key) decrease value of item i int delMin() delete and return smallest item boolean isEmpty() is the PQ empty? boolean contains(int i) does the PQ contain item i? 19

Indexed Priority Queue: Array Implementation

Indexed PQ: array implementation.

! Maintain vertex indexed array keys[i].

! Insert key: change keys[i].

! Decrease key: change keys[i].

! Delete min: scan through keys[i] for each item i.

! Maintain a boolean array marked[i]to mark items in the PQ.

insert 1 $ V delete-min V $ V decrease-key 1 $ E is-empty 1 $ V contains 1 $ V total V^2 Operation Array Dijkstra 20

Indexed Priority Queue

Indexed PQ: binary heap implementation.

! Assume items are named 0 to N- 1.

! Store priorities in a binary heap.

How to decrease key of item i? Bubble it up.

How to know which heap node to bubble up? Maintains an extra array

qp[i] that stores the heap index of item i.

7 14 78 18 91 81 77 42 47 45 83 decrease key of element 5 from 83 to 31 0 10 3 6 1 8 4 5 2 9 7 0 1 2 3 4 5 6 7 8 9 10

  • 8 4 3 6 1 0 10 5 2 9 i pq 6 5 9 3 2 8 4 11 1 10 7 qp 47 18 91 42 14 83 78 77 7 81 45 key 11 7 - -

25

Priority First Search

Priority first search. Maintain a set of explored vertices S, and

grow S by exploring edges with exactly one endpoint leaving S.

DFS. Edge from vertex which was discovered most recently.

BFS. Edge from vertex which was discovered least recently.

Prim. Edge of minimum weight.

Dijkstra. Edge to vertex which is closest to s.

w S v 26

Edsger W. Dijkstra

The question of whether computers can think is like the question of whether submarines can swim. Do only what only you can do. In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind. The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums. Edger Dijkstra Turing award 1972 27

Bellman-Ford-Moore

28 Currency UK Pound Euro Japanese Yen Swiss Franc

**1.

2.** US Dollar Gold (oz.) 1. 0. Euro **0.

0.**

**0.

0.** Franc **0.

0.**

**0.

0.** Gold **208.

1.**

Application: Currency Conversion

Currency conversion. Given currencies and exchange rates, what is

best way to convert one ounce of gold to US dollars?

! 1 oz. gold # $ 327. 25. ! 1 oz. gold # £ 208. 10 # # $ 327. 00. ! 1 oz. gold # 455. 2 Francs # 304. 39 Euros # $ 327. 28. [ 208. 10 $ 1. 5714 ] [ 455. 2 $. 6677 $ 1. 0752 ]

29

Application: Currency Conversion

Graph formulation.

! Vertex = currency.

! Edge = transaction, with weight equal to exchange rate.

! Find path that maximizes product of weights.

**G $ £ F E

208.100** (^) 455.2 0. 2.1904 (^) **0. 0.004816 1.

¥

0.** 30

Application: Currency Conversion

Reduction to shortest path problem.

! Let &(v, w) be exchange rate from currency v to w.

! Let c(v, w) = - lg &(v, w).

! Shortest path with costs c corresponds to best exchange sequence.

Challenge. Solve shortest path problem with negative weights.

**-lg(455.2) = -8.

-0. G $ £ F E

208.100** (^) 455.2 0. 2.1904 (^) **0. 0.004816 1.

¥

0.** 31

Shortest Paths with Negative Weights: Failed Attempts

Dijkstra. Can fail if negative edge weights.

Re-weighting. Adding a constant to every edge weight can fail.

0 3 1 2 4

- (^92) 6 0 3 1 11 13 0 2 15 Dijkstra selects vertex 3 immediately after 0. But shortest path from 0 to 3 is 0! 1! 2! 3. Adding 9 to each edge changes the shortest path. 32

Shortest Paths: Negative Cost Cycles

Negative cycle. Directed cycle whose sum of edge weights is negative.

Observation. If negative cycle C on path from s to t, then shortest

path can be made arbitrarily negative by spinning around cycle;

otherwise, there exists a shortest s-t path that is simple.

s t C cost(C) < 0

- 7 -

37 Queue q = new Queue(); marked[s] = true; pi[s] = 0 ; q.enqueue(s); while (!q.isEmpty(v)) { int v = q.dequeue(); marked[v] = false; for (Edge e : G.adj(v)) { int w = e.target; if (pi[w] > pi[v] + e.weight) { pi[w] = pi[v] + e.weight; pred[w] = e; if (!marked[w]) { marked[w] = true; q.enqueue(w); } } } }

Bellman-Ford-Moore Algorithm

Initialize pi[v] = ) and marked[v]= false for all vertices v.

38

Single Source Shortest Paths Implementation: Cost Summary

Remark 1. Negative weights makes the problem harder.

Remark 2. Negative cycles makes the problem intractable.

Algorithm Dijkstra (classic) † Dijkstra (heap) † Worst Case V^2 E log V Best Case V^2 E Space E E Dynamic programming ‡ Bellman-Ford ‡

E V

E V

E V

E

E

E

† nonnegative costs ‡ no negative cycles 39

Arbitrage

Arbitrage. Is there an arbitrage opportunity in currency graph?

! Ex: $ 1 # 1.3941 Francs # 0.9308 Euros # $1.00084.

! Is there a negative cost cycle?

! Fastest algorithm very valuable!

**G $ £ F E

208.100** (^) **455.2 1. 2.1904 0. 0.004816 1.

¥

0.**

- 0. 4793 + 0. 5827 - 0. 1046 < 0 -0. -lg(0.6677) = 0. -0. 40

Negative Cycles

If negative cycle reachable from s. Bellman-Ford-Moore gets stuck in

infinite loop, updating vertices in a cycle.

Finding a negative cycle. If any vertex v is updated in phase V,

there exists a negative cycle, and we can trace back pred[v] to find it.

s 3 v 2 6 7 4 5 pred[v]

41

Negative Cycle Detection

Goal. Identify a negative cycle (reachable from any vertex).

Solution. Add 0-weight edge from artificial source s to each vertex v.

Run Bellman-Ford from vertex s.

**-0.48 -0.

s** 42

Shortest Path in a DAG

43

Shortest/Longest Path in DAG

Shortest path in DAG algorithm.

! Consider vertices v in topological order:

  • relax each edge v!w

Theorem. Algorithm computes shortest path in linear time

(even if negative edge weights).

A B C G H D E F 4 I 6 2 5 3 4

**- 0 7

-**