



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concepts of dynamic programming and its applications to the knapsack problem and optimal binary search trees. The mfknapsack algorithm for solving the knapsack problem and the derivation of the recurrence for optimal binary search trees. It also provides an example problem and the average number of comparisons in a successful search.
Typology: Study notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!
Slide
8
‐
5
BSTs with
n
nodes.
c(n) is the n
th
Catalan number, which grows as fast as
,
so brute force is hopeless.
1
1
2
)
(
⎞ ⎟⎟⎠
⎛ ⎜⎜⎝
=
n
n
n
n
c
5 . 1 4 n
n
)!
(
!
!
)
,
(
k
n
k
n
k
n
C
n k
−
=
=
⎞ ⎟⎟ ⎠
⎛ ⎜⎜ ⎝
C(n,k) is number of ways
to choose k things from set of n things
∑
n = i
i
i
p
c
1
average
number
of
comparisons:
0.1*
0.2*
0.4*
0.3*
=
i
j
i
j
i
j
i
j
k
⎫ ⎬ ⎭
⎧ ⎨ ⎩
∑
∑
− =
=
−
≤
≤
1
1
1
1
) 1 ( ) 1 ( 1
min
]
,
[
k
i
s
j k
s
j k s s k i s s k j k i
inT
c
p
inT
c p p j i C
over all
choices of
root a
k
a
k
is root,
so c
k
c
s
in T
k-1 i
means
to find key a
s
in
optimal
i k-
+1 because
these aredepths insub-trees
Recurrence gives following recursive algorithm:• pick a root• solve for optimal BST on both sub-trees• chose best root
⎫ ⎬ ⎭
⎧ ⎨ ⎩
∑
∑
− =
=
−
≤
≤
1
1
1
1
) 1 ( ) 1 ( 1
min
]
,
[
k
i
s
j k
s
j k s s k i s s k j k i
inT
c
p
inT
c p p j i C
⎫ ⎬ ⎭
⎧ ⎨ ⎩
=
∑
∑
∑
∑
− =
=
=
− =
−
≤
≤
1
1
1
1
1
1
)
(
)
(
min
]
,
[
k
i
s
j k
s
j k
s
s j k s s k
i
s
s k i s s k j k i
p
inT
c
p
p
inT
c p p j i C
⎫ ⎬ ⎭
⎧ ⎨ ⎩
=
∑
∑
∑
− =
=
=
−
≤
≤
1
1
1
1
)
(
)
(
min
]
,
[
k
i
s
j k
s
j
i
s
s j k s s k
i s s j k i
p
inT
c
p
inT
c
p
j
i
C
{
}
∑
=
≤
≤
−
=
j
i
s
s
j
k
i
p j k C k i C j i C
] , 1 [ ] 1 , [
min
]
,
[
0
1
2
3
4
1
0
.
.
1.
1.
2
0
.
.
1.
3
0
.
1.
4
0
.
5
0
0
1
2
3
4
1
1
2
3
3
2
2
3
3
3
3
3
4
4
5
optimal BST B
cost table
root table
Time efficiency:
Θ
(
n
3
) not great, but better than exponential
Space efficiency:
Θ
(
n
2
)
Method can be expended to include unsuccessful searchesTime can be reduced to
n
2
) by taking advantage of monotonicity of entries
in the root table, i.e.,
i,j
] is always in the range between
i,j
-1] and R[
i
+1,j]