Algorithms Theory Algorithms Theory 14 Dynamic Programming (3) - - PowerPoint PPT Presentation

algorithms theory algorithms theory 14 dynamic
SMART_READER_LITE
LIVE PREVIEW

Algorithms Theory Algorithms Theory 14 Dynamic Programming (3) - - PowerPoint PPT Presentation

Algorithms Theory Algorithms Theory 14 Dynamic Programming (3) Programming (3) Optimal binary search trees P.D. Dr. Alexander Souza Winter term 11/12 Method of dynamic programming Recursive approach: Solve a problem by solving several


slide-1
SLIDE 1

Algorithms Theory Algorithms Theory 14 – Dynamic Programming (3) Programming (3)

Optimal binary search trees

P.D. Dr. Alexander Souza

Winter term 11/12

slide-2
SLIDE 2

Method of dynamic programming

Recursive approach: Solve a problem by solving several smaller analogous subproblems of the same type. Then combine these l ti t t l ti t th i i l bl solutions to generate a solution to the original problem. Drawback: Repeated computation of solutions Drawback: Repeated computation of solutions Dynamic-programming method: Once a subproblem has been solved, store its solution in a table so that it can be retrieved later by simple table lookup.

2 Winter term 11/12

slide-3
SLIDE 3

Optimal substructure

Dynamic programming is typically applied to

  • ptimization problems.

An optimal solution to the original problem contains ti l l ti t ll b bl

  • ptimal solutions to smaller subproblems.

3 Winter term 11/12

slide-4
SLIDE 4

Construction of optimal binary search trees

(-∞,k1) k1 (k1,k2) k2 (k2,k3) k3 (k3,k4) k4 (k4, ∞) 4 1 3 3 3 10 4 1 0 3 0 3 0 3 10 k2 3 k4 k1 1 3 (-∞, k1) (k1, k2) k3 (k4, ∞) 4 3 10 (

1)

( 1

2) 3

(k k ) (k k ) ( 4 ) (k2, k3) (k3, k4) weighted path length: 3 ⋅ 1 +2 ⋅ (1 + 3) + 3 ⋅ 3 + 2 ⋅ (4 + 10)

4 Winter term 11/12

3 ⋅ 1 +2 ⋅ (1 + 3) + 3 ⋅ 3 + 2 ⋅ (4 + 10)

slide-5
SLIDE 5

Construction of optimal binary search trees

Given: set S of keys Given: set S of keys S = {k1,....kn} -∞ = k0 < k1 < ... < kn< kn+1 = ∞ ai: (absolute) frequency of requests to key ki bj: (absolute) frequency of requests to x ∈ (kj , kj+1) bj: (absolute) frequency of requests to x ∈ (kj , kj+1) Weighted path length P(T) of a binary search tree T for S:

∑ ∑

= + =

+ + =

n j j j j i n i i

b k k depth a k depth T P

