ALGORITHM TYPES - PowerPoint PPT Presentation

About This Presentation
Title:

ALGORITHM TYPES

Description:

ALGORITHM TYPES Greedy, Divide and Conquer, Dynamic Programming, and Backtracking Note the general strategy from the examples The classification is neither exhaustive ... – PowerPoint PPT presentation

Number of Views:296
Avg rating:3.0/5.0
Slides: 58
Provided by: Debasi3
Learn more at: https://cs.fit.edu
Category:

less

Transcript and Presenter's Notes

Title: ALGORITHM TYPES


1
ALGORITHM TYPES
  • Divide and Conquer, Dynamic Programming, Greedy,
    and Backtracking
  • Note the general strategy from the examples
  • The classification is neither exhaustive (there
    may be other types), nor mutually exclusive
    (one may combine)

2
DYNAMIC PROGRAMMING STRATEGY BIG PICTURE
  • Divide and conquer Solves a problem top-down
  • Dynamic Programming Bottom-up
  • DP starts with Recurrence Eq.
  • Recursive with possibility of repeatedly
    computing same
  • Compute the smaller components and keep combining
    them
  • Then, bottom up approach
  • Save and reuse intermediate results in a data
    structure
  • Space traded off for time

3
PROBLEM 0 DYNAMIC PROGRAMMING STRATEGY
  • Bottom up approach for computing a Recurrence
  • Save and reuse intermediate results in a data
    structure
  • Space traded off for time
  • Fibonacci-series calculation
  • f(n) f(n-1) f(n-2), for ngt1 f(0)f(1)1
  • n 1, 2, 3, 4, 5
  • fib 1, 2, 3, 5, 8
  • Draw the recursion tree of Fibonacci-series
    calculation, you will see example of such
    repetitive calculations

4
DYNAMIC PROGRAMMING STRATEGY (Continued)
  • Recursive fibonacci(n)
  • if (nlt1) return 1
  • else return (fibonacci(n-1) fibonacci(n-2)).
  • Time complexity exponential, O(kn) for some
    kgt1.0
  • Iterative fibonacci(n)
  • fib(0) fib(1) 1
  • for i2 through n do
  • fib(i) fib(i-1) fib(i-2)
  • end for
  • return fib(n).
  • Time complexity O(n), Space complexity O(n)

5
DYNAMIC PROGRAMMING STRATEGY (Continued)
  • SpaceSaving_fibonacci(n)
  • if (nlt1) return 1
  • int last1, last2last1, result1
  • for i2 through n do
  • result last last2last
  • last2last last
  • lastresult
  • end for
  • return result.
  • Time complexity O(n), Space complexity O(1)

6
DYNAMIC PROGRAMMING STRATEGY (Continued)
  • In fib(5) recursive call recalculates fib(1) 5
    times, fib(2) 3 times, fib(3) 2 times. The
    complexity is exponential.
  • Iterative calculation avoids repetition stores
    the needed values in variables - complexity
    linear O(n)
  • Dynamic Programming typically consumes more
    memory to store the result of calculations of
    lower levels for the purpose of calculating the
    next higher level.
  • Intermediate results are typically stored in a
    table.

7
Problem 2 0-1 Knapsack Problem (not in the book)
  • Given a set of objects with (Weight, Profit)
    pair, and a Knapsack of limited weight capacity
    (M), find a subset of objects for the knapsack to
    maximize total profit P
  • Sample Problem
  • Objects (wt, p) (2, 1), (3, 2), (9, 20), (5,
    3). M8
  • Exhaustive Algorithm Try all subset of objects.
  • How many?

total(0 lbs, 0) (2,1) total (2lbs,
1) (9,20) Illegal, total wtgt8
(2,1), (3,2) (5lbs, 3)
Total possibilities 2n For each object
present or absent 0 or 1 A bit string is a
good representation for a set, e.g.
000101100. n-bits
8
0-1 Knapsack Problem
  • Formulate a recurrence for optimal profit P(j,
    k)
  • for the first j objects considered
  • (in any arbitrary but pre- ordering of objects)
  • with a variable knapsack limit k
  • Develop the table for P(j, k)
  • with rows j 1 n (objects), and
  • for each row, go from k 0 M (the KS limit)
  • Finally P(n, M) gets the result

