MCS 211 2024 English
MCS 211 2024 English
Q1: a) Develop an efficient algorithm to find a list of prime numbers from 100 to 1000.
a) Algorithm to find prime numbers from 100 to 1000:
Horner's rule is more efficient than the brute-force method, especially for higher-degree
polynomials, as it reduces the number of multiplications.
f) Write and explain the linear search algorithm and discuss its best and worstcase time
complexity. Show the working of the linear search algorithm for the data: 12, 11, 3, 9, 15, 20, 18,
19, 13, 8, 2, 1, 16.
f) Linear search algorithm:
Linear search has a best-case time complexity of O(1) and a worst-case time complexity of O(n).
g) What is a recurrence relation? Solve the following recurrence relations using the Master’s
method
a. 𝑇(𝑛) = 8𝑇 (
𝑛
2
)+𝑛
2
b. 𝑇(𝑛) = 𝑇 (
3𝑛
4
)+1
g) Recurrence relation and solving using Master's method:
a. 𝑇(𝑛) = 8𝑇(𝑛/2) + 𝑛^2
a = 8, b = 2, f(n) = 𝑛^2
log_b(a) = log_2(8) = 3
Since log_b(a) > f(n), the solution is Θ(n^3).
b. 𝑇(𝑛) = 𝑇(3𝑛/4) + 1
a = 1, b = 4/3, f(n) = 1
log_b(a) = log_(4/3)(1) = 0
Since log_b(a) = 0, the solution is Θ(log(n)).
b) What is the purpose of using Huffman Codes? Explain the steps of building a huffman tree.
Design the Huffman codes for the following set of characters and their frequencies: a:15, e:19,
s:5, d:6, f:4, g:7, h:8, t:10.
b) Huffman Codes:
Purpose: Huffman Codes are used for data compression, where more frequently used
characters are assigned shorter codes, reducing the overall space required.
Steps of Building a Huffman Tree:
1. Create a leaf node for each character and its frequency.
2. Create a priority queue and insert all leaf nodes.
3. While there is more than one node in the queue:
Remove two nodes of the lowest frequency.
Create a new internal node with a frequency equal to the sum of the two
nodes' frequencies.
Insert the new node back into the priority queue.
4. The remaining node is the root of the Huffman Tree.
Huffman Codes for Given Frequencies:
Construct the Huffman Tree and assign codes:
a: 0
e: 10
s: 1110
d: 1111
f: 110
g: 111
h: 0
t: 110
c) Expalin the Partition procedure of the Quick Sort algorithm. Use this procedure and quick sort
algorithm to sort the following array of size 8: [12, 9, 17, 15, 23, 19, 16,24]. Compute the worst
case and best case complexity of Quick sort algorithm.
c) Quick Sort:
Partition Procedure:
Choose a pivot element from the array.
Rearrange the elements in the array so that elements smaller than the pivot are on
the left, and elements greater are on the right.
The pivot is now in its final sorted position.
Quick Sort Algorithm:
Recursively apply the partition procedure on the left and right subarrays.
Array Sorting:
Array: [12, 9, 17, 15, 23, 19, 16, 24]
Sorted Array: [9, 12, 15, 16, 17, 19, 23, 24]
Complexity:
Worst Case: O(n^2)
Best Case (average pivot selection): O(n log n)
d) Explain the divide and conquer apprach of multiplying two matrices of large size.Also, explain
the Strassen’s matric multiplication algorithm. Find the time complexity of both these
approaches.
d) Matrix Multiplication:
Divide and Conquer Approach:
Break matrices into smaller submatrices.
Recursively compute submatrix products.
Combine submatrix products to get the final result.
Strassen’s Matrix Multiplication:
Uses fewer multiplications compared to the traditional algorithm.
Time Complexity: O(n^log2(7))
e) What is the use of Topological sorting? Write and explain the Topological sorting algorithm.
Also, compute the time complexity for the topological sorting algorithm.
e) Topological Sorting:
Use: To schedule tasks with dependencies, ensuring that a task is scheduled only after its
dependencies have been completed.
Topological Sorting Algorithm:
1. Select a vertex with in-degree 0.
2. Remove the vertex and its outgoing edges.
3. Repeat until all vertices are processed.
Time Complexity: O(V + E), where V is the number of vertices and E is the number of
edges.
a) Write the Kruskal’s algorithm and Prim’s algorithm to find the minimum cost spanning tree of
the graph given in Figure 1. Show all the steps of computation. Also, compute the time
complexity of both the algorithms.
a) Minimum Spanning Tree Algorithms:
Kruskal's Algorithm:
1. Sort all the edges in non-decreasing order of their weights.
2. Start with an empty graph. Iterate through sorted edges:
If adding the edge doesn't form a cycle, add it to the minimum spanning
tree.
3. Repeat until the tree has V-1 edges (V is the number of vertices).
Prim's Algorithm:
1. Start with an arbitrary vertex.
2. Choose the edge with the minimum weight that connects a vertex in the growing
tree to a vertex outside.
3. Repeat until the tree has V-1 edges (V is the number of vertices).
Time Complexity:
Kruskal's Algorithm: O(E log E), where E is the number of edges.
Prim's Algorithm: O(V^2) with an adjacency matrix, O(E + V log V) with an adjacency list.
b) In the Figure 1, find the shortest path from the vertex ‘a’ using Dijkstra’s shortest path
algorithm. Show all the steps of computation. Also, find the time complexity of the algorithm.
b) Dijkstra's Shortest Path Algorithm:
1. Initialize distances from the source vertex 'a' to all other vertices as infinity and set the
distance to 'a' as 0.
2. Create a priority queue and insert all vertices with their distances.
3. While the priority queue is not empty:
Extract the vertex with the minimum distance.
Update distances to adjacent vertices if a shorter path is found.
4. The final distances are the shortest paths from 'a' to all other vertices.
Time Complexity: O((V + E) log V), where V is the number of vertices and E is the number of edges.
c) What is dynamic programming? What is the principle of Optimality? Use the dynamic
programming approach to find the optimal sequence of chain multiplication of the following
matrices:
d) Make all the possible Binary Search Trees for the key values 25, 50, 75.
d) Binary Search Trees:
For key values 25, 50, 75, possible BSTs:
1. Root: 25
Left: None
Right: 50
Left: None
Right: 75
2. Root: 50
Left: 25
Left: None
Right: None
Right: 75
Left: None
Right: None
3. Root: 75
Left: 50
Left: 25
Left: None
Right: None
Right: None
Right: None
e) Explain the Knuth Morris Pratt algorithm for string matching. Use this algorithm to find a
pattern “algo” in the Text “From algae to algorithms”. Show all the steps. What is the time
complexity of this algorithm.
e) Knuth Morris Pratt (KMP) Algorithm:
Algorithm Steps:
1. Compute the prefix function (pi) for the pattern.
2. Iterate through the text and pattern:
If a mismatch occurs, use the pi function to determine the next possible
matching position.
Continue until a match is found or the end of the text is reached.
Example for "From algae to algorithms" with pattern "algo":
Prefix function: [0, 0, 0, 0]
Matching Steps:
'a': mismatch, shift by 1
'l': mismatch, shift by 1
'g': mismatch, shift by 1
'o': match found at position 17
Time Complexity: O(n + m), where n is the length of the text and m is the length of the pattern.
Q4: a) What are decision problems and Optimisation problems? Differentiate the decision problems and
Optimisation problems with the help of at least two problem statements of each.
2. Decision: Does a path exist between two given nodes in a graph? (Answer: Yes/No)
Optimization Problems: These problems involve finding the best solution among various feasible
solutions. Examples:
2. Optimization: Find the maximum profit by selecting a subset of items with weight
constraints.
b) Define P and NP class of Problems with the help of examples. How are P class of problem different
from NP class of Problems.
b) P and NP Classes:
NP (Non-deterministic Polynomial Time): Class of problems for which a solution can be verified
quickly.
Example: Given a set of cities and distances between them, finding the shortest
Hamiltonian cycle.
Difference:
P problems can be solved in polynomial time deterministically, while NP problems can be verified in
polynomial time.
c) What are NP-Hard and NP-Complete problem? What is the role of reduction?
NP-Hard: Problems at least as hard as the hardest problems in NP, but not necessarily in NP.
Role of Reduction:
Example: Reducing the Hamiltonian Cycle problem to the Traveling Salesman problem.
d) Definition of Problems:
(i) 3-CNF SAT (Satisfiability):
A Boolean formula in conjunctive normal form (CNF) where each clause has exactly three literals,
and the goal is to determine if there exists an assignment of truth values to variables that makes
the entire formula true.
Given an undirected graph and an integer k, does the graph contain a clique (subset of vertices) of
size k?
Given an undirected graph and an integer k, does the graph contain a vertex cover (subset of
vertices) of size k?
Given an undirected graph, can the vertices be colored with at most k colors such that no two
adjacent vertices have the same color.
• WhatsApp Us at 7745913167