Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm....

56
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times If no negative cycles,then: 1)d(v)= (v) 2)triangle inequality should hold Running time? O(VE).

Transcript of Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm....

Page 1: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Single Source Shortest-Path: The General Case (with negative edges)

Bellman-Ford algorithm.

Iteratively relax all edges |V|-1 times

I f no negative cycles, then:

1) d(v)= (v)

2) triangle inequality should hold

Running time? O(VE).

Page 2: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

COSC 3101N J. Elder

All-Pairs Shortest Paths

Page 3: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

All-Pairs Shortest Paths

Given a directed graph G = (V, E), weight function w : E → R, |V| = n.

Assume no negative weight cycles.

Goal: create an n × n matrix of shortest-path distances δ(u, v).

Could run BELLMAN-FORD once from each vertex: O(V2E)—which is O(V4) if the graph is dense (E = (V2)).

If no negative-weight edges, could run Dijkstra’s algorithm once from each vertex:

O(V E lg V) with binary heap—O(V3 lg V) if dense,

We’ll see how to do in O(V3) in all cases, with no fancy data structure.

Page 4: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

All Pairs Shortest Path – Floyd-Warshall Algorithm

Dynamic programming approach.

Use optimal substructure of shortest paths: Any subpath of a shortest path is a shortest path.

Create a 3-dimensional table:

Let dij(k) –shortest path weight of any path from i to j where

all intermediate vertices are from the set {1,2, …, k}.

Ultimately, we would like to know the values of dij(n).

Page 5: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Computing dij(k)

Base condition: dij(0) = ?

dij(0) = wij.

For k>0: Let p=<vi, . . . , vj> be a shortest path from vertex i to

vertex j with all intermediate vertices in {1,2, …, k}. If k is not an intermediate vertex, then all intermediate

vertices are in {1,2, …, k-1}. If k is an intermediate vertex, then p is composed of 2

shortest subpaths drawn from {1,2, …, k-1}.

Page 6: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Recursive Formulation for dij(k)

Page 7: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Algorithm

Running time = ? O(n3).

Memory required = ? O(n2) (if we drop the superscripts).

Page 8: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Page 9: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Step 1

Page 10: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Step 2

Page 11: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Step 3

Page 12: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Step 4

Page 13: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Step 5

Page 14: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

All-Pairs Shortest Path: Johnson’s Algorithm

Idea: If the graph is sparse (|E|<<|V|2), it pays to run Dijkstra’s algorithm once from each vertex.

O(VE log V) using binary heap, O(V2 log V + V E) using Fibonacci heap.

But Dijkstra’s algorithm does not handle negative edges.

Johnson’s Algorithm: reweight edges to form equivalent graph with non-negative edges.

Floyd-Warshall still has advantages: very simple implementation

no fancy data structures

small constant.

Page 15: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

COSC 3101N J. Elder

The Maximum Network Flow Problem

Page 16: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The Problem

Use a graph to model material that flows through conduits.

Each edge represents one conduit, and has a capacity, which is an upper bound on the flow rate = units/time.

Can think of edges as pipes of different sizes. But flows don’t have to be of liquids.

Want to compute max rate that we can ship material from a designated source to a designated sink.

Page 17: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

What is a Flow Network?

Each edge (u,v) has a nonnegative capacity c(u,v). If (u,v) is not in E, assume c(u,v)=0. We have a source s, and a sink t. Assume that every vertex v in V is on some path from s to

t.

c(s,v1)=16; c(v1,s)=0; c(v2,v3)=0

Page 18: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

What is a Flow in a Network?

For each edge (u,v), the flow f(u,v) is a real-valued function that must satisfy 3 conditions:

Note that skew symmetry condition implies that f(u,u)=0.