P(j, k) k ? j
w-gt 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0
O1 0 0 1 1 1 gt 1
O1 O2 0 0 1 2 2 3 3 gt 3
O1 O2 O3 0 0 1 2 2 3 3 4 5
9
0-1 Knapsack Recurrence Formula for computing P
  • The recurrence, for all k0M, j0N
  • P(j, k) (case 1) P(j-1, k),
  • if wjgtk, wj is weight of the j-th object
  • else
  • P(j, k) (case2) max P(j-1, k-wj)pj,
  • P(j-1, k)
  • Explanation for the formula is quite intuitive
  • Recurrence terminates
  • P(0, k)0, P(j, 0)0, for all ks and js

Objects (wt, p) (2, 1), (3, 2), (5, 3).
M9
w-gt 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0
O1 0 0 1 1 1 1 1 1 1
O1 O2 0 0 1 2 2 3 3 3 3
O1 O2 O3 0 0 1 2 2 P(3,7)4
10
Recurrence -gt recursive algorithm
  • Input Set of n objects with (wi, pi) pairs, and
    knapsack limit M
  • Output Maximum profit P for subset of objects
    with total wt M
  • Function P(j, k)
  • If j 0 or k 0 then return 0 //recursion
    termination
  • else
  • if wjgtk then
  • return P(j-1, k) // recursive call
  • else
  • return maxP(j-1, k-Wj) pj, P(j-1, k) //
    recursive call
  • End algorithm.
  • Driver call P(n, M), for given n objects KS
    limit M.
  • This is not table building Ps are recursive
    functions

Complexity ?
Exponential O(2n)
Why?
Same reason as in Fibonacci number
calculation Repeats computing intermediate Ps
11
0-1 Knapsack Recursive call tree
  • Objects (wt, p) (2, 1), (3, 2), (5, 3).
    M8

P(3,8)
P(2,8-5)
P(2,8)
P(1,8-3)
P(1,3-3)
P(1,8)
P(1,3)
w1 2gt0, not called
P(0,8)
P(0,3-2)
P(0,3)
P(0,5-2)
P(0,5)
P(0,8-2)
P(0,0-2)
P(0,0)
Each leaf has w0, and P(0,) returns a value, 0
to the caller above
w-gt 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0
O1 0 0 1 1 1 gt 1
O1 O2 0 0 1 2 2 3 3 gt 3
O1 O2 O3 0 0 1 2 2 3 3 4 5
12
0-1 Knapsack recurrence -gt DP algorithm
Do not repeat the same computation, store them in
a table and reuse
  • Input Set of n objects with (wi, pi) pairs, and
    knapsack limit M
  • Output Maximum profit P for a subset of objects
    with total_wt M
  • Algorithm DPks(j, k)
  • For all k, P(0, k) 0 For all j, P(j, 0) 0 //
    initialize MATRIX P(j, k)
  • For j1 to n do
  • For k 1 to M do
  • if wjgtk then
  • P(j, k) P(j-1, k)
  • else
  • P(j, k) maxP(j-1, k-Wj) pj, P(j-1, k)
  • End loops and algorithm

Complexity O(nM), pseudo-polynomial because M is
an input value. If M30.5 the table would be of
size O(10nM), or if M30.54 it would be O(100NM).
13
0-1 Knapsack Problem (Example)
  • Objects (wt, p) (2, 1), (3, 2), (5, 3).
    M8

w-gt 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0
O1 0 0 1 1 1 gt 1
O1 O2 0 0 1 2 2 3 3 gt 3
O1 O2 O3 0 0 1 2 2 3 3 4 5
2
0
-5 lbs
(23)
14
What if M9
  • Objects (wt, p) (2, 1), (3, 2), (5, 3).
    M9

