4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time....

58
Algorithms in Java, 4 th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2009 · March 28, 2010 7:19:37 PM 4.4 Shortest Paths Reference: Algorithms in Java, 4 th edition, Section 4.4 Dijkstra's algorithm implementation acyclic networks negative weights

Transcript of 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time....

Page 1: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2009 · March 28, 2010 7:19:37 PM

4.4 Shortest Paths

Reference: Algorithms in Java, 4th edition, Section 4.4

‣ Dijkstra's algorithm‣ implementation‣ acyclic networks‣ negative weights

Page 2: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Google maps

2

Page 3: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Given a weighted digraph G, find the shortest directed path from s to t.

3

Shortest paths in a weighted digraph

shortest path: s→6→3→5→t

cost: 14 + 18 + 2 + 16 = 50

s

3

t

2

6

1

5

24

18

2

9

14

155

30

20

44

16

11

6

19

6

0

9 32

14

15 50

34

45

4

Page 4: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Shortest path versions

Which vertices?

• From one vertex to another.

• From one vertex to every other.

• Between all pairs of vertices.

Restrictions on edge weights?

• Nonnegative weights.

• Arbitrary weights.

• Euclidean weights.

Cycles?

4

Page 5: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

5

Early history of shortest paths algorithms

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.

Page 6: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

• Maps.

• Robot navigation.

• Texture mapping.

• Typesetting in TeX.

• Urban traffic planning.

• Optimal pipelining of VLSI chip.

• Telemarketer operator scheduling.

• Subroutine in advanced 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.

6

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

Shortest path applications

Page 7: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

7

‣ Dijkstra's algorithm‣ implementation‣ acyclic networks‣ negative weights

Page 8: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

8

Edsger W. Dijkstra: select quotes

Edger DijkstraTuring award 1972

“ 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. ”

Page 9: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

9

Single-source shortest-paths

Input. Weighted digraph G, source vertex s.Goal. Find shortest path from s to every other vertex.Observation. Use parent-link representation to store shortest path tree.

0

3

7

2

6

1

5

24

18

2

9

14

155

30

20

44

16

11

6

19

6

0

9 32

14

15 50

34

45

0 1 2 3 4 5 6 7

distTo[v]

edgeTo[v]

marked[v]

0 15 9 32 45 34 14 50

- 0→1 0→2 6→3 5→4 3→5 0→6 5→7

T T T T T T T T

source s

4

Page 10: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Start with vertex s and greedily grow tree T

• find cheapest path ending in an edge e with exactly one endpoint in T

• add e to T

• continue until no edges leave T

Dijkstra's algorithm

10

S e

Page 11: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Initialize T to s, distTo[s] to 0.Repeat until T contains all vertices reachable from s:

• find edge e with v in T and w not in T that minimizes distTo[v] + e.weight()

11

Dijkstra's algorithm

s

w

v

distTo[v]

T

e

Page 12: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

12

Dijkstra's algorithm

s

w

v

distTo[v]

T

e

distTo[w] = distTo[v] + e.weight();edgeTo[w] = e;

Initialize T to s, distTo[s] to 0.Repeat until T contains all vertices reachable from s:

• find edge e with v in T and w not in T that minimizes distTo[v] + e.weight()

• set distTo[w] = distTo[v] + e.weight() and edgeTo[w] = e

• add w to T

Page 13: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

13

Dijkstra’s algorithm example

0→1 .41

0→5 .29

1→2 .51

1→4 .32

2→3 .50

3→0 .45

3→5 .38

4→2 .32

4→3 .36

5→1 .29

5→4 .21

01

3 2

5

4

4→2 (.82 = .50 + .32)4→3 (.86 = .50 + .36)1→2 (.92)

01

3 2

5

4

4→3 (0.86)2→3 (1.32 = .82 + .50)

01

3 2

5

4

1

3 2

5

4

0→5 (.29)0→1 (.41)

01

3 2

5

4

0→1 (.41)5→4 (.50 = .29 + .21)5→1 (.58 = .29 + .29)

0

edge with v in T and w not in T

2

4

5→4 (.50)1→4 (.73 = .41 + .32) 1→2 (.92 = .41 + .51)

01

5

3

edge in shortest path tree

Page 14: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Invariant. For v in T, distTo[v] is the length of the shortest path from s to v.

Pf. (by induction on |T|)

• Let w be next vertex added to T.

• Let P* be the s ↝ w path through v.

• Consider any other s ↝ w path P, and let x be first node on path outside T.

