You are on page 1of 19

Transforming Education

Ice Breaker (2 mins)

Count the
number of
triangles in this
image???

Solution: None because


its optical illusion

2
Algorithmic Analysis-II
Concepts

a) Solving Recurrence Relations


b) Analysis of Recursive Algorithms

4
Questions for this session

1. Are there any other types of algorithms except Iterative?

2. How can we analyze the performance of these algorithms?

5
Are there any other
How can we analyze
types of algorithms
the performance of
except Iterative?
these algorithms?

6
Algorithmic Analysis of Recursive Algorithms

● Analysis of recursive algorithms can result in generating recurrence relations

● Methods used for solving recurrence relations-

○ Back Substitution

○ Recursive Tree

○ Master’s Theorem

7
Back Substitution Method

● Mathematical Induction is used for solving recurrence relations using this approach

Consider the recurrence T(n) = n + T(n-1) such that n>1. Assume that T(1)=1

Solution-

T(n)= n + T(n-1)

= n+ (n-1) + T(n-2) …. Substituting the value of T(n-1)

= n+ (n-1)+ (n-2) + T(n-3) …… Substituting the value of T(n-2)

= n+ (n-1)+ (n-2) + (n-3) + T(n-4) ….. Substituting the value of T(n-3) (eqn (1))

And so on, we get

= n+ (n-1)+ (n-2) + (n-3) +....+ (n-k) + T(n-(k+1))

8
Back Substitution Method (Contd…)

Now it is given that T(1)=1. Thus for reducing T(n-(k+1)) to T(1),

n-(k+1)=1

k=n-2

Substituting this in eqn(1), we get

T(n)= n+ (n-1)+ (n-2) + (n-3) +....+ (2) + T(1)

= n+ (n-1)+ (n-2) + (n-3) +....+ (2) + (1)

= n(n+1)/2= O(n2)

9
Recursive Tree Method
● A recurrence tree can be used to figure out the time complexity by adding the work
done at all the levels.

Consider the same example, T(n) = n + T(n-1)


T(n)
/ \
n T(n-1)
If we further break down the expression T(n-1), we get following recursion tree.
T(n)
/ \
n T(n-1)
/ \
(n-1) T(n-2))

10
Recursive Tree Method (Contd…)
Breaking down further gives us following Substituting this in eqn(1), we get
T(n)
T(n)= n+ (n-1)+ (n-2) + (n-3) +....+ (2) +
/ \
T(1)
n T(n-1)
= n+ (n-1)+ (n-2) + (n-3) +....+ (2) +
/ \
(1)
(n-1) T(n-2))
/ \ = n(n+1)/2= O(n2)

(n-2) T(n-3)
And so on, we get

= n+ (n-1)+ (n-2) + (n-3) +....+ (n-k) + T(n-(k+1))

Now it is given that T(1)=1. Thus for reducing T(n-(k+1)) to T(1),

n-(k+1)=1

k=n-2
11
Master’s Theorem

● This approach works only for the recurrence relations that can be transformed into the following
type-

T(n) = aT(n/b) + f(n) where a >= 1 and b > 1

There are the following three cases:

1. If f(n) = Θ(nc) where c < Logba then T(n) = Θ(nLogba)

2. If f(n) = Θ(nc) where c = Logba then T(n) = Θ(ncLogn)

3.If f(n) = Θ(nc) where c > Logba then T(n) = Θ(f(n))

12
InClass Activity: Analyze the Time Complexity (15 mins)

Activity Type- Problem-Solving


Students can divide themselves into groups of 4
Time Allotted for the activity is 15 minutes
The solution along with appropriate explanation should be added to a .docx document and
uploaded on the platform

Question: Analyze the time complexity of the following pseudocode-

A(n)
{
if(n>1)
return (A(n-1);
}
13
Solution: InClass Activity: Analyze the Time Complexity

The recurrence relation for this algorithm is - Thus eqn(1) becomes


T(n)= 1+ T(n-1) such that n>1 and T(1)=1 T(n)= 1+1+1+1+.....+(n-1) times+T(1)
= (n-1) + 1
Analysis- =n
T(n)= 1 + T(n-1) = O(n)
= 1+ 1+ T(n-2)
= 1+1+1+T(n-3)
and so on
= 1+ 1 + 1 + 1+ 1+.........+ k times + T(n-k) …. eqn(1)
Reducing T(n-k) to T(1),
n-k=1
k=n-1

14
Learning Outcomes
In this session, you have learnt to:

1. Explain the complexity derivation of recursive algorithms/ C programs using suitable examples

2. Experiment with the methods used for analyzing the algorithms to derive the complexity of
recursive algorithms/ C programs

Go through the following learning resources on the platform

● Algorithmic Analysis-II

Self-Study:
Go through the following learning resources on the platform
● Arrays in C
● Strings in C
● Dynamic Memory Allocation in C
15 ● Structures and Unions in C
Q&A

If you have more questions, please post them in the community on the platform.

16
What Next?

In the next session the following concepts will be covered

1. Introduction to Arrays

2. Representation of Linear Arrays in Memory

3. Multi-Dimensional Arrays

Go through the following learning resources on the platform

● Arrays in C

Assignment

● Concept Map for basic data structures


● Time Complexity Analysis

17
Assignment: Concept Map for Basic Data Structures
Assignment Name: Concept Map for Basic Data Structures

Instructions

● This is a home-based assignment


● The solution should be added to a .docx document and uploaded on the platform

Question

Design a mind map for all the basic data structures that have been discussed so far.

(Note: refer the following youtube link for a quick understanding about concept/mind maps: Concept
Map)

Submission: As per the deadline specified on the platform


18
Assignment: Time Complexity Analysis
Assignment Name: Time Complexity Analysis

Instructions

● This is a home-based assignment


● The solution should be added to a .docx document and uploaded on the platform

Question

Find the time complexity of the given recurrence relation using the following-

● Back Substitution Method


● Recursive Tree Method

T(n)= 2T(n/2) + n such that n>1

Submission: As per the deadline specified on the platform

19

You might also like