shortest paths - github pages · 2020-05-14 · shortest paths shortest path problem. knapsack...

36
Shortest Paths CE 367R

Upload: others

Post on 22-May-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Shortest Paths

CE 367R

Page 2: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

SHORTEST PATHPROBLEM

Page 3: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Shortest Path Problem

Identify a path connecting a given origin and destination, where the totalcost of the links in the path is minimized.

Shortest Paths Shortest path problem

Page 4: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Applications

Vehicle routing

Critical path analysis in project management

Six degrees of Kevin Bacon

Shortest Paths Shortest path problem

Page 5: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Vehicle routing

Nodes represent physical locations (intersections, trip start and endpoints, etc.)

Links represent roads connecting these locations.

The cost represents the travel time on each road.

The origin and destination are the start and end points of the trip.

The path represents the route the vehicle should follow, and its cost isthe duration of the trip.

Shortest Paths Shortest path problem

Page 6: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Critical path analysis

You are working on a large project which has many tasks. Each task has aspecified duration; some tasks need to be completed before others canbegin (prerequisites). Assume any number of tasks can be done in parallelif they are not prerequisites for each other. What is the fastest the projectcan be completed?

You can use this to plan courses for degrees. G4 does something similar tothis.

Shortest Paths Shortest path problem

Page 7: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Critical path analysis

Nodes represent tasks; each task has two nodes, one for its start and onefor its end. Also, create two nodes to represent the start and end of theentire project.

There are two kinds of links: prerequisite links connecting the end nodefor each task to the start node of every task depending on it; and tasklinks between the start and end node for each task.

Create a prerequisite link between the project start node and the startnode for every task that can begin immediately (no prereqs), and betweenthe end node of every task and the end node for the project.

Shortest Paths Shortest path problem

Page 8: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Critical path analysis

Prerequisite links have zero cost; the cost on task links is the negative ofhow long the task takes.

The origin and destination are the start and end nodes of the project.

The path passes through the critical tasks (those that cannot be delayedwithout delaying the whole project), and the cost is the negative of theproject duration.

Why all the negatives? We are actually solving a longest path problem— remember how we can convert maximization problems to minimizationby changing signs.

Shortest Paths Shortest path problem

Page 9: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Knapsack problem

You are going hiking, and have to choose objects to pack. There are pobjects, and each object i has a weight wi and a utility ui . Decide whichobjects to pack to maximize the total utility, without the total weightexceeding W .

You don’t have to be a hiker. The objects can be projects that fit withina given budget, ways to cut materials to minimize waste, investments in aportfolio, and so on.

Shortest Paths Shortest path problem

Page 10: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Knapsack problem

Create a grid of nodes with W + 1 rows and p columns, plus twoartificial nodes s (in the first row and column 0) and t.

To represent the choice of not packing item i , create a horizontal linkbetween each node in column i − 1 and the node in column p at thesame level, with zero cost.

To represent the choice of packing item i , create a link between eachnode in column i − 1, and the node in column i which is wi levelsdown, if such a node exists, with cost equal to −ui .Also create links between each node in column p and t with zero cost.

From the path, you can read whether you should pack item p or not,and the cost of the path is the negative of the total utility.

This is another longest path problem. Using big-O notation, roughly howmany links and nodes are in this network?

Shortest Paths Shortest path problem

Page 11: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Six Degrees of Kevin Bacon

Connect a given actor or actress to Kevin Bacon in six steps or less, usingfilms they both were in.

Alfred Hitchcock

Shortest Paths Shortest path problem

Page 12: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Alfred Hitchcock was in Show Business at War with Orson Welles.

Shortest Paths Shortest path problem

Page 13: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Orson Welles was in A Safe Place with Jack Nicholson.

Shortest Paths Shortest path problem

Page 14: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Jack Nicholson was in A Few Good Men with Kevin Bacon.

Shortest Paths Shortest path problem

Page 15: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

So, this is a winning chain for Alfred Hitchcock (three steps).

