0% found this document useful (0 votes)
87 views96 pages

Unit-4 (Dynamic Programming)

The document discusses dynamic programming and its key concepts. It defines dynamic programming as a strategy for designing algorithms that is used when a problem breaks down into recurring subproblems. It can be applied to optimization problems to find the optimal solution among many possible solutions. Dynamic programming solves problems using a bottom-up approach by breaking them down into subproblems, storing the results of subproblems, and building up the solution using those results without recomputing repeated subproblems. This contrasts with the top-down divide and conquer approach. The document provides examples to illustrate dynamic programming concepts like memoization, tabulation, and applying the principle of optimality.

Uploaded by

HACKER SPACE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
87 views96 pages

Unit-4 (Dynamic Programming)

The document discusses dynamic programming and its key concepts. It defines dynamic programming as a strategy for designing algorithms that is used when a problem breaks down into recurring subproblems. It can be applied to optimization problems to find the optimal solution among many possible solutions. Dynamic programming solves problems using a bottom-up approach by breaking them down into subproblems, storing the results of subproblems, and building up the solution using those results without recomputing repeated subproblems. This contrasts with the top-down divide and conquer approach. The document provides examples to illustrate dynamic programming concepts like memoization, tabulation, and applying the principle of optimality.

Uploaded by

HACKER SPACE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 96

Unit-4

Dynamic Programming
Prepared by: Bhavini Tandel
 Strategy for designing algorithms is dynamic programming
 Used when problem breaks down into recurring small sub problems
 Dynamic programming is typically applied to optimization problems. In
such problem there can be many solutions.
 Each solution has a value and have to find a solution with the optimal
value.
Divide-and-Conquer

 Divide and conquer method for algorithm design:


 Divide: If the input size is too large than divide the problem into two or more
disjoint subproblems.
 Conquer: Conquer recursively to solve the subproblems.
 Combine: Take the solutions to the subproblems and merge these solution
for the original problem.
 Example: Merge-sort
Dynamic Programming

 Dynamic programming: Find out all possible solutions and than pickup the
best solution/ optimal solution.
 Used for solving optimization problem.
 Idea of dynamic programming: avoid calculating the same thing twice,
usually by keeping a table of known result that fills up a sub instances are
solved. For any problem there may be many solution which are feasible, so
find solutions of all problem and than choose optimal solution.
 Divide and conquer is a top-down method.
 Dynamic programming like divide-and-conquer method solve problems by
combining the solutions to sub-problems.
 On the other hand Dynamic programming is a bottom-up technique.
 We usually start with the smallest and the simplest sub- instances.
 By combining their solutions, we obtain the answers to sub-instances of
increasing size until finally we arrive at the solution of the original instances.
 Dynamic programming always give optimal answer.
 Dynamic programming adopt tabulation method and memoization
method.
Principle of Optimality

 Dynamic programming obtain the solution using principle of optimality.


 A problem can be solved by taking sequence of decisions to get the
optimal solution.
 In a optimal sequence of decisions or choices, each subsequence must
also be optimal.
 When it is not possible to apply the principle of optimality it is almost
impossible to obtain the solution using the dynamic programming
approach.
 Example: Finding of shortest path in a given graph uses the principle of
optimality.
 Suppose that in solving a problem, we have to make a sequence of
decisions D1, D2, ...., Dn. If this sequence is optimal then the last k decisions,
1<k<n must be optimal.
 If i, i1, i2, ..., j is a shortest path from i to j, then i1, i2, ... , must be a shortest
path from i1 to j.
Elements of Dynamic Programming

 Dynamic programming is used to solve problems with the following


characteristics:
 Optimal Substructure:
 The optimal solution to the problem contains within optimal solution to its
subproblems.
 Problem is divide into subproblems than subproblems are solved individually
and than combine all the subproblems to get final answer.
 Overlapping Subproblems:
 there exist some places where we solve the same sub problem more than once.
 In case of dynamic programming, subproblems are repeated. If we store the