w-gt 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
O1 0 0 1 1 1 1 1 1 1 1
O1 O2 0 0 1 2 2 3 3 3 3 3
O1 O2 O3 0 0 1 2 2 3 3 4 5 5
-5 lbs
(23)
HOW TO FIND KNAPSACK CONTENT FROM TABLE? SPACE
COMPLEXITY?
15
Memoisation algorithm 0-1 knapsack
  • Algorithm P(j, k)
  • If j lt 0 or k lt 0 then return 0 //
    recursion termination
  • else
  • if Wjgtk then
  • yA(j-1, k) if ylt0 y P(j-1, k) A(j-1,
    k)y
  • // P( ) is a recursive call, A( ) is matrix
  • return y
  • else
  • xA(j-1, k-Wj) if xlt0 x P(j-1, k-Wj)
    A(j-1, k-Wj)x
  • yA (j-1, k) if ylt0 y P(j-1, k) A(j-1,
    k)y
  • A (j, k) maxx pj, y
  • return maxx pj, y
  • End algorithm.
  • Driver Initialize global matrix A(0-gtn, 0-gtM)
    -1 call P(n, M)
  • Complexity best of the both recursive and DP
    algorithms, but ?

16
Problem 2 Ordering of Matrix-chain
Multiplications
  • A chain of matrices to be multiplied ABCD,
  • dimensions A (5x1), B(1x4), C(4x3), and D(3x6).
  • Resulting matrix will be of size (5x6).
  • scalar (or integer) multiplications for (BC) is
    1.4.3,
  • and the resulting matrixs dimension is (1x3), 1
    column and 3 rows.