• P is already as long as P* as soon as it reaches x by greedy choice.

• Thus, distTo[w] is the length of the shortest path from s to w.

14

Dijkstra's algorithm: correctness proof

P

v

s

x

w

S P*

assuming thatedge weights

are nonnegative

Page 15: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

15

‣ Dijkstra's algorithm‣ implementation‣ acyclic networks‣ negative weights‣

Page 16: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

16

Weighted digraph API

public class DirectedEdgepublic class DirectedEdgepublic class DirectedEdge

DirectedEdge(int v, int w, double weight) create a weighted edge v→w

int from() vertex v

int to() vertex w

double weight() the weight

public class Networkpublic class Network weighted digraph data typeweighted digraph data type

Network(int V)Network(int V) create an empty digraph with V vertices

Network(In in)Network(In in) create a digraph from input stream

void addEdge(DirectedEdge e)addEdge(DirectedEdge e) add a weighted edge from v to w

Iterable<DirectedEdge> adj(int v)adj(int v) return an iterator over edges leaving v

int V()V() return number of vertices

int E()E() return number of edges

Iterable<DirectedEdge> edges()edges() return an iterator overall the network’s edges

Nomenclature reset: “Weighted directed graph” = “Network”

Page 17: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

17

public class Network{ private final int V; private final Bag<Edge>[] adj;

public Network(int V) { this.V = V; adj = (Bag<DirectedEdge>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<DirectedEdge>(); }

public void addEdge(DirectedEdge e) { int v = e.from(); adj[v].add(e); }

public Iterable<DirectedEdge> adj(int v) { return adj[v]; }

public int V() { return V; }}

Network: adjacency-lists implementation in Java

similar to edge-weighted undirected graph, but only add edge to v's adjacency set

Page 18: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

18

public class DirectedEdge{ private final int v, w; private final double weight;

public DirectedEdge(int v, int w, double weight) { this.v = v; this.w = w; this.weight = weight; }

public int from() { return v; }

public int to() { return w; }

public int weight() { return weight; }

}

Weighted directed edge: implementation in Java

from() and to() replaceeither() and other()

similar to Edgefor undirected weighted graphs,but simpler

Page 19: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

19

Shortest path data type

Design pattern.

• DijkstraSPT class is a Network client.

• Client query methods return distance and path iterator.

public class DijkstraSPT public class DijkstraSPT

DijkstraSPT(Network G, int s) shortest path from s in graph G

double distTo(int v) length of shortest path from s to v

Iterable <DirectedEdge> pathTo(int v) shortest path from s to v

In in = new In("network.txt");

Network G = new Network(in);

int s = 0, t = G.V() - 1;

DijktraSPT spt = new DijkstraSPT(G, s);

StdOut.println("distance = " + spt.distTo(t));

for (DirectedEdge e : spt.pathTo(t))

StdOut.println(e);

Page 20: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

20

Find edge e with v in S and w not in S that minimizes distTo[v] + e.weight().

How difficult?

• Intractable.

• O(E) time.

• O(V) time.

• O(log E) time.

• O(log* E) time.

• Constant time.

Dijkstra implementation challenge

try all edges

Dijkstra with a binary heap

s

w

v

distTo[v]

S

e

Page 21: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Lazy vs. eager implementation

Issue:

• PQ contains edges from a vertex v in S to a vertex w not in S.

• Adding w to the tree requires adding its incident edges to PQ.

• Some edges on the PQ become obsolete.

Obsolete edge:

• An edge that willnever be added to the tree

Lazy approach

• Leave obsolete edges on PQ

• Check for obsolescence when removing

Eager approach

• Remove obsolete edges from PQ (need more sophisticated PQ)

• only need one edge per vertex21

P

v

s

w

S P*

obsolete edge

test for obsolescence:are both vertices on the tree?

Page 22: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

22

Lazy Dijkstra’s algorithm example

0→1 .41

0→5 .29

1→2 .51

1→4 .32

2→3 .50

3→0 .45

3→5 .38

4→2 .32

4→3 .36

5→1 .29

5→4 .21

01

3 2

5

4

1→4 (.73) 4→2 (.82 = .50 + .32)4→3 (.86 = .50 + .36)1→2 (.92)

01

3 2

5

4

4→3 (0.86)1→2 (.92)2→3 (1.32 = .82 + .50)

1

3 2

5

4

0→5 (.29)0→1 (.41)

01

3 2

5

4

0→1 (.41)5→4 (.50 = .29 + .21)5→1 (.58 = .29 + .29)

0

2

4

5→4 (.50)1→4 (.73 = .41 + .32) 1→2 (.92 = .41 + .51)

01

5

3

01

3 2

5

4

1→2 (.92) 2→3 (1.32)

priority queue

obsoleteedge

Page 23: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

23

Lazy implementation of Dijkstra's algorithm

import java.util.Comparator;

public class LazyDijkstraSPT{ private boolean[] marked; private double[] distTo; private DirectedEdge[] edgeTo; private MinPQ<DirectedEdge> pq;

private class ByDistanceFromSource implements Comparator<DirectedEdge> { public int compare(DirectedEdge e, DirectedEdge f) { double x = distTo[e.from()] + e.weight(); double y = distTo[f.from()] + f.weight(); if (x < y) return -1; else if (x > y) return +1; else return 0; } }

public LazyDijkstra(Network G, int s) { marked = new boolean[G.V()]; edgeTo = new DirectedEdge[G.V()]; distTo = new double[G.V()]; pq = new MinPQ<DirectedEdge>(new ByDistanceFromSource()); dijkstra(G, s); }}

compare edges in pq by distTo[v] + e.weight()

NOT astatic method

Page 24: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

24

Lazy implementation of Dijkstra's algorithm

private void dijkstra(Network G, int s) { visit(G, s); while (!pq.isEmpty()) { DirectedEdge e = pq.delMin(); int v = e.from(), w = e.to(); if (marked[w]) continue; distTo[w] = e; distTo[w] = distTo[v] + e.weight(); visit(G, w); } }

private void visit(Network G, int v) { marked[v] = true; for (DirectedEdge e : G.adj(w)) if (!marked[e.to()]) pq.insert(e); }

edge is obsolete

add all edges v->w to pq,provided w not already in S

found shortest path to w

Page 25: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

25

Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time.Pf.

Improvements.

• Eagerly eliminate obsolete edges from PQ.

• Maintain on PQ at most one edge incident to each vertex v not in T ⇒ at most V edges on PQ.

• Use fancier priority queue: best in theory yields O(E + V log V).

Dijkstra's algorithm running time

operation frequency time per op

delete min E log E

insert E log E

Page 26: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Remark. Dijkstra examines vertices in increasing distance from source.

26

Shortest path trees

50%

75% 100%

25%

Page 27: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

27

Priority-first search

Insight. All of our graph-search methods are the same algorithm!

• Maintain a set of explored vertices S.

• Grow S by exploring edges with exactly one endpoint leaving S.

DFS. Take edge from vertex which was discovered most recently.BFS. Take edge from vertex which was discovered least recently.Prim. Take edge of minimum weight.Dijkstra. Take edge to vertex that is closest to s.

Challenge. Express this insight in reusable Java code.

s

w

v

distTo[v]

S

e

Page 28: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

28

Priority-first search: application example

Shortest s-t paths in Euclidean graphs (maps)

• Vertices are points in the plane.

• Edge weights are Euclidean distances.

A sublinear algorithm.

• Assume graph is already in memory.

• Start Dijkstra at s.

• Stop when you reach t.

Even better: exploit geometry

• For edge v→w, use weight d(v, w) + d(w, t) – d(v, t).

• Proof of correctness for Dijkstra still applies.

• In practice only O(V 1/2 ) vertices examined.

• Special case of A* algorithm

[Practical map-processing programs precompute many of the paths.]

Euclidean distance

Page 29: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

29

‣ Dijkstra's algorithm‣ implementation‣ acyclic networks‣ negative weights

Page 30: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Suppose that a network has no cycles.

Q. Is it easier to find shortest paths than in a general network?

30

Acyclic networks

5->4 0.35 4->7 0.37 5->7 0.28 5->1 0.32 4->0 0.380->2 0.26 3->7 0.39 1->3 0.29 7->2 0.346->2 0.40 3->6 0.526->0 0.586->4 0.93

A. Yes!A. AND negative weights are no problem

source

Page 31: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Suppose that a network has no cycles.

Q. Is it easier to find shortest paths than in a general network?

31

Acyclic networks

A. Yes!A. AND negative weights are no problem

5->4 -0.35 4->7 -0.37 5->7 -0.28 5->1 -0.32 4->0 -0.380->2 -0.26 3->7 -0.39 1->3 -0.29 7->2 -0.346->2 -0.40 3->6 -0.526->0 -0.586->4 -0.93

source

Page 32: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

32

A key operation

Relax edge e from v to w.

• distTo[v] is length of some path from s to v.• distTo[w] is length of some path from s to w.

• If v→w gives a shorter path to w through v, update distTo[w] and edgeTo[w].

distTo[w] = 47

distTo[v] = 11

distTo[s] = 0

private void relax(DirectedEdge e){ int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; } }

