SINGLE-SOURCE SHORTEST PATHS - PowerPoint PPT Presentation

About This Presentation
Title:

SINGLE-SOURCE SHORTEST PATHS

Description:

SINGLE-SOURCE SHORTEST PATHS Single-Source Shortest Paths in DAGs Shortest paths are always well-defined in dags no cycles = no negative-weight cycles even if there ... – PowerPoint PPT presentation

Number of Views:205
Avg rating:3.0/5.0
Slides: 44
Provided by: UmutT
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: SINGLE-SOURCE SHORTEST PATHS


1
SINGLE-SOURCE SHORTEST PATHS
2
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.

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

4
Example
  • A shorter path will cost only 11

5
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
6
Shortest-Path Variants (Contd)
  • 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
7
Shortest-Path Variants
No need to consider different solution or
algorithm for each variant (we reduce it into
two types)
  • 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.

8
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)
9
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)
10
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

11
Shortest Path
  • Shortest Path Path of minimum weight
  • d(u,v)

p
min?(p) u v if there is a path from u
to v, ? otherwise.
12
Several Properties
13
1- Optimal Substructure Property
  • Theorem Subpaths of shortest paths are also
    shortest paths
  • Let d(1,k) ltv1, ..i, .. j.., .. ,vk gt be a
    shortest path from v1 to vk
  • Let d(i,j) ltvi, ... ,vj gt be subpath of d(1,k)
    from vi to vj
  • for any i, j
  • Then d(I,j) is a shortest path from vi to vj

14
1- Optimal Substructure Property
  • 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

v5
v7
v1
v4
v6
v3

v0
v2
15
2- No Cycles in Shortest Path
  • Definition
  • d(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


cycle lt 0
s
v
16
3- Negative-weight edges
  • No problem, as long as no negative-weight cycles
    are reachable from the source (allowed)

17
4- Triangle Inequality
  • Lemma 1 for a given vertex s in V and for every
    edge (u,v) in E,
  • d(s,v) d(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)

u
v
s
18
Algorithms to Cover
  • Dijkstras Algorithm
  • Shortest Path is DAGs

19
Initialization
  • Maintain dv for each v in V
  • dv is called shortest-path weight estimate
  • and it is upper bound on d(s,v)

INIT(G, s) for each v ? V do dv ?
8 pv ? NIL ds ? 0
Upper bound
Parent node

20
Relaxation
RELAX(u, v) if dv gt duw(u,v)
then dv ? duw(u,v)
pv ? u
When you find an edge (u,v) then check this
condition and relax dv if possible
u
v
u
v
2
2
5
5
9
6
Change dv
No chnage

5
7
5
6
2
2
v
u
v
u
21
Algorithms to Cover
  • Dijkstras Algorithm
  • Shortest Path is DAGs

22
Dijkstras Algorithm For Shortest Paths
  • 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 dv values

23
Dijkstras Algorithm For Shortest Paths
  • DIJKSTRA(G, s)
  • INIT(G, s)
  • S?Ø gt set of discovered nodes
  • Q?VG
  • while Q ?Ø do
  • u?EXTRACT-MIN(Q)
  • S?S U u
  • for each v in Adju do
  • RELAX(u, v) gt May cause
  • gt DECREASE-KEY(Q, v, dv)

24
Example Initialization Step
25
Example
3
26
Example
2
27
Example
2
28
Example
10
29
Example
30
Example
u
5
31
Dijkstras 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)
32
Algorithms to Cover
  • Dijkstras Algorithm
  • Shortest Path is DAGs

33
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

34
Single-Source Shortest Paths in DAGs
  • Shortest paths are always well-defined in dags
  • no cycles gt 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

35
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 Adju do
  • RELAX(u, v)

36
Example
6
1
u
r
t
s
v
w
5
2
7
2
1
?
0
?
?
?
?
4
3
2
37
Example
6
1
u
r
t
s
v
w
5
2
7
2
1
?
0
?
?
?
?
4
3
2
38
Example
6
1
u
r
t
s
v
w
5
2
7
2
1
?
0
2
6
?
?
4
3
2
39
Example
6
1
u
r
t
s
v
w
5
2
7
2
1
?
0
2
6
6
4
4
3
2
40
Example
6
1
u
r
t
s
v
w
5
2
7
2
1
?
0
2
6
5
4
4
3
2
41
Example
6
1
u
r
t
s
v
w
5
2
7
2
1
?
0
2
6
5
3
4
3
2
42
Example
6
1
u
r
t
s
v
w
5
2
7
2
1
?
0
2
6
5
3
4
3
2
43
Single-Source Shortest Paths in DAGs Analysis
O(VE)
  • 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 Adju do
  • RELAX(u, v)

O(V)
Total O(E)
Time Complexity O (V E)
Write a Comment
User Comments (0)
About PowerShow.com