data in table so, whenever the same subproblems get occur we simply put the
data of that subproblems from the table.
 How does the dynamic programming works?
 Breaks down the complex problem into simpler subproblems.
 It finds the optimal solution to these subproblems
 It store the result of subproblems. The process of storing the results of
subproblems is known as memorization.
 It reuses them if the same subproblem is arise more than one.
 Finally, calculate the result of the complex problem
 In the case of dynamic programming, the space complexity would be
increased as intermediate results are stored but time complexity would be
decreased.
 There are two approaches:
 Top-down Approach
 Bottom-up Approach
 Top Down Approach:
 Top down approach follows the memorization technique, while bottom
up approach follows the tabulation method.
 Here memorization is equal to the sum of recursion (calling the function
itself)and caching (storing the intermediate results).
 Advantages:
 It is very easy to understand and implement
 It solves the subproblems only when it is required.
 It is easy to debug
 Disadvantages:
 It uses recursion so it occupied more memory in the call stack. Sometimes when
recursion is too deep than stack overflow condition will occur.
 It occupies more memory that degrades the overall performance.
 Bottom-Up Approach
 It uses the tabulation technique to implement dynamic programming
 It uses same kind of problems but it removes the recursion. If we remove
recursion, there is no stack overflow issue and n overhead of the recursive
function.
 In tabulation method, we solve the problems and store the result in a matrix.
 It is also known as tabulation or table filling method.
 Difference between Divide-and-Conquer and Dynamic Programming:

Divide-and-Conquer Dynamic Programming


Split its input at prespecified Split its input at every possible split
deterministic point (e.g. always in point rather than at a prespecified
middle) point. After trying all split points, it
determines optimal path.
The problem have independent The subproblems are not
subproblems independent

It is recursive It is non recursive


It does more work on subproblems It solves the subproblems only and
hence more time consumption then store in the table.
Divide-and-conquer Dynamic Programming

It is top down approach It is bottom up approach


Subproblems are independent of Subproblems are interdependent
each other
For example: merge sort and binary For example: matrix multiplication
search
Memoization Method:

 By storing the results of function, we avoiding the same call again and
again. To avoid the same call again and again by storing the result in an
array.
 Compute solution in a bottom-up fashion
Example: Fibonacci numbers
 For finding fibbo(6) than total functions calls are 25.
 If we consider all function calls than the time complexity is:
 fibbo(n-2)+fibbo(n-1) than calling itself two times (assume n-2 as n-1)
 Recurrence Relation: T(n)=2T(n-1)+1
 Time Complexity: O(2𝑛 )
 To reduce this time, we take a array and store the value of fibbo() function
to it.
 So, total number of calls are 7.
 If total number of calls are n
 fibbo(n) = n+1 calls
 Time complexity: O(n)
Tabulation Method
 We mostly write iterative functions only in dynamic programming which will
fill up the table to get the values from the smaller value onwards.
 It is bottom-up approach.
 Mostly tabulation method is used in dynamic programming.
Binomial Coefficients

 A binomial coefficient C(n,k) can be defined as the coefficient of 𝑥 𝑘 in the


expansion of (1 + 𝑥)𝑛 .
 Application of Dynamic Programming
1) 0/1 Knapsack problem
2) All pair shortest path problem
3) Matric chain multiplication
4) Longest Common Subsequence (LCS)
Making Change problem:

 Given: Coins of different denomination and amount


 Problem: Find out minimum number of coins to make change of given
amount using given coins [infinite supply of coins].
 Greedy algorithm works only in a limited number of instances or when a
coins of a particular denomination are missing or in short supply, the
algorithm may either find a suboptimal answer or not find an answer at all.
 Steps:
 Exclude the coin
 Include the coin
 Add (1) and (2)
 Example:
 Coins: {2, 3, 5, 10}
 W: 15 (Total Amount)
 Find total possible ways?
W(j) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Coins (i)

2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

3 1 0 1 1 1 1 2 1 2 2 2 2 3 2 3 3

5 1 0 1 1 1 2 2 2 3 3 4 4 5 5 6 7

