Lecture 8
Lecture 8
Lecture No. 8
1
All-Pairs Shortest Path
G = (V, E) be a graph
G has no negative weight cycles
Vertices are labeled 1 to n
If (i, j) is an edge its weight is denoted by wij
Optimal Substructure Property
Easy to Prove
Recursive formulation - 1
(𝒎)
𝐋𝐞𝐭 𝒍𝒊𝒋 be the minimum weight of any path from
vertex i to vertex j that contains atmost m edges.
Then,
(𝟎) 𝟎, 𝒊𝒇 𝒊 = 𝒋
𝒍𝒊𝒋 = ቊ
∞, 𝒊𝒇 𝒊 ≠ 𝒋
𝒑𝒊 + 𝒒𝒊 = 𝟏
𝒊=𝟏 𝒊=𝟎
Because we have probabilities of searches for each key
and each dummy key, we can determine the expected
cost of a search in a given binary search tree T.
Let us assume that the actual cost of a search is the
number of nodes examined,
i.e., the depth of the node found by the search in T,
plus1.
Optimal Binary Search Tree
Then the expected cost of a search in T is
E[ search cost in T]
k1 k4 k1 k5
d0 d1
d0 d1 d5
k3 k5 k4
d2 d3 d4 d5 d4
k3
Figure (a) Figure (a) costs 2.80
i 0 1 2 3 4 5
d2 d3
qi 0.05 0.10 0.05 0.05 0.05 0.10 Figure (b) costs 2.75
Optimal Binary Search Tree
We start with a problem regarding binary search trees
in an environment in which the probabilities of
accessing elements and gaps between elements is
known.
Goal:
We want to find the binary search tree that minimizes
the expected number of nodes probed on a search.
Optimal Binary Search Tree
Brute Force Approach:
Exhaustive checking of all possibilities.
Question
What is the number of binary search trees on n keys?
Answer
𝒘 𝒊, 𝒋 = 𝒑𝒍 + 𝒒𝒊
𝒍=𝒊 𝒍=𝒊−𝟏
Optimal Binary Search Tree
Thus, if kr is the root of an optimal subtree containing
keys ki ,…,kj , we have
e[i, j]= pr + (e[i, r-1]+w(i, r-1)) + (e[r+1, j]+w(r+1, j))
Noting that w (i, j) = w(i,r-1)+ pr +w(r+1,j)
We rewrite e[i, j] as
e[i, j]= e[i, r-1] + e[r+1, j] + w(i, j)
The recursive equation as above assumes that we know
which node kr to use as the root.
We choose the root that gives the lowest expected
search cost
Optimal Binary Search Tree
Final recursive formulation:
𝑞𝑖−1 , 𝑖𝑓 𝑗 = 𝑖 − 1
𝑒 𝑖, 𝑗 = ቐ min { 𝑒 𝑖, 𝑟 − 1 + 𝑒 𝑟 + 1, 𝑗 + 𝑤(𝑖, 𝑗)}, 𝑖𝑓 𝑖 ≤ 𝑗
𝑖≤𝑟≤𝑗
c[i, j] = c[i - 1, j - 1] + 1
2 x2 0 second
0
0
m xm 0 c[m, n]
Computing the table
0 if i = 0 or j = 0
c[i, j] = c[i-1, j-1] + 1 if xi = yj
max(c[i, j-1], c[i-1, j]) if xi yj
Else
b[i, j] = “ ”
Pseudo Code for LCS
1. for i ← 1 to m
2. do c[i, 0] ← 0
3. for j ← 0 to n
4. do c[0, j] ← 0
5. for i ← 1 to m
6. do for j ← 1 to n
7. do if xi = yj
8. then c[i, j] ← c[i - 1, j - 1] + 1
9. b[i, j ] ← “ ”
10. else if c[i - 1, j] ≥ c[i, j - 1]
11. then c[i, j] ← c[i - 1, j]
12. b[i, j] ← “↑”
13. else c[i, j] ← c[i, j - 1]
14. b[i, j] ← “←”
15.return c and b Running time: O(mn)
Example
X = A, B, C, B, D, A, B 0 if i = 0 or j = 0
Y = B, D, C, A, B, A c[i, j] = c[i-1, j-1] + 1 if xi = yj
max(c[i, j-1], c[i-1, j]) if xi yj
0 1 2 3 4 5 6
yj B D C A B A
If xi = yj
0 xi
b[i, j] = “ ” 0 0 0 0 0 0 0
Else if c[i - 1, j] ≥ c[i, j-1] 1 A 0 0 0 0 1 1 1
b[i, j] = “ ”
2 B 0 1 1 1 1 2 2
else
3 C 0 1 1 2 2 2 2
b[i, j] = “ ”
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Constructing a LCS
0 xi
0 0 0 0 0 0 0
When we
1 A 0 0 0 0 1 1 1
encounter a “ “ in
b[i, j] 2 B 0 1 1 1 1 2 2
xi = yj is an 3 C 0
1 1 2 2 2 2
element of the LCS
4 B 0
1 1 2 2 3 3
LCS is BCBA 5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4