Professional Documents
Culture Documents
Count the
number of
triangles in this
image???
2
Algorithmic Analysis-II
Concepts
4
Questions for this session
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
○ 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)+ (n-2) + (n-3) + T(n-4) ….. Substituting the value of T(n-3) (eqn (1))
8
Back Substitution Method (Contd…)
n-(k+1)=1
k=n-2
= 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.
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-(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-
12
InClass Activity: Analyze the Time Complexity (15 mins)
A(n)
{
if(n>1)
return (A(n-1);
}
13
Solution: InClass Activity: Analyze the Time Complexity
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
● 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?
1. Introduction to Arrays
3. Multi-Dimensional Arrays
● Arrays in C
Assignment
17
Assignment: Concept Map for Basic Data Structures
Assignment Name: Concept Map for Basic Data Structures
Instructions
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)
Instructions
Question
Find the time complexity of the given recurrence relation using the following-
19