Unit 3 Greedy & Dynamic Programming
Unit 3 Greedy & Dynamic Programming
1. Stages: The problem can be divided into several sub-problems, which are called stages. A stage is
a small portion of a given problem. For example, in the shortest path problem, they were defined
by the structure of the graph.
2. States: Each stage has several states associated with it. The states for the shortest path problem
was the node reached.
3. Decision: At each stage, there can be multiple choices out of which one of the best decisions
should be taken. The decision taken at every stage should be optimal; this is called a stage
decision.
4. Optimal policy: It is a rule which determines the decision at each stage; a policy is called an
optimal policy if it is globally optimal. This is known as Bellman principle of optimality.
5. Given the current state, the optimal choices for each of the remaining states does not depend on
the previous states or decisions. In the shortest path problem, it was not necessary to know how
we got a node only that we did.
6. There exist a recursive relationship that identify the optimal decisions for stage j, given that stage
j+1, has already been solved.
7. The final stage must be solved by itself.
1.It deals (involves) three steps •1.It involves the sequence of four
at each level of recursion: steps:Characterize the structure
Divide the problem into a of optimal solutions.
number of subproblems. •Recursively defines the values of
Conquer the subproblems by optimal solutions.
solving them recursively. •Compute the value of optimal
Combine the solution to the solutions in a Bottom-up
subproblems into the solution for minimum.
original subproblems. •Construct an Optimal Solution
from computed information.
2. It is Recursive. 2. It is non Recursive.
3. It does more work on 3. It solves subproblems only
subproblems and hence has more once and then stores in the table.
time consumption.
4. It is a top-down approach. 4. It is a Bottom-up approach.
5. In this subproblems are 5. In this subproblems are
independent of each other. interdependent.
6. For example: Merge Sort & 6. For example: Matrix
Binary Search etc. Multiplication.
In a greedy Algorithm, we make whatever choice seems best In Dynamic Programming we make decision at each step
Feasibility at the moment in the hope that it will lead to global optimal considering current problem and solution to previously
solution. solved sub problem to calculate optimal solution .
It is more efficient in terms of memory as it never look back It requires dp table for memorization and it increases it’s
Memorization
or revise previous choices memory complexity.
Greedy methods are generally faster. For example, Dijkstra’s Dynamic Programming is generally slower. For
Time complexity
shortest path algorithm takes O(ELogV + VLogV) time. example, Bellman Ford algorithm takes O(VE) time.
The greedy method computes its solution by making its Dynamic programming computes its solution bottom up or
Fashion choices in a serial forward fashion, never looking back or top down by synthesizing them from smaller optimal sub
revising previous choices. solutions.
0 1
A 00
B 010 0 1 1
0
C 0110
D 0111 A 0 1
F
E
E 10
F 11 B
0 1
C D
Huffman Codes
• In Huffman coding, variable length code is used
• Data considered to be a sequence of characters.
• Huffman codes are a widely used and very effective technique for
compressing data
• Savings of 20% to 90% are typical, depending on the
characteristics of the data being compressed.
• Huffman’s greedy algorithm uses a table of the frequencies of
occurrence of the characters to build up an optimal way of
representing each character as a binary string.
• Now let us see an example to understand the concepts used in
Huffman coding
Example: Huffman Codes
a b c d e f
Frequency (in thousands) 45 13 12 16 9 5
Fixed-length codeword 000 001 010 011 100 101
Variable-length codeword 0 101 100 111 1101 1100
1
Q: c:12 b:13
4
d:16 a:45
0 1
f:5 e:9
for i ← 1
Allocate a new node z
left[z] ← x ← Extract-Min (Q) = f:5
right[z] ← y ← Extract-Min (Q) = e:9
f [z] ← f [x] + f [y] (5 + 9 = 14)
Insert (Q, z)
Constructing a Huffman Codes
1 2
Q: 4
d:16
5
a:45
0 1 0 1
2 3
Q: 5 0
a:45
0 1 0 1
f:5 e:9
5
Q: a:45
0
5 1
for i ← 4
2 3
Allocate a new node z 5 0
0 1 0 1
left[z] ← x ← Extract-Min (Q) = z:25
c:12 b:13 1 d:16
right[z] ← y ← Extract-Min (Q) = z:30 0
4 1
f [z] ← f [x] + f [y] (25 + 30 = 55) f:5 e:9
Insert (Q, z)
Constructing a Huffman Codes
for i ← 5
Allocate a new node z
1 left[z] ← x ← Extract-Min (Q) = a:45
Q: 0
0
0 1
right[z] ← y ← Extract-Min (Q) =
5 z:55
a:45
0
5 1 f [z] ← f [x] + f [y] (45 + 55 = 100)
2 3 Insert (Q, z)
5 0
0 1 0 1
f:5 e:9
Lemma 1: Greedy Choice
There exists an optimal prefix code such that the two characters with
smallest frequency are siblings and have maximal depth in T.
Proof:
T
• Let x and y be two such characters, and let
T be a tree representing an optimal prefix code.
• Let a and b be two sibling leaves of x y
maximal depth in T, and assume with a b
out loss of generality T'
that f(x) ≤ f(y) and f(a) ≤ f(b).
• This implies that f(x) ≤ f(a) and f(y) ≤ f(b).
a b
• Let T' be the tree obtained by
x y
exchanging a and x and b and y.
Conclusion
• Huffman Algorithm is analyzed
• Design of algorithm is discussed
• Computational time is given
• Applications can be observed in various domains e.g.
data compression, defining unique codes, etc.
generalized algorithm can used for other optimization
problems as well
Two questions
42
Session Learning Outcome-SLO
43
GREEDY INTRODUCTION
Greedy algorithms are simple and straightforward.
44
GREEDY INTRODUCTION
• Many real-world problems are optimization problems in
that they attempt to find an optimal solution among many
possible candidate solutions.
• An optimization problem is one in which you want to find, not
just a solution, but the best solution
• A “greedy algorithm” sometimes works well for optimization
problems
• A greedy algorithm works in phases. At each phase: You take the
best you can get right now, without regard for future
consequences. It hope that by choosing a local optimum at
each step, you will end up at a global optimum.
45
APPLICATIONS OF GREEDY
APPROACH
46
47
48
49
50
51
52
53
54
55
56
57
Home assignment /Questions
58
Tree Traversals
59
Session Learning Outcome-SLO
These usage patterns can be divided into the three ways that we access
the nodes of the tree.
60
Tree Traversal
61
Binary Tree Traversal Techniques
• Three recursive techniques for binary tree traversal
• In each technique, the left subtree is traversed recursively, the right
subtree is traversed recursively, and the root is visited
• What distinguishes the techniques from one another is the order of
those 3 tasks
62
Preoder, Inorder, Postorder
• In Preorder, the root Preorder Traversal:
1. Visit the root
is visited before (pre) 2. Traverse left subtree
the subtrees traversals 3. Traverse right subtree
63
Illustrations for Traversals
• Assume: visiting a node 1
is printing its label 3 7
• Preorder:
5 8 9
1 3 5 4 6 7 8 9 10 11 12
10
• Inorder: 4 6
4 5 6 3 1 8 7 9 11 10 12 11 12
• Postorder:
4 6 5 3 8 11 12 10 9 7 1
64
Illustrations for Traversals (Contd.)
• Assume: visiting a node 15
is printing its data 8 20
• Preorder: 15 8 2 6 3 7 2 11 27
11 10 12 14 20 27 22 30
6 10 12 22 30
• Inorder: 2 3 6 7 8 10 11
3 7 14
12 14 15 20 22 27 30
• Postorder: 3 7 6 2 10 14
12 11 8 22 30 27 20 15
65
Code for the Traversal Techniques
void preOrder(Tree *tree){
• The code for visit if (tree->isEmpty( )) return;
is up to you to visit(tree->getRoot( ));
preOrder(tree->getLeftSubtree());
provide, depending preOrder(tree->getRightSubtree());
}
on the application void inOrder(Tree *tree){
• A typical example if (tree->isEmpty( )) return;
inOrder(tree->getLeftSubtree( ));
for visit(…) is to visit(tree->getRoot( ));
inOrder(tree->getRightSubtree( ));
print out the data }
part of its input void postOrder(Tree *tree){
if (tree->isEmpty( )) return;
node postOrder(tree->getLeftSubtree( ));
postOrder(tree->getRightSubtree( ));
visit(tree->getRoot( ));
}
66
Home assignment /Questions
Write the preorder, inorder and postorder traversals of the binary tree shown
below.
Minimum Spanning Tree
Agenda
• Spanning Tree
• Minimum Spanning Tree
• Prim’s Algorithm
• Kruskal’s Algorithm
• Summary
• Assignment
Session Outcomes
• Apply and analyse different problems using greedy techniques
• Evaluate the real time problems
• Analyse the different trees and find the low cost spanning tree
Spanning Tree
• Given an undirected and connected graph G=(V,E), a spanning tree
of the graph G is a tree that spans G (that is, it includes every vertex
of G) and is a subgraph of G (every edge in the tree belongs to G)
Undirected
For of the spanning trees of the graph
Graph
Minimum Spanning Tree
• The cost of the spanning tree is the sum of the weights of all the edges in the
tree. There can be many spanning trees. Minimum spanning tree is the spanning
tree where the cost is minimum among all the spanning trees. There also can be
many minimum spanning trees.
1 1 1
4 3 2 4 3 4 3
2 2
5 5 5
Undirected Spanning Tree Cost=11 Minimum Spanning Tree
Graph (4+5+2) Cost = 7 (4+1+2)
Applications – MST
• Minimum spanning tree has direct application in the design of
networks. It is used in algorithms approximating the travelling
salesman problem, multi-terminal minimum cut problem and
minimum-cost weighted perfect matching. Other practical applications
are:
• Cluster Analysis
• Handwriting recognition
• Image segmentation
Prim’s Algorithm
• Prim’s Algorithm also use Greedy approach to find the minimum
spanning tree. In Prim’s Algorithm we grow the spanning tree from a
starting position. Unlike an edge in Kruskal's, we add vertex to the
growing spanning tree in Prim's.
• Prim’s Algorithm is preferred when-
• The graph is dense.
• There are large number of edges in the graph like E = O(V2).
Steps for finding MST - Prim’s Algorithm
This time complexity can be improved and reduced to O(E + VlogV) using Fibonacci heap.
Prim’s Algorithm - Example
a
7 8
c
b
5
9 7 5 Consider vertex ‘d’
15 e
d
9
8
6 11
f g
a
7 8
c
b
5
9 7 5
15 e
d
9
8
6 11
f g
a
7 8
c
b
5
9 7 5
15 e
d
9
8
6 11
g
f
a 7
8 c
b
5
9 7 5
15
e
d
9
8
6 11
g
f
a 7
8 c
b
5
9 7 5
15
e
d
9
8
6 11
g
f
a 7
8 c
b
5
9 7 5
15
e
d
9
8
6 11
g
f
a 7
8 c
b
5
9 7 5
Cost = 39
15
e
d
9
8
6 11
g
f
Real-time Application – Prim’s
Algorithm
• Network for roads and Rail tracks connecting all the cities.
• Irrigation channels and placing microwave towers
• Designing a fiber-optic grid or ICs.
• Travelling Salesman Problem.
• Cluster analysis.
• Path finding algorithms used in AI.
• Game Development
• Cognitive Science
Minimum Spanning tree – Kruskal’s
Algorithm
•Kruskal’s Algorithm builds the spanning tree by adding edges
one by one into a growing spanning tree.
•Kruskal's algorithm follows greedy approach as in each
iteration it finds an edge which has least weight and add it to
the growing spanning tree.
•Kruskal’s Algorithm is preferred when-
•The graph is sparse.
•There are less number of edges in the graph like E = O(V)
•The edges are already sorted or can be sorted in linear time.
Steps for finding MST - Kruskal’s
algorithm
Step 1: Sort all the edges in non-decreasing order of their weight.
Step 2: Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If cycle is not formed, include this edge. Else, discard
it.
Step 3: Repeat step#2 until there are (V-1) edges in the spanning tree.
Kruskal’s Algorithm
algorithm Kruskal(G) is
F:= ∅
for each v ∈ G.V do
MAKE-SET(v)
for each (u, v) in G.E ordered by weight(u, v), increasing do
if FIND-SET(u) ≠
FIND-SET(v) then
F:= F ∪ {(u, v)} UNION(FIND-SET(u), FIND-SET(v))
return F
Example – Kruskal’s Algorithm
a Forms Cycle
7 8
c
b MST Edge
5
9 7 5
15 e
d
9
8
6 11
f g
a Forms Cycle
7 8
c
b MST Edge
5
9 7 5
15 e
d
9
8
6 11
f g
a
7 8
c
b
5
9 7 5 Forms Cycle
15 e MST Edge
d
9
8
6 11
g
f
a Forms Cycle
7 8
c
b MST Edge
5
9 7 5
15 e
d
9
8
6 11
g
f
a Forms Cycle
7 8
c
b MST Edge
5
9 7 5
15 e
d
9
8
6 11
g
f
a
7 8
c
b
5
9 7 5
Cost = 39
15 e
d
9
8
6 11
g
f
Time Complexity –Kruskal’s Algorithm
The tree that we are making or growing always The tree that we are making or growing usually
remains connected. remains disconnected.
Prim’s Algorithm grows a solution from a random Kruskal’s Algorithm grows a solution from the
vertex by adding the next cheapest vertex to the cheapest edge by adding the next cheapest edge to
existing tree. the existing tree / forest.
Prim’s Algorithm is faster for dense graphs. Kruskal’s Algorithm is faster for sparse graphs.
Activity
1. Find the minimum spanning tree of the graph given below using Prim’s
and Kruskal’s Algorithms
a
28 b 16
c
10
14
e
12
d 18
25 24
22
f g
2. Find the minimum spanning tree of the graph given below using Prim’s
and Kruskal’s Algorithms
16
a b
21 11 5
19
c 6
33 g
14
18 10
d e
Summary
• Both algorithms will always give solutions with the same length.
• They will usually select edges in a different order.
• Occasionally they will use different edges – this may happen when
you have to choose between edges with the same length.
Dynamic Programming
Introduction to Dynamic Programming
Given n items of known weights w1,...,wn and values v1,...,vn and a knapsack of
capacity W, find the most valuable subset of the items that fit into the knapsack.
Example
• Let’s illustrate that point with an example:
Item Weight Value
I0 3 10
I1 8 4
I2 9 9
I3 8 11
• The best subset of Sk that has the total weight w, either contains item k or not.
Second case: wk ≤ w
• Then the item k can be in the solution, and we choose the case
with greater value.
Algorithm
for w = 0 to W { // Initialize 1st row to 0’s
B[0,w] = 0
}
for i = 1 to n { // Initialize 1st column to 0’s
B[i,0] = 0
}
for i = 1 to n {
for w = 0 to W {
if wi <= w { //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
}
else B[i,w] = B[i-1,w] // wi > w
}
}
Example Problem
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
for i = 1 to n
B[i,0] = 0
Items:
1: (2,3)
Knapsack 0/1 Example 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 vi = 3
2 0 wi = 2
3 0 w=1
4 0 w-wi = -1
• This algorithm only finds the max possible value that can be carried in the
knapsack
• The value in B[n,W]
• To know the items that make this maximum value, we need to trace back through
the table.
Knapsack 0/1 Algorithm
Finding the Items
• Let i = n and k = W
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1 // Assume the ith item is not in the knapsack
// Could it be in the optimally packed knapsack?
Items: Knapsack:
1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=4
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 6
wi = 5
3 0 0 3 4 5 7
B[i,k] = 7
4 0 0 3 4 5 7
B[i-1,k] = 7
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 5
wi = 4
3 0 0 3 4 5 7
B[i,k] = 7
4 0 0 3 4 5 7
B[i-1,k] = 7
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Finding the Items 1: (2,3) Item 2
2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 4
wi = 3
3 0 0 3 4 5 7
B[i,k] = 7
4 0 0 3 4 5 7
B[i-1,k] = 3
k – wi = 2
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Finding the Items 1: (2,3) Item 2
2: (3,4) Item 1
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 k=2
2 0 0 3 4 4 7 vi = 3
wi = 2
3 0 0 3 4 5 7
B[i,k] = 3
4 0 0 3 4 5 7
B[i-1,k] = 0
k – wi = 0
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Finding the Items 1: (2,3) Item 2
2: (3,4) Item 1
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 k=2
2 0 0 3 4 4 7 vi = 3
wi = 2
3 0 0 3 4 5 7
B[i,k] = 3
4 0 0 3 4 5 7
B[i-1,k] = 0
k – wi = 0
k = 0, it’s completed
The optimal knapsack should contain:
Item 1 and Item 2
Complexity calculation of knapsack problem
for w = 0 to W
O(W)
B[0,w] = 0
for i = 1 to n O(n)
B[i,0] = 0
Repeat n times
for i = 1 to n O(W)
for w = 0 to W
< the rest of the code >
MATRIX-MULTIPLY(Ap×q , Bq×r)
1. for i ← 1 to p
2. for j ← 1 to r
3. C[i, j] ← 0
4. for k ← 1 to q
5. C[i, j] ← C[i, j] + A[i, k] · B[k, j]
6. return C
Total:
75,000
Matrix-chain Multiplication …contd
• That is, the optimal solution to the problem contains within it the
optimal solution to subproblems
Dynamic Programming Approach …contd
• Recursive definition of the value of an optimal solution
• Let m[i, j] be the minimum number of scalar multiplications
necessary to compute Ai..j
• Minimum cost to compute A1..n is m[1, n]
• Suppose the optimal parenthesization of Ai..j splits the product
between Ak and Ak+1 for some integer k where i ≤ k < j
Dynamic Programming Approach …contd
• Ai..j = (Ai Ai+1…Ak)·(Ak+1Ak+2…Aj)= Ai..k · Ak+1..j
• Cost of computing Ai..j = cost of computing Ai..k + cost of computing
Ak+1..j + cost of multiplying Ai..k and Ak+1..j
• Cost of multiplying Ai..k and Ak+1..j is pi-1pk pj
0 if i=j
m[i, j ] =
min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j
i ≤ k< j
Dynamic Programming Approach …contd
• To keep track of how to construct an optimal solution, we use a table s
• s[i, j ] = value of k at which Ai Ai+1 … Aj is split for optimal
parenthesization
• Algorithm: next slide
• First computes costs for chains of length l=1
• Then for chains of length l=2,3, … and so on
• Computes the optimal cost bottom-up
Algorithm to Compute Optimal Cost
Input: Array p[0…n] containing matrix dimensions and n
Result: Minimum-cost table m and split table s
MATRIX-CHAIN-ORDER(p[ ], n)
for i ← 1 to n
Takes O(n3) time
m[i, i] ← 0
for l ← 2 to n Requires O(n2) space
for i ← 1 to n-l+1
j ← i+l-1
m[i, j] ← ∞
for k ← i to j-1
q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j]
if q < m[i, j]
m[i, j] ← q
s[i, j] ← k
return m and s
Constructing Optimal Solution
• Our algorithm computes the minimum-cost table m and the split table s
• The optimal solution can be constructed from the split table s
• Each entry s[i, j ]=k shows where to split the product Ai Ai+1 … Aj
for the minimum cost
Example
• Show how to multiply this matrix Matrix Dimension
chain optimally
A1 30×35
• S2: c d g i
• Subsequence, SS1: c d g i
Example
• S1= a b c d e f g h i j
• S2= c d g i
• Subsequence
• S1: a b c d e f g h i j
• S2: c d g i
• Subsequence, SS2: d g i
Example
• S1= a b c d e f g h i j
• S2= c d g i
• Subsequence
• S1: a b c d e f g h i j
• S2: c d g i
• Subsequence, SS3: g i
Example
• SS1= c d g i
• SS2: d g i
• SS3: g i
• Longest Common Subsequence: subsequence with maximum length.
maximum(length(subsequence1),
length(subsequence2)….
length(subsequence n)).
Longest Common Subsequence :
maximum(length(c d g i), length(d g i),length(g i)).
• Therefore LCS (S1,S2) is c d g i with length 4
Activity
• Give the LCS for the following strings
• X: ABCBDAB
Y: BDCABA
Motivation
Brute Force Approach-Check every subsequence of x[1 . . m] to see if it
is also a subsequence of y[1 . . n].
Analysis
• 2m subsequences of x (each bit-vector of length m determines a distinct
subsequence of x).
• Hence, the runtime would be exponential !
Example:
Consider S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA…..
S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA….
Towards a better algorithm: a Dynamic Prpgramming strategy
• Key: optimal substructure and overlapping sub-problems
• First we’ll find the length of LCS. Later we’ll modify the algorithm to find LCS
itself.
LCS Problem Formulation using DP
Algorithm
• Key: find out the correct order to solve the sub-problems
• Total number of sub-problems: m * n
c[i–1, j–1] + 1 if x[i] = y[j],
c[i, j] =
max{c[i–1, j], c[i, j–1]} otherwise.
0 j n
i C(i, j)
m
LCS Example
We’ll see how LCS algorithm works on the following example:
• X = ABCB
• Y = BDCAB
LCS(X, Y) = BCB
X=ABCB
Y=BDCAB
Computing the Length of the LCS
ABCB
LCS Example (0) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
A
1
2 B
3 C
4 B
X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[5,6]
ABCB
LCS Example (1) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0
2 B
0
3 C 0
4 B 0
for i = 1 to m c[i,0] = 0
for j = 1 to n c[0,j] = 0
ABCB
LCS Example (2) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (3) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (4) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (5) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (6) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (7) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (8) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (9) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (10) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (11) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (12) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (13) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (14) BDCAB
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
Finding LCS
j 0 1 2 3 4 5
i Y[j] B D C A B
0 X[i]
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
O(m*n)
since each c[i,j] is calculated in constant time, and there are m*n elements in
the array
Asymptotic Analysis:
Worst case time complexity: O(n*m)
Average case time complexity: O(n*m)
Best case time complexity: O(n*m)
Space complexity: O(n*m)
Real Time Application
• Molecular biology. DNA sequences (genes) can be represented as sequences of four
letters ACGT, corresponding to the four sub molecules forming DNA.
• To find a new sequences by generating the LCS of existing similar
sequences
• To find the similarity of two DNA Sequence by finding the length of
their longest common subsequence.
• File comparison. The Unix program "diff" is used to compare two different versions
of the same file, to determine what changes have been made to the file.
• It works by finding a longest common subsequence of the lines of the
two files; any line in the subsequence has not been changed, so what it
displays is the remaining set of lines that have changed.
• Screen redisplay. Many text editors like "emacs" display part of a file on the screen,
updating the screen image as the file is changed.
• Can be a sort of common subsequence problem (the common
subsequence tells you the parts of the display that are already correct and
don't need to be changed)
Summary
• Solving LCS problem by brute force approach requires O(2m).
• Applying Dynamic Programming to solve LCS problem reduces the
time complexity to O(nm).
Review Questions
1. What is the time complexity of the brute force algorithm used to find the longest
common subsequence?
2. What is the time complexity of the dynamic programming algorithm used to find
the longest common subsequence?
5. What is the value stored in zeroth row and zeroth column of the LCS table?
Home Assignment
• Determine the LCS of (1,0,0,1,0,1,0,1) and (0,1,0,1,1,0,1,1,0).
OPTIMAL BINARY SEARCH TREE
• Session Learning Outcome-SLO
• Motivation of the topic
• Binary Search Tree
• Optimal Binary Search Tree
• Example Problem
• Analysis
• Summary
• Activity /Home assignment /Questions
193
INTRODUCTION
Session Learning Outcome:
194
OPTIMAL BINARY SEARCH TREE(OBST)
• Binary Search tree(BST) which is mainly constructed for searching a key from it
• For searching any key from a given BST, it should take optimal time.
• For this, we need to construct a BST in such a way that it should take optimal time to
search any of the key from given BST
195
BINARY SEARCH TREE:
A binary search tree is a special kind of binary tree. In binary search tree, the
elements in the left and right sub-trees of each node are respectively lesser and
greater than the element of that node. Fig. 1 shows a binary search tree.
Fig. 1 is a binary search tree but is not balanced tree. On the other hand, this is a skewed
tree where all the branches on one side. The advantage of binary search tree is that it
facilitates search of a key easily. It takes O(n) to search for a key in a list. Whereas, search
tree helps to find an element in logarithmic time.
196
How an element is searched in binary search tree?
Let us assume the given element is x. Compare x with the root element of the binary tree, if the binary tree is
non-empty. If it matches, the element is in the root and the algorithm terminates successfully by returning the address
of the root node. If the binary tree is empty, it returns a NULL value.
• If x is less than the element in the root, the search continues in the left sub-tree
• If x is greater than the element in the root, the search continues in the right sub-tree.
This is by exploiting the binary search property. There are many applications of binary search trees. One application is
construction of dictionary.
There are many ways of constructing the binary search tree. Brute force algorithm is to construct many binary search
trees and finding the cost of the tree. How to find cost of the tree? The cost of the tree is obtained by multiplying the
probability of the item and the level of the tree. The following example illustrates the way of find this cost of the tree.
HOW TO FIND THE COST OF BINARY SEARCH TREE
Example 1: Find the cost of the tree shown in Fig. 2 where the items probability is given as follows:
a1 = 0.4 , a2= 0.3 , a3 = 0.3
Solution
As discussed earlier, the cost of the tree is obtained by multiplying the item probability and the level of the tree. The
cost of the tree is computed as follows;
a1 level=3, a2 level=2, a3 level=1
Example 2: Construct optimal binary search tree for the three items a1 = 0.4 , a2= 0.3 , a3 = 0.3 ?
Solution:
• There are many ways one can construct binary search trees. Some of the constructed
binary search trees and its cost are shown in Fig. 3.
• It can be seen the cost of the trees are respectively, 2.1, 1.3, 1.6, 1.9 and 1.9. So the minimum cost is 1.3. Hence, the
optimal binary search tree is (b) Fig. 3.
OPTIMAL BINARY SEARCH TREE(Contd..)
How to construct optimal binary search tree? The problem of optimal binary search tree is given as follows:
• Given sequence K = k1 < k2 < kn of n sorted keys, with a search probability pi for each key ki . The aim is to build a binary
search tree with minimum expected cost.
• One way is to use brute force method, by exploring all possible ways and finding the expected cost. But the method is not
practical as the number of trees possible is Catalan sequence. The Catalan number is given as follows:
Hence, five search trees are possible. In general, different BSTs are possible with n nodes. Hence, alternative way is
to explore dynamic programming approach.
• So, the idea is to examine all candidate roots ak , for 1 ≤ k ≤ n and determining all optimal BSTs containing a1,...,ak1 and
containing ak+1,...,an
The informal algorithm for constructing optimal BST based on [1,3] is given as follows:
OPTIMAL BINARY SEARCH TREE WITH DYNAMIC PROGRAMMING
• The aim of the dynamic programming approach is to fill this table for constructing optimal BST.
• What should be entry of this table? For example, to compute C[2,3] of two items, say key 2 and key 3, two possible
trees are constructed as shown below in Fig. 4 and filling the table with minimum cost.
Solution:
The initial Table is given below in Table 1,
Table 1: Initial table
It can be observed that the table entries are initial probabilities given. Then, using the recursive formula, the remaining
entries are calculated.
OPTIMAL BINARY SEARCH TREE(Contd..)
0 1 2 3 4
3 0 3/7 5/7
4 0 1/7
5 0
0 1 2 3 4
1 0 2/7 4/7 10/7 12/7
3 0 3/7 5/7
4 0 1/7
5 0
Table 4: Final
Table
OPTIMAL BINARY SEARCH TREE(Contd..)
It can be observed that minimum cost is 12/7. What about the tree structure? This can be reconstructed by
noting the minimum k in another table as shown in Table 5
0 1 2 3 4
1 1 1 3 3
2 2 3 3
3 3 3
4 4
Table 4: Minimum k
It can be seen from the table 5 that C(1,4) is 3. So the item 3 is root of the tree. Continuing this fashion, one
can find the binary search tree as shown in Fig. 5.
OPTIMAL BINARY SEARCH TREE(Contd..)
It can be seen that the constructed binary tree is optimal and balanced. The formal algorithm for constructing
optimal BST is given as follows:
ALGORITHM : OPTIMAL BINARY SEARCH TREE
ALGORITHM : OPTIMAL BINARY SEARCH TREE
Complexity Analysis :
The time efficiency is Θ (n 3 ) but can be reduced to Θ(n 2 ) by taking advantage of monotonic property of the entries.
The monotonic property is that the entry R[i,j] is always in the range between R[i,j-1] and R[i+1,j]. The space
complexity is Θ(n 2 ) as the algorithm is reduced to filling the table.
SUMMARY
• Dynamic Programming is an effective technique.