Shortest Paths Shortest path problem

Page 16: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Six Degrees of Kevin Bacon

Create a node for each actor/actress.

For each pair of nodes, create links in both directions if they haveacted in the same film.

Assign a cost of 1 to every link.

The origin is the node representing a given name, the destination isthe node representing Kevin Bacon.

The path is the shortest connection to Kevin Bacon, and its cost isthe number of films needed to connect them.

Shortest Paths Shortest path problem

Page 17: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

FORMULATION

Page 18: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

In a shortest path problem, we are given a network G = (N,A) in whicheach link has a fixed cost cij , an origin s, and a destination t. The goal isto find the path in G from s to t with minimum total cost.

minx

∑(i ,j)∈A

cijxij

s.t.∑

(i ,j)∈A(i)

xij −∑

(h,i)∈B(i)

xij =

1 if i = s

−1 if i = t

0 otherwise

∀i ∈ {1, . . . , n}

xij ∈ {0, 1} ∀(i , j) ∈ A

To find this path efficiently, we need to avoid enumerating every possiblepath.

Shortest Paths Formulation

Page 19: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

A physical solution method

Construct a physical model of the network, representing each link with astring of length cij , tying together the ends at nodes.

Grab the knots corresponding to s and t, and pull them apart until youencounter resistance.

The taut strings represent the shortest path in the network.

Shortest Paths Formulation

Page 20: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

One odd twist of shortest path problems: it’s not that much harder to findthe shortest path from s to t than to find many shortest paths at the sametime. Two broad approaches:

One-to-all: Find the shortest paths from node s to all destination nodes.

All-to-one: Find the shortest paths from all origin nodes to node t.

For the purposes of this course, either will work. For clarity, we’ll stick withone-to-all shortest paths.

Shortest Paths Formulation

Page 21: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

One-to-all shortest path relies on Bellman’s Principle, which lets usre-use information between different origins and destinations:

If π∗ = [s, i1, i2, . . . , in, t] is a shortest path from s to t, then the subpath[s, i1, . . . , ik ] is a shortest path from s to ik

The upshot: we don’t have to consider the entire route from s to t at once.Instead, we can break it up into smaller, easier problems. (This is why the“one-to-all” problem isn’t much harder than the “one-to-one” problem.)

Shortest Paths Formulation

Page 22: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Why does Bellman’s principle hold?

s

i1

i2

t

If there is a shorter path from s to ik , I could “splice” that into π∗ andobtain a shorter path from s to t.

Shortest Paths Formulation

Page 23: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

A compact way to store all of the shortest paths from s to every othernode is to maintain two labels Li and qi for each node.

Li is the cost label, giving the travel time on the shortest known pathfrom s to i .

qi is the path label, which specifies the previous node on the shortestknown path from s to i .

By convention, Ls = 0 and qs = −1; Li = ∞ and qi = −1 if we haven’tyet found any path from s to i

Shortest Paths Formulation

Page 24: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

DIJKSTRA’S ALGORITHM

Page 25: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Dijkstra’s algorithm is a label-setting shortest path algorithm.

That is, once we scan a node, its labels are set permanently and neverchanged again.

Dijkstra’s algorithm maintains a set of finalized nodes F , which we havealready found the shortest path to.

Shortest Paths Dijkstra’s algorithm

Page 26: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Algorithm

(Assume there is at least one path from s to all other nodes in thenetwork, and that cij ≥ 0 for all links.)

1 Initialize all labels Li =∞, except for the origin Ls = 0

2 Initialize F = ∅, and the path vector q = −13 Find the node i not in F with minimum Li value.4 For each arc (i , j) ∈ A(i), repeat these steps:

1 Calculate Ltempij = Li + cij

2 If Ltempij < Lj , then update Lj = Ltemp

ij and set qj = i .

5 Add i to F .

6 If F = N, terminate. Otherwise, return to step 3.

This implementation of Dijkstra’s algorithm only requires O(n2) steps.Why?

Shortest Paths Dijkstra’s algorithm

