Introduction and The Shortest Path Problem
Introduction and The Shortest Path Problem
● Use computational tools to solve these models with medium-to-large scale data
○ Python and its many data science packages (e.g. pandas, networkx)
○ Focus on
◇ setting up models with the help of design patterns
◇ analyzing and interpreting solutions
Solving
Modeling directed graph (N , E),
source s ∈ N, target t ∈ N,
edge lengths c i j for (i, j) ∈ E
Mathematical model
2 This lesson...
● What is the shortest way to get from Point A to Point B?
1
3 Graphs and networks
● Graphs model how various entities are connected
● A directed graph (also known as a digraph) (N , E) consists of
○ set of nodes N (also known as vertices)
○ set of edges E (also known as arcs)
◇ edges are directed from one node to another
◇ edge from node i to node j is denoted by (i, j)
Example 1.
1 4
5 Paths
● A path is a sequence of edges connecting two specified nodes in a graph:
○ Each edge must have exactly one node in common with its predecessor in the sequence
○ Edges must be passed in the forward direction
○ No node may be visited more than once
Example 2. Give some examples of paths from node 1 to node 4 in the network in Example 1.
2
6 The shortest path problem
The shortest path problem
● Data:
○ Digraph (N , E)
○ Source node s ∈ N and sink node t ∈ N (s ≠ t)
○ Each edge (i, j) in E has a length c i j
● The length of a path is the sum of the lengths of the edges in the path
Example 3. Consider the digraph below. The labels next to each edge represent that edge’s length. What is the
shortest path from node 1 to node 6?
4
2 4
2 1
3
1 6
3
1
3 2
1
3 5
3
Example 4. You have just purchased a new car for $22,000. The cost of maintaining a car during a year depends on its
age at the beginning of the year:
To avoid the high maintenance costs associated with an older car, you may trade in your car and purchase a new car.
The price you receive on a trade-in depends on the age of the car at the time of the trade-in:
For now, assume that at any time, it costs $22,000 to purchase a new car. Your goal is to minimize the net cost
(purchasing costs + maintenance costs − money received in trade-ins) incurred over the next five years. Formulate
your problem as a shortest path problem.
4
Example 5. The Simplexville College campus shuttle bus begins running at 7:00pm and continues until 2:00am. Several
drivers will be used, but only one should be on duty at any time. If a shift starts at or before 9:00pm, a regular driver can
be obtained for a 4-hour shift at a cost of $50. Otherwise, part-time drivers need to be used. Several part-time drivers
can work 3-hours shifts at $40, and the rest are limited to 2-hour shifts at $30. The college’s goal is to schedule drivers
in a way that minimizes the total cost of staffing the shuttle bus. Formulate this problem as a shortest path problem.
5
Example 6. The Dijkstra Brewing Company is planning production of its new limited run beer, Primal Pilsner. The
company must supply 30 batches in the next quarter, then 25, 10, and 35 in successive quarters. Each quarter in which
the company produces the beer requires a factory setup cost of $100,000. Each batch of beer costs $3,000 to produce.
Batches can be held in inventory, but due to refrigeration requirements, the cost is a high $5,000 per batch per quarter.
The company wants to find a production plan that minimizes its total cost. Formuate this problem as a shortest path
problem.
6
Example 7. Beverly owns a vacation home in Cape Fulkerson that she wishes to rent for the summer season (May 1 to
September 1). She has solicited bids from eight potential renters:
Renter Rental start date Rental end date Amount of bid ($)
1 May 1 June 1 1800
2 May 1 July 1 3400
3 June 1 July 1 2000
4 June 1 August 1 4000
5 June 1 September 1 4800
6 July 1 August 1 1600
7 July 1 September 1 3200
8 August 1 September 1 1400
A rental starts at 15:00 on the start date, and ends at 12:00 on the end date. As a result, one rental can end and another
rental can start on the same day. However, only one renter can occupy the vacation home at any time.
Beverly wants to identify a selection of bids that would maximize her total revenue. Formulate Beverly’s problem as a
shortest path problem.
7
7 Longest paths and negative cycles
● We saw in the previous example that formulating a shortest path problem with negative edge lengths often makes
sense, especially when a problem is naturally formulated as a longest path problem
Example 8. Find the shortest path from node 1 to node 4 in the following digraph:
2
1 2
3
−10
10
4
3 4
● A cycle in a digraph is a path from a source node s to a target node t plus an arc (t, s)
○ For example: (2, 3), (3, 4), (4, 2) in the digraph above
● Negative cycles make things complicated: if we traverse a negative cycle, we can reduce the cost of getting point
A to point B infinitely
○ Standard shortest path algorithms fail when the digraph has a negative cycle
● Having a negative cycle in your shortest path problem might indicate (i) your problem will be hard to solve, or
(ii) there is a mistake in your formulation!