10 1 0 1 1 1 2 2 2 3 3 5 4 6 6 7 9
9

Total Possible ways


Algorithm:
 Example:
 Coins: {1, 5, 6, 9} (Use infinite number of coins)
 Amount W = 10
 Find minimum number of coins?
 We use tabular method/bottom-up approach
W(j) 0 1 2 3 4 5 6 7 8 9 10
Coins (i)

1 0 1 2 3 4 5 6 7 8 9 10

5 0 1 2 3 4 1 2 3 4 5 2

6 0 1 2 3 4 1 1 2 3 4 2

9 0 1 2 3 4 1 1 2 3 1 22

Minimum no. of coins


W(j) 0 1 2 3 4 5 6 7 8 9 10
Coins (i)

1 0 1 2 3 4 5 6 7 8 9 10 Different

5 0 1 2 3 4 1 2 3 4 5 2 Same

6 0 1 2 3 4 1 1 2 3 4 2 Same

9 0 1 2 3 4 1 1 2 3 1 22

When we finally reach 0. i.e. initial state than further no coins are selected. So, 2
coins of 5rs. Is selected which is optimal solution.
Time Complexity: O(n * w)
Where n=no. of coins and w=amount
0/1 Knapsack Problem

 This problem is similar to ordinary knapsack problem but we may not take a
fraction of an object.
 We are given ‘ N ‘ object with weight Wi and profits Pi where i varies from 1
to N and also a knapsack with capacity ‘ M ‘.
 The problem is, we have to fill the bag with the help of ‘ N ‘ objects and the
resulting profit has to be maximum.
 Formally, the problem can be started as,
 Where Xi are constraints on the solution Xi {0,1}. Xi is required to be 0 or 1. if
the object is selected then the unit in 1. if the object is rejected than the
unit is 0. So, that why it is called as 0/1 knapsack problem.
 0 = Object is absent
 1 = Object is present
 You have to pick the item completely means it is not divisible. Either you
have to select that object or not selected. You can’t use fraction of that
object.
 Rules to fill the table:
 If i=1 and j < w(i) then T(i,j) =0 filled in the table.
 If i=1 and j ≥ w (i) then T (i,j) = p(i), the cell is filled with the profit p[i],
since only one object can be selected to the maximum.
 If i>1 and j < w(i) then T(i,1) = T (i-1,j) the cell is filled the profit of previous