w

v

33

44

s

Initialization:distTo[s] = 0.0;

all other distTo[] = ∞

Page 33: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Algorithm:

• Consider vertices in topologically sorted order

• Relax all edges incident on vertex

33

Shortest paths in acyclic networks

Page 34: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Algorithm:

• Consider vertices in topologically sorted order

• Relax all edges incident on vertex

Proposition. Shortest path to each vertex is known before its edges are relaxedProof (strong induction)

• let v->w be the last edge on the shortest path from s to w.

• v appears before w in the topological sort

- shortest path to v is known before its edges are relaxed- v’s edges are relaxed before w’s edges are relaxed, including v->w

• therefore, shortest path to w is known before w’s edges are relaxed.

34

Shortest paths in acyclic networks

w

v

s

Page 35: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

35

Shortest paths in acyclic networks

public class AcyclicNetworkSPT{ private double[] distTo; private DirectedEdge[] edgeTo; public AcyclicNetworkSPT(Network G, int s) { distTo = new double[G.V()]; edgeTo = new DirectedEdge[G.V()]; for (int v = 0; v < G.V(); v++) distanceTo[v] = Double.POSITIVE_INFINITY; distanceTo[s] = 0.0;

NetworkSort sort = new NetworkSort(G); for (int v : sort.topological()) for (DirectedEdge e : G.adj(v)) relax(e); } }

