100% found this document useful (1 vote)
30 views16 pages

MCS 211 2024 English

The document outlines an assignment for the course MCS-211: Design and Analysis of Algorithms, detailing submission deadlines, marking scheme, and a series of questions covering topics such as algorithms for prime number generation, complexity analysis, greedy algorithms, Huffman coding, quick sort, matrix multiplication, and dynamic programming. Each question requires theoretical explanations, algorithm development, and practical examples or computations. The assignment emphasizes understanding algorithm efficiency, problem-solving techniques, and the application of various algorithmic strategies.

Uploaded by

Émmý Róçk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
30 views16 pages

MCS 211 2024 English

The document outlines an assignment for the course MCS-211: Design and Analysis of Algorithms, detailing submission deadlines, marking scheme, and a series of questions covering topics such as algorithms for prime number generation, complexity analysis, greedy algorithms, Huffman coding, quick sort, matrix multiplication, and dynamic programming. Each question requires theoretical explanations, algorithm development, and practical examples or computations. The assignment emphasizes understanding algorithm efficiency, problem-solving techniques, and the application of various algorithmic strategies.

Uploaded by

Émmý Róçk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Course Code : MCS-211

Course Title : Design and Analysis of Algorithm


Assignment Number : MCA_NEW(1)/211/Assign/2024
Maximum Marks : 100
Weightage : 30%
Last Dates for Submission : 30
th
April 2024 (for January Session)
31
st
October 2024 (for July Session)
This assignment has four questions (80 Marks). Answer all questions. Rest 20 marks are foryour
viva voce. You may use illustrations and diagrams to enhance the explanations. Please
gothrough the guidelines regarding assignments given in the Programme guide for the format
ofthe presentation.

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:

b) Differentiate between Polynomial-time and exponential-time algorithms. Give an example of


one problem each for these two running times.
b) Polynomial-time vs. exponential-time algorithms:
 Polynomial-time algorithm: An algorithm is polynomial-time if its runtime is a polynomial
function of the input size. Example: Bubble sort (O(n^2)).
 Exponential-time algorithm: An algorithm is exponential-time if its runtime grows
exponentially with the input size. Example: Brute-force solution for the Traveling Salesman
Problem.

c) Using Horner's rule, evaluate the polynomial p(x)= 2x5


-5x4
-3x2+15 at x=2.
Analyse the computation time required for polynomial evaluation using Horner’s rule against
the Brute force method.

Horner's rule is more efficient than the brute-force method, especially for higher-degree
polynomials, as it reduces the number of multiplications.

d) State and explain the theorems for computing the


these theorem to find the O-notation, Ω-notation and Θ-notation for the
function: 𝑓(𝑛)= 10𝑛
3 +18𝑛
2 +1
d) Computing bounds (O, Ω, Θ) for 𝑓(𝑛) = 10𝑛^3 + 18𝑛^2 + 1:
 O-notation: 𝑓(𝑛) = O(𝑛^3)
 Ω-notation: 𝑓(𝑛) = Ω(𝑛^3)
 Θ-notation: 𝑓(𝑛) = Θ(𝑛^3)
e) Explain binary exponentiation for computing the value 519. Write the right-toleft binary
exponentiation algorithm and show its working for the exponentiation 519. Also, find the worst-
case complexity of this algorithm.
e) Binary exponentiation for computing 519:

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)).

Q2: a) What is a Greedy Approach to Problem-solving? Formulate the fractional Knapsack


Problem as an optimisation problem and write a greedy algorithm to solve this problem. Solve
the following fractional Knapsack problem using this algorithm. Show all the steps.
Suppose there is a knapsack of capacity 15 Kg and 6 items are to packed in it. The weight and
profit of the items are as under:
(p1, p2,…, p6) = (3, 2, 4, 5, 1, 6)
(w1, w2,…, w6) = (2, 1, 2, 1, 5, 1)
a) Greedy Approach and Fractional Knapsack Problem:
 Greedy Approach: A greedy algorithm makes locally optimal choices at each stage with the
