Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views13 pages

ITA Unit 1 - Ans

The document outlines the course structure for 'Introduction to Algorithms' at Francis Xavier Engineering College, focusing on algorithm design, analysis of efficiency, and time complexity. It includes various algorithm examples, their complexities, and exercises related to recursive and non-recursive solutions. Additionally, it discusses specific algorithms like GCD, matrix multiplication, and factorial calculations, emphasizing their time and space complexities.

Uploaded by

madboyaswin2206
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
0% found this document useful (0 votes)
17 views13 pages

ITA Unit 1 - Ans

The document outlines the course structure for 'Introduction to Algorithms' at Francis Xavier Engineering College, focusing on algorithm design, analysis of efficiency, and time complexity. It includes various algorithm examples, their complexities, and exercises related to recursive and non-recursive solutions. Additionally, it discusses specific algorithms like GCD, matrix multiplication, and factorial calculations, emphasizing their time and space complexities.

Uploaded by

madboyaswin2206
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/ 13

FRANCIS XAVIER ENGINEERING COLLEGE, TIRUNELVELI

(An Autonomous Institution)

DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS

ACADEMIC YEAR: 2024-25/EVEN BATCH: 2023 – 27 SEM: 04

Course Code | Name: 21IT4601 – INTRODUCTION TO ALGORITHMS

Unit I – Introduction

Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving – Fundamentals of the


Analysis of Algorithm Efficiency – Analysis Framework – Asymptotic Notations and its properties
– Mathematical analysis for Recursive and Non-recursive algorithms.

Course Outcome – CO1: Design algorithms for computing problems and analyze the time and
space complexity of algorithms.

Important topics
● Topic 1 – Fundamentals of the Analysis of Algorithm Efficiency -Analysis Framework
● Topic 2 –Asymptotic Notations and its properties
● Topic 3 – Mathematical analysis for Recursive and Non-recursive algorithms
PART – A
Q.
Question
No
Consider the algorithm which computes the GCD of two numbers. Replace the
question marks with the appropriate variable.
ALGORITHM GCD(m, n)
//Input: Two nonnegative, not -both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠0 do
1. r ←m mod n
m ←?
n ←?
return m

Ans :
m→n n→r

What is the time complexity of the following Code:


int a = 0, i = N;
while (i > 0)
{
a += i;
i /= 2;
}
2.
Ans:

• i starts at N.
• In each iteration, i is divided by 2 (integer division).
• This is a halving process.
Number of iterations

The loop runs while i>0. Since i is divided by 2 each time, the number of
iterations is approximately:

log2(N)

This is because N can only be halved about log2(N) times before reaching 1
(and then 0).

Time Complexity

O (log N)
Given an algorithm with a time complexity of O(n^2), write how the performance
changes as the input size increases.

Ans:
3
The performance of an algorithm with O(n2) time complexity deteriorates rapidly
as the input size increases. While it may be acceptable for small inputs, it becomes
increasingly inefficient and impractical for larger inputs due to the quadratic
growth in the number of operations.
Apply Big O notation to analyze the time complexity of a given algorithm with
nested loops.
for i in range(n):
for j in range(n):
# Some operation

Ans:

4 Time Complexity Calculation:


• The outer loop runs n times.
• For each iteration of the outer loop, the inner loop runs nn times.
• Therefore, the total number of times the inner operation is executed
is n×n=n2.
Since the operation inside the inner loop takes constant time O(1), the overall time
complexity is O(n2). The time complexity of the given algorithm with nested loops
is O(n2), which means it grows quadratically with the input size n.

Compare the time complexity of a recursive and non-recursive solution for solving
factorial.

Ans:

Approach Time Complexity Space Complexity


Recursive O(n) O(n)
Non Recursive O(n) O(1)
5
• Both recursive and Non Recursive solutions have the same time complexity:
O(n).
• Recursive approach uses more space due to the recursive call stack — O(n)
space.
• Non Recursive approach is more space-efficient, using only O(1) space.
Iterative is generally preferred for factorial calculations, especially for larger n, due
to better space efficiency.
Develop an recursive algorithm for computing the sum of the first n cubes: S(n) = 13
+ 23 + ... + n3
Ans:

PART – B
Max.
Q.No Question Mar
ks
Apply the algorithm to find
a) the maximum element in a given array.
b) GCD of 2 numbers using Euclid’s algorithm
Compute the space and time complexity of both the algorithms.

Ans:
a)

Algorithm FindMax(arr):

1. max_element = arr[0] // Initialize max_element to the first element


2. FOR i = 1 TO n-1
if arr[i] > max_element
max_element = arr[i] // Update max_element if a larger value is
found
3. RETURN max_element // Return the maximum element
Time Complexity
1 i) 1. Basic Operation: 13
The basic operation is the comparison (if num > max_element), which is
performed once for each element in the array.
2. Number of Comparisons:
o For an array of size n, the loop runs exactly n times.
o Therefore, the total number of comparisons is nn.
3. Time Complexity:
o The time complexity is: T(n)=O(n)

b)
Euclid’s algorithm for computing gcd(m, n) expressed in pseudocode
ALGORITHM Euclid_gcd(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ←m mod n
m←n
Max.
Q.No Question Mar
ks
n←r
return m

Time Complexity:
• The time complexity of Euclid’s algorithm depends on the number
of steps required to reduce b to 0.
• In the worst case, the number of steps is proportional to the
number of digits in the smaller number, which is O(log(min(a, b))).
• Therefore, the time complexity is: O(log(min(a, b))).
Space Complexity:
• The algorithm uses a constant amount of space to store the
variables a and b.
• No additional space is used that depends on the input size.
• Therefore, the space complexity is: O(1)

ALGORITHM
mat(A[0 .. n- 1, 0 .. n- 1], B[0 .. n- 1, 0 .. n- 1])
for i ← 0 to n-1 do
for j ← 0 to n - 1 do
C[i, j] ← 0.0
for k ← 0 to n - 1 do
C[i, j] ← C[i, j] + A[i, kj * B[k, j]
return C
a. What does this algorithm compute?
b. What is its basic operation?
c. How many times is the basic operation executed?
d. What is the efficiency class of this algorithm?

Ans:

What does this algorithm compute?


2 i) The algorithm multiples two matrices A and B 08

What is its basic operation?


• In the innermost loop there are two operation:
multiplication and addition
• Basic operation: Multiplication
• Addition and subtraction are executed exactly
once on each repetition of the innermost loop.
c. How many times is the basic operation
executed?
• Let us set up a sum for the total number of
multiplications M(n) executed by the algorithm.
Max.
Q.No Question Mar
ks
• The total number of multiplications M(n) is
expressed by the following triple sum:

d. What is the efficiency class of this algorithm?


• The total number of multiplications M(n) is
expressed by the following triple sum:

Therefore n3= ( n3 )
• The running time of the algorithm on
multiplication

Cm→the time of one multiplication


• We would get a more accurate estimate if we took
into account the time spent on the additions, too:

where cais the time of one addition.


• The running time estimates differ only by their
multiplicative constants, not by their order of
growth.

ii) ALGORITHM Mystery(n)


05
//Input: A nonnegative integer n
Max.
Q.No Question Mar
ks
S← 0
for i ←1 to n do
S←S+ i*i
return S
a. What does this algorithm compute?
b. What is its basic operation?
c. How many times is the basic operation executed?
d. What is the efficiency class of this algorithm?

Ans:
a. What does this algorithm compute?

b. What is its basic operation?

c. How many times is the basic operation executed?

d. What is the efficiency class of this algorithm?

Analyze the space and time complexity of the algorithms below

3 i) 13
Max.
Q.No Question Mar
ks

Algorithm 1:

Time Complexity Analysis


1. Operations:
o The algorithm performs a series of arithmetic operations:
▪ Addition (a + b), multiplication (b * c), subtraction
(a + b - c), division ((a + b - c) / (a + b)), and
addition with a constant (+ 4.0).
o Each of these operations (addition, multiplication,
subtraction, division) takes constant time, i.e., O(1).
2. Total Time Complexity:
o Since all operations are performed sequentially and each
takes O(1) time, the total time complexity is:
T(a,b,c)=O(1)

Space Complexity Analysis


1. Variables:
o The algorithm uses three input variables (a, b, c) and
performs calculations without using any additional data
structures or variables.
o The space required for the input variables is O(1).
2. Total Space Complexity:
o The algorithm does not use any extra space proportional
to the input size. Therefore, the space complexity is:
S(a,b,c)=O(1)

