43
SINGLE-SOURCE SHORTEST PATHS

SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Embed Size (px)

Citation preview

Page 1: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

SINGLE-SOURCE SHORTEST PATHS

Page 2: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Shortest Path Problems• Directed weighted graph.

• Path length is sum of weights of edges on path.

• The vertex at which the path begins is the source vertex.

• The vertex at which the path ends is the destination vertex.

0

3 9

5 11

3

6

57

6

s

t x

y z

22 1

4

3

Page 3: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

• Consider Source node 1 and destination node 7• A direct path between Nodes 1 and 7 will cost 14

Page 4: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

• A shorter path will cost only 11

Page 5: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Shortest-Path Variants

• Single-source single-destination (1-1): Find the shortest path from source s to destination v.

• Single-source all-destination(1-Many): Find the shortest path from s to each vertex v.

• Single-destination shortest-paths (Many-1): Find a shortest path to a given destination vertex t from each vertex v.

• All-pairs shortest-paths problem (Many-Many): Find a shortest path from u to v for every pair of vertices u and v.

We talk about directed, weighted graphs

Page 6: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Shortest-Path Variants (Cont’d)

• For un-weighted graphs, the shortest path problem is mapped to BFS– Since all weights are the same, we search for the smallest number of edges

BFS creates a tree called BFS-Tree

Page 7: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Shortest-Path Variants

• Single-source single-destination (1-1): Find the shortest path from source s to destination v.

• Single-source all-destination(1-Many): Find the shortest path from s to each vertex v.

• Single-destination shortest-paths (Many-1): Find a shortest path to a given destination vertex t from each vertex v.

• All-pairs shortest-paths problem (Many-Many): Find a shortest path from u to v for every pair of vertices u and v.

No need to consider different solution or algorithm for each variant

(we reduce it into two types)

Page 8: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Shortest-Path Variants

Single-Source Single-Destination (1-1)

-No good solution that beats 1-M variant -Thus, this problem is mapped to the 1-M variant

Single-Source All-Destination (1-M)

-Need to be solved (several algorithms)-We will study this one

All-Sources Single-Destination (M-1)

-Reverse all edges in the graph -Thus, it is mapped to the (1-M) variant

All-Sources All-Destinations (M-M)

-Need to be solved (several algorithms)-We will study it (if time permits)

Page 9: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Shortest-Path Variants

Single-Source Single-Destination (1-1)

-No good solution that beats 1-M variant -Thus, this problem is mapped to the 1-M variant

Single-Source All-Destination (1-M)

-Need to be solved (several algorithms)-We will study this one

All-Sources Single-Destination (M-1)

-Reverse all edges in the graph -Thus, it is mapped to the (1-M) variant

All-Sources All-Destinations (M-M)

-Need to be solved (several algorithms)-We will study it (if time permits)

Page 10: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Introduction

Generalization of BFS to handle weighted graphs

• Direct Graph G = ( V, E ), edge weight fn ; w : E → R

• In BFS w(e)=1 for all e E

Weight of path p = v1 v2 … vk is

1

11

( ) ( , )k

i ii

w p w v v

Page 11: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Shortest Path

Shortest Path = Path of minimum weight

δ(u,v)=

min{ω(p) : u v}; if there is a path from u to v,

otherwise.

p

Page 12: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Several Properties

Page 13: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

1- Optimal Substructure Property

Theorem: Subpaths of shortest paths are also shortest paths

• Let δ(1,k) = <vv11, ..i, .. j.., .. ,, ..i, .. j.., .. ,vvkk > be a shortest path from vv11 to vvkk

• Let δ(i,j) = <vvii, ... ,, ... ,vvjj > be subpath of δ(1,k) from vvii to vvjj

for any i, j • Then δ(I,j) is a shortest path from vvii to vvjj

Page 14: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Proof: By cut and paste

• If some subpath were not a shortest path

• We could substitute a shorter subpath to create a shorter total path

• Hence, the original path would not be shortest path

vv22 vv33 vv44vv55 vv66 vv77

1- Optimal Substructure Property

vv11vv00

Page 15: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Definition:

• δ(u,v) = weight of the shortest path(s) from u to v

• Negative-weight cycle: The problem is undefined– can always get a shorter path by going around the cycle again

• Positive-weight cycle : can be taken out and reduces the cost

s v

cycle< 0

2- No Cycles in Shortest Path

Page 16: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

3- Negative-weight edges

• No problem, as long as no negative-weight cycles are reachable from the source (allowed)