object since it is not possible with the current object.
 If i>1 and j ≥ w(i) then T (i,j) = {f(i) +T(i-1,j-w(i)),. since only ‘1’ unit can be
selected to the maximum. If is the current profit + profit of the previous
object to fill the remaining capacity of the bag.
 Example:
 Weight: {3, 4, 6, 5}
 Profit: {2, 3, 1, 4}
 W=8
W 0 1 2 3 4 5 6 7 8
i
Pi Wi 0 0 0 0 0 0 0 0 0 0

2 3 1 0 0 0 2 2 2 2 2 2

3 4 2 0 0 0 2 3 3 3 5 5

4 5 3 0 0 0 2 3 4 4 5 6

1 6 4 0 0 0 2 3 4 4 5 66

Maximum Profit
W 0 1 2 3 4 5 6 7 8
i
Pi Wi 0 0 0 0 0 Different 0 0 0 0 0

2 3 1 0 0 0 2 Same 2 2 2 2 2

3 4 2 0 0 0 2 3 3 3 5 5
Different

4 5 3 0 0 0 2 3 4 4 5 6
Same

1 6 4 0 0 0 2 3 4 4 5 6

Profit => 6-4 = 2 So, Selected Objects = {1, 0, 0, 1}


2-2 = 0
Algorithm:

Time complexity = O(n * m)


when i=1 when i=2
 T(i,w) = max {T[i-1,w], m[i-1,w-w[i]]  T(i,w) = max {T[i-1,w], m[i-1,w-w[i]]
+ p[i]} + p[i]}
 T(1,3) = max {T(2+0),0} = 2  T(2,4) = max {T(3+0),2} = 3
 T(1,4) = max {T(2+0),0} = 2  T(2,5) = max {T(3+0),2} = 3
 T(1,5) = max {T(2+0),0} = 2  T(2,6) = max {T(3+0),2} = 3
 T(1,6) = max {T(2+0),0} = 2  T(2,7) = max {T(3+2),2} = 5
 T(1,7) = max {T(2+0),0} = 2  T(2,8) = max {T(3+2),2} = 5
 T(1,8) = max {T(2+0),0} = 2
when i=3 when i=4
 T(i,w) = max {T[i-1,w], m[i-1,w-w[i]]  T(i,w) = max {T[i-1,w], m[i-1,w-w[i]]
+ p[i]} + p[i]}
 T(3,5) = max {T(4+0),3} = 4  T(4,6) = max {T(1+0),4} = 4
 T(3,6) = max {T(4+0),3} = 4  T(4,7) = max {T(1+0),5} = 5
 T(3,7) = max {T(4+0),5} = 5  T(4,8) = max {T(1+0),6} = 6
 T(3,8) = max {T(4+2),5} = 6
Floyd’s Algorithm for shortest path

 Floyd-Warshall Algorithm is an algorithm for finding the shortest path


between all the pairs of vertices in a weighted graph. As a result of this
algorithm, it will generate a matrix which will represent the minimum
distance from any node to all other nodes in the graph.
 This algorithm works for both the directed and undirected weighted graphs.
But, it does not work for the graphs with negative cycles (where the sum of
the edges in a cycle is negative).
 Floyd-Warshall algorithm is also called as Floyd's algorithm, Roy-Floyd
algorithm, Roy-Warshall algorithm and WFI algorithm.
 How Floyd’s warshall algorithm works?

 Create a matrix A0 of dimension n*n where n is the number of vertices. The


row and the column are indexed as i and j respectively. i and j are the
vertices of the graph.
 Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If
there is no path from ith vertex to jth vertex, the cell is left as infinity.
 Now, create a matrix A1 using matrix A0. The elements in the first column
and the first row are left as they are. The remaining cells are filled in the
following way.
 Let k be the intermediate vertex in the shortest path from source to
destination. In this step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j])
if (A[i][j] > A[i][k] + A[k][j]
 if the direct distance from the source to the destination is greater than the
path through the vertex k, then the cell is filled with A[i][k] + A[k][j].
 Similarly, A2 is created using A1.
 Formula for getting value from any intermediate vertex.
𝐴𝑘 [i, j] = min { 𝐴𝑘−1 [i, j], 𝐴𝑘−1 [i, k] + 𝐴𝑘−1 [k, j]}
 This kind of algorithm is better when graph is dense graph (Each vertex is
connected with each other and also having negative weights).
For intermediate vertex

To generate the matrix

Time Complexity: O(𝒏𝟑 )


Space Complexity: O(𝒏𝟐 )
 Floyd Warshall Algorithm application:
 To find the shortest path in a directed graph
 To find a transitive closure of directed graphs: Basically for determining
reachability of nodes.
 Transitive Closure: Transitive Closure is the reachability matrix to
reach from vertex u to vertex v of a graph.
 When there is a value 1 for vertex u to vertex v, it means that there is
at least one path from u to v
 Detecting negative-weight cycles in graphs. (If there is a negative
weight cycle, the distance from a the start node to itself will be
negative after running the algorithm)
 Limitation:
 The graph should not contain negative cycles. The graph can have
positive and negative weight edges.
 First list out all the edges of the graph. You can select any edges first but
make sure that each and every edges should be selected.
 And relaxed each edge one by one.
 Total how many time edges should be relaxed?
 If the graph has total 7 vertices so relaxation can be done 6 (n-1)times.
 Example:

 List out all edges: (1,2), (1,3), (1,4), (2,5), (3,2), (3,5), (4,3), (4,6), (5,7), (6,7)
(1,2), (1,3), (1,4), (2,5), (3,2), (3,5), (4,3), (4,6), (5,7), (6,7)

Initialization End of Pass One


End of Pass Two End of Pass Three
End of Pass Four

 After pass four edge weights are not changed.


 So, the shortest paths are
 Vertex 1: 0, Vertex 2: 1, Vertex 3: 3, Vertex 4: 5, Vertex 5: 0, Vertex 6: 4,
Vertex 7: 3.
Algorithm:
 Time Complexity:
 Relaxing all the edges|E| and total edges are |V|-1.
 So, Total time is O(|V||E|). If edges and vertices are taken as n than
time complexity is O(𝑛2 ).
 If the graph is complete graph,
 Complete graph means there is a edge between every pair of
vertex.
 Total edges = (|V||V-1|)/2 and total vertices are |V-1|
 If edges and vertices are taken as n. So, Time complexity is [(n*(n-
1)/2)*(n-1)] = O(𝑛3 ).
Disadvantage of Bellman-Ford Algo.

 If a value d[v] fails to converge after |V|-1 passes, there exists a negative
weight cycle in G reachable from S.
 Example:

 List out Edges: (1,2), (1,4), (2,4), (3,2), (4,3)


 Total we have to relaxed edges 3 times.
End of Pass One End of Pass Two End of Pass Three
 When we try to relax one more time than also it will relax.
 Reason behind this id the graph contains a negative weight cycle.

Negative weight cycle = 5+3-10


End of Pass Four = -2
 Bellman ford algorithm failed when graph has negative weight cycle but it
can detect if there is an negative weight cycle in the graph.
 Bellman Ford's algorithm and Dijkstra's algorithm are very similar in structure.
While Dijkstra looks only to the immediate neighbours of a vertex and
Bellman goes through each edge in every iteration.
 Application:
 For calculating shortest paths in routing algorithms.
Matrix Chain Multiplication

 Determine the optimal parenthesization of a product of n matrices.


 We have already seen multiplication of only two matrices. However when
more than two matrices are multiplied then the way in which the matrices
are multiplied can change the cost of multiplication.
 Matrix chain multiplication is an optimization problem that to find the most
efficient way to given sequence of matrices. The Question is not to multiply
and get the answer but which pair we should take such that the total cost
of multiplying all of them is minimum.
 So, matrix chain multiplication problem means how should we parenthesis
them.
The formula used for matrix chain multiplication:
You can also draw the table like this

 Example:
A1 = 3 Χ 2, A2 = 2 Χ 4, A3 = 4 Χ 2, A4 = 2 Χ 5
A1 A2 A3 A4
3Χ2 2Χ4 4Χ2 2Χ5
d0 d1 d2 d3 d4

M 1 2 3 4 K 1 2 3 4
1 0 24 28 58 1 0 1 1 3
2 0 16 36 2 0 2 3
3 0 40 3 0 3
4 0 4 0
Algorithm:
 Time Complexity:
 Here, we are preparing only half of the table. So, n(n+1)/2 elements are
generated. So, n(n+1)/2 = 𝒏𝟐
 but for getting each element we are calculating all and than choose
minimum. So, it will take at most n time,
n(n-1)/2 = 𝒏𝟐 * n = 𝒏𝟑
 Time Complexity of matrix chain multiplication: O(𝒏𝟑 )
 Example:
 For the given he sequence {4,10,3,12,20,7}. The matrices have sizes 4*10, 10*3,
3*12,12*20,20*7. Compute M[I,j], 0<=I,j<=5. M[I,j]=0 for all i.
Longest Common Subsequence (LCS)

 The longest common subsequence (LCS) is defined as the longest


subsequence that is common to all the given sequences, provided that the
elements of the subsequence are not required to occupy consecutive
positions within the original sequences.
 If S1 and S2 are the two given sequences then Z is the common
subsequence of S1 and S2 if Z is a subsequence of both S1 and S2.
Furthermore, Z must be a strictly increasing sequence of the indices of both
S1 and S2.
 In a strictly increasing sequence, the indices of the elements chosen from
the original sequences must be in ascending order in Z.
 Example:
S1 = {B, C, D, A, A, C, D}, S2 = {A, C, D, B, A, C}
Then, common subsequences are
{B, C}, {C, D, A, C}, {D, A, C}, {A, A, C}, {A, C}, {C, D}, ...
Among these subsequences, {C, D, A, C} is the longest common subsequence.

Equations used for LCS:

If matched

If no matched
 Find longest common subsequence using dynamic programming.

LCS = bd

a b c d a b c d
0 1 2 3 4 0 1 2 3 4
0 0 0 0 0 0 0 0 0 0 0 0
b 1 0 0 1 1 1 b 1 0 0 1 1 1
d 2 0 0 1 1 2 d 2 0 0 1 1 2
Maximum Length of subsequence b d
Algorithm:
 Time Complexity:

m no. of alphabets

n no. of alphabets

 Total no. of cells are Θ(m * n)


 So, time complexity is Θ(m * n)
 Applications of Longest Common Subsequence:
 In compressing genome resequencing data
 To authenticate users within their mobile phone through in-air signatures
Assembly Line Scheduling
 Manufacturing problem to find the fastest way through a factory.
 Pair of assembly lines, each with n stations
 parameters:
 Si,j: The 𝑗𝑡ℎ station on assembly line i
 ai,j: Assembly time at the 𝑗𝑡ℎ station on line i
 ei: Time to enter assembly line 1
 xi: Time to exit from assembly line 1
 ti,j: Time to transfer away from station Si,j to station Sx,j+1 on other line
if i=1, x=2 and if i=2, x=1
 Assembly line programming is an example of dynamic programming solves
a manufacturing problem.
 Example: The Motor Corporation produces automobiles in a factory that
has two assembly lines, shown in below figure.
 Problem Statement:
 Determine which stations to choose from line 1 and which to choose
from line 2 in order to minimize assembly time.
 First consider the optimal way for a product to get from the starting point
through station S1,j
 If j=1, there is one choice
go through station 1,1
 For j=2, 3, 4, ..., n there are two choices
 if the fastest way through S1,j-1 then
stay on line 1 through S1,j
 if the fastest way through S2,j-1 then
transfer from line 2 to line 1 and then through station S1,j
 Recursive Solution
 Assembly line 1
 for station 1
 F1[1] = e1 + a1,1

 for station 2, 3, ...., n, we have


 F1[j] = min { (F1[j-1] + a1,j), (F2[j-1] + t2,j-1 + a1,j)}

 Top down approach, recalculation


 Optimal substructure property
 to find the fastest way through station j, we solve the sub problems of
finding the fastest way through station j-1
 Definition:
 F* : the fastest time to get through the entire factory
 Fi[ j ] : the fastest time to get from the starting point through station Si, j
 L* : the line number which is used to exit the factory from the nth station
 Li[ j] : the line number (1 or 2) whose Si, j-1is used to reach Si, j.
 Time to get through station 1
Fi[1] = ei + ai,1, for both i=1 and i=2
 The goal is to compute the fastest way through the factory F*
F* = min{F1[n] + x1, F2[n] + x2} Objective Function
 Problem:
 What stations should be chosen from line 1 and which from line 2 in
order to minimize the total time through the factory from one car?

7 9 3 4 8 4 3
2
2 3 1 3 4
Exit
Enter
2 1 2 2 1
2
4
8 5 6 4 5 7
1 2 3 4 5 6
F* = 38
F1[j] 9 18 20 24 32 35
l* = 1
F2[j] 12 16 22 25 30 37

1 2 3 4 5 6

L1[j] 1 1 2 1 1 2
L2[j] 2 1 2 1 2 2
Algorithm

1. f1[1] <- e1 + a1,1


Compute Initial values of f1 and f2
2. f2[1] <- e2 + a2,1
3. for j <- 2 to n
4. do if f1[j-1] + a1,j <= f2[j-1] + t2,j-1 + a1,j
5. then f1[j] <- f1[j-1] + a1,j Compute the values of f1[j] and l1[j]
6. l1[j] <- 1
7. else f1[j] <- f2[j-1] + t2,j-1 + a1,j
8. l1[j] <- 2
9. if f2[j-1] + a2,j <= f1[j-1] + t1,j-1 + a2,j then
10. f2[j] <- f2[j-1] + a2,j Compute the values of f2[j] and l2[j]
11. l2[j] <- 2
12. else f2[j] <- f1[j-1] + t1,j-1 + a2,j
13. l2[j] <- 1
14. if f1[n] + x1 <= f2[n] + x2
15. then f* = f1[n] + x1
16. l* = 1 Compute the values of the fastest time through the entire factory

17. else f* = f2[n] + x2


18. l* = 2

Where,
a[i,j] = assembly time on jth station on ith line
t[i,j] = transit time from jth station on ith line to j+1th station on another line
e[i] = entry time on line i
x[i] = exit time on line i
n = no of station on every line

Time Complexity = Ɵ(n)


 Construct Optimal Solution:

1 2 3 4 5 6

F1[j] 9[1] 18[1] 20[2] 24[1] 32[1] 35[2]


L* = 1
F2[j] 12[2] 16[1] 22[2] 25[1] 30[2] 37[2]
Min{18,23} Min{21,20} Min{24,28} Min{32,35} Min{36,35}
=18 =20 =24 =32 =35

2 7 9 3 4 8 4 3
2 3 1 3 4
Exit
Enter
2 1 2 2 1
2
4 8 5 6 4 5 7
Min{16,17} Min{27,22} Min{25,26} Min{32,30} Min{43,37}
=16 =22 =25 =30 =37
F* = 28
l* = 1
 d0, d1, d2, …., dn representing values not in K. here d0 represents all values
less than k and dn represents all values greater than kn and for i=1,2,…,n-1,
the dummy key di represents all values between ki and ki+1. Figure shows
two binary search trees for a set of n=5 keys.
 Each key ki is an internal node and each dummy key di is a leaf. Every
search is either successful (finding some key ki ) or unsuccessful (finding
some dummy key di) so
 Optimal Binary Search Tree: If the keys and their probabilities are given than
we have to generate the BST such that the cost is minimum.
 Cost of the tree is depends on height of a binary tree.
2𝑛𝑐𝑛
 If n nodes are there in the tree, total trees are possible.
𝑛+1
 So using dynamic programming is easier and faster method for trying out all
possible tree and picking up the best one without trying up all of them. So
try out all of them not directly but indirectly.
 For the base case, Compute w[i,i-1] = qi-1 for 1≤ i ≤ n+1. For j ≥ i, so compute

 And compute cost


 Example:
 Keys: {10, 20, 30, 40}, Pi: {3, 3, 1, 1}, qi: {2, 3, 1, 1, 1}
 Probabilities should be in decimal, so should not greater than 1. here the
values are integer so divide each integer value by 16 to get its probability.
W00 = 2 W11 = 3 W22 = 1 W33 = 1 W44 = 1
j-i=0 C00 = 2 C11 = 3 C22 = 1 C33 = 1 C44 = 1
R00 = 0 R11 = 0 R22 =0 R33 = 0 R44 = 0
W01 = 8 W12 = 7 W23 = 3 W34 = 3
j-i=1 C01 = 13 C12 = 11 C23 = 5 C34 = 5
R01 = 1 R12 = 2 R23 = 3 R34 = 4
W02 = 12 W13 = 9 W24 = 5
j-i=2 C02 = 21 C13 = 17 C24 = 11
R02 = 2 R13 = 2 R24 = 3
W03 = 14 W14 = 11
j-i=3 C03 = 32 C14 = 25
R03 = 2 R14 = 2
W04 = 16
j-i=4 C04 = 40
R04 = 2

Table for C[i,j]


R[0,4]
20

K=2
R[0,1] R[2,4]
10 30

K=1 K=3
R[3,4]
R[0,0]
R[1,1]
40

R[2,2]
K=4

R[3,3] R[4,4]

Optimal Binary Search Tree


O(1)

O(𝒏𝟑 )

Time Complexity: O(𝒏𝟑 )

You might also like