Download as pdf or txt
Download as pdf or txt
You are on page 1of 77

Algorithms - I

CSC 302

SK Hafizul Islam

Department of CSE, IIIT Kalyani

October 25, 2022

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 1 / 42
Agenda I

1 Optimal Binary Search Tree


Naive Approach
Dynamic Programming Approach
An Example

2 Suggested Readings

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 2 / 42
Binary Search Tree

In a binary search tree, all the


nodes in the left sub-tree have a
value less than that of the root
node. Correspondingly, all the
nodes in the right sub-tree have
a value either equal to or greater
than the root node. The same
rule is applicable to every sub-tree
in the tree.
Binary search tree is also called
an ordered binary tree. Because
the nodes are arranged in an or-
der.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 3 / 42
Binary Search Tree

In a binary search tree, all the


39
nodes in the left sub-tree have a
value less than that of the root 27 45
node. Correspondingly, all the
nodes in the right sub-tree have 18 29 40 54

a value either equal to or greater


9 21 28 36 59
than the root node. The same
rule is applicable to every sub-tree
10 19 65
in the tree.
Binary search tree is also called
an ordered binary tree. Because
the nodes are arranged in an or-
der.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 3 / 42
Binary Search Tree

levelT (u): level of a node u in a BST T- equals the number of edges on the path from
the root to u.
The level of the root (45) is 0.
The level of 39 and 56 is 1.
The level of 12 is 2.
The level of 34 is 3.
The depth of a tree is the maximum level of the nodes in the tree.

45

39 56

12

34

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 4 / 42
Binary Search Tree

levelT (u): level of a node u in a BST T- equals the number of edges on the path from
the root to u.
The level of the root (45) is 0.
The level of 39 and 56 is 1.
The level of 12 is 2.
The level of 34 is 3.
The depth of a tree is the maximum level of the nodes in the tree.
Assuming, searching for a node u incurs cost propor-
45
tional to levelT (u) + 1.
How many nodes do you need to access to search for
39 56
node 45, 39, 56, 12, and 34, respectively?

12

34

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 4 / 42
Binary Search Tree

Let S = {a1 , a2 , · · · , an } be a set of n integers.


45
A balanced BST on S has depth O(logn). This is good if
we assume that all the integers in S are searched with equal 39 56
probabilities.
In practice, not all keys are equally likely: some are searched 12 78

more often than others. This gives rise to an interesting


question:
If we know the search frequencies of the integers in S, how to
build a better BST to minimize the average search cost?

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 5 / 42
Binary Search Tree

Suppose that we know the frequencies of 45, 39, 56, 12, 34,
and 78 are 10%, 15%, 20%, 25%, 30% and 35%, respectively.
Then, the average cost of searching for a key in the BST
equals:

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 6 / 42
Binary Search Tree

Suppose that we know the frequencies of 45, 39, 56, 12, 34,
and 78 are 10%, 15%, 20%, 25%, 30% and 35%, respectively.
Then, the average cost of searching for a key in the BST
equals:

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 6 / 42
Binary Search Tree

Suppose that we know the frequencies of 45, 39, 56, 12, 34,
45
and 78 are 10%, 15%, 20%, 25%, 30% and 35%, respectively.
Then, the average cost of searching for a key in the BST
39 56
equals:
12 78
frq(45) · cost(45) + frq(39) · cost(39) + frq(56) · cost(56)
+frq(12) · cost(12) + frq(34) · cost(34) + frq(78) · cost(78)
= 10% · 1 + 15% · 2 + 20% · 2 + 25% · 3 + 30% · 3 + 35% · 3
= 3.5

where frq(k) denotes the search frequency of key k, and


cost(k) denotes the cost of searching for k in the tree.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 6 / 42
Optimal Binary Search Tree

Input: We are given a sequence K = ⟨k1 , k2 , · · · , kn ⟩ of n distinct keys in sorted order


(k1 < k2 < · · · < kn ), and (n + 1) “dummy keys", d0 , d1 , · · · , dn .
For each key ki , we have a probability pi that a search is successful.
For each dummy key di , we have a probability qi that a search is unsuccessful. Therefore
n
X n
X
pi + qi = 1 (1)
i=1 i=0

d0 represents all values less than k1 , and dn represents all values greater than kn
For i = 1, 2, · · · , n − 1, the dummy key di represents all values between ki and ki+1
Output: Construct an optimal binary search tree whose expected search cost is smallest.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 7 / 42
Optimal Binary Search Tree

Then the expected cost of a search in optimal BST T is


n
X n
X
E[search cost in T ] = (depthT (ki ) + 1)pi + (depthT (di ) + 1)qi
i=1 i=0
n
X n
X
= 1+ depthT (ki )pi + depthT (di )qi (2)
i=1 i=0

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 8 / 42
Optimal Binary Search Tree

i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10

k2 Node Depth Level Probability Contribution


k1 1 2 0.15 0.30
k2 0 1 0.10 0.10
k1 k4 k3 2 3 0.05 0.45
k4 1 2 0.10 0.20
k5 2 3 0.20 0.60
d0 2 3 0.05 0.15
d0 d1 k3 k5
d1 2 3 0.10 0.30
d2 3 4 0.05 0.20
d3 3 4 0.05 0.20
d2 d3 d4 d5
d4 3 4 0.05 0.20
d5 3 4 0.10 0.40
Figure: Expected search cost 2.80 Total 2.80

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 9 / 42
Optimal Binary Search Tree

i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10

k2 Node Depth Level Probability Contribution