Page 27: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Complexity

There are O(n) iterations (one for each node except the origin).

At each iteration, we must perform O(n) work: finding the node withminimal temporary label.

So, this implementation of Dijkstra’s algorithm requires O(n2) steps.

Shortest Paths Dijkstra’s algorithm

Page 28: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

The bottleneck is finding the node with minimum temporary label.

There are more efficient versions of Dijkstra’s, targeted at this step, whichcan reduce the running time to O(m + n log n) steps.

Shortest Paths Dijkstra’s algorithm

Page 29: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

A∗ ALGORITHM

Page 30: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

If we only care about the shortest path between one origin and onedestination, it is possible to create a more efficient version of Dijkstra’salgorithm.

Dijkstra’s “fans out” in all directions from the origin; can we direct ittowards the destination?

Let gi be an underestimate of the shortest path cost between node i andthe destination t.

Replace step 3 in Dijkstra’s algorithm with the following:

Find the node i not in F with minimum Li + gi value.

Shortest Paths A∗ algorithm

Page 31: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Some ways to choose gi for the vehicle routing problem:

Use the Euclidean (“as-the-crow-flies”) distance between i and t,divided by the fastest possible speed.

Use the travel time on the shortest path between i and t usingfree-flow times (assuming no congestion).

Some not-so-good ways:

Choose gi = 0. (This just gives you regular Dijkstra’s.)

Choose gi to be the actual shortest path cost between i and t (Really

If gi is greater than the actual shortest path cost from i to t, A∗ is notguaranteed to find the shortest path. But you might get lucky.

Can you think of ways to choose g for project scheduling and the otherapplications of shortest paths?

Shortest Paths A∗ algorithm

Page 32: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

LABEL CORRECTINGALGORITHM

Page 33: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Dijkstra’s algorithm is label-setting because once a node is finalized, itslabels will never change.

For this reason, it can’t be used if any of the link costs are negative.

A label-correcting algorithm can continue to update the labels for anynode until termination, and can handle negative costs. (Unless there arecycles with negative cost.)

We maintain a scan eligible list SEL of nodes which still need to bescanned before we are sure all shortest paths have been found.

Shortest Paths Label correcting algorithm

Page 34: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

1 Initialize by setting Li =∞ and qi = −1 ∀i ∈ N, and set Ls = 0

2 Initialize SEL to contain only the origin: SEL← {s}3 Choose a node i ∈ SEL and remove it from that list.4 Scan node i by repeating the following steps for all (i , j) ∈ A(i):

1 Lj = min{Lj , Li + cij}2 If Lj changed in the last step, set qj = i and add j to SEL.

5 If SEL is empty, then terminate. Otherwise, return to step 3.

Shortest Paths Label correcting algorithm

Page 35: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

How do we select a node from SEL in step 3?

If we choose the oldest node in SEL for i , the algorithm requiresO(mn) steps.

If we modify the above rule so that nodes re-entering SEL arescanned first, the algorithm is often faster in practice (but slower inthe worst case).

Shortest Paths Label correcting algorithm

Page 36: Shortest Paths - GitHub Pages · 2020-05-14 · Shortest Paths Shortest path problem. Knapsack problem You are going hiking, and have to choose objects to pack. There are p objects,

Define the reduced cost of link (i , j) as the value c̄ij = cij + Li − Lj .

In the physical solution method, you can interpret c̄ij as the amount ofslack in each string.

In the critical path problem, you can interpret c̄ij as the amount each taskcan be delayed without delaying the project.

The reduced costs are useful to define even when we are not yet done andthe Li values are not final. We can show the following:

1 After the cost labels Li have been reduced below ∞, they representshortest paths to every node iff c̄ij ≥ 0 for every link.

2 For any directed path between two nodes k and `, the sum of thereduced costs in the path equals the sum of the costs in the path,plus Lk − L`.

3 In particular, in any directed cycle, the sum of the reduced costsequals the sum of the costs.

Shortest Paths Label correcting algorithm