C ,apacity , cons (traint: , ) , ) (u v V f u v c u v

Skew symmetry: , , ( , ) ( , )u v V f u v f v u

Flow conservation: { , }, ( , ) 0v V

u V s t f s v

Page 19: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example of a Flow:

f(v2, v1) = 1, c(v2, v1) = 4.

f(v1, v2) = -1, c(v1, v2) = 10.

f(v3, s) + f(v3, v1) + f(v3, v2) + f(v3, v4) + f(v3, t) =

0 + (-12) + 4 + (-7) + 15 = 0

flowcapacity

Page 20: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The Value of a flow

The value of a flow is given by

VvVv

tvfvsff ),(),(||

This it the total flow leaving s = the total flow arriving in t.

Page 21: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example:

|f| = f(s, v1) + f(s, v2) + f(s, v3) + f(s, v4) + f(s, t) =

11 + 8 + 0 + 0 + 0 = 19

|f|= f(s, t) + f(v1, t) + f(v2, t) + f(v3, t) + f(v4, t) =

0 + 0 + 0 + 15 + 4 = 19

Page 22: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

A flow in a network

We assume that there is only flow in one direction at a time.

Sending 7 trucks from Edmonton to Calgary and 3 trucks from Calgary to Edmonton has the same net effect as sending 4 trucks from Edmonton to Calgary.

Page 23: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Multiple Sources Network

We have several sources and several targets. Want to maximize the total flow from all sources to all targets. Reduce to max-flow by creating a supersource and a

supersink:

Page 24: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The Ford-Fulkerson Method

Try to improve the flow, until we reach the maximum. The residual capacity of the network with a flow f is given

by:

),(),(),( vufvucvuc f

Always nonnegative (why?)

Page 25: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example of residual capacities

Network:

Residual Network:

Augmenting path

Page 26: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The residual network

The edges of the residual network are the edges on which the residual capacity is positive.

Page 27: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Augmenting Paths

An augmenting path p is a simple path from s to t on the residual network.

We can put more flow from s to t through p.

We call the maximum capacity by which we can increase the flow on p the residual capacity of p.

}on is ),( :),(min{)( pvuvucpc ff

Page 28: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Augmenting Paths - example

The residual capacity of p = 4. Can improve the flow along p by 4.

Page 29: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Ford-Fulkerson Method

Page 30: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Flow(1)

Residual(1)

Flow(2)

Residual(2)

No more augmenting paths max flow attained.

Page 31: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Augmenting Paths – example

The maximum possible flow through the cut = 12 + 7 + 4 = 23

The network has a capacity of at most 23.

This is called a minimum cut.

Flow(2)

Page 32: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Cuts of Flow Networks

A cut in a network is a partition of V into S and T=V-S so that s is in S and t is in T.

Page 33: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The net flow through a cut (S,T)

f(S,T) = 12 – 4 + 11 = 19

TvSu

vufTSf,

),(),(

Page 34: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The capacity of (S,T)

c(S,T)= 12+ 0 + 14 = 26

TvSu

vucTSc,

),(),(

Page 35: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Lemma 26.5

For any cut (S,T), the net flow across (S,T) is f(S,T)=|f|.

Page 36: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Corollary 26.6

The value of any flow f in a flow network G is bounded from above by the capacity of any cut of G.

Page 37: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Theorem 26.7 (Max-flow min-cut theorem)

If f is a flow in a flow network G=(V,E), with source s and sink t, then the following conditions are equivalent:

1. f is a maximum flow in G.

2. The residual network Gf contains no augmented paths.

3. |f| = c(S,T) for some cut (S,T) (a min-cut).

Page 38: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The Basic Ford-Fulkerson Algorithm

Page 39: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Resulting Flow =

Original Network

augmenting path

4

Page 40: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Resulting Flow = 4

Residual Network

augmenting path

Page 41: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Residual Network

Resulting Flow = 1

1

Page 42: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Resulting Flow = 1

1

Residual Network

augmenting path

Page 43: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Residual Network

Resulting Flow = 1

9

Page 44: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Resulting Flow = 1

9

Residual Network

augmenting path

Page 45: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Example

Residual Network

Resulting Flow = 23

Page 46: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Residual Network

Example

Resulting Flow = 23

No augmenting path:

Maxflow=23

Page 47: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Analysis

O(E)

O(E)

Page 48: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Analysis

If capacities are all integer, then each augmenting path raises |f| by ≥ 1.

If max flow is f*, then need ≤ |f*| iterations ⇒ time is O(E|f*|).

Note that this running time is not polynomial in input size. It depends on |f*|, which is not a function of |V| or |E|.

If capacities are rational, can scale them to integers.

If irrational, FORD-FULKERSON might never terminate!

Page 49: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The basic Ford-Fulkerson Algorithm

With time O ( E |f*|), the algorithm is not polynomial. This problem is real: Ford-Fulkerson may perform very

badly if we are unlucky:

|f*|=2,000,000

Page 50: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Run Ford-Fulkerson on this example

Augmenting Path

Residual Network

Page 51: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Run Ford-Fulkerson on this example

Augmenting Path

Residual Network

Page 52: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Run Ford-Fulkerson on this example

Repeat 999,999 more times…

Page 53: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The Edmonds-Karp Algorithm

A small fix to the Ford-Fulkerson algorithm makes it work in polynomial time.

Specify how to compute the path in line 4.

Page 54: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The Edmonds-Karp Algorithm

Compute the path in line 4 using breadth-first search on residual network.

The augmenting path p is the shortest path from s to t in the residual network (treating all edge weights as 1).

Runs in time O(V E2).

Page 55: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

The Edmonds-Karp Algorithm - example

Edmonds-Karp’s algorithm runs only 2 iterations on this graph.

Page 56: Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).

Further Improvements

Push-relabel algorithm ([CLRS, 26.4]) – O(V2 E). The relabel-to-front algorithm ([CLRS, 26.5) – O(V3).