• Non ci sono risultati.

Shortest paths on weighted DAGs

N/A
N/A
Protected

Academic year: 2021

Condividi "Shortest paths on weighted DAGs"

Copied!
21
0
0

Testo completo

(1)

Single Source Shortest Paths for DAGs

Paolo Camurati and Stefano Quer

Dipartimento di Automatica e Informatica

Politecnico di Torino

(2)

Shortest paths on weighted DAGs

 For a DAG the SSSPs problem can be solved with a simplified algorithm

 Shortest paths are always well defined even if there are negative-weight edges

 This is because, obviously, negative-weight cycles

cannot exist in a DAG

(3)

Shortest paths on weighted DAGs

 As there are no cycles it is enough to

 Topologically sort the DAG

 Impose a linear order on the vertices

 Relax all vertices following the sorted order given by the topological sort

 In other words, it suffices to make just one pass over the vertices in the topological sorted order

 As we process a vertex, we relax each edge that leaves the vertex

Perform a DFS computing end-processing times Order vertices using the end-

processing times

(4)

SSSP for DAGs

Pseudo-code

sssp_for_DAGs (G, w, s)

topological sort the vertices of G initialize_single_source (G, s) for each vertex uV

for each vertex v adjacency list of u relax (u, v, w)

Taken in topologically

sorted order

(5)

Example

B C

A

7

3 7

6

D E

3

9 8

S

2

0

∞ ∞

(6)

Solution

B C

A

7

7 6

D E

3

9 8

S

2

A B D E C

1/10

5/8 6/7

3/4 2/9

∞ 0 ∞  6 ∞  7 ∞  3 ∞  8

3

Relaxation order

(A, B)

(A, D)

(A, E)

(B, D)

(B, C)

(D, E)

(D, C)

(E, C)

(7)

Complexity

sssp_for_DAGs (G, w, s)

topological sort the vertices of G initialize_single_source (G, s) for each vertex uV

for each vertex v adjacency list of u relax (u, v, w)

Pseudo-code

Θ (|V|+|E|)

Θ (|V|)

Taken in topological sorted order

Executed E times alltogheter

Θ(1)  Θ (|E|)

Overall running time complexity

T(n) = Θ (|V| + |E|)

(8)

Exercise

 Given the following graph find all shortest paths

starting from vertex A

(9)

Solution

A B E D F G C

1/14

2 4

2 1

1 1

3

0 ∞1 ∞ 3 ∞3 ∞5 ∞4 ∞ 4

2/13

6/9 3/10

7/8 4/5

11/12

5

1

3

(10)

 Given the following graph find all shortest paths starting from vertex A

Exercise

(11)

Solution

A B D F C E H G

1/16

2/15 3/10

4/9

5/6 7/8

11/14

12/13

5

1

4 1 2

2

1

4 3

1

0 ∞ 5 ∞2 ∞ 3 ∞7 ∞8 ∞3 ∞ 6

1

(12)

Longest path on weighted DAG

 Problem intractable on generic weighted graph

 As on a DAG there are no cycles, the problem become computationally feasible

 Topologically sort the DAG

 For all ordered vertices

 Apply the "inverse" relaxation rule starting from that vertex

inverse_relax (u, v, w) {

if (v.dist < u.dist + w(v,u)) { v.dist = u.dist + w(v,u)

v.pred = u }

}

(13)

Example

v

u 6

5 9

v

u 6

5 11

v

u 2

5 9

v

u 2

5 9

Relax Relax

v.dist = 9 u.dist = 5 w(u,v) = 2 v.dist <

u.dist + w(u,v)

Longest path from s to v = longtest path from s to u + edge (u,v)

v.dist =

= u.dist + w(u,v)

= 5 + 2 = 7

v.pred = u

(14)

Example

v

u 6

5 9

v

u 6

5 11

v

u 2

5 9

v

u 2

5 9

Relax Relax

v.dist = 9 u.dist = 5 w(u,v) = 2 v.dist >

u.dist + w(u,v)

Relaxation has no effect v.dist = unchanged

= 6

v.pred = unchanged

(15)

Example

B C

A

7

5 6

D E

2

9 8

S

2

0

0 0

0 0

3

The initial estimate is equal

to zero for all vertices

(16)

Solution

B C

A

7

5 6

D E

2

9 8

S

2

A B D E C

Relaxation order

1/10

5/8 6/7

3/4 2/9

0 06 0714 0223

081728

3

Relaxation order

(A, B)

(A, D)

(A, E)

(B, D)

(B, C)

(D, E)

(D, C)

(E, C)

(17)

Complexity

 As the algorithm for the shortest paths for DAGs

(18)

Exercise

 Given the following graph find all longest paths

starting from vertex A

(19)

Solution

A B E D F G C

1/14

2 4

2 1

1 1

3 2/13

6/9 3/10

7/8 4/5

11/12

5

1 3

0 01 03 05 07 08 06

(20)

 Given the following graph find all longest paths starting from vertex A

Exercise

(21)

Solution

A B D F C E H G

1/16

2/15 3/10

4/9

5/6 7/8

11/14

12/13

5

1

4 1 2

2

1

4 3

1

0 05 06 07 07 08 011 09

1

Riferimenti

Documenti correlati

Cartesian reference system belonging to the triangle

Then we have studied the abundance gradients along the Galactic disk produced by our best cosmological model and their dependence upon several parameters: a threshold in the surface

We hold these truths to be self-evident, that all men are created equal, that they are endowed by their creator with certain inalienable rights, that among these are life, liberty,

On behalf of the International Observatory on Participatory Democracy (IOPD), we congratulate you, especially this year which had 32 nominations of high level.. The award will

Tuttavia, il prezzo predatorio rappresenta un dilemma che ha tradizionalmente sollecitato l’attenzione della comunità antitrust: da un lato la storia e la teoria economica

Without loss of generality we may assume that A, B, C are complex numbers along the unit circle. |z|

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma,

The issue addressed in this work is the determination of the quenched critical point for the localization/delocalization phase transition of a polymer interacting with an