1 1

)) , (( ) 1 ) ( ( ) (

Goal: Binary search tree with minimum weighted path length P for S .

5 Winter term 11/12

slide-6
SLIDE 6

Construction of optimal binary search trees

k k 4 2 k1 k2 k2 k1 4 2 2 4 1 5 k1 k2 3 5 5 1 3 3 5 1 3 P(T1) = 21 P(T2) = 27 P(T1) 21 (

2)

6 Winter term 11/12

slide-7
SLIDE 7

Construction of optimal binary search trees

ki ai kj, kj + 1 bj

An optimal binary search tree is a binary search tree with minimum weighted path length.

7 Winter term 11/12

slide-8
SLIDE 8

Construction of optimal binary search trees

P(T) = P(Tl) + W(Tl) + P(Tr) + W(Tr) + ak

k ak T

= P(Tl ) + P(Tr ) + W(T) where W(T) := total weight of all nodes in T

Tl Tr

If T is a tree with minimum weighted path length for S, then subtrees Tl and Tr are trees with minimum weighted path length for subsets of S.

8 Winter term 11/12

slide-9
SLIDE 9

Construction of optimal binary search trees

Let T(i, j): optimal binary search tree for (ki, ki+1) ki+1... kj (kj, kj+1), W(i, j): weight of T(i, j), i.e. W(i, j) = bi + ai+1 + ... + aj + bj , P(i j) i ht d th l th f T(i j) P(i, j): weighted path length of T(i, j).

9 Winter term 11/12

slide-10
SLIDE 10

Construction of optimal binary search trees

kl T(i, j) = T(i, l - 1) T(l, j)

( ki, ki + 1) ki + 1 ..... kl – 1 (kl –1, kl) ( kl, kl + 1) kl + 1 ..... kj (kj, kj + 1)

request

bi ai + 1 al – 1 bl – 1 al bl al + 1 aj bj

frequency:

10 Winter term 11/12

slide-11
SLIDE 11

Construction of optimal binary search trees

W(i, i) = bi , for 0 ≤ i ≤ n W(i, j) = W(i, j – 1) + aj + bj , for 0 ≤ i < j ≤ n P(i, i) = 0 , for 0 ≤ i ≤ n P(i j) W(i j) + i { P(i l 1 ) + P(l j) } f 0 ≤ i < j ≤ (*) P(i, j) = W(i, j) + min { P(i, l –1 ) + P(l, j) }, for 0 ≤ i < j ≤ n (*)

i < l ≤ j

r(i, j) = the index l for which the minimum is achieved in (*)

11 Winter term 11/12

slide-12
SLIDE 12

Construction of optimal binary search trees

Base cases Case 1: s = j – i = 0 T(i, i) = (ki, ki +1) W(i, i) = bi P(i, i) = 0, r(i, i) not defined

12 Winter term 11/12

slide-13
SLIDE 13

Construction of optimal binary search trees

Case 2: s = j – i = 1 Case 2: s j i 1 T(i, i+1)

ki 1 ki+1 k k k k ai+1 ki, ki+1 ki+1, ki+2 bi bi+1

W(i ,i +1) = bi + ai+1 + bi+1 = W(i, i) + ai+1 + W(i+1, i+1) P(i, i+1) = W(i, i + 1) r(i, i +1) = i + 1

13 Winter term 11/12

slide-14
SLIDE 14

Computing the minimum weighted path length using dynamic programming using dynamic programming

Case 3: s = j - i > 1 Case 3: s j i 1 for s = 2 to n do for i = 0 to (n – s) do { j = i + s; d t i ( t t) l i < l ≤ j t P(i l 1) + P(l j) i i i l determine (greatest) l, i < l ≤ j, s.t. P(i, l – 1) + P(l, j) is minimal P(i, j) = P(i, l –1) + P(l, j) + W(i, j); r(i, j) = l; r(i, j) l; }

14 Winter term 11/12

slide-15
SLIDE 15

Construction of optimal binary search trees

Define:

P f l h h i h d i ) ( ⎫

j j i i i

b a b a b j i W j i P K

1 1

  • f

sum : ) , ( for length path weighted min. : ) , (

+ +

⎭ ⎬ ⎫ = =

Then:

⎧ = if j i b ⎩ ⎨ ⎧ + + − =

  • therwise

) , ( ) 1 , ( if ) , ( j j W a j i W j i b j i W

j i

{ }

⎪ ⎨ ⎧ + − + = =

  • therwise

) , ( ) 1 , ( min ) , ( if ) , ( j l P l i P j i W j i j i P

{ }

⎪ ⎩ ⎨ + +

≤ <

  • therwise

) , ( ) 1 , ( min ) , ( ) , ( j l P l i P j i W j

j l i

Computing the solution P(0,n) takes O(n3) time.

15 Winter term 11/12

and requires O(n2) space.

slide-16
SLIDE 16

Construction of optimal binary search trees

Theorem Theorem An optimal binary search tree for n keys and n + 1 intervals with known request frequencies can be constructed in O(n3) time.

16 Winter term 11/12