hope of finding a global optimum.
 Fractional Knapsack Problem:
 Given items with weights (wi) and profits (pi) and a knapsack of capacity W.
 The goal is to maximize the total profit while staying within the knapsack capacity.
 The greedy choice is to select items with the maximum profit-to-weight ratio.

Fractional Knapsack Greedy Algorithm:


1. Calculate profit-to-weight ratios for all items.
2. Sort items based on the ratios in descending order.
3. Initialize the current weight in the knapsack as 0.
4. Iterate through sorted items:
- If adding the entire item is possible without exceeding the capacity, add it.
- Otherwise, add a fraction of the item to fill the knapsack capacity.
5. Continue until the knapsack is full or no items are left.

Example with knapsack capacity 15 Kg and items:


(p1, p2, ..., p6) = (3, 2, 4, 5, 1, 6)
(w1, w2, ..., w6) = (2, 1, 2, 1, 5, 1)

1. Calculate ratios: (p1/w1, p2/w2, ..., p6/w6) = (1.5, 2, 2, 5, 0.2, 6)


2. Sort items based on ratios: 4, 6, 2, 3, 1, 5
3. Initialize knapsack: current weight = 0, total profit = 0
4. Add items until knapsack is full:
- Add item 4 (weight 1) -> current weight = 1, total profit = 5
- Add item 6 (weight 1) -> current weight = 2, total profit = 11
- Add item 2 (weight 1) -> current weight = 3, total profit = 13
- Add fraction of item 3 (remaining capacity 4/5) -> current weight = 5, total profit = 15
- Add fraction of item 1 (remaining capacity 2/5) -> current weight = 7, total profit = 17

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.

Q3: Consider the following Graph:

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:

c) Dynamic Programming for Matrix Chain Multiplication:


 Principle of Optimality:
 Optimal solutions to subproblems can be used to construct optimal solutions to
larger problems.
 Matrix Chain Multiplication:
 Define a cost matrix M[i, j] representing the minimum cost of multiplying
matrices from Ai to Aj.
 Compute M[i, j] recursively: M[i, j] = min(M[i, k] + M[k+1, j] + dim[i-
1]*dim[k]*dim[j] for k in range(i, j)).

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.

a) Decision Problems and Optimization Problems:

 Decision Problems: These problems require a yes/no answer. Examples:

1. Decision: Is the given number prime? (Answer: Yes/No)

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:

1. Optimization: Find the shortest path between two nodes in a graph.

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:

 P (Polynomial Time): Class of problems that can be solved in polynomial time.

 Example: Finding the sum of n numbers.

 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?

Explain with the help of an example.

c) NP-Hard and NP-Complete:

 NP-Hard: Problems at least as hard as the hardest problems in NP, but not necessarily in NP.

 NP-Complete: Problems that are both in NP and NP-Hard.

Role of Reduction:

 Reduction is a method to show that one problem is at least as hard as another.

 If problem A reduces to problem B, solving B can help solve A.

 Example: Reducing the Hamiltonian Cycle problem to the Traveling Salesman problem.

d) Define the following Problems:

(i) 3-CNF SAT

(ii) Clique problem

(iii) Vertex cover problem

(iv) Graph Colouring 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.

(ii) Clique Problem:

 Given an undirected graph and an integer k, does the graph contain a clique (subset of vertices) of
size k?

(iii) Vertex Cover Problem:

 Given an undirected graph and an integer k, does the graph contain a vertex cover (subset of
vertices) of size k?

(iv) Graph Coloring Problem:

 Given an undirected graph, can the vertices be colored with at most k colors such that no two
adjacent vertices have the same color.

Check-out Our Student Support Services:-

• To visit IGNOU website - ignou.ac.in

• To know more visit ignougalaxy.in

• WhatsApp Us at 7745913167

• To Order Ready to Submit IGNOU Handwritten Assignments [Courier Home-Delivery] -


https://www.ignougalaxy.in/ignou-handwritten-assignments/

• To Order 100% Approval Guaranteed Customised Project & Synopsis-


https://www.ignougalaxy.in/ignou-project-synopsis/

• To Order Assignments Solutions PDF https://www.ignougalaxy.in/ignou-solved-assignment/

You might also like