Algorithm 2:
Time Complexity Analysis
1. Initialization:
o s = 0.0 takes O(1) time.
2. Loop:
Max.
Q.No Question Mar
ks
o The for loop runs from i = 1 to n, iterating n times.
o Inside the loop, the operation s = s + a[i] (addition and
assignment) takes O(1) time per iteration.
o Therefore, the loop contributes O(n) time.
3. Return:
o Returning s takes O(1) time.
4. Total Time Complexity:
T(n)=O(1)+O(n)+O(1)=O(n)

Space Complexity Analysis


1. Variables:
o The algorithm uses a single variable s to store the running
sum.
o It also uses a loop counter i, but this does not depend on
the input size.
o No additional data structures are used.
2. Total Space Complexity:
o The space required is constant, regardless of the input
size n
o Therefore, the space complexity is: S(n)=O(1)

Algorithm 3:

Time Complexity
The time complexity of the algorithm can be determined by the number
of recursive calls it makes. For each call to RSum(a, n), the function makes
a recursive call to RSum(a, n-1) until it reaches the base case where n ≤ 0.
• Base Case: When n ≤ 0, the function returns immediately, which
takes constant time, O(1).
• Recursive Case: For each recursive call, the function performs a
constant amount of work (addition and array access) and then
makes a recursive call with n-1.
Since the function makes n recursive calls, each taking constant time, the
overall time complexity is O(n).

Space Complexity
The space complexity of the algorithm is determined by the amount of
memory used during the execution of the algorithm, particularly the call
stack due to recursion.
• Recursive Calls: Each recursive call adds a new layer to the call
stack. Since the function makes n recursive calls, the maximum
depth of the call stack is n.
• Stack Space: Each call uses a constant amount of stack space for
the function's local variables and return address.
Therefore, the space complexity is O(n) due to the depth of the recursive
call stack.
Max.
Q.No Question Mar
ks
Examine the efficiency of factorial of a number n with the help of General
plan.
i. Set up a recurrence relation for this function's values and
solve it to determine what this algorithm computes.
ii. Set up a recurrence relation for the number of multiplications
made by this algorithm and solve it.
Ans:

ALGORITHM F(n)

//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 return 1

else return F(n − 1) * n

Recurrence relations

The last equation defines the sequence M(n) that we need to


find. This equation defines M(n) not explicitly, i.e., as a function of n,
but implicitly as a function of its value at another point, namely n − 1.
Such equations are called recurrence relations or recurrences.
Solve the recurrence relation 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1, i.e., to
4 i) find an explicit formula for 13
M(n) in terms of n only.
To determine a solution uniquely, we need an initial condition
that tells us the value with which the sequence starts. We can obtain
this value by inspecting the condition that makes the algorithm stop its
recursive calls:
if n = 0 return 1.

This tells us two things. First, since the calls stop when n = 0,
the smallest value of n for which this algorithm is executed and hence
M(n) defined is 0. Second, by inspecting the pseudocode’s exiting line,
we can see that when n = 0, the algorithm performs no multiplications.

Thus, the recurrence relation and initial condition for the algorithm’s
number of multiplications

M(n):

M(n) = M(n − 1) + 1 for n > 0, M(0) = 0 for n = 0.

Method of backward substitutions


Max.
Q.No Question Mar
ks
M(n) = M(n − 1) + 1 substitute M(n − 1) = M(n − 2) + 1

= [M(n − 2) + 1]+ 1

= M(n − 2) + 2 substitute M(n − 2) = M(n − 3) + 1

= [M(n − 3) + 1]+ 2

= M(n − 3) + 3

= M(n − i) + i

= M(n − n) + n

= n.

Therefore M(n)=n

Find the complexity of the below recurrence:


{ 3T(n-1), if n>0,
T(n) = { 1, otherwise

Ans:

Step 1: Unfolding the Recurrence


Let's unfold the recurrence for a few terms to identify a pattern:
• T(0)=1T(0)=1 (base case)
• T(1)=3T(0)=3⋅1=3T(1)=3T(0)=3⋅1=3
• T(2)=3T(1)=3⋅3=9T(2)=3T(1)=3⋅3=9
• T(3)=3T(2)=3⋅9=27T(3)=3T(2)=3⋅9=27
• T(4)=3T(3)=3⋅27=81T(4)=3T(3)=3⋅27=81
From this, we observe that T(n)=3nT(n)=3n.
5 i) 5
Step 2: Solving the Recurrence
The recurrence relation is:
T(n)=3T(n−1),T(n)=3T(n−1),
with the base case T(0)=1T(0)=1.
This is a linear homogeneous recurrence relation with constant
coefficients. To solve it, we can use the characteristic equation method.
1. Characteristic Equation:
The recurrence T(n)=3T(n−1)T(n)=3T(n−1) suggests the
characteristic equation:
r=3.r=3.
The root of this equation is r=3r=3.
2. General Solution:
The general solution for a recurrence of this form is:
Max.
Q.No Question Mar
ks
T(n)=A⋅rn,T(n)=A⋅rn,
where AA is a constant determined by the base case.
Substituting r=3r=3, we get:
T(n)=A⋅3n.T(n)=A⋅3n.
3. Solve for AA:
Use the base case T(0)=1T(0)=1:
T(0)=A⋅30=A⋅1=A.T(0)=A⋅30=A⋅1=A.
Since T(0)=1T(0)=1, we have A=1A=1.
4. Final Solution:
Substituting A=1A=1, the solution to the recurrence is:
T(n)=3n.T(n)=3n.

Step 3: Complexity
The solution T(n)=3nT(n)=3n grows exponentially with nn. Therefore,
the complexity of the recurrence is exponential, denoted as O(3n)O(3n).

Final Answer:
The complexity of the recurrence is O(3n)O(3n).

Find the complexity of the recurrence:


{ 2T(n-1) – 1, if n>0,
T(n) = { 1, otherwise

Ans:
Step 1: Unfolding the Recurrence
Let's unfold the recurrence for a few terms to identify a pattern:
• T(0)=1T(0)=1 (base case)
• T(1)=2T(0)−1=2⋅1−1=1T(1)=2T(0)−1=2⋅1−1=1
• T(2)=2T(1)−1=2⋅1−1=1T(2)=2T(1)−1=2⋅1−1=1
• T(3)=2T(2)−1=2⋅1−1=1T(3)=2T(2)−1=2⋅1−1=1
From this, we observe that T(n)=1T(n)=1 for all n≥0n≥0.
Step 2: Solving the Recurrence
The recurrence relation simplifies to:
T(n)=2T(n−1)−1,T(n)=2T(n−1)−1,
ii) 8
with the base case T(0)=1T(0)=1.
To solve this, we can use the method of iteration or substitution. Let's
use substitution.
Assume T(n)=1T(n)=1 for all nn. We verify this by substituting into the
recurrence:
T(n)=2T(n−1)−1.T(n)=2T(n−1)−1.
If T(n−1)=1T(n−1)=1, then:
T(n)=2⋅1−1=1.T(n)=2⋅1−1=1.
Thus, T(n)=1T(n)=1 satisfies the recurrence.
Step 3: Complexity
Since T(n)=1T(n)=1 for all nn, the complexity of the recurrence
is constant time, denoted as O(1)O(1).
Final Answer:
The complexity of the recurrence is O(1)O(1).
PART – C

1) Consider a mathematical puzzle where we have three rods (source, auxiliary and destination)
and n disks. The problem is to shift all n disks from source peg to destination peg using auxiliary
peg with the following constraint
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the peg and placing it on top of
another peg i.e. a disk can only be moved if it is the uppermost disk on a peg.
3) No disk may be placed on top of a smaller disk.
Develop a recursive algorithm to move disk from the source peg to destination peg using
auxiliary peg with the constraint specified above.
Find the time complexity of the algorithm that you have developed.

Ans:

Algorithm Hanoi(disk, source, dest, aux)


IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF

The number of disks n is the obvious choice for the input's size indicator, and so is moving
one disk as the algorithm's basic operation. Clearly, the number of moves M(n) depends
on n only, and we get the following recurrence equation for it:
M(n) = M(n-1)+1+M(n-1)
With the obvious initial condition M(1) = 1, the recurrence relation for the number of
moves M(n) is:
M(n) = 2M(n- 1) + 1 for n> 1, M(1) = 1.
Solving this recurrence by the same method of backward substitutions:

You might also like