Algorithms - Last Yr Ans Key PDF
Algorithms - Last Yr Ans Key PDF
(AUTONOMOUS)
Question Paper Code: 2CS2021D404
Fourt Semester
B.E../B.Tech., (CSE)
CS3401 – ALGORITHMS
(Regulations 2021)
Part-A:
Answer the following (10 × 2 = 20 marks)
1.Write the difference between worst case, best case, and average case analysis.
Worst Case: The maximum time or space an algorithm takes for the most difficult input
of size n. It provides an upper bound on performance.
Example: Searching for an element not in the array using linear search.
Best Case: The minimum time or space an algorithm requires under the most favorable
conditions.
Example: Searching for the first element in an array using linear search.
Average Case: The expected time or space for typical inputs, assuming a probability
distribution of inputs.
Lower bounds represent the minimum amount of time or space an algorithm must take to
solve a problem, regardless of the approach used. They are significant because:
The purpose of a Minimum Spanning Tree (MST) is to connect all the vertices in a
weighted, connected, and undirected graph with:
4. Minimum Total Weight: The sum of edge weights in the MST is minimized.
5. No Cycles: The MST forms a tree, ensuring no cycles are present while still
connecting all vertices.
Bidirectional Reachability
Strongly Connected Components (SCCs)
Irreducibility
Transitivity
6. Describe the steps to find the maximum and minimum of a set of numbers using
divide and conquer.
Combine: Compare the maxima of the two halves to find the overall maximum and the
minima to find the overall minimum.
7. List out the applications of N-Queens problem.
Optimal Substructure
Overlapping Subproblems:
Memoization or Tabulation:
Bottom-Up Approach.
State and Transition
Time and Space Efficiency
Knapsack Problem
Travelling Salesman Problem (TSP)
Graph Coloring
Hamiltonian Path Problem
Subset Sum Problem
Bin Packing Problem
11a. Explain the concept of asymptotic analysis in algorithm design. Discuss the
significance of Big O, Omega, and Theta notations with examples. (13 marks)
Merge Sort:
Upper Bound (Worst- Describes the maximum time
Big O (O) O(nlogn)O(n \log
case) an algorithm can take
n)O(nlogn)
Omega Lower Bound (Best- Describes the minimum time Linear Search:
(Ω) case) the algorithm will take Ω(1)Ω(1)Ω(1)
Merge Sort:
Tight Bound (Exact Describes the exact growth
Theta (Θ) Θ(nlogn)Θ(n \log
asymptotic behavior) rate of the algorithm
n)Θ(nlogn)
11b. Describe the Knuth Morris Pratt (KMP) algorithm for string matching. How
does it improve upon the Naïve approach in terms of time complexity? (13 marks)
1. Preprocessing Phase:
o Compute the LPS array for the pattern. This array stores the length of the
longest proper prefix of the pattern that is also a suffix for each prefix of the
pattern.
o Example:
For pattern ABAB, the LPS array would be [0, 0, 1, 2].
2. Pattern Matching Phase:
o Start matching the pattern with the text.
o If a mismatch occurs, use the LPS array to shift the pattern, skipping over
characters that have already been matched, instead of starting from the
beginning of the pattern.
o The shift is determined by the value in the LPS array, reducing unnecessary
re-checks of characters.
12a. Explicate the Breadth First Search (BFS) algorithm for graph traversal. How
does BFS work in unweighted graphs and its applications. (13 marks)
Breadth First Search (BFS) is an algorithm used for traversing or searching through a
graph. It explores the graph level by level, visiting all nodes at the present depth level
before moving on to nodes at the next depth level. BFS can be applied to both directed
and undirected graphs.
1. Initialization:
o Start at a source node and mark it as visited.
o Use a queue to manage the nodes to be explored. The queue ensures that
nodes are processed in the order they are discovered (FIFO: First In, First
Out).
2. Exploration:
o Dequeue a node from the front of the queue.
o Visit all unvisited neighbors of this node, mark them as visited, and enqueue
them.
o Repeat until the queue is empty.
3. Termination:
o BFS terminates when all reachable nodes have been visited.
Time and Space Complexity:
1. Shortest Path in Unweighted Graphs: BFS is often used to find the shortest
path in unweighted graphs, as it explores nodes in order of their distance from the
source.
2. Finding Connected Components: BFS can be used to find all nodes in a
connected component of an undirected graph. Starting from a node, BFS will
explore all reachable nodes and mark them as part of the same component.
3. Cycle Detection: BFS can detect cycles in an undirected graph. If BFS
encounters a node that has already been visited and is not the immediate parent of
the current node, a cycle is present.
4. Level Order Traversal of Trees: BFS is used in tree traversal to explore nodes
level by level, often used in algorithms related to tree structures, such as printing
nodes by levels or finding the height of a tree.
5. Web Crawling: BFS is used in web crawlers to explore web pages. It starts from
an initial page and explores all the linked pages, level by level.
12b. Illustrate the Bellman Ford algorithm for finding the shortest path in a
weighted graph. Analyze its time complexity and provide an example where it can
handle negative weight edges. (13 Marks)
Bellman-Ford Algorithm for Finding the Shortest Path in a Weighted Graph (3 Marks)
The Bellman-Ford algorithm is used to find the shortest path from a single source
vertex to all other vertices in a weighted graph, and it works for graphs with negative
edge weights. It is more general than Dijkstra’s algorithm because it can handle graphs
with negative weight edges, though it cannot handle graphs with negative weight cycles.
Key Features:
1. Initialization:
o Set the distance to the source node as 0: distance[source] = 0.
o Set the distance to all other nodes as infinity: distance[v] = ∞ for all v ≠
source.
2. Relaxation (Main Step):
o For each edge in the graph, attempt to relax it. Relaxing an edge means
updating the shortest distance to a vertex if a shorter path is found.
o Relaxing an edge (u, v) with weight w means updating the distance to v if
distance[u] + w < distance[v].
Repeat the relaxation step V-1 times, where V is the number of vertices. The idea
is that in the worst case, a shortest path can have at most V-1 edges, so repeating
the relaxation for V-1 times ensures all shortest paths are found.
1. Initialization:
css
Copy code
distance[A] = 0
distance[B] = ∞
distance[C] = ∞
distance[D] = ∞
distance[E] = ∞
distance[F] = ∞
css
Copy code
distance[A] = 0
distance[B] = 4
distance[C] = -1
distance[D] = 2
distance[E] = 3
distance[F] = 2
css
Copy code
distance[A] = 0
distance[B] = 4
distance[C] = -1
distance[D] = 2
distance[E] = 3
distance[F] = 2
5. Negative Weight Cycle Detection: No further changes occur after V-1 iterations,
so there is no negative weight cycle detected in this example.
13a. Explain how quick sort works with the following example array [8,2,5,1,7,6,3,4]
(13 marks)
1. Choose a Pivot: Choose an element from the array (typically, the first, last, middle,
or random element).
2. Partition the Array: Reorganize the array so that elements less than the pivot are
on the left, and elements greater than the pivot are on the right. The pivot will be in
its final sorted position after this step.
3. Recursively Apply: Apply the same process to the left and right sub-arrays (the
elements before and after the pivot) until the base case is reached (sub-arrays of
size 1 or 0).
Example: Quick Sort on the Array [8, 2, 5, 1, 7, 6, 3, 4]
Best and Average Case: O(nlogn)O(n \log n)O(nlogn), where nnn is the number
of elements. This occurs when the pivot divides the array into two nearly equal
parts at each step.
Worst Case: O(n2)O(n^2)O(n2), which happens when the pivot is always the
smallest or largest element (e.g., if the array is already sorted or nearly sorted). This
happens because the partitioning step will result in very unbalanced sub-arrays.
Space Complexity: O(logn)O(\log n)O(logn) due to the recursion stack in the best
case (balanced partitioning).
Given a sequence of matrices, A1, A2, ..., An, where matrix Ai has dimensions p[i-
1] x p[i], we are tasked with finding the optimal parenthesization of the matrix chain
multiplication that minimizes the total number of scalar multiplications.
Given matrices:
makefile
Copy code
A1 = 10x20, A2 = 20x30, A3 = 30x40, A4 = 40x30
The goal is to find the optimal way to parenthesize the matrix chain multiplication to
minimize the scalar multiplications.
css
Copy code
m[i][j] = min{m[i][k] + m[k+1][j] + p[i-1] * p[k] *
p[j]} for all k between i and j-1.
14a. State and explain N queens problem and graph coloring problem with suitable
example. (13 Marks)
Example:
For N = 4, the board is a 4x4 grid. The goal is to place 4 queens on this board in such a
way that no two queens can attack each other. Here's a solution:
css
Copy code
. Q . .
. . . Q
Q . . .
. . Q .
In this arrangement:
Solution Approach:
Steps (2 Marks)
O(N!): The time complexity of this algorithm is factorial because there are N!
possible ways to place queens on the board.
14b. Describe Knapsack problem and travelling salesman problem with suitable
examples. (13 Marks)
The Traveling Salesman Problem (TSP) is a classic optimization problem where the
goal is to find the shortest possible route that visits each city exactly once and returns to
the origin city. It is a problem in graph theory, where cities are nodes, and the distances
between them are edges.
Problem Statement:
Given a set of n cities and the distances between every pair of cities, find the shortest
possible route that visits each city exactly once and returns to the starting city.
Example:
Suppose we have 4 cities labeled A, B, C, and D, and the distances between them are as
follows:
From/To A B C D
From/To A B C D
A 0 10 15 20
B 10 0 35 25
C 15 35 0 30
D 20 25 30 0
The task is to find the shortest possible route that visits all the cities exactly once and
returns to the starting city.
Goal: Maximize the total value of items placed in a knapsack without exceeding the
weight limit.
Example: Given items with weights and values, select the most valuable
combination without exceeding the knapsack's capacity.
Solution: Solved using Dynamic Programming with time complexity O(n * W) for
the 0/1 knapsack.
Goal: Find the shortest possible route that visits each city exactly once and returns
to the origin city.
Example: Given a set of cities and distances between them, determine the shortest
path visiting each city once and returning to the starting point.
Solution: Can be solved using Brute Force (O(n!)) or Dynamic Programming
(Held-Karp) with time complexity O(n^2 * 2^n).
15a. Illuminate the process of finding the kth smallest number in an unsorted array.
Discua the different algorithm available, such as Quickselect and compare their
time complexities. (13 Marks)
Kth smallest number: (2 Marks)
Finding the kth smallest number in an unsorted array is a common problem in computer
science. Instead of fully sorting the array to get the kth smallest element, more efficient
algorithms can be used to solve the problem faster, especially for large datasets. Below are
different algorithms that can be employed to find the kth smallest number and their
associated time complexities.
Given an unsorted array arr[] of n elements, and an integer k, we need to find the kth
smallest element in the array. For example, if arr = [7, 10, 4, 3, 20, 15] and k =
4, the 4th smallest element is 10 after sorting the array.
Algorithm:
o Sort the array in non-decreasing order and return the element at the index
k-1 (0-based index).
o Sorting can be done using algorithms like QuickSort or MergeSort.
Steps:
1. Sort the array.
2. Return the element at position k-1.
Time Complexity:
o O(n log n) for sorting (using comparison-based sorting algorithms like
MergeSort or QuickSort).
o Space Complexity: O(1) if in-place sorting is used (like QuickSort), or O(n)
for algorithms that require extra space (like MergeSort).
Pros:
o Simple to implement.
o The sorted array can be useful for other purposes as well.
Cons:
o Sorting the entire array is inefficient if you only need the kth smallest
element.
o It is not optimal for large arrays.
2. Quickselect Algorithm
Algorithm:
o Quickselect is a variation of QuickSort that selects a pivot and partitions the
array into two subarrays—one with elements smaller than the pivot and the
other with elements greater than the pivot. The process continues until the
kth smallest element is found.
Steps:
1. Choose a pivot element from the array.
2. Partition the array around the pivot, so that elements less than the pivot are on the
left and greater are on the right.
3. If the pivot’s position is k-1, return the pivot as the kth smallest element.
4. Otherwise, repeat the process recursively on the left or right subarray based on the
position of k.
o Average case: O(n). On average, Quickselect will partition the array into
two roughly equal halves and recurse into one of the subarrays, making it
linear in time.
o Worst case: O(n^2). If the pivot always happens to be the smallest or
largest element, the partitioning step will not effectively reduce the problem
size, leading to quadratic time complexity.
Space Complexity: O(1) (in-place operation with no additional space needed, apart from
recursive stack space).
Pros:
Cons:
o The worst-case time complexity is O(n^2), but this is rare if a good pivot is
chosen.
o The algorithm may need to be optimized with a better pivot selection
strategy (e.g., random pivot).
15b. . Explain P. NP, NP Complete and NP Hard problem with suitable examples.
(13 Marks)
In computational complexity theory, problems are classified based on their difficulty. The
P, NP, NP-Complete, and NP-Hard classes are central to understanding the complexity
of algorithmic problems. Below is an explanation of each class with examples.
1. P (Polynomial Time)
Definition:
P is the class of decision problems (problems with a yes/no answer) that can be
solved in polynomial time by a deterministic Turing machine.
A problem belongs to P if there exists an algorithm that solves the problem in time
proportional to a polynomial function of the size of the input.
Definition:
NP is the class of decision problems for which a proposed solution can be verified
in polynomial time by a deterministic Turing machine.
In other words, even if we don’t know how to solve the problem efficiently, we can
verify a candidate solution in polynomial time.
3. NP-Complete
Definition:
4. NP-Hard
Definition:
NP-Hard is the class of problems that are at least as hard as the hardest problems in
NP.
A problem is NP-Hard if every problem in NP can be reduced to it in polynomial
time. However, an NP-Hard problem does not necessarily belong to NP, meaning its
solution might not be verifiable in polynomial time.
Characteristics: (3 Marks)
NP-Hard problems are the hardest problems in the sense that every problem in NP
can be reduced to them in polynomial time. However, unlike NP-Complete
problems, NP-Hard problems do not need to have a solution that can be verified in
polynomial time.
PART-C
16a i. Find all possible routes using Hamiltonian circuit for the following graph
consider the starting vertex as ‘a’.
a b
e
c
d
f g
A Hamiltonian circuit is a route that visits every vertex exactly once and returns to the
starting vertex. Since the starting vertex is given as a, we need to explore all possible
routes that visit all other vertices and then return to a.
Explanation:
Summary:
The Hamiltonian circuits for the graph, considering a as the starting vertex, are:
1. a → b → d → a
2. a → d → b → a
These are all the possible Hamiltonian circuits in the graph, based on the assumption of
the given connectivity between the vertices. If the graph structure or connections are
different, the Hamiltonian circuits would change accordingly.
16.b. The company wants to find the most efficient route for a delivery truck that
needs to visit several cities. The goal is to minimize the total distance travelled while
visiting each exactly once and returning to the origin city.
The problem you're describing is the Traveling Salesman Problem (TSP), which is a
classic optimization problem in computer science and operations research. The goal is to
find the most efficient route for a delivery truck to visit several cities, each exactly once,
and return to the starting city while minimizing the total distance traveled. Here's a step-
by-step breakdown of how to approach this problem:
Given:
A set of cities.
The distance between each pair of cities.
The truck must start at a designated city (often called the origin city), visit all other
cities exactly once, and return to the origin city.
2. Constraints
3. Objective
Let’s assume you have 4 cities: A,B,C,DA, B, C, DA,B,C,D, and the distances between
the cities are given as follows:
A→B 10
A→C 15
A→D 20
B→C 35
B→D 25
C→D 30
Using Brute Force (since it's a small example), we can list all the possible routes starting
at AAA and calculate the total distance for each route:
From these, the optimal solution is the route A→B→D→C→AA \to B \to D \to C \to
AA→B→D→C→A with a total distance of 80.