Professional Documents
Culture Documents
Algorithms-I CSC 302 (July-Dec2022 IIITK) L45 - L46
Algorithms-I CSC 302 (July-Dec2022 IIITK) L45 - L46
CSC 302
SK Hafizul Islam
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 1 / 42
Agenda I
2 Suggested Readings
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 2 / 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
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
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 6 / 42
Optimal Binary Search Tree
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
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
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
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
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 13 / 42
Find an Optimal BST: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 14 / 42
Find an Optimal BST: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 15 / 42
Find an Optimal BST: Dynamic Programming
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
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 16 / 42
Find an Optimal BST: Dynamic Programming
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
For j ≥ i, we compute
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 18 / 42
Find an Optimal BST: Dynamic Programming
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
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
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
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
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
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
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
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
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
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
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
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
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
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
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 25, 2022 39 / 42
Find an Optimal BST: Dynamic Programming
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