Page 17: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

4- Triangle Inequality

Lemma 1: for a given vertex s in V and for every edge (u,v) E,

• δ(s,v) ≤ δ(s,u) + w(u,v)

Proof: shortest path s v is no longer than any other path.

• in particular the path that takes the shortest path s v and

then takes cycle (u,v)

s

u v

Page 18: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Algorithms to Cover

• Dijkstra’s Algorithm

• Shortest Path is DAGs

Page 19: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Initialization

• Maintain d[v] for each v in V • d[v] is called shortest-path weight estimate

and it is upper bound on δ(s,v)

INIT(G, s)

for each v V do

d[v] ← ∞

π[v] ← NIL

d[s] ← 0

Parent node

Upper bound

Page 20: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Relaxation

RELAX(u, v)

if d[v] > d[u]+w(u,v) then

d[v] ← d[u]+w(u,v)

π[v] ← u

u v

vu

2

2

Change d[v]

u v

vu

2

2

When you find an edge (u,v) then check this condition and relax d[v] if possible

No chnage

Page 21: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Algorithms to Cover

• Dijkstra’s Algorithm

• Shortest Path is DAGs

Page 22: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

• Non-negative edge weight

• Like BFS: If all edge weights are equal, then use BFS, otherwise use this algorithm

• Use Q = min-priority queue keyed on d[v] values

Dijkstra’s Algorithm For Shortest Paths

Page 23: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

DIJKSTRA(G, s) INIT(G, s)

S←Ø > set of discovered nodes Q←V[G]

while Q ≠Ø do u←EXTRACT-MIN(Q)

S←S U {u} for each v in Adj[u] do

RELAX(u, v) > May cause> DECREASE-KEY(Q, v,

d[v])

Dijkstra’s Algorithm For Shortest Paths

Page 24: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example: Initialization Step

Page 25: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

3

s

u v

yx

10

5

1

2 94 6

7

2

Page 26: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

2

s

u v

yx

10

5

1

39

4 6

7

2

Page 27: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

2

s

yx

10

5

1

3 94 6

7

2

u v

Page 28: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

10

s

u v

yx

5

1

2 3 94 6

7

2

Page 29: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

u v

yx

10

5

1

2 3 94 6

7

2

s

Page 30: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Exampleu

5

v

yx

10

1

2 3 94 6

7

2

s

Page 31: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Dijkstra’s Algorithm Analysis

O(V)

O(V Log V)

Total in the loop: O(V Log V)

Total in the loop: O(E Log V)

Time Complexity: O (E Log V)

Page 32: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Algorithms to Cover

• Dijkstra’s Algorithm

• Shortest Path is DAGs

Page 33: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Key Property in DAGs

• If there are no cycles it is called a DAG

• In DAGs, nodes can be sorted in a linear order such that all edges are forward edges– Topological sort

Page 34: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Single-Source Shortest Paths in DAGs

• Shortest paths are always well-defined in dags no cycles => no negative-weight cycles even if

there are negative-weight edges

• Idea: If we were lucky To process vertices on each shortest path from left

to right, we would be done in 1 pass

Page 35: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Single-Source Shortest Paths in DAGs

DAG-SHORTEST PATHS(G, s) TOPOLOGICALLY-SORT the vertices of G INIT(G, s) for each vertex u taken in topologically sorted order do for each v in Adj[u] do

RELAX(u, v)

Page 36: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

0 r s t u v w

5 2 7 –1 –2

6 1

32

4

Page 37: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

0 r s t u v w

5 2 7 –1 –2

6 1

32

4

Page 38: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

0 2 6 r s t u v w

5 2 7 –1 –2

6 1

32

4

Page 39: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

0 2 6 6 4

r s t u v w5 2 7 –1 –2

6 1

32

4

Page 40: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

0 2 6 5 4

r s t u v w5 2 7 –1 –2

6 1

32

4

Page 41: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

0 2 6 5 3

r s t u v w5 2 7 –1 –2

6 1

32

4

Page 42: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Example

0 2 6 5 3

r s t u v w5 2 7 –1 –2

6 1

32

4

Page 43: SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the

Single-Source Shortest Paths in DAGs: Analysis

DAG-SHORTEST PATHS(G, s) TOPOLOGICALLY-SORT the vertices of G INIT(G, s) for each vertex u taken in topologically sorted order do for each v in Adj[u] do

RELAX(u, v)

O(V+E)

O(V)

Total O(E)

Time Complexity: O (V + E)