suppose g = (v, e) is a directed network. each edge (i,j) in e has an associated length c ij (cost,...

23
Suppose G = (V, E) is a directed network. Each edge ( i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest length between two specified nodes s and t. The length c(P) of a path P is the sum of its constituent arc lengths: c(P) = Σc ij Applications: transportation planning (shortest route) telecommunications (most reliable connection) scheduling (critical path) pattern recognition, computer graphics subproblem to a larger optimization problem Shortest Paths

Upload: cory-jefferson

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

1.Integer data 2.Directed network 3.There is a path from s to all other vertices 4.There is no negative length directed cycle Two algorithms to be studied: Label-setting – no negative length edges Label-correcting – negative length edges allowed KEY IDEA: Distance labels – D(i) = how far away vertex i is from s Assumptions & Algorithms

TRANSCRIPT

Page 1: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ cij (cost, time, distance, …).

Determine a path of shortest length between two specified nodes s and t. The lengthc(P) of a path P is the sum of its constituent arc lengths: c(P) = Σcij

Applications:transportation planning (shortest route)telecommunications (most reliable connection)scheduling (critical path)pattern recognition, computer graphicssubproblem to a larger optimization problem

Shortest Paths

Page 2: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

1. Differentiated by the number of origin/destination(source/sink) vertices:• from s to t• from s to all other vertices• to t from all other vertices• from i to j

2. Differentiated by the properties of the edge length:• Nonnegative edge lengths• Negative edge lengths

Different Types of SPP

Page 3: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

1. Integer data2. Directed network3. There is a path from s to all other vertices4. There is no negative length directed cycle

Two algorithms to be studied:

Label-setting – no negative length edges

Label-correcting – negative length edges allowed

KEY IDEA: Distance labels – D(i) = how far away vertex i is from s

Assumptions & Algorithms

Page 4: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

Let dj correspond to the distance of the shortest path from vertex s to vertex j.

Certainly ds=0. Moreover, the condition

dj ≤ di + cij (***)

says that if we have a shortest path its length cannot be improved by means of an edge (i, j) not currently on the path.

This condition (***) is used in both algorithms to efficiently solve the SPP.

FOCUS: single source problems, first with nonnegative costs, then allow negatives.

Optimality Conditions

Page 5: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

Key observation: any subpath of a shortest path is also a shortest path

When we find a shortest path from s to t, we also find a shortest path from s to any intermediate vertices.

This leads to a way of representing all shortest paths from vertex s. Find a shortest path from s to some t; call it P1. Repeat with another vertex t not on

path P1. Repeat until all vertices are used.

This results in a shortest path tree for G. Namely, a directed tree in G whose paths s to j are all shortest paths from s to j.

sb

a c t

Shortest Path Trees

Page 6: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

• Use two arrays to represent the shortest path tree.• d(i) records the shortest distance to vertex i.• p(i) records the predecessor of vertex i in the shortest path

61

2

3 5

42

1

4

3 5

3

-1

4

2

561

3 5

2

0

3

5

8

4

2 4

Example

-p(i)

654321i

0d(i)

Page 7: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

Nonnegative edge lengths: At each step in the algorithm, temporarily labeled vertices have a distance label of D(i), which is an upper bound on d(i);permanently labeled vertices are guaranteed to have D(i) = d(i).

Two sets to keep track of: S is the set of permanently labeled vertices; Ŝ = V – S is the set of temporarily labeled vertices

Vertices are transferred from Ŝ to S, one at a time. Upon termination, all vertices are permanently labeled. The algorithm processes vertices in increasing distance from vertex s. At the kth iteration, the kth closest vertex to s will be permanently labeled.

S S

s

b

ac t

Label Setting Algorithms

Page 8: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

If network has a topological order, examine vertices from 1 to n (reaching).If no topological order exists, examine vertices in order of increasing distance from s (Dijkstra’s Algorithm).

DIJKSTRA’Sstart with all vertices in Ŝ;give vertex s a distance D(s) = 0; give all other vertices the distance D(j) = ∞;while set S is not full

pick a vertex, i, in Ŝ with the smallest distance;move that vertex from Ŝ to S;update distances for vertices reachable from i; for each (i,j) in E(i)

if D(j) > D(i) + cij then D(j) = D(i) + cij and pred(j) = i;

Topological(Acyclic) or Not Topological

Page 9: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

61

2

3 5

42

1

4

4 1

2

32

i 1 2 3 4 5 6p(i) 0 0 0 0 0 0D(i) 0 ∞ ∞ ∞ ∞ ∞

61

2

3 5

42

1

4 1

2

32

i 1 2 3 4 5 6p(i) 0D(i) 0

0

3

3

Example

move to S

move to S

Page 10: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

61

2

3 5

42

1

4

4 1

2

32

i 1 2 3 4 5 6p(i) 0 1D(i) 0 2

61

2

3 5

42

1

4 1

2

32

i 1 2 3 4 5 6p(i) 0 1 2D(i) 0 2 3

0

0

2

2 4

3 3

3

Example

move to S

move to S

Page 11: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

61

2

3 5

42

1

4

4 1

2

32

i 1 2 3 4 5 6p(i) 0 1 2 2D(i) 0 2 3 6

61

2

3 5

42

1

4 1

2

32

i 1 2 3 4 5 6p(i) 0 1 2 2 3D(i) 0 2 3 6 6

0

0

2

2 4

3 3

3

6

6

3

6

Example

move to S

move to S

Page 12: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

61

2

3 5

42

1

4 1

2

32 532210p(i)

654321i

766320D(i)

0

2 4

3 3

6

6

Example

7

Page 13: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

0D(i)

∞∞∞∞∞0D(i)

0D(i)0D(i)0D(i)0D(i)

654321i

Example – Topological

61

2

3 5

43

4

5

2 6

3

86

3

6

7

4

Page 14: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

Example – Dijkstra’s

0D(i)

∞∞∞∞∞0D(i)

0D(i)0D(i)0D(i)0D(i)

654321i

61

2

3 5

43

4

5

2 6

4

81

3

6

7

4

Page 15: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

s

3

6

5

5

3

4

2

4 3

4

3

2

3

2

1

5

4

6

7

8

9

10

12

11

13

t

6

1

6

6

6

7

4

5

11

1

11

1

2

2

2

3

35

3

0

6

1

5

Example

Page 16: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ cij = positive, zero, negative

d(j) = minimum length of a path from s to jD(j) = distance label, current length of some path from s to j

D(j) = d(j) for all j in V iff D(j) ≤ D(i) + cij for all (i,j) in E(i) and D(s) = 0

Label Correcting

In a network with negative edge lengths, maintain a LIST of vertices with changed labels (Bellman-Ford Algorithm). Search edge list and update until no distance is changed.Maintain LIST as:

• Queue – FIFO• Stack – LIFO• Dequeue – vertex selected from top; first time vertex is added, it is placed at

the bottom; and anytime after that, a vertex re-enters at top of list

Page 17: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

• Queue

• Stack

• Dequeue (Pape)

List Structures

enter

leave

enter

leave

enterre-enter

leave

Page 18: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

61

2

3 5

42

-2

3

3 1

3

-12

3

4

∞∞∞∞∞0D(i)

0D(i)

20D(i)

0D(i)

0D(i)

0D(i)0D(i)

654321i

=List

=List

=List

=List

=List

=List

=List

0D(i)0D(i)

D(i) 0

0D(i)

=List

∞ ∞ ∞3

{1}{2, 3}

queue

Page 19: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

61

2

3 5

42

-2

3

3 1

3

-12

3

4

∞∞∞∞∞0D(i)

0D(i)

20D(i)

0D(i)

0D(i)

0D(i)0D(i)

654321i

=List

=List

=List

=List

D(i) 0=List

∞ ∞ ∞3

{1}{2, 3}

dequeue

Page 20: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

A negative length cycle will cause some distance labels to decrease without limit.With label correcting algorithms, negative length cycles can be detected in a variety of ways.

1. If C is max of all |cij|, then any D(j) falling below -nC.

2. In the queue(FIFO) implementation, more than n-1 updates for a single vertex.

3. If the graph based on predecessors fails to be a tree.

61

2

3 5

42

-2

-3

3 1

3

-12

3

4

Negative cycle detection

Page 21: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

1. Apply Dijkstra’s n times, once for each vertex. Good for sparse network.2. Floyd-Warshall Algorithm finds best in phases. Good for dense network.Assume network is strongly connected. Matrix entry D(i, j) = distance from i to j.

Floyd-Warshallbeginset D(i, j) = cij, pred(i, j) = i for each (i, j) in E;set D(i, i) = cii, pred(i, i) = i for each i;for k = 1 to n do

for all i,j = 1 to n do if D(i, j) > D(i, k) + D(k, j) then

D(i, j) = D(i, k) + D(k, j) and pred(i, j) = pred(k, j);end

All Pairs Shortest Path

Page 22: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

1

2

3 5

41

1

4

3

1

2

Example 0 1 3 ∞ ∞2 0 1 ∞ ∞∞ ∞ 0 ∞ 2∞ 4 ∞ 0 ∞∞ ∞ ∞ 1 0

1 1 1 - -2 2 2 - -- - 3 - 3- 4 - 4 -- - - 5 5

2

k = 0k = 1

D = pred=

0 12 0 1 ∞ ∞

∞ 04 0∞ 0

1 12 2 2 - -

- 34 4- 5

k = 2

D = pred=

Page 23: Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated length c ij (cost, time, distance, ). Determine a path of shortest

Example

0 1 2 5 42 0 1 4 39 7 0 3 26 4 5 0 77 5 6 1 0

1 1 2 5 32 2 2 5 32 4 3 5 32 4 2 4 32 4 2 5 5

k = 5

D = pred=

1

2

3 5

41

1

3

1

2

2

What is shortest path from 1 to 4?

What is shortest path from 5 to 3?

4