topological sort

Page 36: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Algorithm:

• Negate all weights

• Find shortest path

• Negate weights in result

36

Longest paths in acyclic networks

5->4 0.35 4->7 0.37 5->7 0.28 5->1 0.32 4->0 0.380->2 0.26 3->7 0.39 1->3 0.29 7->2 0.346->2 0.40 3->6 0.526->0 0.586->4 0.93

5->4 -0.35 4->7 -0.37 5->7 -0.28 5->1 -0.32 4->0 -0.380->2 -0.26 3->7 -0.39 1->3 -0.29 7->2 -0.346->2 -0.40 3->6 -0.526->0 -0.586->4 -0.93

Note: Best known algorithm for general networks is exponential!

Equivalent: reverse sense of equality in relax()

Page 37: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Longest paths in acyclic networks: application

Job scheduling. Given a set of jobs, with durations and precedence constraints, schedule the jobs (find a start time for each) so as to achieve the minimumcompletion time while respecting the constraints.

37

0 41.0 3 1 7 9 1 51.0 1 2 2 50.0 0 3 36.0 0 4 38.0 0 5 45.0 0 6 21.0 2 3 8 7 32.0 2 3 8 8 32.0 1 2 9 29.0 2 4 6

job duration count successorsprecedence constraints

Ex:

Solution:

0

4

3

5

9

7

6 8 2

1

41 91 173

Page 38: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Critical path method

38

CPM. To solve a job-scheduling problem, create a network

• source, sink

• two vertices (begin and end) for each job

• three edges for each job- begin to end (weighted by duration)

- source to begin

- end to sink

Critical path method: Use longest path from the source to schedule each job

410 0

511 1

502 2

363 3

384 4

455 5

216 6

327 7

328 8

29 9 9

precedence constraint(zero weight)

job start job finish

duration

zero-weight edge to each

job start

zero-weight edge from each

job finish

0 41.0 3 1 7 9 1 51.0 1 2 2 50.0 0 3 36.0 0 4 38.0 0 5 45.0 0 6 21.0 2 3 8 7 32.0 2 3 8 8 32.0 1 2 9 29.0 2 4 6

job duration count successorsprecedence constraints

Page 39: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Critical path method

39

CPM. To solve a job-scheduling problem, create a network

• source, sink

• two vertices (begin and end) for each job

• three edges for each job- begin to end (weighted by duration)

- source to begin

- end to sink

Critical path method: Use longest path from the source to schedule each job

410 0

511 1

502 2

363 3

384 4

455 5

216 6

327 7

328 8

29 9 9

precedence constraint(zero weight)

job start job finish

duration

zero-weight edge to each

job start

zero-weight edge from each

job finish

0 41.0 3 1 7 9 1 51.0 1 2 2 50.0 0 3 36.0 0 4 38.0 0 5 45.0 0 6 21.0 2 3 8 7 32.0 2 3 8 8 32.0 1 2 9 29.0 2 4 6

job duration count successorsprecedence constraints

Page 40: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Critical path method

40

Use longest path from the source to schedule each job.

410 0

511 1

502 2

363 3

384 4

455 5

216 6

327 7

328 8

29 9 9

critical path

duration

0

4

3

5

9

7

6 8 2

1

Page 41: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Deep water

Add deadlines to the job-scheduling problem.

Ex. “Job 2 must start no later than 70 time units after job 7.”Or, “Job 7 must start no earlier than 70 times units before job 2”

41

zero-weight edge from each

job finish

zero-weight edge to each

job start

410 0

511 1

502 2

363 3

38

-12

-70

-80

4 4

455 5

216 6

327 7

328 8

29 9 9

deadline

Need to solve longest paths problem in general networks (cycles, neg weights).Possibility of infeasible problem (negative cycles)

Page 42: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

42

‣ Dijkstra's algorithm‣ implementation‣ negative weights

Page 43: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Dijkstra. Doesn’t work with negative edge weights.

Re-weighting. Add a constant to every edge weight also doesn’t work.

Bad news. Need a different algorithm.43

Shortest paths with negative weights: failed attempts

0

3

1

2

4

2-9

6

0

3

1

11

13

20

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 pathbecause it adds 9 to each edge;wrong thing to do for paths with many edges.

Page 44: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

44

Negative cycles

Def. A negative cycle is a directed cycle whose sum of edge weights is negative.

Observations. If negative cycle C is on a path from s to t, then shortest pathcan be made arbitrarily negative by spinning around cycle.

Worse news. Need a different problem.

s t

Ccost(C) < 0

-6

7

-4

Page 45: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

45

Shortest paths with negative weights

Problem 1. Does a given digraph contain a negative cycle?Problem 2. Find the shortest simple path from s to t.

Bad news. Problem 2 is intractable.

Good news. Can solve problem 1 in O(VE) steps;if no negative cycles, can solve problem 2 with same algorithm!

s t

Ccost(C) < 0

Page 46: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

46

Shortest paths with negative weights: dynamic programming algorithm

A simple solution that works!

• Initialize distTo[v] = ∞, distTo[s]= 0.

• Repeat V times: relax each edge e.

for (int i = 1; i <= G.V(); i++) for (int v = 0; v < G.V(); v++) for (DirectedEdge e : G.adj(v)) relax(e);

phase i

Page 47: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

47

Dynamic programming algorithm trace

0→1 .41

0→5 .29

1→2 .51

1→4 .32

2→3 .50

3→0 .45

3→5 .38

4→2 .32

4→3 .36

5→1 .29

5→4 .21

1

3 2

5

4

0

distTo[v]

0∞

∞∞

1

3 2

5

4

00

.41

.92∞

.50

.29

1→2 (.92 = .41 + .51)1→4 (.73 = .41 + .32)5→4 (.50 = .29 + .21)

1

3 2

5

4

00

.41

.82.86

.50

.29

2→3 (1.33 = .83 + .50) 4→3 (.86 = .50 + .36)4→2 (.82 = .50 + .32)

1

3 2

5

4

00.41

∞∞

.29

0→1 (.41 = 0 + .41)0→5 (.50 = 0 + .50)

relaxed edges that update distTo[]

1

3 2

5

4

00

.41

.82.86

.50

.29 can stop early sinceno entries in distTo[] updated

Page 48: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

48

Dynamic programming algorithm: analysis

Running time. Proportional to E V.

Invariant. At end of phase i, distTo[v] ≤ length of any path from s to vusing at most i edges.

Proposition. If there are no negative cycles, upon termination distTo[v] is the length of the shortest path from from s to v.

and edgeTo[] gives the shortest paths

Page 49: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

49

Observation. If distTo[v] doesn't change during phase i,no need to relax any edge leaving v in phase i+1.

FIFO implementation. Maintain queue of vertices whose distance changed.

Running time.

• Proportional to EV in worst case.

• Much faster than that in practice.

Bellman-Ford-Moore algorithm

be careful to keep at most one copy of each vertex on queue

Page 50: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

50

Bellman-Ford-Moore algorithm