17
Ordering of Matrix-chain Multiplications
  • A (5x1), B(1x4), C(4x3), and D(3x6)
  • Multiple ways to multiply
  • (A(BC))D,
  • ((AB)C)D,
  • (AB)(CD),
  • A(B(CD)),
  • A((BC)D)
  • note Resulting matrix would be the same
  • but the computation time may vary drastically
  • Time depends on scalar multiplications
  • In the case of (A(BC))D, it is 1.4.3 5.1.3
    5.3.6 117
  • In the case (A(B(CD)), it is 4.3.6 1.4.6
    5.1.6 126
  • Our problem here is to find the best such
    ordering
  • An exhaustive search over all orderings is too
    expensive - Catalan number involving n!

18
Recurrence for Ordering of Matrix-chain
Multiplications
  • For a sequence A1...(Aleft....Aright)...An, we
    want to find the optimal break point for the
    parenthesized sequence.
  • Calculate for ( r-l1) number of cases and find
    the minimum min(Al...Ai) (Ai1... Ar), with l?
    i lt r,
  • Recurrence for optimum scalar-multiplication
  • M(l, r)
  • minM(l, i) M(i1, r) rowl .coli .colr,
  • with l? i lt r.
  • Termination M(l, l) 0

r 1 2 3 4 lft 1 0 15 35(2) 69(2) 2 0 12
42 3 0 24 4 0
Sample M(i,j)
19
Matrix-chain Recursive algorithm
  • Recursive Algorithm M(l, r)
  • if rlt l then return 0 // Recurrence
    termination no scalar-mult
  • else
  • return minM(l, i) M(i1, r) rowl.coli
    .colr ,
  • for l?iltr
  • end algorithm.
  • Driver call M(1, n) for the final answer.

20
Recurrence for Ordering of Matrix-chain
Multiplicationsto Bottom-up Computation
  • Recurrence for optimum scalar-multiplication
  • M(l, r)
  • minM(l, i) M(i1, r) rowl .coli .colr,
  • with l? i lt r.
  • To compute M(l,r), you need M(l,i) and M(i1,r)
    available for ALL is
  • E.g., For M(3,9),
  • you need M(3,3), M(4,9), and M(3,4),
    M(5,9),
  • Need to compute smaller size Ms first
  • Gradually increase size from 1, 2, 3, , n

21
Strategy for Ordering of Bottom-up Computation
  • Recurrence for optimum number of
    scalar-multiplications
  • M(l, r)
  • minM(l, i) M(i1, r) rowl .coli .colr
    ,
  • with l? i lt r.
  • Compute by increasing size r-l11, r-l12,
    r-l13, r-l1n
  • Start the calculation at the lowest level with
    two matrices, AB, BC, CD etc.
  • Really?
  • Where does the recurrence terminate?
  • Then calculate for the triplets, ABC, BCD etc.
  • And so on

0, for lr
22
Ordering of Matrix-chain Multiplications (Example)
  • r 1 2 3 4
  • l
  • 1 0 15 35(2) 69(2)
  • 2 0 12 42
  • 3 0 24
  • 4 0

Triplets
Pairs
Singlet's
23
Matrix-chain DP-algorithm
  • Input list of pairwise dimensions of matrices
  • Output optimum number of scalar multiplications
  • for all 1? i ? n do M(i,i) 0 // diagonal
    elements 0
  • for size 2 to n do // size of subsequence
  • for l 1 to n-size1 do
  • r lsize-1 //move along diagonal
  • M(l,r) infinity //minimizer
  • for i l to r-1 do
  • x M(l, i) M(i1, r)
  • rowl .coli .colr
  • if x lt M(l, r) then M(l, r) x
  • End.
  • // Complexities?

24
Ordering of Matrix-chain Multiplications (Example)
A1 (5x3), A2 (3x1), A3 (1x4), A4 (4x6).
  • r 1 2 3 4
  • l
  • 1 0
  • 2 0
  • 3 0
  • 4 0

15
35(2)
69(2)
12
42(?)
Triplets
24
Pairs
How do you find out the actual matrix ordering?
Calculation goes diagonally. COMPLEXITY?
25
DP Ordering of Matrix-chain Multiplications
(Example)
  • A1 (5x3), A2 (3x1), A3 (1x4), A4 (4x6).
  • M(1,1) M(2,2) M(3,3) M(4,4) 0
  • M(1, 2) M(1,1) M(2,2) 5.3.1 0 0 15.
  • M(1, 3) mini1 M(1,1)M(2,3)5.3.4,
  • i2 M(1,2)M(3,3)5.1.4
  • min72, 35 35(2)
  • M(1,4) min i1 M(1,1)M(2,4)5.3.6,
  • i2 M(1,2)M(3,4)5.1.6,
  • i3 M(1,3)M(4,4)5.4.6 min132, 69,
    155 69(2)
  • 69 comes from the break-point i2
    (A1.A2)(A3.A4)
  • Recursively break the sub-parts if necessary,
  • e.g., for (A1A2A3) optimum is at i2 (A1.A2)A3

26
Ordering of Matrix-chain Multiplications (Example)
A1 (5x3), A2 (3x1), A3 (1x4), A4 (4x6).
  • r 1 2 3 4
  • l
  • 1 0 15 35(2) 69(2)
  • 2 0 12 42
  • 3 0 24
  • 4 0

Triplets
Pairs
For a chain of n matrices, Table sizeO(n2),
computing for each entryO(n) COMPLEXITY O(n2
n) O(n3)
A separate matrix I(i,j) keeping track of optimum
i, for actual matrix ordering
27
Computing the actual break points I(i,j)
r 1 2 3 4 5 6 l 1 - - (2) (2) (3) (3) 2
- - (3) (3) (4) 3 - - (4) (4) 4 - (3) (4
) 5 - - - 6 - - -
Backtrack on this table (A1..A3)(A4A6) ABCDEF
-gt (ABC)(DEF)
Then (A1..A3) -gt (A1A2)(A3), (A4A6)
-gt A4(A5 A6) ABCDEF -gt (ABC)(DEF) -gt ((AB)C)
(D(EF))
28
Inductive Proof of Matrix-chain Recurrence
  • Induction base
  • if rlt l then absurd case, there is no matrix to
    multiply return 0
  • If r l, then only one matrix, no
    multiplication return 0
  • Inductive hypothesis
  • For all sizeltk, and k1, assume M(l,r) returns
    correct optimum
  • Note size r-l1
  • Inductive step, for sizek
  • Consider all possible ways to break up (lr)
    chain, for l? iltr
  • Make sure to compute and add the resulting pair
    of matrices multiplication rowl.coli .colr
  • Since M(l,i) and M(i1,r) are correct smaller
    size optimums, as per hypothesis, minM(l, i)
    M(i1, r) rowl.coli .colr , for l?iltr is the
    correct return value for M(l,r)

29
Be careful with the correctness of the Recurrence
behind Dynamic Programming
  • Inductive step
  • Consider all possible ways to break up (lr)
    chain, for l?iltr
  • Make sure to compute the resulting pair of
    matrices multiplication rowl.coli .colr
  • Since M(l,i) and M(i1,r) are correct smaller
    size optimums, per hypothesis, minM(l, i)
    M(i1, r) rowl.coli .colr , for l?iltr is the
    correct return value for M(l,r)
  • It is NOT always possible to combine smaller
    steps to the larger one.
  • Addition, Multiplication are associative
  • 43129 (((43) (12)) 9) , but
  • average(4,3,1,2,9) av( av(av(4,3), av(1,2)),
    9), NOT true
  • DP needs the correct formulation of a Recurrence
    first,
  • Then, bottom up combination such that smaller
    problems contributes to the larger ones

30
Problem 3 Optimal Binary Search Tree
  • Binary Search Problem
  • Input sorted objects, and key
  • Output the keys index in the list, or not
    found
  • Binary Search on a Tree Sorted list is organized
    as a binary tree
  • Recursively each root t is l t r, where l is
    any left descendant and r is any right descendant

Example sorted list of objects A2, A3, A4, A7,
A9, A10, A13, A15 Sample correct binary-search
trees
A1
A7
A3
A4
A10
A3
A7
A9
A13
A4
A2
A9
A10
A13
A15
A15
There are many such correct trees
31
Problem 3 Optimal Binary Search Tree
  • Problem Optimal Binary-search Tree Organization
  • Input Sorted list, with elements access
    frequency (how many times to be accessed/searched
    as a key over a period of time)
  • Output Optimal binary tree organization so that
    total cost is minimal
  • Cost for accessing each object frequencyaccess
    steps to the object in the tree)
  • Number of Access steps for each node is its
    distance from the root, plus one.

