DAA UNIT -2
DAA UNIT -2
2
Greedy Algorithm
We will study
• The activity selection problem
• Element of a greedy strategy
• The problem of generating Huffman codes
4
Greedy Algorithm
•Algorithm Greedy(a,n)
//a[1:n] contains the n inputs.
{
Solution :=0;
For i=1 to n do
{
X:=select(a);
If Feasible(solution, x) then
Solution :=Union(solution,x);
}
Return solution;
}
5
Fractional Knapsack Problem
6
Fractional Knapsack Problem
Knapsack capacity: W
Goal:
wixi W and
xivi is maximum
7
Fractional Knapsack Problem
Alg.: Fractional-Knapsack (W, v[n], w[n])
5. w w – xiwi
20
E.g.: ---
$80
Item 3 30 +
Item 2 50 50
20 $100
Item 1 30
20 +
10 10 $60
9
Fractional Knapsack Problem
Greedy strategy 1:
Pick the item with the maximum value
E.g.:
W=1
w1 = 100, v1 = 2
w2 = 1, v2 = 1
Taking from the item with the maximum value:
Total value taken = v1/w1 = 2/100
Smaller than what the thief can take if choosing the other item
Total value (choose item 2) = v2/w2 = 1
10
Fractional Knapsack Problem
Greedy strategy 2:
Pick the item with the maximum value per pound vi/wi
11
REFERENCES
Text books:
• Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of India, 3rd edition 2012.
problem, Graph coloring.
Websites:
• https://www.tutorialspoint.com/data_structures_algorithms/kruskals_spanning_tree_algorithm.htm
• https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/
THANK YOU
Apex Institute of Technology
Department of Computer Science & Engineering
Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)
Huffman Coding
2
JOB SEQUENCING WITH DEADLINES
There is set of n-jobs. For any job i, is a integer deadling di≥0 and profit Pi>0, the
profit Pi is earned iff the job completed by its deadline.
•To complete a job one had to process the job on a machine for one unit of time.
Only one machine is available for processing jobs.
•A feasible solution for this problem is a subset J of jobs such that each job in this
subset can be completed by its deadline.
•The value of a feasible solution J is the sum of the profits of the jobs in J, i.e.,
∑i∈jPi
•An optimal solution is a feasible solution with maximum value.
•The problem involves identification of a subset of jobs which can be completed by
its deadline. Therefore the problem suites the subset methodology and can be
solved by the greedy method.
3
JOB SEQUENCING WITH DEADLINES
algorithm js(d, j, n)
//d=dead line, j=subset of jobs ,n=total number of jobs
// d[i]≥1 1 ≤ i ≤ n are the dead lines,
// the jobs are ordered such that p[1]≥p[2]≥---≥p[n]
//j[i] is the ith job in the optimal solution 1 ≤ i ≤ k, k subset range
{
d[0]=j[0]=0;
j[1]=1;
k=1;
for i=2 to n do{
r=k;
while((d[j[r]]>d[i]) and [d[j[r]]≠r)) do
r=r-1;
if((d[j[r]]≤d[i]) and (d[i]> r)) then
{
for q:=k to (r+1) setp-1 do j[q+1]= j[q];
j[r+1]=i;
k=k+1;
}}
return k; 4
}
Huffman Coding
5
Huffman Coding
Huffman codes: compressing data (savings of 20% to 90%)
Huffman’s greedy algorithm uses a table of the frequencies of occurrence of
each character to build up an optimal way of representing each character as a
binary string
6
Huffman Coding
Assume we are given a data file that contains only 6 symbols, namely a, b, c, d, e, f With
the following frequency table:
Find a variable length prefix-free encoding scheme that compresses this data file as much
as possible?
7
Huffman Coding
8
Huffman Coding
9
Constructing A Huffman Code
// C is a set of n characters
O(lg n)
O(lg n)
O(lg n)
10
Cost of a Tree T
11
Running time of Huffman's algorithm
12
Prefix Code
13
Huffman Code
Encoding:
Given the characters and their frequencies, perform the algorithm and
generate a code. Write the characters using the code
Decoding:
Given the Huffman tree, figure out what each character is (possible
because of prefix property)
14
Application on Huffman code
15
REFERENCES
Text books:
• Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of India, 3rd edition 2012.
problem, Graph coloring.
Websites:
• https://www.tutorialspoint.com/data_structures_algorithms/kruskals_spanning_tree_algorithm.htm
• https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/
THANK YOU
Apex Institute of Technology
Department of Computer Science & Engineering
Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)
KRUSKALS ALGORITHM
2
MST Algorithm
3
GenericAlgorithm
“Grows” a set A.
A := ;
while A not complete tree do
find a safe edge (u, v);
A := A {(u, v)}
od
4
MST Algorithm
5
Prims Algorithm
6
Prims Algorithm
7
Prim’s Algorithm
Q := V[G]; Complexity:
for each u Q do Using binary heaps: O(E lg V).
key[u] := Initialization – O(V).
od; Building initial queue – O(V).
key[r] := 0; V Extract-Min’s – O(V lgV).
[r] := NIL; E Decrease-Key’s – O(E lg V).
while Q do
u := Extract - Min(Q); Using Fibonacci heaps: O(E + V lg V).
for each v Adj[u] do (see book)
if v Q w(u, v) < key[v]
then decrease-key operation
[v] := u;
key[v] := w(u, v)
fi
od
Note: A = {(v, [v]) : v v - {r} - Q}.
od
Prims Algorithm
9
Prims Algorithm
10
Prims Algorithm
11
Prims Algorithm
12
Prims Algorithm
13
Example of Prim’s Algorithm
11 3 1 -3 Q=f
-3
d/0 e/3 f/-3
0 2
14
Example of Prim’s Algorithm
11 3 1 -3 Q=
d/0 e/3 f/-3
0 2
15
Example of Prim’s Algorithm
3 1 -3
16
MST Algorithm
17
REFERENCES
Text books:
• Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall
of India, 3rd edition 2012. problem, Graph coloring.
Websites:
• https://www.tutorialspoint.com/data_structures_algorithms/kruskals_span
ning_tree_algorithm.htm
• https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-
algorithm-greedy-algo-2/
18
THANK YOU
Apex Institute of Technology
Department of Computer Science & Engineering
Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)
2
Activity-Selection Problem
• Here are a set of start and finish times
5
An Iterative Approach
• The recursive algorithm is almost tail recursive (what is that?) but there is a
final union operation
• We let fi be the maximum finishing time for any activity in A
• The loop in lines 4-7 stops when the earliest finishing time is found
Websites:
• https://www.tutorialspoint.com/data_structures_algorithms/kruskals_spanning_tree_algorithm.htm
• https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/
THANK YOU
Apex Institute of Technology
Department of Computer Science & Engineering
Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)
2
Dynamic Programming
3
Steps of Dynamic Programming Approach
4
Applications of Dynamic Programming Approach
5
0-1 Knapsack
Problem Statement:
A thief is robbing a store and can carry a maximal weight
of W into his knapsack. There are n items and weight of ith item
is wi and the profit of selecting this item is pi. What items should
the thief take?
6
Dynamic-Programming Approach
7
Dynamic-0-1-knapsack (v, w, n, W)
8
Longest common Subsequence
9
Algorithm
10
Example
X: A,B,C,B,D,A,B and
Y: B,D,C,A,B,A
11
Algorithm for printing LCS
12
REFERENCES
Text books:
• Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd edition 2012. problem, Graph coloring.
Websites:
• https://www.javatpoint.com/longest-common-sequence-algorithm
• https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_an
d_analysis_of_algorithms_longest_common_subsequence.htm
13
THANK YOU
Apex Institute of Technology
Department of Computer Science & Engineering
Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)
OBST
2
Optimal Binary Search Tree
A set of integers are given in the sorted order and another array freq to
frequency count.
Our task is to create a binary search tree with those data to find the
minimum cost for all searches.
An auxiliary array cost[n, n] is created to solve and store the solution of
subproblems.
Cost matrix will hold the data to solve the problem in a bottom-up
manner.
3
Example
Input:
The key values as node and the frequency.
Keys = {10, 12, 20}
Frequency = {34, 8, 50}
Output:
The minimum cost is 142.
4
Possible BST from the given values
6
Matrix Chain Multiplication
7
Example
8
Number of ways for parenthesizing the matrices:
Or(A1,A2,A3.............An-1) (An)
It can be observed that after splitting the kth matrices, we are left
with two parenthesized sequence of matrices: one consist 'k'
matrices and another consist 'n-k' matrices.
9
Development of Dynamic Programming
Algorithm
10
Algortihm
11
ALGORITHM
12
Example
13
References
Text books:
Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice
Hall of India, 3rd edition 2012. problem, Graph coloring.
Websites:
https://www.tutorialspoint.com/Optimal-Binary-Search-Tree
14
THANK YOU
Apex Institute of Technology
Department of Computer Science & Engineering
Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)
2
Change making problem/Coin Change Problem
3
Example
4
Approach to Solve the Coin Change Problem
Let's say Mn is the minimum number of coins needed to make the change
for the value n.
Let's start by picking up the first coin i.e., the coin with the value d1. So, we
now need to make the value of n−d1 and Mn−d1 is the minimum number of
coins needed for this purpose. So, the total number of coins needed
are 1+Mn−d1 (1 coin because we already picked the coin with
value d1 and Mn−d1 is the minimum number of coins needed to make the
rest of the value).
Similarly, we can pick the second coin first and then attempt to get the
optimal solution for the value of n−d2 which will require Mn−d2 coins and
thus a total of 1+Mn−d2.
We can repeat the process with all the k coins and then the minimum value
of all these will be our answer. i.e., mini:di≤n {Mn−di+1}.
5
Analysis
6
Travelling Salesman Problem
7
Naive Solution
8
Using Dynamic Programming
9
Algorithm
10
Differentiate between Divide & Conquer Method vs Dynamic
Programming.
1.It deals (involves) three steps at each level of recursion: 1.It involves the sequence of four steps:
Divide the problem into a number of subproblems.
o Characterize the structure of optimal solutions.
Conquer the subproblems by solving them recursively.
Combine the solution to the subproblems into the solution for original o Recursively defines the values of optimal solutions.
subproblems.
o Compute the value of optimal solutions in a Bottom-up minimum.
3. It does more work on subproblems and hence has more time 3. It solves subproblems only once and then stores in the table.
consumption.
5. In this subproblems are independent of each other. 5. In this subproblems are interdependent.
6. For example: Merge Sort & Binary Search etc. 6. For example: Matrix Multiplication.
11
Differentiate between Dynamic Programming and Greedy
Method
1. Dynamic Programming is used to obtain the optimal 1. Greedy Method is also used to get the optimal
solution. solution.
2. In Dynamic Programming, we choose at each step, 2. In a greedy Algorithm, we make whatever choice
but the choice may depend on the solution to sub- seems best at the moment and then solve the sub-
problems. problems arising after the choice is made.
3. Less efficient as compared to a greedy approach 3. More efficient as compared to a greedy approach
5. It is guaranteed that Dynamic Programming will 5. In Greedy Method, there is no such guarantee of
generate an optimal solution using Principle of getting Optimal Solution.
Optimality.
12
13
14
15
16
References
Text books:
Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”,
Prentice Hall of India, 3rd edition 2012. problem, Graph coloring.
Horowitz, Sahni and Rajasekaran, “Fundamentals of
ComputerAlgorithms”, University Press (India), 2nd edition
Websites:
https://www.tutorialspoint.com/design_and_analysis_of_algorithm
s/design_and_analysis_of_algorithms_01_knapsack.htm
https://algorithms.tutorialhorizon.com/dynamic-programming-
subset-sum-problem/
https://www.codesdope.com/course/algorithms-coin-change/
17
THANK YOU