CS3401 Algorithms
CS3401 Algorithms
CS3401 Algorithms
TECHNOLOGY (JAISAKTHI
EDUCATIONAL TRUST) CHENNAI 602 103
DEPARTMENT OF CSE
II YEAR – IV SEMESTER
CS3401 ALGORITHMS
QUESTION BANK
CS 3401- ALGORITHMS
UNIT I
INTRODUCTION
Algorithm analysis: Time and space complexity - Asymptotic Notations and its properties
Best case, Worst case and average case analysis – Recurrence relation: substitution method -
Lower bounds – searching: linear search, binary search and Interpolation Search, Pattern
search: The naïve string- matching algorithm - Rabin-Karp algorithm - Knuth-Morris-Pratt
algorithm. Sorting: Insertion sort – heap sort
PART - A
1. What do you mean by algorithm? (Nov/Dec 2008) (May/June 2013) (Apr/May 17)
(U)
An algorithm is a sequence of unambiguous for solving a problem i.e., for obtaining a
required output for any legitimate input in a finite amount of time. In addition, all
algorithms must satisfy the following criteria:
1) Input
2) Output
3) Definiteness
4) Finiteness
5) Effectiveness.
PROBLEM
ALGORITHM
10. What do you mean by “worst-case efficiency” of and algorithm? (R) (Nov 17)
The worst case efficiency of an algorithm, its efficiency for the worst-case input of size n,
which is an input or inputs of size n for which the algorithm runs the longest among all
possible inputs of that size.
14. Define the asymptotic notation “Big oh” (0)(May 2008)(April/May 2012&2013) (R)
Let, f(n) and g(n) be two non-negative functions. Let, n0 and constant c are two integers
such that no denotes some value of input similarly c is constant such that c > 0. We can
write
F(n) <= c*g(n) For all n ≥ n0
A function t(n) is said to be in O(g(n)) denoted t(n) ε O (g(n)), if t(n) is bounded above by
some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c
and some non-negative integer n0 such that
T (n) < c g (n) for n > n0
20. What are the Best, Worst and Average Case complexity of Linear Search? (R)
Best Case – O(1)
Average Case – O(N)
Worst Case – O(N)
25. Give the general plan for analyzing non recursive algorithm. (R)
Decide a parameter indicating an Input’s Size.
Identify the algorithms Basic Operation
Check whether the number of times the basic operation is executed only on thesize of
an input. If it also depends on some additional property, the worst case, average case,
and if necessary, best case efficiencies have to be investigated separately.
Using standard formulas and rules of sum manipulation either find a closedformula
for the count or, at the very least, establish its order of growth.
27. What are all the methods available for solving recurrence relations? (R)
Forward Substitution
Backward Substitution
Smoothness Rule
Master Theorem
31. Give the general plan for analyzing recursive algorithm. (R)
Decide a parameter indicating an Input’s Size.
Identify the algorithms Basic Operation
Check whether the number of times the basic operation is executed only on thesize of an
input. If it also depends on some additional property, the worst case,average case, and if
necessary, best case efficiencies have to be investigatedseparately.
Set up a recurrence relation, with an appropriate initial condition, for thenumber of times
basic operation is executed.
33. Give the General Plan for the Empirical Analysis of Algorithm Time Efficiency? (C)
1. Understand the experiment’s purpose.
2. Decide on the efficiency metric M to be measured and the measurement unit (an
operation count vs. a time unit).
3. Decide on characteristics of the input sample (its range, size, and so on).
4. Prepare a program implementing the algorithm (or algorithms) for the experimentation.
5. Generate a sample of inputs.
6. Run the algorithm (or algorithms) on the sample’s inputs and record the data observed.
7. Analyze the data obtained.
35. Give the Euclid algorithm for computing gcd(m, n) (MAY/JUN 2016) (Apr/May 17)
(APR 17) (C)
ALGORITHM Euclid_gcd(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ←m mod n
m←n
n←r
return m
Example: gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12.
41. Prove that the if f(n)=O(g(n)) and g(n)=O(f(n)),then f(n)= θg (n) (APR/MAY 2019)
If a match occurs, then the index of the item is returned. To split the list into two parts,
we use the following method −
mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])
where −
A = list
Lo = Lowest index of the list
Hi = Highest index of the list
A[n] = Value stored at index n in the list
The naïve algorithm finds all valid shifts using a loop that checks the condition P
[1.......m] = T [s+1.......s+m] for each of the n - m +1 possible value of s.
NAIVE-STRING-MATCHER (T, P)
1. n ← length [T]
2. m ← length [P]
3. for s ← 0 to n -m
4. do if P [1.....m] = T [s + 1....s + m]
5. then print "Pattern occurs with shift" s
The Rabin-Karp string matching algorithm calculates a hash value for the pattern, as well as
for each M-character subsequences of text to be compared. If the hash values are unequal,
the algorithm will determine the hash value for next M-character sequence. If the hash
values are equal, the algorithm will analyze the pattern and the M-character sequence. In
this way, there is only one comparison per text subsequence, and character matching is only
required when the hash values match.
RABIN-KARP-MATCHER (T, P, d, q)
1. n ← length [T]
2. m ← length [P]
3. h ← dm-1 mod q
4. p ← 0
5. t0 ← 0
6. for i ← 1 to m
7. do p ← (dp + P[i]) mod q
8. t0 ← (dt0+T [i]) mod q
9. for s ← 0 to n-m
10. do if p = ts
11. then if P [1.....m] = T [s+1... .s + m]
12. then "Pattern occurs with shift" s
13. If s < n-m
14. then ts+1 ← (d (ts-T [s+1]h)+T [s+m+1])mod q
45. Define Knuth-Morris-Pratt algorithm.
Knuth-Morris and Pratt introduce a linear time algorithm for the string matching
problem. A matching time of O (n) is achieved by avoiding comparison with an element
of 'S' that have previously been involved in comparison with some element of the pattern
'p' to be matched. i.e., backtracking on the string 'S' never occurs
Shape Property: Heap data structure is always a Complete Binary Tree, which means all
Heap Property: All nodes are either greater than or equal to or less than or equal to
each of its children. If the parent nodes are greater than their child nodes, heap is called a
Max-Heap, and if the parent nodes are smaller than their child nodes, heap is called Min-
Heap.
49. What are the two basic parts of Heap sort ?
Creating a Heap of the unsorted list/array.
Then a sorted array is created by repeatedly removing the largest/smallest
element from the heap, and inserting it into the array. The heap is reconstructed
after each removal.
Heap sort is not a Stable sort, and requires a constant space for sorting a list.
Heap Sort is very fast and is widely used for sorting
PART B
1. Define the asymptotic notations used for best case average case and worst case analysis?
(APIRL/MAY2009) (APRIL/MAY-2008)(R) (Apr 18)
2. How do you evaluate the performance of the algorithms? (E)
3. Explain properties of BIG (oh) Notation.(8) (MAY 2016) (R) (Nov 17)
4. What is meant by recurrence? Give one example to solve recurrence equations.
(APRIL/MAY 2012) (r)
5. (i) Distinguish between Big Oh, Theta and Omega natation. (NOV/DEC 2012) (AN)
(ii) Analyze the best, worst and average case analysis for linear search.
6. Find complexity of algorithm C (n) of the algorithm for the best, worst, average case,
(evaluate average case complexity of n=3 n mean number of inputs.) (E)
7. (i) Define Asymptotic notations. Distinguish between Asymptotic notation and
Conditional asymptotic notation. (10)(APRIL/MAY 2010)(APRIL/MAY 2011)
(Apr/May 17)
ii) Explain how the removing condition is done from the conditional asymptotic
notation with an example. (6)(NOV/DEC 2011) (R)
8. (i) Explain how analysis of linear search is done with a suitable illustration. (10)
(ii) Define recurrence equation and explain how solving recurrence equations are
done.(6) (NOV/DEC 2011) (R)
9. Explain how Time Complexity is calculated. Give an example. (APRIL/MAY 2010) (E)
12. Write an algorithm to find mean and variance of an array perform best, worst and average
case complexity, defining the notations used for each type of analysis. (APRIL/MAY
2008) (AN)
13. (i)Briefly explain the time complexity, space complexity estimation.(6) (MAY/JUNE
2013)
(ii) Write the linear search algorithm and analyze its time complexity.(10) (Nov/Dec 2016)
14. (i)Find the time complexity and space complexity of the following problems. Factorial
using recursion and Compute nth Fibonacci number using Iterative statements. (C)
1).T(n) = n
2T ( ) + 3n > 2
2 2
n=2
𝗅
n
2T ( ) + cn n > 1
2)T(n) = { 2 Where a and c are constants.
a n=1
15. Solve the following inequalities are correct (MAY/JUNE 2013) (A)
(i)5n2 − 6n = ∅(n2)
(ii) n!=O(nn)
(iii)n3 + 10n2 = 𝜃(n3)
(iv)2n22n + nlogn = 𝜃(n22n)
16. If you have to solve the searching problem for a list of n numbers, how can you take
advantage of the fact that the list is known to be sorted? Give separate answer for
17. (i) Derive the worst case analysis of merge sort using suitable illustrations.(8)(MAY
2015)
(ii) Derive a loose bound on the following equation (8) (A)
F(x) =35x8 -22x7 +14x5 – 2x4-4x2+x-15
18. Give an algorithm to check whether all the Elements in a given array of n elements are
distinct. Find the worst case complexity of the same.(8) (MAY / JUNE 2016) (A)
19. Give the recursive algorithm which finds the number of binary digits in the binary
representation of a positive decimal integer. Find the recurrence relation and complexity.
(MAY / JUNE 2016) (A)
20. State the general plan for analyzing the time efficiency of non-recursive algorithm and
explain with an example. (8) (Nov/Dec 2016) (AN)
22. Briefly explain the mathematical analysis of recursive and non-recursive algorithm. (8)
(Apr/May 2017) (R)
23. Discuss the steps in mathematical analysis for recursive algorithms. Do the same for
finding the factorial of the number. (NOV/DEC 2017)
24. Give the general plan for analyzing the time efficiency of recursive algorithm and use
recurrence to find number of moves Towers of Hanoi problem. (APR/MAY 18)
25. Consider the problem of finding the smallest and largest elements in an array of n
numbers. (APR/MAY 18)
(i) Design a presorting- based algorithm for solving this problem and determine its
efficiency class. (7)
(A) the brute force algorithm (B) this presorting based algorithm (C) the divide and conquer
algorithm.
26. (i)Prove that if g(n) is Ω(f(n)) then f(n) is O(g(n)). (5) (NOV/DEC 2018)
(ii) Discuss vrious methods used for mathematical analysis of recursive algorithms.(8)
(NOV/DEC 2018)
27. Write the asymptotic notations used for best case,average case and worst case analysi of
lgorithms. Writa an algorithm for finding maximum elements in an array. Give best,worst
and average case complexities. (NOV/DEC 2018)
(2) T(n)=T(n/3)+T(2n/3)+cn, where ‘c’ is a constant and ‘n’ is the input size.
UNIT II
GRAPH ALGORITHMS
Graph algorithms: Representations of graphs - Graph traversal: DFS – BFS - applications
- Connectivity, strong connectivity, bi-connectivity - Minimum spanning tree: Kruskal’s
and Prim’s algorithm- Shortest path: Bellman-Ford algorithm - Dijkstra’s algorithm -
Floyd-Warshall algorithm Network flow: Flow networks - Ford-Fulkerson method –
Matching: Maximum bipartite matching
In general, A graph is a collection of nodes (also called vertices) and edges (also called
arcs or links) each connecting a pair of nodes.
Example: V1 V2
1. Directed Graphs.
2. Undirected Graphs.
Example for Directed Graph: Example for undirected Graph:
VV
4. What is sub graph? - U
A subgraph G’ of graph G is a graph such that the set of vertices and set of edges of
G’ are proper subset of the set of edges of G.
Example:
V2
V1 V2
V3 V3
Example: V1 V2
V3 V4
(v1,v2) ≠ (v2,v1)
V3
7. What are undirected graphs? - U
An undirected graph is a graph, which consists of undirected edges. If (v,w) is an
undirected edge then (v,w) = (w,v)
V V
(v1,v2) = (v2,v1)
V
8. Define cycle. - U
A cycle in a graph is a path in which first and last vertex are the same.
3
A graph which has a cycle is referred to as cyclic graph.
B C
D E
Ex: 1 11
1 2
1 2
3 3
2 1 2 1
Diagram: V1 V2
V4 V3
V5
Diagram: V2
V5 V1
V4 V3
Example:
B D
Degree (A) = 0
Degree (C) = 1
Degree (D) = 3
The out degree is the numbers of edges that are exiting or leaving from the vertex V.
V1 V2 Indegree(V1) = 1
Outdegree (V1) = 2
V3 V4
Example:
A C
B D
Degree =1
This implies that the no. of vertices is even for odd degree graphs.
Example:
V V
aV V V V V z
V V V
The paths from a to z are as below:
Example: 1
Example:
2 3
Example
In the given example, after removing edge E1 the graph does not become disconnected.
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)
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree
from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explores all the adjacent nodes with all
the connecting edges at every step. The edges with the minimal weights causing no cycles
in the graph got selected.
Kruskal's Algorithm is used to find the minimum spanning tree for a connected
weighted graph. The main target of the algorithm is to find the subset of edges by using
which we can traverse every vertex of the graph. It follows the greedy approach that finds
an optimum solution at every stage instead of focusing on a global optimum.
Bellman Ford algorithm helps us find the shortest path from a vertex to all other vertices
of a weighted graph.It can work with graphs in which edges can have negative weights.
29. Define Dijkstra’s algorithm.
Dijkstra's Algorithm finds the shortest path between a given node (which is called the
"source node") and all other nodes in a graph.This algorithm uses the weights of the
edges to find the path that minimizes the total distance (weight) between the source node
and all other nodes.
The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to
find all pair shortest path problem from a given weighted graph. As a result of this
algorithm, it will generate a matrix, which will represent the minimum distance from any
node to all other nodes in the graph.
At first the output matrix is same as given cost matrix of the graph. After that the output
matrix will be updated with all vertices k as the intermediate vertex.
34. Define the single source shortest path problem. (MAY\JUNE 2016) (R)
Dijkstra’s algorithm solves the single source shortest path problem of finding
shortest paths from a given vertex( the source), to all the other vertices of a weighted
graph or digraph.
Dijkstra’s algorithm provides a correct solution for a graph with non-negative weights.
35. How to calculate the efficiency of dijkstra’s algorithm. (NOV/DEC 16) (R)
The time efficiency depends on the data structure used for implementing the
priority queue and for representing the input graph. It is in θ (|V|) 2 for graphs represented
by their weight matrix and the priority queue implemented as an unordered array. For
graphs represented by their adjacency linked lists and the priority queue implemented as
a min_heap it is in O(|E|log|V|).
39. Define maximum cardinality. (R) (NOV/DEC 2016) (R) (Apr 18)
Maximum cardinality matching—is a matching with the largest number of edges.
The maximum-matching problem is the problem offinding a maximum matching in a
given graph. For an arbitrary graph, this is arather difficult problem. It was solved in
1965 by Jack Edmonds.
42. What do you meant by perfect matching in bipartite graph? (APR/MAY 2017) (R)
When there are an equal number of nodes on each side of a bipartite graph, a
perfect matching is an assignment of nodes on the left to nodes on the right, in such a way
that
i)each node is connected by an edge to the node it is assigned to, and
ii)no two nodes on the left are assigned to the same node on the right
43. Define flow cut. (R)
Cut is a collection of arcs such that if they are removed there is no path from s to
t. A cut is said to be minimum in a network whose capacity is minimum over all cuts of
the network.
It contains only one vertex with no entering edges called as source and assumed to be 1.
It contains only one vertex at end with no leaving edges called as sink and assumed as n.
The weight uij of each directed edge (i, j) is a positive integer, called as edge capacity.
45. Define the constraint in the context of maximum flow problem. (APR/MAY 2019)
It represents the maximum amount of flow that can pass through an edge. ... , for each
(capacity constraint: the flow of an edge cannot exceed its capacity); , for each
(conservation of flows: the sum of the flows entering a node must equal the sum of
the flows exiting a node, except for the source and the sink nodes).
PART –B
1. Write an algorithm for all pairs shortest path algorithm and what are the time and space
complexity of the algorithm. (APIRL/MAY2009) (APRIL/MAY 2012) (R)
2. Explain Floyd algorithm with example. Write down and explain the algorithm to solve all
pairs shortest paths problem. (APRIL/MAY 2010)(MAY/JUNE 2013). (R)
5. Discuss about the algorithm and pseudocode to find the Minimum Spanning Tree using
Prim’s Algorithm. Find the Minimum Spanning Tree for the graph. Discuss about the
efficiency of the algorithm.(MAY\JUNE 2016) (C) (Apr 18)
6. Solve the all-Pairs shortest-path problem for the diagraph with the following weight
matrix: (NOV/DEC 2016) (A)
7. Apply Kruskal’s algorithm to find a minimum spanning tree of the following graph.
8. Explain the Dijkstra’s shortest path algorithm and its efficiency.( R) (NOV/DEC 2017)
9. Write down the Dijkstra’s algorithm and explain it with an example (8)
(APR/MAY 11) – Ap
10. Explain Dijkstra’s algorithm with example.(May/June 2012, Nov/Dec 2012) – Ap
11. Write suitable ADT operation for shortest path problem. Show the simulation of shortest
path with an example graph. (April/May 2008) – Ap
12. What is single source shortest path problem? Discuss Dijkstra’s single source shortest
path
algorithm with an example. (8) (April/May 2007) –Ap
14. Explain depth first search on a graph with necessary data structures.
(8)
(April/May
2008) – U
15. Write short notes on Biconnectivity? – U
16. Explain Dijkstra’s algorithm using the following graph. Find the shortest path between
v1, v2, v3, v4, v5, v6 & v7. 2
(May/June 2007) - Ap
V2
V1
4 1 3 10
2 7
V4
V3 V5
5 8 4 6
1 V6
V7
17. Distinguish between breadth first search and depth first search with example.(13)
(NOV/DEC 2018)
18. Explain the algorithm for Maximum Bipartite Matching. (R)
19. How do you compute maximum flow for the following graph using Ford-Fulkerson
method? (MAY 2015) (E)
u v
x y
20. State and Prove Maximum Flow Min cut Theorem. (MAY\JUNE 2016) (R)
21. Apply the shortest Augmenting Path algorithm to the network shown below.
(MAY\JUNE
2016) (A)
UNIT III
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
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.
Conquer Step: Combine the elements back in A by merging the sorted arrays A1 and
A2 into a sorted sequence
15. What are the differences between dynamic programming and divide and
conquer approaches? (NOV/DEC 2018)
Divide and Conquer
Divide and Conquer works by dividing the problem into sub-problems, conquer each sub-
problem recursively and combine these solutions.
Dynamic Programming
Dynamic Programming is a technique for solving problems with overlapping
subproblems. Each sub-problem is solved only once and the result of each sub-problem is
stored in a table ( generally implemented as an array or a hash table) for future references.
These sub-solutions may be used to obtain the original solution and the technique of
storing the sub-problem solutions is known as memoization.
16. What is the time and space complexity of Merge sort? (APR/MAY 2019)
Time complexity = θ(n log
n) Space complexity =
n+log2n
=θ(n)
21. Write the difference between the Greedy method and Dynamic
programming.(APRIL/MAY 2011)(NOV/DEC 2012) (AN)
22. Define the principle of optimality. (APR/MAY 2012) (NOV/DEC 2016) (R) (Nov
17)
It states that in an optimal sequence of decisions, each sub sequence must be
optimal. Touse dynamic programming the problem must observe the principle of
optimality thatwhatever the initial state is, remaining decisions must be optimal with
regard the statefollowing from the first decision.
25. List out the memory functions used under Dynamic programming. (MAY 2015)
(R)
Memory functions solve in a top-down manner only sub problems that are
necessary. Memory functions are an improvement of dynamic programming because they
only solve sub problems that are necessary and do it only once. However they require
more because it makes recursive calls which require additional memory.
Greedy Algorithm
Branch and Bound
Genetic Algorithm
26. State the general principle of greedy algorithm. (NOV/DEC 16) (R)
A greedy algorithm is an algorithmic paradigm that follows the problem solving
heuristic of making the locally optimal choice at each stage with the hope of finding a
global optimum.
Merge a set of sorted files of different length into a single sorted file. We need to find an
optimal solution, where the resultant file will be generated in minimum time.
If the number of sorted files are given, there are many ways to merge them into a single
sorted file. This merge can be performed pair wise. Hence, this type of merging is called
as 2-way merge patterns.
PART-B
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)
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)
17. (i) Let A ={l/119,m/96,c/247,g/283,h/72,f/77,k/92,j/19} be the letters and its frequency of
distribution in a text file. Compute a suitable Huffman coding to compress the data
effectively. (8) (MAY 2015)
(ii)Write an algorithm to construct the optimal binary search tree given the roots r(i,j)
,0≤i≤j≤n. Also prove that this could be performed in time O(n). (8) (MAY 2015) (AN)
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
UNIT IV
STATE SPACE SEARCH ALGORITHMS
Lower - Bound Arguments - P, NP NP- Complete and NP Hard Problems. Backtracking – n-
Queen problem - Hamiltonian Circuit Problem – Subset Sum Problem. Branch and Bound –
LIFO Search and FIFO search - Assignment problem – Knapsack Problem – Travelling
Salesman Problem - Approximation Algorithms for NP-Hard Problems – Travelling Salesman
problem – Knapsack problem.
PART - A
2. What are the factors that influence the efficiency of the backtracking
algorithm?(APRIL/MAY 2008) (R)
The efficiency of the backtracking algorithm depends on the following four
factors. They are:
i. The time needed to generate the next xk
ii. The number of xk satisfying the explicit constraints.
iii. The time for the bounding functions Bk
iv. The number of xk satisfying the Bk.
3. What is backtracking?(Nov/Dec 2011) (R)
Backtracking constructs solutions one component at a time and such partially constructed
solutions are evaluated as follows ._If a partially constructed solution can be developed further
without violating the problem’s constraints, it is done by taking the first remaining legitimate
option for the next component. ._If there is no legitimate option for the next component, no
alternatives for the remaining component need to be considered. In this case, the algorithm
backtracks to replace the last component of the partially constructed solution with its next option.
j i _
1
sj d (the sum s’ is too small)
13. What are the two types of constraints used in Backtracking? (R)
Explicit constraints
Implicit constraints
16. What is a promising node in the state-space tree? (R) (Nov 17)
A node in a state-space tree is said to be promising if it corresponds toa partially
constructed solution that may still lead to a complete solution.
17. What is a non-promising node in the state-space tree? (R) (Nov 17)
A node in a state-space tree is said to be promising if it corresponds toa partially
constructed solution that may still lead to a complete solution;
otherwise it is called non-promising.
18. What do leaves in the state space tree represent? (R)
Leaves in the state-space tree represent either non-promising deadends or complete solutions
found by the algorithm.
24. When can a search path are terminated in a branch-and-bound technique. (R)
A search path at the current node in a state-space tree of a branch and-bound algorithm can
be terminated if the value of the node’s bound is not better than the value of the best solution
seen so far the node represents no feasible solution because the constraints of the problem are
already violated. The subset of feasible solutions represented by the node consists of asingle
point in this case compare the value of the objective function for this feasible solution with that
of the best solution seen so far andupdate the latter with the former if the new solution is better.
25. Give the formula used to find the upper bound for knapsack problem.
A simple way to find the upper bound ‘ub’ is to add ‘v’, the total valueof the items already
selected, the product of the remaining capacity of theknapsack W-w and the best per unit payoff
among the remaining items, whichis vi+1/wi+1, ub = v + (W-w)( vi+1/wi+1)
31. What is The Euclidean minimum spanning tree problem? (MAY\JUNE 2016) (R) (Apr
18)
The Euclidean minimum spanning tree or EMST is a minimum spanning tree of a set of n
Points in the plane where the weight of the edge between each pair of points is the Euclidean
distance between those two points. In simpler terms, an EMST connects a set of dots using lines
such that the total length of all the lines is minimized and any dot can be reached from any other
by following the lines.
33. Write the formula for decision tree for searching a sorted array? (NOV/DEC 2016) (R)
34. State the reason for terminating search path at the current node in branch and bound
algorithm. (NOV/DEC 2016) (R)
The value of the node‘s bound is not better than the value of the best solution seen
so far.
The node represents no feasible solutions because the constraints of the problem are
already violated.
The subset of feasible solutions represented by the node consists of a single point
(and hence no further choices can be made)—in this case we compare the value of
the objective function for this feasible solution with that of the best solution seen so
far and update the latter with the former if the new solution is better.
PART –B
2. Apply backtracking technique to solve the following instance of subset sum problem :
S={1,3,4,5} and d=11(NOV/DEC 2006) (AN)
3. Explain subset sum problem & discuss the possible solution strategies using backtracking.
(R)
4. Write a recursive backtracking algorithm to find all the Hamiltonian cycles of a given graph.
(APIRL/MAY2009) (NOV/DEC 2008) (E)
8. Explain the algorithm for finding all m-colorings of a graph.(APRIL/MAY 2012) (R)
11. Explain the n-Queen’s problem and trace it for n=6.(APRIL/MAY 2011) (A)
14. How backtracking works on 8-Queens problem with suitable example? (MAY/JUNE 2013)
(A)
15.(i) Give the backtracking algorithm for knapsack problem. (MAY/JUNE 2013) (8)
(ii) Explain elaborately recursive backtracking algorithm. (8) (R)
16. Using backtracking, find the optimal solution to a knapsack problem for the knapsack
instance n=8, m=110,(p1…p7)=(11,21,31,33,43,53,55,65) and
(w1…w7)=(1,11,21,33,43,53,55,65). (A)
17. Write an algorithm to determine Hamiltonian cycle in a given graph using back tracking.
For the following graph determine the Hamiltonian cycle. (A)
A B
C F
D E
18. Write an algorithm to determine the Sum of Subsets for a given sum and a Set of numbers.
Draw the tree representation to solve the subset sum problem given the numbers set as {3, 5,
6, 7, 2} with the sum=15. Derive all the subsets. (A)
19. Write an algorithm for N QUEENS problem and Trace it for n=6. (R)
21. (i)Suggest an approximation algorithm for travelling salesperson problem. Assume that the
cost function satisfies the triangle inequality. (MAY 2015) (R)
(ii)Explain how job assignment problem could be solved, given n tasks and n agents where
each agent has a cost to complete each task, using branch and bound. (MAY 2015) (AN)
22. (i)The knight is placed on the first block of an empty board and, moving according to the
rules of chess, must visit each square exactly once. Solve the above problem using
backtracking procedure. (MAY 2015) (AN)
(ii)Implement an algorithm for Knapsack problem using NP-Hard approach. (MAY 2015)
(R)
23. State the subset-sum problem and Complete state-space tree of the backtracking algorithm
applied to the instance A={3, 5, 6, 7} and d=15 of the subset-sum problem.(MAY\JUNE
2016) (A)
24. (i) Draw a decision tree and find the number of key comparisons in the worst and average
cases for the three element bubble sort. (8) (NOV/DEC 2016) (AN)
(ii) Write backtracking algorithm for 4 queen’s problem and discuss the possible solution. (8)
(NOV/DEC 2016) (R)
25. Solve the following instance of knapsack problem by branch and bound algorithm W= 15.
(16) (NOV/DEC 2016) (AN)
PROFIT
ITEMS WEIGHT
40
1 5
35
2 7
3 2 18
4
4 4
10
5 5
2
6 1
26. Apply Branch and bound algorithm to solve the travelling salesman problem. (NOV/DEC
2016) (A)
27. Give the methods for Establishing Lower Bound. (NOV/DEC 17)
28. Find the optimal solution using branch and bound for the following assignment problem.
29. Elaborate on the nearest-neighbor algorithm and multifragment- heuristic algorithm for TSP
problem. (APR/MAY 2018)
30. Consider the travelling salesperson instances defined by the following cost matrix.
(NOV/DEC 2018)
Draw the state space and show the reduced matrices corresponding to each of the node.
(13)(NOV/DEC 2018)
31. Write an algorithm for subset sum and explain with an example. (13)(APR/MAY 2019)
32. Explain the 4-queens problem using backtracking. Write the algorithms . Giv the estimated
cost for all possible solutions of 4-queens problem.Specify the implicit and explicit
constrints. (APR/MAY 2019)
UNIT V
NP- Non deterministic Polynomial time solving. Problem which can't be solved in
polynomial time like TSP( travelling salesman problem)
PART –B
1. Suggest an approximation algorithm for travelling salesperson problem. Assume that the
cost function satisfies the triangle inequality. (MAY 2015) (R)
2. Implement an algorithm for Knapsack problem using NP-Hard approach. (MAY 2015) (R)
3. Discuss the approximation algorithm for NP hard problem? (NOV/DEC 2016) (R)
4. What is class NP? Discuss about any five problems for which no polynomial time for TSP
problem. (APR/MAY 2018)
5. Elaborate on the nearest-neighbor algorithm and multifragment- heuristic algorithm for TSP
problem. (APR/MAY 2018)
6. Discuss the approximation algorithm for NP-hard problem. (13)(NOV/DEC 2018)
7. Write an algorithm to solve the travelling salesman problem and prove that it is a 2 time
approximation algorithm.(13)(APR/MAY 2019)
COURSE OUTCOMES:
CO3 Make use of algorithm design techniques like divide and conquer and dynamic
programming
CO4 Use design techniques like greedy technique to solve a problem.
CO5 Use the state space tree method for solving problems
CO6 Solve problems using approximation algorithms and randomized algorithms
CO-PO MATRIX:
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO.1
2 1 3 2 - - - - 2 1 2 3
CO.2
2 1 1 1 1 - - - 1 3 3 3
CO.3
1 3 3 3 1 - - - 1 2 1 2
CO.4
1 3 3 3 1 - - - 1 2 1 2
CO.5
1 2 2 3 - - - - 2 3 3 1
CO.6
1 2 3 2 3 - - - 3 1 3 3
CO
1 2 2 2 2 - - - 2 2 2 2
CO – PSO MATRIX: