• No results found

Warshall s Algorithm: Transitive Closure

N/A
N/A
Protected

Academic year: 2021

Share "Warshall s Algorithm: Transitive Closure"

Copied!
10
0
0

Loading.... (view fulltext now)

Full text

(1)

CS 440 Theory of Algorithms /

CS 468 Al ith i Bi i f ti

CS 468 Algorithms in Bioinformatics

Dynamic Programming

Part II

Copyright © 2007 Pearson Addison-Wesley. All rights reserved Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Warshall’s Algorithm: Transitive Closure

• Computes the transitive closure of a relation

• (Alternatively: all paths in a directed graph)

• Example of transitive closure:

• Example of transitive closure:

1 3 3

1

1 1

2 4 0 0 1 0 0 0 1 0

1 1 1 1 1 1

2 4

(2)

Warshall’s Algorithm

• Main idea: a path exists between two vertices i, j, iff

• there is an edge from i to j; or g j;

• there is a path from i to j going through vertex 1; or

• there is a path from i to j going through vertex 1 and/or 2; or

• there is a path from i to j going through vertex 1, 2, and/or 3; or

•... • there is a path from i to j going through any of the other vertices

3 3 3 3

3 1

4

1

4

1

4

1

2 4

1

4

1

2 4 2 4 2 4 2 4

R 0

0 0 1 0 R 1

0 0 1 0 R 2

0 0 1 0 R 3

0 0 1 0 R 4

0 0 1 0

2 4

1 0 0 1

0 0 0 0

0 1 0 0

1 0 11 1

0 0 0 0

0 1 0 0

1 0 1 1

0 0 0 0

1

1 1 1 1 1 1

1 0 1 1

0 0 0 0

1 1 1 1

1 11 1 1

0 0 0 0

1 1 1 1

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-2

Warshall’s Algorithm

• On the k th iteration, the algorithm determine if a path exists , g p

between two vertices i, j using just vertices among 1,…,k allowed

as intermediate

R (k-1) [ i,j] (path using just 1 ,…,k-1)

R (k) [ i j] = { or

R (k) [ i,j] = or

( R (k-1) [ i,k] and R (k-1) [ k,j]) (path from i to k

and from k to i

{ k

using just 1 ,…, k-1)

i

k

k th iteration

j

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-3

j

(3)

Warshall’s Algorithm: Transitive Closure

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-4

Warshall’s Algorithm (matrix generation)

Recurrence relating elements R ( k) to elements of R ( k-1) is:

R ( k) [ i,j] = R ( k-1) [ i,j] or (R ( k-1) [ i,k] and R ( k-1) [ k,j])

It implies the following rules for generating R ( k) from R ( k-1) :

Rule 1 If an element in row i and column j is 1 in R ( k-1) ,

it remains 1 in R ( k)

Rule 2 If an element in row i and column j is 0 in R ( k-1) ,

(4)

Warshall’s Algorithm: Transitive Closure

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-6

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-7

(5)

Warshall’s Algorithm (pseudocode and analysis)

Time efficiency: ( n 3 )

Space efficiency: Matrices can be written over their predecessors

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

p y p

Floyd’s Algorithm: All pairs shortest paths

Problem: In a weighted (di)graph, find shortest paths between

every pair of vertices

Same idea: construct solution through series of matrices D (0)

Same idea: construct solution through series of matrices D ( ) , …,

D ( n) using increasing subsets of the vertices allowed

as intermediate

• Example: 3

1

4

1

1

6 1

5

(6)

Floyd’s Algorithm (matrix generation)

On the k-th iteration, the algorithm determines shortest paths

between every pair of vertices i j that use only vertices among

between every pair of vertices i, j that use only vertices among

1,…, k as intermediate

D ( k) [ i,j] = min {D ( k-1) [ i,j], D ( k-1) [ i,k] + D ( k-1) [ k,j]}

D ( k-1) [ i,k] k

i

D ( k-1) [ k,j]

j

D ( k-1) [ i,j]

[ ,j]

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

j

Floyd’s Algorithm: All pairs shortest paths

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-11

(7)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-12

Floyd’s Algorithm (pseudocode and analysis)

Time efficiency: ( e e c e cy: ( n 3 ) )

(8)

Knapsack Problem by DP

Given n items of

integer weights: w w w

integer weights: w 1 w 2 … w n

values: v 1 v 2 … v n

a knapsack of integer capacity W

find most valuable subset of the items that fit into the knapsack

Consider instance defined by first i items and capacity j (j d W).

Let V[i,j] be optimal value of such instance. Then

max { V[i-1,j], v i + V[i-1,j- w i ]} if j- w i t 0

V[i j] =

V[i,j] =

V[i-1,j] if j- w i < 0

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Initial conditions: V[0,j] = 0 and V[i,0] = 0

Knapsack Problem by DP (example)

Example: Knapsack of capacity W = 5

item weight value

item weight value

1 2 $12

2 1 $10

2 1 $10

3 3 $20

4 2 $15 capacity p y j j

0 1 2 3 4 5

0

w 1 = 2, v 1 = 12 1

w 2 2 = 1, , v 2 2 = 10 2

w 3 = 3, v 3 = 20 3

w 4 = 2 v 4 = 15 4 ?

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

w 4 2, v 4 15 4 ?

(9)

Knapsack Problem

• V[i, j] = max (V[i – 1, j], V[i – 1, j – w i ] + v i )

object i not used object i used

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-16

Knapsack Problem

• V[i, j] = max (V[i – 1, j], V[i – 1, j – w i ] + v i )

object i not used object i used

(10)

Knapsack Problem – Memory Function

• Implement the recurrence recursively

• Do not calculate a value if it is not needed

• Do not calculate a value if it is not needed

• Do not recalculate a value

• Row 0 and column 0 of V are initialized to 0, other entries are -1

• MFKnapsack(i, j)

if V[i, j] < 0

if V[i, j] 0

if j < w[i]

value Å MFKnapsack(i – 1, j)

l

else

value Å max (MFKnapsack(i – 1, j),

MFKnapsack(i – 1, j – w[i]) + v[i]) p ( , j [ ]) [ ])

V[i, j] Å value

return V[i, j]

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-18

Knapsack Problem – Memory Function

Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8 8-19

References

Related documents

E.g., files being processed (in Elan, Toolbox or Flex) and ancillary files (.typ, .prj, etc) that are only necessary while working to create the..

8 Please bring (if applicable) copies of your child’s report cards, multi-factored evaluations/ETR, IEP, 504 Plan, cognitive testing (Iowa Test, SCOGAT, Stanford Tests,

Analizamos a ontinua ión el omportamiento de la traye toria solu ión del modelo de. red neuronal

Immediately after the fire, available Fe content decreased in burnt soils (either treated or not with fire-fighting chemicals) compared with the unburnt soils, although

1977 - La Unión Internacional para la Conservación de la Naturaleza y sus Recursos; 1977 - La Unión Internacional para la Conservación de la Naturaleza y sus

I f you have complaints about the work or a dispute that you can’t resolve with your contractor through mediation, you can file a complaint with the MHIC against a

Throughout their travels they had agreed that both Cracow and Vienna could be possible locations where the RF could assist with the implementation of the two year bifurcating