Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

2marks DAA

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Ln-1

1. Define algorithm visualization and list their two principal variations.


Algorithm visualization is the process of creating visual representations of
algorithms to aid in understanding their behavior, structure, and
performance. These visualizations can help in comprehending complex
algorithms, debugging, and teaching.
The two principal variations of algorithm visualization are:
● Static Visualization
● Dynamic Visualization

2. Consider the following C function and find the time complexity.


int check(int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
printf("%d", i + j);
}
}
return 0;
}
● Outer Loop (i-loop):
- Runs from ( i = 1 ) to ( i = n ).
- Therefore, it runs ( n ) times.

● Inner Loop (j-loop):


- Runs from ( j = 1 ) to ( j = n ).
- Therefore, it also runs ( n ) times.

● Operation Inside the Inner Loop:


- The `printf` function is called once for each iteration of the inner loop.
- This operation is ( O(1) ) since it takes a constant amount of time.

Total Time Complexity • O(n)×O(n)×O(1)=O( n² )

3. Write the steps to perform mathematical analysis of a non-recursive


algorithm.
● Identify basic operations.
● Count the frequency of these operations.
● Express the total count as a function of input size.
● Determine the Big-O notation. (as we have done in question 2)
4. Find gcd(415, 542) using Euclid's algorithm.
Euclidc’s algorithm :
● 542 mod 415 = 127
● 415 mod 127 = 34
● 127 mod 34 = 25
● 34 mod 25 = 9
● 25 mod 9 = 7
● 9 mod 7 = 2
● 7 mod 2 = 1
● 2 mod 1 = 0
Since the remainder is now 0, the algorithm terminates, and the GCD is the
last non-zero remainder, which is 1.

Thus, the GCD of 415 and 542 is 1.

5. Define i) Time Complexity ii) Space Complexity.


i) Time Complexity:
Measures the amount of computational time an algorithm takes as a
function of input size.It is typically expressed in Big-O notation.

ii) Space Complexity:


Measures the amount of memory an algorithm uses as a function of
input size.

6. List the factors which affect the running time of the algorithm.
Factors affecting the running time of an algorithm include
● Input size
● Algorithm complexity
● Hardware and system architecture
● Compiler optimizations
● Data structure efficiency
● Implementation details.

7. Compute the order of growth for f(n) =3n³ +20n²+5.


8. Compare the orders of growth of n(n+1) and n³.

Ln-2
1. What are the advantages of the Brute-force algorithm over other
algorithms?
● Simplicity and ease of implementation.
● Guaranteed solution if one exists.
● Applicable to a wide range of problems.

2. Determine the number of character comparisons made by the brute force


algorithm in searching for the pattern "SEARCH" in the text "INTRODUCTION
TO ALGORITHMS" in the worst case.
The pattern length ( m ) is 6.
The text length ( n ) is 27.
then,Therefore, the total number of comparisons in the worst case is:
( ( 𝑛−𝑚 )+1 ) × 𝑚 = 22 × 6 = 132

3. Write the general plan for divide and conquer algorithm strategy.
Divide:
Break the problem into smaller subproblems of the same type.
Conquer:
Solve the subproblems recursively.
Combine:
Merge the solutions of the subproblems to form the solution to the
original problem.
4. Apply Master's Theorem to solve the following recurrence equation
T(n)=2T(n/2)+n,
T(1) = 1.

5. Where do we use the closest-pair problem?


The closest-pair problem is used in computational geometry and spatial
analysis to efficiently find the pair of closest points among a set, crucial for
applications like nearest neighbor search and computational biology.
Ln-3
1. Distinguish between greedy and dynamic programming methods.
Greedy Method:
Makes the locally optimal choice at each stage with the hope of
finding a global optimum.
Dynamic Programming:
Breaks down a problem into smaller subproblems and stores the
results of these subproblems to avoid redundant computations.

2. Can you identify the problems that require presorting to solve?


Problems like searching, closest pair, and convex hull often require
presorting for efficient solutions.

3. What is meant by the principle of optimality?


