CS3401 Algorithm Unit3
CS3401 Algorithm Unit3
UNIT III
ALGORITHM DESIGN TECHNIQUES
Divide and Conquer methodology: Finding maximum and minimum - Merge sort -
Quick sort Dynamic programming: Elements of dynamic programming — Matrix-chain
multiplication - Multi stage graph — Optimal Binary Search Trees. Greedy Technique:
Elements of the greedy strategy - Activity-selection problem –- Optimal Merge pattern —
Huffman Trees.
PART A
4. Define feasibility.
Feasibility indicates that it is practical and capable of being executed within reasonable
constraints and resources.
5. Define Quick Sort.
Quick sort is the most efficient internal sorting technique. It possesses a very good
average case behavior among all sorting algorithms. Quick sort is not stable search,
but it is very fast and requires very less additional space.
1
Prepared by D.Sujatha, AP/CSE SRRCET/II/IVSEM/CS3401-ALGORITHM
Basis for
comparison Quick Sort Merge Sort
Worst case
O(n^2) O(nlogn)
complexity
Divide Step: If given array A has zero or one element, return S; it is already sorted.
Otherwise, divide A into two arrays, A1 and A2, each containing about half of the
elements of A.
2
Prepared by D.Sujatha, AP/CSE SRRCET/II/IVSEM/CS3401-ALGORITHM
Conquer Step: Combine the elements back in A by merging the sorted arrays A1
and A2 into a sorted sequence
10. List out Disadvantages of Divide and Conquer Algorithm
Conceptual difficulty
Recursion overhead
Repeated
subproblems
5
Prepared by D.Sujatha, AP/CSE SRRCET/II/IVSEM/CS3401-ALGORITHM
1. Identify the problem as an optimization problem where we need to find the best solution
among a set of possible solutions.
2. Determine the set of feasible solutions for the problem.
3. Identify the optimal substructure of the problem, meaning that the optimal solution to the
problem can be constructed from the optimal solutions of its subproblems.
4. Develop a greedy strategy to construct a feasible solution step by step, making the locally
optimal choice at each step.
Prove the correctness of the algorithm by showing that the locally optimal choices at each step
lead to a globally optimal solution.
6
Prepared by D.Sujatha, AP/CSE SRRCET/II/IVSEM/CS3401-ALGORITHM
{
solution:=0;//initialize the solution.
for i:=1 to n do
{
x:=Select(a);
if Feasible( solution, x) then
solution:=Union(solution,x);
}
return solution
30. Define multistage graphs. Give an example. (NOV/DEC 2018)
The vertex s Є s1 is called the source and the vertex t Є sk is called sink.
The principles of optimality states that “in an optimal sequence of decisions or choices,
each subsequence must be optimal”
7
Prepared by D.Sujatha, AP/CSE SRRCET/II/IVSEM/CS3401-ALGORITHM
elements.
iii. Explain merge sort with suitable example. .(AU-APR/MAY2023)
iv. Explain quick sort with suitable example.
v. What is dynamic Programming? Explain dynamic programming
solution for matrix chain multiplication.(AU-APR/MAY2023)
vi. Explain briefly multistage graph with suitable example.
vii. What is meant by optimal binary search tree? Explain with suitable
example.
viii. What is Huffman tree? Outline the steps to build a Huffman tree
using greedy algorithm design paradigm with an example.
Review
1. Define divide and conquer to apply the technique in binary search algorithm and to
analysis it. (APR/MAY 2006) (APR/MAY 2017) (R)
2. Explain in detail in merge sort give an example (APR/MAY 2008) (MAY 2016). (R)
3. What is divide and conquer strategy and explain the binary search with suitable example
problem.(NOV/DEC 2011) (R)
4. Distinguish between Quick sort and Merge sort, and arrange the following numbers in
increasing order using merge sort. (18, 29, 68, 32, 43, 37, 87, 24, 47, 50) (NOV/DEC
2011) (MAY/JUNE 2013). (A)
5. Trace the steps of Mergesort algorithm for the elements 122,25,70,175,89,90,95,102,123
and also compute its time complexity.(NOV/DEC 2012) (A) (Nov 17) (Apr 18)
6. Write an algorithm to perform binary search on a sorted list of elements. Analyze the
algorithm for the best case, average case and worst case.(APR/MAY 2011). (AN)
7. Using the divide and conquer approach to find the maximum and minimum in a set of ‘n’
elements. Also find the recurrence relation for the number of elements compared and
solve the same.(APR/MAY 2011). (A)
8. Trace maximum and minimum (using divide and conquer) algorithm for the following set
of numbers. 20, 35, 18, 8, 14, 41, 3, 39,-20. (A)
8
Prepared by D.Sujatha, AP/CSE SRRCET/II/IVSEM/CS3401-ALGORITHM
9. Write a pseudo code using divide and conquer technique for finding the position of the
largest element in an array of N numbers. (A)
10. Sort the following set of elements using merge sort: 12, 24, 8, 71, 4, 23, 6, 89, and 56.
(A)
11. Explain in detail quick sorting method. Provide a complete analysis of quick sort.
(APR/MAY 2008) (Nov/Dec 2016) (APR/MAY 2017) (AN)
12. A pair contains two numbers and its second number is on the right side of the first one in
an array. The difference of a pair is the minus result while subtracting the second number
from the first one. Implement a function which gets the maximal difference of all pairs in
an array (using divide and conquer method). (MAY/JUNE 2015) ®
13. Write the algorithm for quick sort. Provide a complete analysis of quick sort for the
given set of numbers 12,33,23,43,44,55,64,77 and 76. (13)(NOV/DEC 2018)
14. Write the quick sort algorithm and explain it with an example. Derive the worst case
and average case time complexity. (5+4+4) (APR/MAY 2019)
15. (i)Write an algorithm to construct the optimal binary search tree (or) Discuss
the algorithm for finding a minimum cost binary search trees.(8)
(ii) Explain how dynamic programming is applied to solve travelling salesperson
problem. (APR/MAY 2010)(NOV/DEC 2012)(8) (R)
16. Using Dynamic approach programming, solve the following graph using the backward
approach.(APRIL/MAY2011)(A)
18. Write the Huffman’s Algorithm. Construct the Huffman’s tree for the following data and
obtain its Huffman’s Code. (APR/AMY 2017) (A)
Characters A B C D E _
Probability 0.5 0.35 0.5 0.1 0.4 0.2
19. Explain the steps in building a Huffman Tree. Find the codes for the alphabets
given below as according to frequency (NOV/DEC 2017)
A 2
E 5
H 1
I 2
L 2
M 2
P 2
R 1
S 2
X 1
20. (i) Write the Huffman code algorithm and derive its time complexity. (5+2)
(APR/MAY 2019)
(ii) Generate the Huffman code for the following data comprising of alphabet and their
frequency.(6) (APR/MAY 2019)
a:1,b:1,c:2,d:3,e:5,f:8,g:13,h:21
10