public class BellmanFordSPT{ private double[] distTo; private DirectedEdge[] edgeTo; private int phase; private int[] beenTo; private Queue<Integer> q = new Queue<Integer>(); private Queue<Integer> relaxed; public BellmanFordSPT(Network G, int s) { distTo = new double[V]; edgeTo = new DirectedEdge[V]; beenTo = new int[V];

for (int v = 0; v < V; v++) distTo[v] = Double.POSITIVE_INFINITY;

q.enqueue(s); distanceTo[s] = 0.0; for (phase = 1; phase <= V; phase++) { relaxed = new Queue<Integer>(); for (int v : q) for (DirectedEdge e : G.adj(v)) relax(e); q = relaxed; if (q.isEmpty()) break; } }}

private void relax(DirectedEdge e){ int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (beenTo[w] < phase) relaxed.enqueue(w); beenTo[w] = phase; } }

Maintain queue of verticeswhose distance changes.

Relax all edges incident onall vertices in the queue.

Page 51: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

51

Single source shortest paths implementation: cost summary

Remark 1. Cycles make the problem harder.Remark 2. Negative weights make the problem harder.Remark 3. Negative cycles makes the problem intractable.

algorithm worst case typical case

no cycles topological sort + relax E E

nonnegativecosts

Dijkstra (binary heap) E log E E

no negativedynamic programming E V E V

no negativecycles

Bellman-Ford E V E

Page 52: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Problem. Given currencies and exchange rates, what is best way to convertone 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.

52

Currency conversion

[ 208.10 × 1.5714 ]

[ 455.2 × .6677 × 1.0752 ]

currency £ Euro ¥ Franc $ Gold

UK pound 1.0000 0.6853 0.005290 0.4569 0.6368 208.100

Euro 1.45999 1.0000 0.007721 0.6677 0.9303 304.028

Japanese Yen 189.50 129.520 1.0000 85.4694 120.400 39346.7

Swiss Franc 2.1904 1.4978 0.01574 1.0000 1.3941 455.200

US dollar 1.5714 1.0752 0.008309 0.7182 1.0000 327.250

Gold (oz.) 0.004816 0.003295 0.0000255 0.002201 0.003065 1.0000

Page 53: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Graph formulation.

• Vertex = currency.

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

• Find path that maximizes product of weights.

Challenge. Express as a shortest path problem.53

Currency conversion

$G

£ EF

0.003065

1.3941208.100 455.2

2.1904 0.6677

1.07520.004816

327.25

¥

129.520

0.008309

Page 54: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Reduce to shortest path problem by taking logs.

• Let weight of edge v→w be - lg (exchange rate from currency v to w).

• Multiplication turns to addition.

• Shortest path with given weights corresponds to best exchange sequence.

Challenge. Solve shortest path problem with negative weights.54

Currency conversion

-lg(455.2) = -8.8304

0.5827

-0.1046

¥

$G

£ EF

0.003065

1.3941208.100 455.2

2.1904 0.6677

1.07520.004816

327.25

¥

129.520

0.008309

Page 55: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

55

Shortest paths application: 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?

Remark. Fastest algorithm is valuable!

0.5827

-0.1046

¥

$G

£ EF

0.003065

1.3941208.100 455.2

2.1904 0.6677

1.07520.004816

327.25

¥

129.520

0.008309

-0.4793

0.5827 - 0.1046 - 0.4793 < 0

Page 56: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

56

Negative cycle detection

If there is a negative cycle reachable from s.Bellman-Ford-Moore gets stuck in loop, updating vertices in cycle.

Proposition. If any vertex v is updated in phase V, there exists a negative cycle, and we can trace back edgeTo[v] to find it.

edgeTo[v]

s 3

v

2 6

1

4

5

Page 57: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

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

Solution. Initialize Bellman-Ford by setting distTo[v] = 0 for all vertices v and putting all vertices on the queue.

57

Negative cycle detection

¥

$G

£ EF

8.3499

-0.4793-7.7011 -8.8303

-1.1311 0.5827

-0.10467.6979

-8.3542

¥

-7.0170

6.91111

Page 58: 4.4 Shortest Paths · Proposition. Dijkstra's algorithm computes shortest paths in O(E log E) time. Pf. Improvements. •Eagerly eliminate obsolete edges from PQ. •Maintain on PQ

Shortest paths summary

Dijkstra’s algorithm.

• Nearly linear-time when weights are nonnegative.

• Generalization encompasses DFS, BFS, and Prim.

Acyclic networks.

• Arise in applications.

• Faster than Dijkstra’s algorithm.

• Negative weights are no problem.

Negative weights.

• Arise in applications.

• If negative cycles, shortest simple-paths problem is intractable (!)

• If no negative cycles, solvable via classic algorithms.

Shortest-paths is a broadly useful problem-solving model.

58