Example list sorted by object order (index of A)
A1(7), A2(10), A3(5), A4(8), and A5(4)
A1(7)
A3(5)
A2(10)
A3(5)
A4(8)
A1(7)
A4(8)
A5(4)
A2(10)
A5(4)
Cost 51
72 103
82 43 77
Cost 71 102 53 84 45
94
32
Problem 3 Optimal Binary Search Tree
  • Input Sorted list, with each elements access
    frequency (how many times to be accessed/searched
    as a key over a period of time)
  • Output Optimal binary tree organization with
    minimal total cost
  • Every optimization problem optimizes an objective
    function
  • Here it is total access cost
  • Different tree organizations have different
    aggregate costs,
  • because the depths of the nodes are different on
    different tree.
  • Problem We want to find the optimal aggregate
    cost, and a corresponding bin-search tree.

33
Problem 3 Optimal Binary Search Tree
  • Step 1 of DP Formulate an objective function
  • For our example list A1(7), A2(10), A3(5),
    A4(8), and A5(4)
  • Objective function is C(1, 5), cost for optimal
    tree for above list
  • How many ways can it be broken to sub-trees?

A3(5), (A1(7),
A2(10),) (A4(8), A5(4))
34
A1(7), A2(10), A3(5), A4(8), A5(4)
(null left tree) A1(7) at root (A2(10),
A3(5), A4(8), A5(4) on right)
(A1(7) left tree) A2(10) at root (A3(5),
A4(8), A5(4) on right)
How many such splits needed?
35
First i 1 , A1(7), ( ) (A2(10),
A3(5), A4(8), A5(4))
Next i 2, A2(10), (A1(7))
(A3(5),A4(8), A5(4))
Next i 3, A3(5),
(A1(7), A2(10)) (A4(8), A5(4))
Next i 4,
A4(8), (A1(7), A2(10), A3(5))
(A5(4))
Last i 5,
A5(4) (A1(7), A2(10), A3(5), A4(8))
( ) All values of i from 1 to 5
are to be tried, to find MINIMUM COST C(1, 5)
36
A1 A2 Aleft Ai Aright An
Ai (fi)
C(i1, right)
C(left, i-1)
Generalize the Recurrence formulation for
varying left and right pointers C(l, r) ?
C(l, i-1) and C(i1, r)
For a choice of Ai C(l, r) ? C(l, i-1)
C(i1, r) ?
37
A1 A2 Aleft Ai Aright An
Ai
C(i1, right)
C(left, i-1)
For a choice of Ai we would like to write C(l,
r) ? C(l, i-1) C(i1, r) fi 1, l ? i ?
r BUT,
38
But,
A3(5)
Cost 51 102 73 82 43
.. 41 ..
A4(8)
A2(10)
A5(4)
A1(7)
C(stand alone left sub-tree) 101 72 10
14 24
39
A1 A2 Aleft Ai Aright An
Ai
C(i1, right)
C(left, i-1)
Now Dynamic Programming does not work! C(l, i-1)
and C(i1, r) are no longer useful to compute
C(l, r), unless
40
Observe
A3(5)
Cost 51 102 73 82 43
.. 41 ..
A4(8)
A2(10)
A5(4)
A1(7)
C(stand alone left sub-tree) 101 72 10
14 24 C (inside full tree) 10(11)
7(21), for 1 extra steps 24 (101
71) 41
41
Generalize
A3(5)
Cost 51 102 73 82 43
.. 41 ..
A4(8)
A2(10)
A5(4)
A1(7)
C(stand alone left sub-tree) 101 72 10
14 24 C (inside full tree) 10(11)
7(21), for 1 extra steps 24 (10
7) C(stand-alone) (sum of node costs) We
can make DP work by reformulating the recurrence!
42
Recurrence for Optimal Binary Search Tree
  • If the i-th node is chosen as the root for this
    sub-tree, then
  • C(l, r) minl? i ? r
  • f(i) C(l,
    i-1) C(i1, r)
  • åjli-1 f(j) additional cost for left
    sub-tree
  • åji1r f(j) additional cost for right
    sub-tree
  • minl ? i ? r åjlrf(j) C(l, i-1) C(i1,
    r)

