{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cse101_12_2_11

# cse101_12_2_11 - T[I i-1 = 0 Compute all P ij For d = 0 to...

This preview shows pages 1–2. Sign up to view the full content.

Optimal binary search trees 1. Example Begin 5% Do 40% Else 8% End 4% If10% Then 10% While 23% Instead of: End Do then Begin else if while Expected number of queries = 2.42 Do Begin while If Else then End Expected number of queries = 2.18 2. Given n words (in sorted order) with frequencies p1, p2, pn, find the cost (expected # queries) of the optimal tree. 3. Dynamic programming Get word #2, so 1 2 3 4 5 6 7 ------------------------ <-> --------------- ---------- --  --> a. Subproblem: T(I, j) = cost of optimal tree for words I through j b. Recursive formulation T(I, j) = min of i<=r<=j T(I, r-1) + T(+1, j) + (pi, p i+1 , p j ) R 1, r-1 r+1, j c. Complexity of subproblem T(I, j) is j-i. Solve the simplest one first. d. Code: For I = 1 to n+1:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: T[I, i-1] = 0 Compute all P ij For d = 0 to n-1: For I = 1 to n-d: J = i+d (solve subproblem I, j)) T[I, j] = infinite For r = I to j: If T[I, j] > T[I, r-1] + T[r+1, j] + P ij T[I, j] = T[I, r-1] + T[r+1, j] + P ij 4. Subset sum a. Given intergers x1, xn and integer . is there a subset of xi that adds to t. b. Subproblem: for I <= n and s <=t: T(I, s) = true if some subset of x1, x. .xi adds up to S c. Recursive formulation T(I, s) = T(i-1, s-x) d. In what order? e. Code: For I =0 to n: T[I, 0] = true For s = 1 to 5: T[0 s] – false For I = 1 to n: For s = 1 to t: T[I, s] = t[i-1, s] If s >= xi: T[I, s] = T[I, s] or T[i-1, s-xi] f....
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

cse101_12_2_11 - T[I i-1 = 0 Compute all P ij For d = 0 to...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online