Module 4 Ada New
Module 4 Ada New
Lecture Notes
on
Prepared By,
Mrs. Prathibha S ,
Assistant Professor,
Department of CSE,PESITM
Module 4-ADA (BCS401)
MODULE -4
Dynamic Programming
Dynamic Programming is a method used in mathematics and computer science to
solve complex problems by breaking them down into simpler subproblems. By solving each
subproblem only once and storing the results, it avoids redundant computations, leading to
Examples:
Input: N = 3, W = 4, weight[] = {1, 2, 3}, profit[] = {4, 5, 1}
Output: Maximum Profit =5, Item 1 and 3 are chosen.
Explanation: There are two items 1 and 3 whose tota weight is equal to 4. So the
maximum possible profit is 5. Note that we cannot put only 1 and 3 because both the items
fit to the capacity 4.
Optimal Substructure: To consider all subsets of items, there can be two cases for every
item.
Case 1: The item is included in the optimal subset.(say 1)
Case 2: The item is not included in the optimal set.(say 0)
Given N=3, weight[] = {1, 2, 3}, profit[] = {10, 15, 40}, Capacity = 6
0 0 0 0 0 0 0 0
1
w=1,v=10
3
For filling the first item in the bag: If we follow the above mentioned procedure, the
table will look like the following.
weight⇢
item⇣ / 0 1 2 3 4 5 6
0 0 0 0 0 0 0 0
1 0 10 10 10 10 10 10
3
For filling the second item:
When jthWeight = 2,
then maximum possible profit is max (10, DP[1][2-2] + 15) = max(10, 15) = 15.
When jthWeight = 3, then maximum possible profit is max(2 not put, 2 is put into bag) =
max(DP[1][3], 15+DP[1][3-2]) = max(10, 25) = 25.
weight⇢
item⇣ / 0 1 2 3 4 5 6
0 0 0 0 0 0 0 0
1
0 10 10 10 10 10 10
w=1,v=10
2
0 10 15 25 25 25 25
w=2,v=15
weight⇢
item⇣ / 0 1 2 3 4 5 6
0 0 0 0 0 0 0 0
1
0 10 10 10 10 10 10
w=1,v=10
2
0 10 15 25 25 25 25
w=2,v=15
3
w=3,v=40
0 10 15 40 50 55 65
Total capacity =6, Max Profit =65
Transitive Closure is
GREEDY METHOD
General method
The greedy method is the straight forward design technique applicable to variety of
applications. The greedy approach suggests constructing a solution through a
sequence of steps, each expanding a partially constructed solution obtained so far,
until a complete solution to the problem is reached. On each step the choice made
must be:
feasible, i.e., it has to satisfy the problem’s constraints
locally optimal, i.e., it has to be the best local choice among all feasible choices
available on that step
irrevocable, i.e., once made, it cannot be changed on subsequent steps of the
algorithm
Algorithm Greedy(a,n)
//Input a[1:n] conatins n inputs
solution =𝟇; //Intialize solution vector
for i=1 to n do
{
x=select (a);
If(feasible(solution,x)then
Solution =union(solution,x);
}
return solution
Step 1 Step 2
Take source node and start spanning
tree
Step 5
Minimum Cost=46
Step1: Sort the edges in ascending order. Here is the list after sorting.
𝑢->v cost
4->5 2
4->6 2
3->4 3
2->3 3
3->5 4
5->6 5
2->5 6
1->2 7
1->3 8
Step 2:
A->B=3
A->C=13
A->D=8
A->E=9
A->F=10
Step 2: d[B]=3
d[C]=inf
d[0] d[1] d[3] d[4] d[5] d[6] d[D]=8 &D not viisted
d[A] d[B] d[c] d[d] d[e] d[f] d[E]=9
A->A A->B A->C A->D A->E A->F d[F]=inf
0 3 inf 8 9 10 Min=d[D]=A->D=8
and D not visited
Visited
[0] [1] [2] [3] d[4] d[5] Min=8, u=D(mark D visited)
A B C D E F
1 1 0 1 0 0 Take D as intermediate node
d[E]=min+D->E= 8+3=11
d[F]=min+D->F= 8+2=10
Step 3: d[B]=3
d[C]=inf
Take A as source Node d[D]=8
d[0] d[1] d[2] d[3] d[4] d[5] d[E]=9 &E not visited
d[A] d[B] d[c] d[d] d[e] d[f] d[F]=10
A->A A->B A->C A->D A->E A->F
Intially Min=d[E]=A->E and
0 3 inf 8 9 10 E not visited
Step 4: d[B]=3
d[C]=18
Take A as source Node d[D]=8
d[0] d[1] d[2] d[3] d[4] d[5] d[E]=9
d[A] d[B] d[c] d[d] d[e] d[f] d[F]=10 &F not visited
A->A A->B A->C A->D A->E A->F
Intially Min=d[F]=A->F and
0 3 18 8 9 10 F not visited
Huffman Tree
Huffman coding is a lossless data compression algorithm. The idea is to assign variable-
length codes to input characters, lengths of the assigned codes are based on the
frequencies of corresponding characters.
The variable-length codes assigned to input characters are Prefix Codes, means the
codes (bit sequences) are assigned in such a way that the code assigned to one character
is not the prefix of code assigned to any other character. This is how Huffman Coding
makes sure that there is no ambiguity when decoding the generated bitstream.
Problem 1:
Construct a Huffmam Coding tree for the following data:
Character A B C D _
Probability 0.4 0.1 0.2 0.15 0.15
A B A C A B A D
1 010 1 011 1 010 1 000
So, 1010101110101000
b) Decode the text
1 000 1 011 1 001 010
A D A C A _ B
So,
ADACA_B