k1 1 2 0.15 0.30
k2 0 1 0.10 0.10
k1 k5 k3 3 4 0.05 0.60
k4 2 3 0.10 0.30
k5 1 2 0.20 0.40
d0 d1 k4 d5
d0 2 3 0.05 0.45
d1 2 3 0.10 0.10
k3 d4 d2 4 5 0.05 0.25
d3 4 5 0.05 0.25
d4 3 4 0.05 0.20
d2 d3 d5 2 3 0.10 0.30
Total 2.75
Figure: Expected search cost 2.75
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 10 / 42
Optimal Binary Search Tree

An optimal BST is not necessarily a tree whose overall height is smallest.


An optimal BST is not necessarily a tree whose root is a key with the greatest probability.

Key k5 has the greatest search probability of any key, yet the root of the optimal binary search
tree is k2 .

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 11 / 42
Find an Optimal BST: Naive Approach

Total number of possible Binary Search Trees with n different keys


2n
Cn
n+1
which is nth Catalan Number1 . Catalan numbers satisfy the following recursive formula
n
X
C0 = 1 and Cn+1 = Ci Cn−i for n ≥ 0
i=0
The first few Catalan numbers for n = 0, 1, 2, 3, · · · are
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, · · ·
Asymptotically, the Catalan numbers grow as
4n
Cn ∼ 3√
n2 π
3
Find an optimal BST T (n) = Ω(4n /n ) 2
1
https://en.wikipedia.org/wiki/Catalan_number
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 12 / 42
Find an Optimal BST: Dynamic Programming

Step 1: The structure of an optimal binary search tree (Optimal Substructure)


We can construct an optimal solution to the problem from optimal solutions to subproblems
Assume that T : ⟨k1 , k2 , · · · , kn ⟩ ⟨d0 , d1 , · · · , dn ⟩ is an optimal BST.
A subtree T ′ : ⟨ki , ki+1 , · · · , kj ⟩ ⟨di−1 , di , · · · , dj ⟩ contain keys in a contiguous range ki , ki+1 , · · · ,
and dummy keys in a contiguous range di−1 , di , · · · , dj for 1 ≤ i ≤ j ≤ n must be optimal.
If there were a subtree T ′′ whose expected cost is lower than that of T ′ , then we could cut
T ′ out of T and paste in T ′′ , resulting in a binary search tree of lower expected cost than T ,
thus contradicting the optimality of T .
From Eq. (2), the expected search cost of a subtree of a node increases by the sum of all the
probabilities in the subtree.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 13 / 42
Find an Optimal BST: Dynamic Programming

Step 1: The structure of an optimal binary search tree (Optimal Substructure)


Given ⟨ki , ki+1 , · · · , kj ⟩ ⟨di−1 , di , · · · , dj ⟩, one of these keys, say kr (i ≤ r ≤ j), is the root of
an optimal subtree containing these keys. The left subtree of the root kr contains the keys ⟨ki ,
ki+1 , · · · , kr −1 ⟩, ⟨di−1 , di , · · · , dr −1 ⟩, and the right subtree contains the keys ⟨kr +1 , kr +2 ,
· · · , kj ⟩, ⟨dr , dr +1 , · · · , dj ⟩.
How to determine kr ?
Go for all possible r , i ≤ r ≤ j
We determine all optimal binary search trees containing ⟨ki , ki+1 , · · · , kr −1 ⟩, ⟨di−1 , di , · · · , dr −1 ⟩
and those containing ⟨kr +1 , kr +2 , · · · , kj ⟩, ⟨dr , dr +1 , · · · , dj ⟩, we are guaranteed that we will find
an optimal binary search tree.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 14 / 42
Find an Optimal BST: Dynamic Programming

Step 2: Recursive solution


We wish to find an optimal BST T containing the keys ⟨ki , ki+1 , · · · , kj ⟩ ⟨di−1 , di , · · · , dj ⟩,
where 1 ≤ i, j ≤ n, and j ≥ i − 1. When j = i − 1, there are no actual keys; we have just the
dummy key di−1 .
e[i, j]: The expected cost of searching an optimal BST containing the keys ⟨ki , ki+1 , · · · ,
kj ⟩ ⟨di−1 , di , · · · , dj ⟩.
Goal: Compute e[1, n].
When j ≥ i, we need to select a root kr from among ki , ki+1 , · · · , kj and then make an optimal
BST with keys ⟨ki , ki+1 , · · · , kr −1 ⟩, ⟨di−1 , di , · · · , dr −1 ⟩ as its left subtree and an optimal BST
with keys ⟨kr +1 , kr +2 , · · · , kj ⟩, ⟨dr , dr +1 , · · · , dj ⟩ as its right subtree.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 15 / 42
Find an Optimal BST: Dynamic Programming

Step 2: Recursive solution


For a subtree of a node with keys ⟨ki , ki+1 , · · · , kj ⟩ ⟨di−1 , di , · · · , dj ⟩, let us denote this sum
of probabilities as
j
X j
X
w (i, j) = pℓ + qℓ (3)
ℓ=i ℓ=i−1

Thus, if kr is the root of an optimal subtree containing keys ⟨ki , ki+1 , · · · , kj ⟩ ⟨di−1 , di , · · · ,
dj ⟩, we have
   
e[i, j] = pr + e[i, r − 1] + w (i, r − 1) + e[r + 1, j) + w (r + 1, j) (4)

Note that

w (i, j) = w (i, r − 1) + pr + w (r + 1, j) (5)

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 16 / 42
Find an Optimal BST: Dynamic Programming

Step 2: Recursive solution


Therefore

e[i, j] = e[i, r − 1] + e[r + 1, j) + w (i, j) (6)

