Algorithms and Data Structure
Algorithms and Data Structure
Structures
Lecture X
Simonas altenis
Nykredit Center for Database
Research
Aalborg University
[email protected]
This Lecture
Dynamic programming
October 21,
Correctness
Concrete and asymptotic running time
Hashing
Binary trees, Red-Black trees
Data structures for secondary storage (B-trees)
October 21,
Iterative algorithms
Divide-and-conquer algorithms
October 21,
October 21,
For example,
MergeSort
The
subproblems are
independent, all
different
October 21,
Merge-Sort(A,
Merge-Sort(A, p,
p, r)
r)
if
p
<
r
then
if p < r then
q(p+r)/2
q(p+r)/2
Merge-Sort(A,
Merge-Sort(A, p,
p, q)
q)
Merge-Sort(A,
Merge-Sort(A, q+1,
q+1, r)
r)
Merge(A,
Merge(A, p,
p, q,
q, r)
r)
Fibonacci Numbers
F0 =0, F1 =1
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
F(4)
F(3)
F(2)
F(1) F(1)
F(0) F(1)
F(2)
F(3)
F(2)
F(1)
F(0)
F(1)
F(2)
F(1) F(1)
F(0)
F(0)
F(0)
October 21,
Fibonacci(n)
Fibonacci(n)
FF00
00
FF11
11
for
for ii
11 to
to nn do
do
FFi
FFi-1 ++ FFi-2
i
i-1
i-2
10
Optimization Problems
11
Multiplying Matrices
...
c
22 ...
ci , j ai ,l bl , j
l 1
12
Costs:
A1 A2 K An , where Ai is a d i 1 d i matrix
October 21,
13
k i
Key observations
October 21,
14
(
n
)
But there are only 2
different
subproblems (i,j), where 1i j n
October 21,
15
Thus, it requires only (n2) space to store the optimal cost M(i,j)
for each of the subproblems: half of a 2d array M[1..n,1..n]
Matrix-Chain-Order(d
Matrix-Chain-Order(d00d
dnn))
11 for
for i1
i1 to
to nn do
do
22
M[i,i]
M[i,i]
33 for
for l2
l2 to
to nn do
do
44
for
for i1
i1 to
to n-l+1
n-l+1 do
do
55
jj i+l-1
i+l-1
66
M[i,j]
M[i,j]
for
for ki
ki to
to j-l
j-l do
do
88
qq M[i,k]+M[k+1,j]+d
dkdj
M[i,k]+M[k+1,j]+di-1
i-1dkdj
99
if
if qq << M[i,j]
M[i,j] then
then
10
M[i,j]
10
M[i,j] q
q
11
c[i,j]
11
c[i,j] k
k
12
12 return
return M,
M, cc
October 21,
16
17
Running time
October 21,
18
Memoization
October 21,
19
Dynamic Programming
Solution to a problem:
October 21,
20
Mopt = Minover all choices k {(Sum of Mopt of all subproblems, resulting from choice k) + (the cost
associated with making the choice k)}
October 21,
21
22
Longest Common
Subsequence
23
LCS: Definition
Z is a subsequence of X, if it is possible
to generate Z by skipping some
(possibly none) characters from X
For example: X =ACGGTTA, Y=CGTAT,
LCS(X,Y) = CGTA or CGTT
To solve LCS problem we have to find
skips that generate LCS(X,Y) from X,
and skips that generate LCS(X,Y) from
Y
October 21,
24
Cut-and-paste argument
October 21,
25
LCS: Reccurence
0
if i 0 or j 0
c[i, j ] c[i 1, j 1] 1
if i, j 0 and xi y j
max{c[i, j 1], c[i 1, j ]} if i, j 0 and x y
i
j
Observe: conditions in the problem restrict subproblems (What is the total number of subproblems?)
October 21,
26
33 for
for j0
j0 to
to nn do
do
44
c[0,j]
c[0,j]
55 for
for i1
i1 to
to mm do
do
66
for
for j1
j1 to
to nn do
do
77
if
if xxii== yyjj then
then
88
c[i,j]
c[i,j] c[i-1,j-1]+1
c[i-1,j-1]+1
99
b[i,j]
b[i,j] copy
copy
10
10 else
else if
if c[i-1,j]
c[i-1,j] c[i,j-1]
c[i,j-1] then
then
11
c[i,j]
11
c[i,j] c[i-1,j]
c[i-1,j]
12
b[i,j]
skipx
12
b[i,j] skipx
13
else
13
else
14
c[i,j]
14
c[i,j] c[i,j-1]
c[i,j-1]
15
b[i,j]
15
b[i,j] skipy
skipy
16
16 return
return c,
c, bb
October 21,
27
LCS: Example
October 21,
28
Next Lecture
Graphs:
Representation in memory
Breadth-first search
Depth-first search
Topological sort
October 21,
29