The principle of optimality states that an optimal solution to a problem
contains optimal solutions to its subproblems. This principle is fundamental
to dynamic programming and ensures that local optimal solutions can be
used to construct a global optimal solution.

4. How Dynamic Programming is Used to Solve the Knapsack Problem?


Dynamic programming solves the Knapsack problem by breaking it down into
smaller subproblems and solving each subproblem only once. It uses a table
to store solutions to these subproblems, ensuring that each combination of
items and capacities is computed efficiently.

5. Differentiate Divide and Conquer vs. Dynamic Programming


Divide and Conquer: Breaks problems into independent subproblems and
combines their solutions.
Dynamic Programming: Solves overlapping subproblems, storing solutions to
avoid redundant computations, ensuring efficiency.
6. “A child buys candy valued at 41 rupees and gives a l00 rupees to the
cashier. The cashier wishes to return change using the fewest number of
coins. Use the greed method to find the solution set.
Change required: 59 rupees.
● Use the largest denomination coin (20 rupees):
59 - 20 = 39 (1 coin of 20 rupees)
39 - 20 = 19 (2 coins of 20 rupees)
● Next largest coin (10 rupees):
19 - 10 = 9 (1 coin of 10 rupees)
● Next largest coin (5 rupees):
9 - 5 = 4 (1 coin of 5 rupees)
● Next largest coin (2 rupees):
4 - 2 = 2 (1 coin of 2 rupees)
2 - 2 = 0 (2 coins of 2 rupees)
Result: The cashier returns the change using the fewest number of coins: 2
(20 rupees), 1 (10 rupees), 1 (5 rupees), and 2 (2 rupees).

7. How can we get from a system with an arbitrary coefficient matrix A to an


equivalent system with an upper triangular coeficient matrix A?
● Select a pivot element.
● Eliminate all elements below the pivot by row operations.
● Move to the next row and column.
● Repeat until the matrix is upper triangular.

Ln-4&5
1. What is a bi-connected component?
A maximal subgraph where removing any single vertex does not disconnect
the graph.
● If we remove a vertex ,it remains connected and is known as
biconnected.
● Articulation point is zero.

2. Apply the sum of subset algorithm to construct the state space tree for the
set S = {2, 5,4} and d=6
{2.4}=6
3. Classify the following problems as NP Hard or NP Complete.
Sum of Subset - NP Complete
Knapsack problem - NP Complete
Hamiltonian circuit problem - NP Complete
Assignment Problem - NP Hard

4. IfG is a forest with n vertices and k connected components, how many edges
does G have?
G is a forest with n vertices and k connected components,
No. of edges
E=n−k

5. Define: i) LC - Search ii) Branch and Bound (BB).


i) LC-Search:
Least Cost (LC) Search is a search strategy in branch and bound that
selects the next node with the least cost to expand, aiming to find
the optimal solution efficiently.
ii) Branch and Bound (BB):
Branch and Bound is an algorithm design paradigm for solving
combinatorial and optimization problems by systematically
enumerating candidate solutions while pruning suboptimal solutions
using bounds.

6. How is lower bound found by problem reduction?


Lower bounds are found by reducing the problem to a simpler version,
solving it to get a lower estimate of the optimal solution, which helps in
pruning the search space.

7. Name the three variants of transform and conquer.


Instance Simplification:
Simplifying the problem instance before solving.
Representation Change:
Changing the data representation.
Problem Reduction:
Reducing the problem to a simpler or different problem.
8. In what aspect NP-hard is different from NP-completeness?
NP-hard: At least as hard as the hardest problems in NP.
NP-complete: Both NP-hard and in NP (verifiable in polynomial time).

9. What are the advantages of using the backtracking method?

Exploration of All Solutions:


● Backtracking explores all possible solutions systematically.
Flexibility:
● It can handle problems with multiple solutions or constraints.
Optimality:
● When properly implemented, backtracking can find an optimal
solution.
Space Efficiency:
● It generally uses less memory compared to brute-force methods.

10. Find the number of binary search trees that can be constructed using 5
keys.
The Catalan number 𝐶𝑛 is calculated using the formula:


C₅ = 142

You might also like