The recursive Eq. (6) assumes that we know which node kr to use as the root. We choose
the root that gives the lowest expected search cost, giving us our final recursive formulation:
(
qi−1 if j = i − 1,
e[i, j] =  
Mini≤r ≤j e[i, r − 1] + e[r + 1, j) + w (i, j) if i ≤ j

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 17 / 42
Find an Optimal BST: Dynamic Programming

Step 3: Computing the expected search cost of an optimal BST


We store e[i, j] values in a table e[1 · · · n + 1, 0 · · · n].
We use only the entries e[i, j] for j ≥ i − 1.
We use a table root[1 · · · n, 1 · · · n] for 1 ≤ i ≤ j ≤ n, for recording the root of the subtree
containing keys ⟨k1 , k2 , · · · , kn ⟩, ⟨d0 , d1 , · · · , dn ⟩.
We use a table w [1 · · · n + 1, 0 · · · n] to store the values w [i, j].
For 1 ≤ i ≤ n + 1 (base case), we compute

w [i, i − 1] = qi−1 (7)

For j ≥ i, we compute

w [i, j] = w [i, j − 1] + pj + qj (8)

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 18 / 42
Find an Optimal BST: Dynamic Programming

Step 3: Computing the expected search cost of an optimal BST


Algorithm: OPT-BST(q, p, n) T (n) = O(n3 )
1 Let e[1 · · · n + 1, 0 · · · n], 6 for ℓ ← 1 to n do
root[1 · · · n, 1 · · · n], and 7 for i ← 1 to n − ℓ + 1 do
w [1 · · · n + 1, 0 · · · n] be new tables 8 j := i + ℓ − 1
2 for i ← 1 to n + 1 do 9 e[i, j] := ∞
3 e[i, i − 1] := qi−1 10 w [i, j] := w [i, j − 1] + pj + qj
4 w [i, i − 1] := qi−1 11 for r ← i to j do
5 end 12 t := e[i, r − 1] + e[r +
1, j] + w [i, j]
13 if (t < e[i, j]) then
14 e[i, j] := t
15 root[i, j] := r
16 end
17 end
18 end
19 end
20 Return(e[1 · · · n + 1, 0 · · · n],
root[1 · · · n, 1 · · · n])
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 19 / 42
Find an Optimal BST: Dynamic Programming

Example 1 (Optimal BST)


Determine the cost and structure of an optimal binary search tree for a set of n = 5 keys with
the following probabilities:

i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 20 / 42
Find an Optimal BST: Dynamic Programming

Example 1 (Optimal BST)


Determine the cost and structure of an optimal binary search tree for a set of n = 5 keys with
the following probabilities:

i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 20 / 42
Find an Optimal BST: Dynamic Programming

Example 1 (Optimal BST)


Determine the cost and structure of an optimal binary search tree for a set of n = 5 keys with
the following probabilities:

i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10

e[i, j] 0 1 2 3 4 5
1
2
3
4
5
6

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 20 / 42
Find an Optimal BST: Dynamic Programming

Example 1 (Optimal BST)


Determine the cost and structure of an optimal binary search tree for a set of n = 5 keys with
the following probabilities:

i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 1
2 2
3 3
4 4
5 5
6 6

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 20 / 42
Find an Optimal BST: Dynamic Programming

Example 1 (Optimal BST)


Determine the cost and structure of an optimal binary search tree for a set of n = 5 keys with
the following probabilities:

i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


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

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 20 / 42
Find an Optimal BST: Dynamic Programming

i = 1, 2, · · · n + 1
e[i, i − 1] := qi−1
w [i, i − 1] := qi−1

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 21 / 42
Find an Optimal BST: Dynamic Programming

i = 1, 2, · · · n + 1
e[i, i − 1] := qi−1
w [i, i − 1] := qi−1

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 21 / 42
Find an Optimal BST: Dynamic Programming

i = 1, 2, · · · n + 1
e[i, i − 1] := qi−1
w [i, i − 1] := qi−1
e[i, j] 0 1 2 3 4 5
1 0.05
2 0.10
3 0.05
4 0.05
5 0.05
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 21 / 42
Find an Optimal BST: Dynamic Programming

i = 1, 2, · · · n + 1
e[i, i − 1] := qi−1
w [i, i − 1] := qi−1
e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5
1 0.05 1 0.05 1
2 0.10 2 0.10 2
3 0.05 3 0.05 3
4 0.05 4 0.05 4
5 0.05 5 0.05 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 21 / 42
Find an Optimal BST: Dynamic Programming

ℓ=1
i = 1, 2, · · · n, then j = i
e[1, 1] := ∞, e[2, 2] := ∞, e[3, 3] := ∞, e[4, 4] := ∞, e[5, 5] := ∞
w [1, 1] := w [1, 0] + p1 + q1 = 0.05 + 0.15 + 0.10 = 0.30
w [2, 2] := w [2, 1] + p2 + q2 = 0.10 + 0.10 + 0.05 = 0.25
w [3, 3] := w [3, 2] + p3 + q3 = 0.05 + 0.05 + 0.05 = 0.15
w [4, 4] := w [4, 3] + p4 + q4 = 0.05 + 0.10 + 0.05 = 0.20
w [5, 5] := w [5, 4] + p5 + q5 = 0.05 + 0.20 + 0.10 = 0.35

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 22 / 42
Find an Optimal BST: Dynamic Programming

ℓ=1
i = 1, 2, · · · n, then j = i
e[1, 1] := ∞, e[2, 2] := ∞, e[3, 3] := ∞, e[4, 4] := ∞, e[5, 5] := ∞
w [1, 1] := w [1, 0] + p1 + q1 = 0.05 + 0.15 + 0.10 = 0.30
w [2, 2] := w [2, 1] + p2 + q2 = 0.10 + 0.10 + 0.05 = 0.25
w [3, 3] := w [3, 2] + p3 + q3 = 0.05 + 0.05 + 0.05 = 0.15
w [4, 4] := w [4, 3] + p4 + q4 = 0.05 + 0.10 + 0.05 = 0.20
w [5, 5] := w [5, 4] + p5 + q5 = 0.05 + 0.20 + 0.10 = 0.35

e[i, j] 0 1 2 3 4 5
1 0.05 ∞
2 0.10 ∞
3 0.05 ∞
4 0.05 ∞
5 0.05 ∞
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 22 / 42
Find an Optimal BST: Dynamic Programming

ℓ=1
i = 1, 2, · · · n, then j = i
e[1, 1] := ∞, e[2, 2] := ∞, e[3, 3] := ∞, e[4, 4] := ∞, e[5, 5] := ∞
w [1, 1] := w [1, 0] + p1 + q1 = 0.05 + 0.15 + 0.10 = 0.30
w [2, 2] := w [2, 1] + p2 + q2 = 0.10 + 0.10 + 0.05 = 0.25
w [3, 3] := w [3, 2] + p3 + q3 = 0.05 + 0.05 + 0.05 = 0.15
w [4, 4] := w [4, 3] + p4 + q4 = 0.05 + 0.10 + 0.05 = 0.20
w [5, 5] := w [5, 4] + p5 + q5 = 0.05 + 0.20 + 0.10 = 0.35

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 ∞ 1 0.05 0.30
2 0.10 ∞ 2 0.10 0.25
3 0.05 ∞ 3 0.05 0.15
4 0.05 ∞ 4 0.05 0.20
5 0.05 ∞ 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 22 / 42
Find an Optimal BST: Dynamic Programming

ℓ=1
i = 1, 2, · · · n, then j = i
r = i · · · j, i.e., r = i
For i = 1, j = 1, r = 1, t = e[1, 0] + e[2, 1] + w [1, 1] = 0.05 + 0.10 + 0.30 = 0.45 < e[1, 1],
then e[1, 1] = t = 0.45, and root[1, 1] = 1.
For i = 2, j = 2, r = 2, t = e[2, 1] + e[3, 2] + w [2, 2] = 0.05 + 0.10 + 0.30 = 0.40 < e[2, 2],
then e[2, 2] = t = 0.40, and root[2, 2] = 2.
For i = 3, j = 3, r = 3, t = e[3, 2] + e[4, 3] + w [3, 3] = 0.05 + 0.10 + 0.30 = 0.25 < e[3, 3],
then e[3, 3] = t = 0.25, and root[3, 3] = 3.
For i = 4, j = 4, r = 4, t = e[4, 3] + e[5, 4] + w [4, 4] = 0.05 + 0.10 + 0.30 = 0.30 < e[4, 4],
then e[4, 4] = t = 0.30, and root[4, 4] = 4.
For i = 5, j = 5, r = 5, t = e[5, 4] + e[6, 5] + w [5, 5] = 0.05 + 0.10 + 0.35 = 0.50 < e[5, 5],
then e[5, 5] = t = 0.50, and root[5, 5] = 5.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 23 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5
1 0.05 0.45
2 0.10 0.40
3 0.05 0.25
4 0.05 0.30
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 24 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 1 0.05 0.30
2 0.10 0.40 2 0.10 0.25
3 0.05 0.25 3 0.05 0.15
4 0.05 0.30 4 0.05 0.20
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 24 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 1 0.05 0.30 1 1
2 0.10 0.40 2 0.10 0.25 2 2
3 0.05 0.25 3 0.05 0.15 3 3
4 0.05 0.30 4 0.05 0.20 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 24 / 42
Find an Optimal BST: Dynamic Programming

ℓ=2
i = 1, 2, · · · n − 1, then j = i + 1
e[1, 2] := ∞, e[2, 3] := ∞, e[3, 4] := ∞, e[4, 5] := ∞, e[5, 6] := ∞
w [1, 2] := w [1, 1] + p2 + q2 = 0.30 + 0.10 + 0.05 = 0.45
w [2, 3] := w [2, 2] + p3 + q3 = 0.25 + 0.05 + 0.05 = 0.35
w [3, 4] := w [3, 3] + p4 + q4 = 0.15 + 0.10 + 0.05 = 0.30
w [4, 5] := w [4, 4] + p5 + q5 = 0.20 + 0.20 + 0.10 = 0.50

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 25 / 42
Find an Optimal BST: Dynamic Programming

ℓ=2
i = 1, 2, · · · n − 1, then j = i + 1
e[1, 2] := ∞, e[2, 3] := ∞, e[3, 4] := ∞, e[4, 5] := ∞, e[5, 6] := ∞
w [1, 2] := w [1, 1] + p2 + q2 = 0.30 + 0.10 + 0.05 = 0.45
w [2, 3] := w [2, 2] + p3 + q3 = 0.25 + 0.05 + 0.05 = 0.35
w [3, 4] := w [3, 3] + p4 + q4 = 0.15 + 0.10 + 0.05 = 0.30
w [4, 5] := w [4, 4] + p5 + q5 = 0.20 + 0.20 + 0.10 = 0.50

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 ∞
2 0.10 0.40 ∞
3 0.05 0.25 ∞
4 0.05 0.30 ∞
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 25 / 42
Find an Optimal BST: Dynamic Programming

ℓ=2
i = 1, 2, · · · n − 1, then j = i + 1
e[1, 2] := ∞, e[2, 3] := ∞, e[3, 4] := ∞, e[4, 5] := ∞, e[5, 6] := ∞
w [1, 2] := w [1, 1] + p2 + q2 = 0.30 + 0.10 + 0.05 = 0.45
w [2, 3] := w [2, 2] + p3 + q3 = 0.25 + 0.05 + 0.05 = 0.35
w [3, 4] := w [3, 3] + p4 + q4 = 0.15 + 0.10 + 0.05 = 0.30
w [4, 5] := w [4, 4] + p5 + q5 = 0.20 + 0.20 + 0.10 = 0.50

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 ∞ 1 0.05 0.30 0.45
2 0.10 0.40 ∞ 2 0.10 0.25 0.35
3 0.05 0.25 ∞ 3 0.05 0.15 0.30
4 0.05 0.30 ∞ 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 25 / 42
Find an Optimal BST: Dynamic Programming

ℓ=2
i = 1, 2, · · · n − 1, then j = i + 1
e[1, 2] := ∞, e[2, 3] := ∞, e[3, 4] := ∞, e[4, 5] := ∞, e[5, 6] := ∞
w [1, 2] := w [1, 1] + p2 + q2 = 0.30 + 0.10 + 0.05 = 0.45
w [2, 3] := w [2, 2] + p3 + q3 = 0.25 + 0.05 + 0.05 = 0.35
w [3, 4] := w [3, 3] + p4 + q4 = 0.15 + 0.10 + 0.05 = 0.30
w [4, 5] := w [4, 4] + p5 + q5 = 0.20 + 0.20 + 0.10 = 0.50

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 ∞ 1 0.05 0.30 0.45 1 1
2 0.10 0.40 ∞ 2 0.10 0.25 0.35 2 2
3 0.05 0.25 ∞ 3 0.05 0.15 0.30 3 3
4 0.05 0.30 ∞ 4 0.05 0.20 0.50 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 25 / 42
Find an Optimal BST: Dynamic Programming

ℓ=2
i = 1, 2, · · · n − 1, then j = i + 1, i.e., j = 2, · · · , n
r = i · · · j, i.e., r = i · · · i + 1
i =1
For r = 1, t = e[1, 0] + e[2, 2] + w [1, 2] = 0.05 + 0.40 + 0.45 = 0.90 < e[1, 2], then e[1, 2] =
t = 0.90, and root[1, 2] = 1.
For r = 2, t = e[1, 1] + e[3, 2] + w [1, 2] = 0.45 + 0.05 + 0.45 = 0.95 > e[1, 2], then e[1, 2] =
t = 0.90, and root[1, 2] = 1.
i =2
For r = 2, t = e[2, 1] + e[3, 3] + w [2, 3] = 0.10 + 0.25 + 0.35 = 0.70 < e[2, 3], then e[2, 3] =
t = 0.70, and root[2, 3] = 2.
For r = 3, t = e[2, 2] + e[3, 3] + w [2, 3] = 0.40 + 0.25 + 0.35 = 0.70 = e[2, 3], then e[2, 3] =
t = 0.70, and root[2, 3] = 2.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 26 / 42
Find an Optimal BST: Dynamic Programming

ℓ=2
i = 1, 2, · · · n − 1, then j = i + 1, i.e., j = 2, · · · , n
r = i · · · j, i.e., r = i · · · i + 1
i =3
For r = 3, t = e[3, 2] + e[4, 4] + w [3, 4] = 0.05 + 0.30 + 0.30 = 0.65 < e[3, 4], then e[3, 4] =
t = 0.65, and root[3, 4] = 3.
For r = 4, t = e[3, 3] + e[5, 4] + w [3, 4] = 0.25 + 0.05 + 0.30 = 0.60 < e[3, 4], then e[3, 4] =
t = 0.60, and root[3, 4] = 4.
i =4
For r = 4, t = e[4, 3] + e[5, 5] + w [4, 5] = 0.05 + 0.50 + 0.50 = 1.05 < e[4, 5], then e[4, 5] =
t = 1.05, and root[4, 5] = 4.
For r = 5, t = e[4, 4] + e[6, 5] + w [4, 5] = 0.30 + 0.10 + 0.50 = 0.90 < e[4, 5], then e[4, 5] =
t = 0.90, and root[4, 5] = 5.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 27 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90
2 0.10 0.40 0.70
3 0.05 0.25 0.60
4 0.05 0.30 0.90
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 28 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1 0.05 0.30 0.45
2 0.10 0.40 0.70 2 0.10 0.25 0.35
3 0.05 0.25 0.60 3 0.05 0.15 0.30
4 0.05 0.30 0.90 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 28 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 0.90 1 0.05 0.30 0.45 1 1 1
2 0.10 0.40 0.70 2 0.10 0.25 0.35 2 2 2
3 0.05 0.25 0.60 3 0.05 0.15 0.30 3 3 4
4 0.05 0.30 0.90 4 0.05 0.20 0.50 4 4 5
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 28 / 42
Find an Optimal BST: Dynamic Programming

ℓ=3
i = 1, 2, · · · n − 2, then j = i + 2
e[1, 3] := ∞, e[2, 4] := ∞, e[3, 5] := ∞, e[4, 6] := ∞
w [1, 3] := w [1, 2] + p3 + q3 = 0.45 + 0.05 + 0.05 = 0.55
w [2, 4] := w [2, 3] + p4 + q4 = 0.35 + 0.10 + 0.05 = 0.50
w [3, 5] := w [3, 4] + p5 + q5 = 0.30 + 0.20 + 0.10 = 0.60

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 29 / 42
Find an Optimal BST: Dynamic Programming

ℓ=3
i = 1, 2, · · · n − 2, then j = i + 2
e[1, 3] := ∞, e[2, 4] := ∞, e[3, 5] := ∞, e[4, 6] := ∞
w [1, 3] := w [1, 2] + p3 + q3 = 0.45 + 0.05 + 0.05 = 0.55
w [2, 4] := w [2, 3] + p4 + q4 = 0.35 + 0.10 + 0.05 = 0.50
w [3, 5] := w [3, 4] + p5 + q5 = 0.30 + 0.20 + 0.10 = 0.60

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 ∞
2 0.10 0.40 0.70 ∞
3 0.05 0.25 0.60 ∞
4 0.05 0.30 1.05
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 29 / 42
Find an Optimal BST: Dynamic Programming

ℓ=3
i = 1, 2, · · · n − 2, then j = i + 2
e[1, 3] := ∞, e[2, 4] := ∞, e[3, 5] := ∞, e[4, 6] := ∞
w [1, 3] := w [1, 2] + p3 + q3 = 0.45 + 0.05 + 0.05 = 0.55
w [2, 4] := w [2, 3] + p4 + q4 = 0.35 + 0.10 + 0.05 = 0.50
w [3, 5] := w [3, 4] + p5 + q5 = 0.30 + 0.20 + 0.10 = 0.60

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 ∞ 1 0.05 0.30 0.45 0.55
2 0.10 0.40 0.70 ∞ 2 0.10 0.25 0.35 0.50
3 0.05 0.25 0.60 ∞ 3 0.05 0.15 0.30 0.60
4 0.05 0.30 1.05 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 29 / 42
Find an Optimal BST: Dynamic Programming

ℓ=3
i = 1, 2, · · · n − 2, then j = i + 2
e[1, 3] := ∞, e[2, 4] := ∞, e[3, 5] := ∞, e[4, 6] := ∞
w [1, 3] := w [1, 2] + p3 + q3 = 0.45 + 0.05 + 0.05 = 0.55
w [2, 4] := w [2, 3] + p4 + q4 = 0.35 + 0.10 + 0.05 = 0.50
w [3, 5] := w [3, 4] + p5 + q5 = 0.30 + 0.20 + 0.10 = 0.60

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 0.90 ∞ 1 0.05 0.30 0.45 0.55 1 1 1
2 0.10 0.40 0.70 ∞ 2 0.10 0.25 0.35 0.50 2 2 2
3 0.05 0.25 0.60 ∞ 3 0.05 0.15 0.30 0.60 3 3 4
4 0.05 0.30 1.05 4 0.05 0.20 0.50 4 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 29 / 42
Find an Optimal BST: Dynamic Programming

ℓ=3
i = 1, 2, · · · n − 2, then j = i + 2, r = i · · · j, i.e., r = i · · · i + 2
i = 1, r = 1, 2, 3
For r = 1, t = e[1, 0] + e[2, 3] + w [1, 3] = 0.05 + 0.70 + 0.55 = 1.30 < e[1, 3], then e[1, 3] =
t = 1.30, and root[1, 3] = 1.
For r = 2, t = e[1, 1] + e[3, 3] + w [1, 3] = 0.45 + 0.25 + 0.55 = 1.25 < e[1, 3], then e[1, 3] =
t = 1.25, and root[1, 3] = 2.
For r = 3, t = e[1, 2] + e[4, 3] + w [1, 3] = 0.90 + 0.05 + 0.55 = 1.50 > e[1, 3], then e[1, 3] =
t = 1.25, and root[1, 3] = 2.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 30 / 42
Find an Optimal BST: Dynamic Programming

ℓ=3
i = 1, 2, · · · n − 2, then j = i + 2, r = i · · · j, i.e., r = i · · · i + 2
i = 2, r = 2, 3, 4
For r = 2, t = e[2, 1] + e[3, 4] + w [2, 4] = 0.10 + 0.60 + 0.50 = 1.20 < e[2, 4], then e[2, 4] =
t = 1.20, and root[2, 4] = 2.
For r = 3, t = e[2, 2] + e[4, 4] + w [2, 4] = 0.40 + 0.30 + 0.50 = 1.20 = e[2, 4], then e[2, 4] =
t = 1.20, and root[2, 4] = 2.
For r = 4, t = e[2, 3] + e[5, 4] + w [2, 4] = 0.70 + 0.05 + 0.50 = 1.25 > e[2, 4], then e[2, 4] =
t = 1.20, and root[2, 4] = 2.
i = 3, r = 3, 4, 5
For r = 3, t = e[3, 2] + e[4, 5] + w [3, 5] = 0.05 + 1.05 + 0.60 = 1.70 < e[3, 5], then e[3, 5] =
t = 1.70, and root[3, 5] = 3.
For r = 4, t = e[3, 3] + e[5, 5] + w [3, 5] = 0.25 + 0.50 + 0.60 = 1.35 < e[3, 5], then e[3, 5] =
t = 1.35, and root[3, 5] = 4.
For r = 5, t = e[3, 4] + e[6, 5] + w [3, 5] = 0.60 + 0.10 + 0.60 = 1.30 < e[3, 5], then e[3, 5] =
t = 1.30, and root[3, 5] = 5.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 31 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25
2 0.10 0.40 0.70 1.20
3 0.05 0.25 0.60 1.30
4 0.05 0.30 1.05
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 32 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 1 0.05 0.30 0.45 0.55
2 0.10 0.40 0.70 1.20 2 0.10 0.25 0.35 0.50
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60
4 0.05 0.30 1.05 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 32 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 0.90 1.25 1 0.05 0.30 0.45 0.55 1 1 1 2
2 0.10 0.40 0.70 1.20 2 0.10 0.25 0.35 0.50 2 2 2 2
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60 3 3 4 5
4 0.05 0.30 1.05 4 0.05 0.20 0.50 4 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 32 / 42
Find an Optimal BST: Dynamic Programming

ℓ=4
i = 1, 2, · · · n − 3, then j = i + 3
e[1, 4] := ∞, e[2, 5] := ∞
w [1, 4] := w [1, 3] + p4 + q4 = 0.55 + 0.10 + 0.05 = 0.70
w [2, 5] := w [2, 4] + p5 + q5 = 0.50 + 0.20 + 0.10 = 0.80

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 33 / 42
Find an Optimal BST: Dynamic Programming

ℓ=4
i = 1, 2, · · · n − 3, then j = i + 3
e[1, 4] := ∞, e[2, 5] := ∞
w [1, 4] := w [1, 3] + p4 + q4 = 0.55 + 0.10 + 0.05 = 0.70
w [2, 5] := w [2, 4] + p5 + q5 = 0.50 + 0.20 + 0.10 = 0.80

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 ∞
2 0.10 0.40 0.70 1.20 ∞
3 0.05 0.25 0.60 1.30
4 0.05 0.30 1.05
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 33 / 42
Find an Optimal BST: Dynamic Programming

ℓ=4
i = 1, 2, · · · n − 3, then j = i + 3
e[1, 4] := ∞, e[2, 5] := ∞
w [1, 4] := w [1, 3] + p4 + q4 = 0.55 + 0.10 + 0.05 = 0.70
w [2, 5] := w [2, 4] + p5 + q5 = 0.50 + 0.20 + 0.10 = 0.80

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 ∞ 1 0.05 0.30 0.45 0.55 0.70
2 0.10 0.40 0.70 1.20 ∞ 2 0.10 0.25 0.35 0.50 0.80
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60
4 0.05 0.30 1.05 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 33 / 42
Find an Optimal BST: Dynamic Programming

ℓ=4
i = 1, 2, · · · n − 3, then j = i + 3
e[1, 4] := ∞, e[2, 5] := ∞
w [1, 4] := w [1, 3] + p4 + q4 = 0.55 + 0.10 + 0.05 = 0.70
w [2, 5] := w [2, 4] + p5 + q5 = 0.50 + 0.20 + 0.10 = 0.80

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 0.90 1.25 ∞ 1 0.05 0.30 0.45 0.55 0.70 1 1 1 2
2 0.10 0.40 0.70 1.20 ∞ 2 0.10 0.25 0.35 0.50 0.80 2 2 2 2
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60 3 3 4 5
4 0.05 0.30 1.05 4 0.05 0.20 0.50 4 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 33 / 42
Find an Optimal BST: Dynamic Programming

ℓ=4
i = 1, 2, · · · n − 3, then j = i + 3, r = i · · · j, i.e., r = i · · · i + 3
i = 1, r = 1, 2, 3, 4
For r = 1, t = e[1, 0] + e[2, 4] + w [1, 4] = 0.05 + 1.20 + 0.70 = 1.95 < e[1, 4], then e[1, 4] =
t = 1.95, and root[1, 4] = 1.
For r = 2, t = e[1, 1] + e[3, 4] + w [1, 4] = 0.45 + 0.60 + 0.70 = 1.75 < e[1, 4], then e[1, 4] =
t = 1.75, and root[1, 4] = 2.
For r = 3, t = e[1, 2] + e[4, 4] + w [1, 4] = 0.90 + 0.30 + 0.70 = 1.90 > e[1, 4], then e[1, 4] =
t = 1.75, and root[1, 4] = 2.
For r = 4, t = e[1, 3] + e[5, 4] + w [1, 4] = 1.25 + 0.05 + 0.70 = 2.00 > e[1, 4], then e[1, 4] =
t = 1.75, and root[1, 4] = 2.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 34 / 42
Find an Optimal BST: Dynamic Programming

ℓ=4
i = 1, 2, · · · n − 3, then j = i + 3, r = i · · · j, i.e., r = i · · · i + 3
i = 2, r = 2, 3, 4, 5
For r = 2, t = e[2, 1] + e[3, 5] + w [2, 5] = 0.10 + 1.30 + 0.80 = 2.20 < e[2, 5], then e[2, 5] =
t = 2.20, and root[2, 5] = 2.
For r = 3, t = e[2, 2] + e[4, 5] + w [2, 5] = 0.40 + 1.05 + 0.80 = 2.25 > e[2, 5], then e[2, 5] =
t = 2.20, and root[2, 5] = 2.
For r = 4, t = e[2, 3] + e[5, 5] + w [2, 5] = 0.70 + 0.50 + 0.80 = 2.00 < e[2, 5], then e[2, 5] =
t = 2.00, and root[2, 5] = 4.
For r = 5, t = e[2, 4] + e[6, 5] + w [2, 5] = 1.20 + 0.10 + 0.80 = 2.10 > e[2, 5], then e[2, 5] =
t = 2.00, and root[2, 5] = 2.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 35 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 1.75
2 0.10 0.40 0.70 1.20 2.00
3 0.05 0.25 0.60 1.30
4 0.05 0.30 1.05
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 36 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 1.75 1 0.05 0.30 0.45 0.55 0.70
2 0.10 0.40 0.70 1.20 2.00 2 0.10 0.25 0.35 0.50 0.80
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60
4 0.05 0.30 1.05 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 36 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 0.90 1.25 1.75 1 0.05 0.30 0.45 0.55 0.70 1 1 1 2 2
2 0.10 0.40 0.70 1.20 2.00 2 0.10 0.25 0.35 0.50 0.80 2 2 2 2 4
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60 3 3 4 5
4 0.05 0.30 1.05 4 0.05 0.20 0.50 4 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 36 / 42
Find an Optimal BST: Dynamic Programming

ℓ=5
i = 1, 2, · · · n − 4, then j = i + 4
e[1, 5] := ∞
w [1, 5] := w [1, 4] + p5 + q5 = 0.70 + 0.20 + 0.10 = 1.00

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 37 / 42
Find an Optimal BST: Dynamic Programming

ℓ=5
i = 1, 2, · · · n − 4, then j = i + 4
e[1, 5] := ∞
w [1, 5] := w [1, 4] + p5 + q5 = 0.70 + 0.20 + 0.10 = 1.00

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 1.75 ∞
2 0.10 0.40 0.70 1.20 2.00
3 0.05 0.25 0.60 1.30
4 0.05 0.30 1.05
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 37 / 42
Find an Optimal BST: Dynamic Programming

ℓ=5
i = 1, 2, · · · n − 4, then j = i + 4
e[1, 5] := ∞
w [1, 5] := w [1, 4] + p5 + q5 = 0.70 + 0.20 + 0.10 = 1.00

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 1.75 ∞ 1 0.05 0.30 0.45 0.55 0.70 1.00
2 0.10 0.40 0.70 1.20 2.00 2 0.10 0.25 0.35 0.50 0.80
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60
4 0.05 0.30 1.05 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 37 / 42
Find an Optimal BST: Dynamic Programming

ℓ=5
i = 1, 2, · · · n − 4, then j = i + 4
e[1, 5] := ∞
w [1, 5] := w [1, 4] + p5 + q5 = 0.70 + 0.20 + 0.10 = 1.00

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 0.90 1.25 1.75 ∞ 1 0.05 0.30 0.45 0.55 0.70 1.00 1 1 1 2 2
2 0.10 0.40 0.70 1.20 2.00 2 0.10 0.25 0.35 0.50 0.80 2 2 2 2 4
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60 3 3 4 5
4 0.05 0.30 1.05 4 0.05 0.20 0.50 4 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 37 / 42
Find an Optimal BST: Dynamic Programming

ℓ=5
i = 1, 2, · · · n − 4, then j = i + 4, r = i · · · j, i.e., r = i · · · i + 4
i = 1, r = 1, 2, 3, 4, 5
For r = 1, t = e[1, 0] + e[2, 5] + w [1, 5] = 0.05 + 2.00 + 1.00 = 3.05 < e[1, 5], then e[1, 5] =
t = 3.05, and root[1, 5] = 1.
For r = 2, t = e[1, 1] + e[3, 5] + w [1, 5] = 0.45 + 1.30 + 1.00 = 2.75 < e[1, 5], then e[1, 5] =
t = 2.75, and root[1, 5] = 2.
For r = 3, t = e[1, 2] + e[4, 5] + w [1, 5] = 0.90 + 1.05 + 1.00 = 2.95 > e[1, 5], then e[1, 5] =
t = 2.75, and root[1, 5] = 2.
For r = 4, t = e[1, 3] + e[5, 5] + w [1, 5] = 1.25 + 0.50 + 0.35 = 3.10 > e[1, 5], then e[1, 5] =
t = 2.75, and root[1, 5] = 2.
For r = 5, t = e[1, 4] + e[6, 5] + w [1, 5] = 1.75 + 0.10 + 0.35 = 3.20 > e[1, 5], then e[1, 5] =
t = 2.75, and root[1, 5] = 2.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 38 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 1.75 2.75
2 0.10 0.40 0.70 1.20 2.00
3 0.05 0.25 0.60 1.30
4 0.05 0.30 1.05
5 0.05 0.50
6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 39 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5
1 0.05 0.45 0.90 1.25 1.75 2.75 1 0.05 0.30 0.45 0.55 0.70 1.00
2 0.10 0.40 0.70 1.20 2.00 2 0.10 0.25 0.35 0.50 0.80
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60
4 0.05 0.30 1.05 4 0.05 0.20 0.50
5 0.05 0.50 5 0.05 0.35
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 39 / 42
Find an Optimal BST: Dynamic Programming

e[i, j] 0 1 2 3 4 5 w [i, j] 0 1 2 3 4 5 r [i, j] 1 2 3 4 5


1 0.05 0.45 0.90 1.25 1.75 2.75 1 0.05 0.30 0.45 0.55 0.70 1.00 1 1 1 2 2 2
2 0.10 0.40 0.70 1.20 2.00 2 0.10 0.25 0.35 0.50 0.80 2 2 2 2 4
3 0.05 0.25 0.60 1.30 3 0.05 0.15 0.30 0.60 3 3 4 5
4 0.05 0.30 1.05 4 0.05 0.20 0.50 4 4 4
5 0.05 0.50 5 0.05 0.35 5 5
6 0.10 6 0.10

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 39 / 42
Find an Optimal BST: Dynamic Programming

Step 4: Construct an optimal BST


Algorithm: CONSTRUCT-OPTIMAL-BST(r , i, j, last)
1 if i = j then
2 Return
3 end
4 if last ̸= 0 then
5 Print root[i, j] + “is the root”
6 end
7 else if j < last then
8 Print root[i, j] + “is the left child of” + last
9 end
10 else
11 Print root[i, j] + “is the right child of” + last
12 end
13 CONSTRUCT-OPTIMAL-BST(root, i, root[i, j] − 1, root[i, j])
14 CONSTRUCT-OPTIMAL-BST(root, root[i, j] + 1, j, root[i, j])
https://walkccc.me/CLRS/Chap15/15.5/
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 40 / 42
Suggested Readings

Chapter 15, Thomas H. Cormen, Charles E. Lieserson, Ronald L. Rivest and Clifford Stein,
Introduction to Algorithms, 3rd Edition, MIT Press/McGraw-Hill, 2009.
Chapter 05, E. Harowitz, S. Sahani, and S. Rajasekaran, Fundamentals of Computer
Algorithms, 2nd Edition, University Press, 2008.
Chapter 12, Michael T. Goodrich and Roberto Tamassia, Algorithm Design and Appli-
cations, Wiley, 2014.

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 41 / 42
Thank You

SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 42 / 42

You might also like