43
Recurrence for Optimal Binary Search Tree
  • If the i-th node is chosen as the root for this
    sub-tree, then
  • C(l, r) minl? i ? r
  • f(i) C(l,
    i-1) C(i1, r)
  • åjli-1 f(j) additional cost for left
    sub-tree
  • åji1r f(j) additional cost for right
    sub-tree
  • minl ? i ? r åjlrf(j) C(l, i-1) C(i1,
    r)
  • Recurrence termination?
  • Observe in the formula, which boundary values you
    will need.
  • Final result in C(1, n).
  • Start from zero element sub-tree (size0),
  • and gradually increase size,
  • Finish when sizen, for the full tree

44
Optimal Binary Search Tree (Continued)
  • Like matrix-chain multiplication-ordering we will
    develop a triangular part of the cost matrix (rgt
    l), and
  • we will develop it diagonally (r l size),
    with varying size
  • Note that our boundary condition,
  • c(l, r)0 if l gt r (meaningless cost) This is
    recurrence termination
  • We start from, l r single node trees
  • (not with lr-1 pairs of matrices, as in
    matrix-chain-mult case).
  • Also, i now goes from left through right, and
  • i is excluded from both the subtrees Cs.

45
Optimal Binary-search Tree Organization
problem(Example)
  • Keys A1(7), A2(10), A3(5), A4(8), and A5(4).
  • Initialize

r -gt 1 2 3 4 5
l1
l2 0
l3 0 0
l4 0 0 0
l5 0 0 0 0
46
Optimal Binary-search Tree Organization
problem(Example)
  • Keys A1(7), A2(10), A3(5), A4(8), and A5(4).
  • Diagonals C(1, 1)7, C(2, 2)10, .

