0% found this document useful (0 votes)
99 views10 pages

Cs 440 Theory of Algorithms / Cs 468 Al Ith Ibiif Ti Cs 468 Algorithms in Bioinformatics

chapter 8

Uploaded by

Praveen Dhanush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views10 pages

Cs 440 Theory of Algorithms / Cs 468 Al Ith Ibiif Ti Cs 468 Algorithms in Bioinformatics

chapter 8

Uploaded by

Praveen Dhanush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CS 440 Theory of Algorithms /

CS 468 Al
Algorithms
ith
iin Bi
Bioinformatics
i f
ti
Dynamic Programming
Part II

Copyright 2007 Pearson Addison-Wesley. All rights reserved


Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Warshalls Algorithm: Transitive Closure


Computes the transitive closure of a relation
(Alternatively: all paths in a directed graph)
Example of transitive closure:

0
1
0
0

0
0
0
1

1
0
0
0

0
1
0
0

Copyright 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8

0
1
0
1

0
1
0
1

1
1
0
1

0
1
0
1

8-1

Warshalls Algorithm
Main idea: a path exists between two vertices i, j, iff
there is an edge
g from i to j; or
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
1
1
1
1

2
0
1
0
0

R0
0 1
0 0
0 0
1 0

2
R1
0 0
1 0
0 0
0 1

0
1
0
0

4
1
1
0
0

0
1
0
0

R2
0 0
1 0
0 0
1 1

1
1
0
1

Copyright 2007 Pearson Addison-Wesley. All rights reserved

0
1
0
1

2
R3
0 0
1 0
0 0
1 1

1
1
0
1

0
1
0
1

Design and Analysis of Algorithms - Chapter 8

2
R4
0 0
1 1
0 0
1 1

1
1
0
1

0
1
0
1
8-2

Warshalls Algorithm
On the kth iteration,, the algorithm
g
determine if a p
path exists
between two vertices i, j using just vertices among 1,,k allowed
as intermediate

R(k)[i,j]
[i j] =

R(k-1)[i,j]
(path using just 1 ,,k-1)
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)
kth iteration
j

Copyright 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8

8-3

Warshalls Algorithm: Transitive Closure

Copyright 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8

Warshalls 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),
it has to be changed to 1 in R(k) if and only if
the element in its row i and column k and the element
in its column j and row k are both 11ss in R(k-1)
Copyright 2007 Pearson Addison-Wesley. All rights reserved

8-4

Warshalls 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

Warshalls Algorithm (pseudocode and analysis)

Time efficiency: (n3)


p
efficiency:
y Matrices can be written over their p
predecessors
Space
Copyright 2007 Pearson Addison-Wesley. All rights reserved

Floyds 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)), ,


D(n) using increasing subsets of the vertices allowed
as intermediate
Example:

6
1

Copyright 2007 Pearson Addison-Wesley. All rights reserved

4
Design and Analysis of Algorithms - Chapter 8

8-9

Floyds Algorithm (matrix generation)


On the k-th iteration, the algorithm determines shortest paths
between every pair of vertices i,
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]

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

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

Copyright 2007 Pearson Addison-Wesley. All rights reserved

Floyds Algorithm: All pairs shortest paths

Copyright 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8

8-11

Copyright 2007 Pearson Addison-Wesley. All rights reserved

Design and Analysis of Algorithms - Chapter 8

8-12

Floyds Algorithm (pseudocode and analysis)

Timee efficiency:
e c e cy: (
(n3)
Space efficiency: Matrices can be written over their predecessors
Copyright 2007 Pearson Addison-Wesley. All rights reserved

Knapsack Problem by DP
Given n items of
integer weights:
values:

w1 w2 wn
v1 v2 vn

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], vi + V[i-1,j- wi]} if j- wi t 0
V[i j] =
V[i,j]
V[i-1,j]
if j- wi < 0
Initial conditions: V[0,j] = 0 and V[i,0] = 0
Copyright 2007 Pearson Addison-Wesley. All rights reserved

Knapsack Problem by DP (example)


Example: Knapsack of capacity W = 5
item weight
value
1
2
$12
2
1
$10
3
3
$20
4
2
$15
capacity
p
yj
0 1 2 3 4
0
w1 = 2, v1= 12
w2 = 1,, v2= 10

w3 = 3, v3= 20

1
2
3

w4 = 2,
2 v4= 15 4
Copyright 2007 Pearson Addison-Wesley. All rights reserved

Knapsack Problem
V[i, j] = max (V[i 1, j], V[i 1, j wi] + vi)
objecti not used

Copyright 2007 Pearson Addison-Wesley. All rights reserved

objecti used

Design and Analysis of Algorithms - Chapter 8

8-16

Knapsack Problem
V[i, j] = max (V[i 1, j], V[i 1, j wi] + vi)
objecti not used

Copyright 2007 Pearson Addison-Wesley. All rights reserved

objecti used

Design and Analysis of Algorithms - Chapter 8

8-17

Knapsack Problem Memory Function


Implement the recurrence recursively
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 j < w[i]
value MFKnapsack(i 1, j)
else
l
value max (MFKnapsack(i 1, j),
MFKnapsack(i
p
( 1,, j w[i])
[ ]) + v[i])
[ ])
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

You might also like