DAA CT1 QB Answers
DAA CT1 QB Answers
Part B
1. Write an algorithm for Fibonacci numbers generation and
find the time complexity of an algorithm using the step count
method.
Ans.
Algorithm:
2. Forward substitution.
Ans. Forward substitution is a method for solving linear
homogeneous recurrence equations with constant coefficients.
There are two steps involved in the forward substitution, namely
1. Plug: Substitute values repeatedly.
2. Chug: Simplify the obtained expressions.
The process is continued till the closed form is obtained, which
can be confirmed by the observation of the common pattern that
emerges from the process of repeated substitution.
Also, Thus the time complexity Big O can be given as O(n), that is
the highest power of n in the resulting equation.
Disadvantages:
● Major Disadvantage is that it does not depend on the
operands. Eg. a=a+10 and a=a+1000000 have the same
step count.
● The method focuses on counting steps but overlooks
constant factors that might affect the actual execution time.
● It becomes cumbersome and error-prone for complex
algorithms with nested loops, conditional statements, and
functions with varying execution times.
Let us take an example algorithm for the sum of n numbers:
Thus, it can be said that T(n) = 2n + 3, which means the time
complexity is O(n).
1. Definiteness:
● Every step of the algorithm must be clearly defined and
unambiguous. There should be no room for interpretation or
individual choices during execution.
● Definiteness guarantees that the algorithm produces the
same output for a given input every time it is run, regardless
of the implementer or platform. This consistency is essential
for reliable and predictable behavior.
2. Finiteness:
● The algorithm must terminate after a finite number of steps
for any valid input. It should not loop indefinitely or run
forever.
● Finiteness ensures that the algorithm completes its task
within a reasonable timeframe and avoids becoming stuck in
an infinite loop. This predictability is crucial for practical
applications.
3. Correctness:
● The algorithm must produce the correct output for all valid or
correct inputs.
● Correctness ensures that the algorithm solves the problem it
is designed for and delivers accurate results. This is
paramount for applications where reliable outcomes are
critical.
4. Efficiency:
● Each instruction must be very basic so that it can be easily
carried out.
● The algorithm should use resources (time, memory) in an
optimal or near-optimal manner. This translates to
minimizing the number of steps and memory usage required
to complete the task.
● Efficiency ensures that the algorithm is practical and can be
executed within reasonable time and resource constraints.
Function search(arr, n, k)
Step 1: Start
Step 2: Set i = 0
Step 3: Repeat Step 4 till i less than n
Step 4: if arr[i] = k
return i
else
i=i+1
[end of if]
[end of loop]
Step 5: return -1
Step 6: Exit.