Singlets
r -gt 1 2 3 4 5
l1 7
l2 0 10
l3 0 0 5
l4 0 0 0 8
l5 0 0 0 0 4
47
Optimal Binary-search Tree Organization
problem(Example)
  • Keys A1(7), A2(10), A3(5), A4(8), and A5(4).
  • Diagonals C(1, 1)7, C(2, 2)10, .
  • C(1,2)mini1 C(1,0)C(2,2)f1f201017,

r -gt 1 2 3 4 5
l1 7
l2 0 10
l3 0 0 5
l4 0 0 0 8
l5 0 0 0 0 4
48
Optimal Binary-search Tree Organization
problem(Example)
  • Keys A1(7), A2(10), A3(5), A4(8), and A5(4).
  • Diagonals C(1, 1)7, C(2, 2)10, .
  • C(1,2)mini1 C(1,0)C(2,2)f1f201017,
  • i2 C(1,1) C(3,2) f1f2 7 0 17

r -gt 1 2 3 4 5
l1 7
l2 0 10
l3 0 0 5
l4 0 0 0 8
l5 0 0 0 0 4
49
Write the DP algorithm for Optimal Binary-search
Tree Organization problem
  • Keys A1(7), A2(10), A3(5), A4(8), and A5(4).
  • Diagonals C(1, 1)7, C(2, 2)10, .
  • C(1,2)mini1 C(1,0)C(2,2)f1f201017,
  • i2 C(1,1) C(3,2) f1f2 7 0 17
    min27, 24 24 (i2)

r -gt 1 2 3 4 5
l1 7 24(2)
l2 0 10
l3 0 0 5
l4 0 0 0 8
l5 0 0 0 0 4
50
Write the DP algorithm for Optimal Binary-search
Tree Organization problem
  • Keys A1(7), A2(10), A3(5), A4(8), and A5(4).
  • Diagonals C(1, 1)7, C(2, 2)10, .
  • C(1,2)mini1 C(1,0)C(2,2)f1f201017,
  • i2 C(1,1) C(3,2) f1f2 7 0 17
    min27, 24 24 (i2)

r -gt 1 2 3 4 5
l1 7 24(2) 34 55 67
l2 0 10 20 41 51
l3 0 0 5 18 26
l4 0 0 0 8 16
l5 0 0 0 0 4
Usual questions How to keep track for finding
optimum tree? Full Algorithm?
51
Problem4 All Pairs Shortest Path
  • A variation of Djikstras algorithm. Called
    Floyd-Warshals algorithm. Good for dense graph.
  • Algorithm Floyd // NOT A GOOD STYLE WHY?
  • Copy the distance matrix in d1..n1..n
  • for k1 through n do //consider each vertex as
    updating candidate
  • for i1 through n do
  • for j1 through n do
  • if (dik dkj lt dij) then
  • dij dik dkj
  • pathij k // last updated via k
  • End algorithm.
  • Time O(n3), for 3 loops. Space ?

Cs.fit.edu/dmitra/ Algorithms/lectures/
FloydExampleKormenPg696.pdf
52
Problem 5 DNA sequence Alignment (Approximate
string matching)
Score 9 matches times 1 1 mismatch times (-1) 1
gap times (-2) 6
Find a best-score alignment
53
Problem 5 DNA sequence Alignment
s1 s2 s3 sn t1 t2 t3 ..tm
54
Problem 5 DNA sequence Alignment
Note, initialization, Or, Recurrence
termination Alignment start from
(n,m)-corner, Stops at (0,0) corner
55
Problem 5 Gene Alignment
56
Problem 5-1 Gene Alignment, local
? Global alignment
We may want Local alignment ?
57
Problem 6-1 Gene Alignment, local
We may want Local alignment ?
? No negative score
Initialize with 0s. Alignment, starts from
highest value on the table, And stops with a
zero
Write a Comment
User Comments (0)
About PowerShow.com