Module 1 Notes
Module 1 Notes
MODULE 1
Definition
It takes input(s), processes them through a sequence of defined steps, and produces the
desired output.
Page 1 of 48
DAA 22CSE52
Importance of Algorithms
✅ Applications of Algorithms
Page 2 of 48
DAA 22CSE52
The Greatest Common Divisor (GCD) of two numbers is the largest number that divides
both without leaving a remainder.
Example:
GCD(12, 18) = 6
1. Euclid’s Algorithm
➤ Principle
The GCD of two numbers A and B is the same as the GCD of B and A mod B.
Mathematically:
GCD(A, B) = GCD(B, A mod B)
➤ Steps
1. Read A and B.
2. While B ≠ 0:
o Find remainder r = A mod B.
o Replace A with B and B with r.
3. When B = 0 → GCD = A.
Page 3 of 48
DAA 22CSE52
➤ Pseudocode
Input: A, B
while B ≠ 0
r=A%B
A=B
B=r
print A
➤ Example
A = 48, B = 18
• Step 1 → r = 48 % 18 = 12 → A = 18, B = 12
• Step 2 → r = 18 % 12 = 6 → A = 12, B = 6
• Step 3 → r = 12 % 6 = 0 → A = 6, B = 0
GCD = 6
➤ Time Complexity
Page 4 of 48
DAA 22CSE52
➤ Principle
Start checking from the smaller of A and B downwards and find the largest integer that
divides both.
➤ Steps
1. Read A and B.
2. Find the smaller of A and B → min.
3. Check from min down to 1:
o If both A and B are divisible by i → GCD = i.
➤ Pseudocode
Input: A, B
min = smaller of A and B
for i = min down to 1
if A % i == 0 and B % i == 0
print i
break
➤ Example
A = 48, B = 18
• min = 18
• Check: 18 → no, 17 → no, 16 → no, ..., 6 → yes
GCD = 6
Page 5 of 48
DAA 22CSE52
➤ Time Complexity
➤ Principle
This method uses prime factorization of both numbers and multiplies the common prime
factors.
➤ Steps
➤ Example
A = 48 → prime factors = 2 × 2 × 2 × 2 × 3
B = 18 → prime factors = 2 × 3 × 3
Common factors → 2 × 3 = 6
GCD = 6
➤ Pseudocode
Input: A, B
Find prime factors of A → listA
Page 6 of 48
DAA 22CSE52
➤ Time Complexity
Comparison of 3 Methods
Division &
Euclid’s Algorithm O(log n) Large numbers
remainder
Consecutive Integer
Check from min to 1 O(n) Small inputs
Check
Page 7 of 48
DAA 22CSE52
Definition:
A prime number is a natural number greater than 1 that has no positive divisors other
than 1 and itself.
Examples:
It works by repeatedly marking the multiples of each prime number starting from 2.
✅ Principle
Pseudocode
Input: n
Create a list isPrime[2...n], initialized to True
for p = 2 to sqrt(n)
if isPrime[p] == True
for i = p*p to n step p
isPrime[i] = False
for p = 2 to n
if isPrime[p] == True
print p
Step 1 → Initialize:
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30
Step 2 → Start with p = 2, mark multiples of 2 → 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,
30.
Step 3 → Next p = 3, mark multiples → 9, 12, 15, 18, 21, 24, 27, 30.
Time Complexity
Page 9 of 48
DAA 22CSE52
✅ Space Complexity
✅ Applications
✔ Factoring problems
✔ Mathematical computations requiring primes
Advantages
Disadvantages
Page 10 of 48
DAA 22CSE52
1. Introduction
Algorithm analysis helps us understand how efficiently an algorithm performs when the
input size increases. The two important aspects are:
2. Space Efficiency
➤ Definition
It includes:
➤ Space Complexity
➤ Example
Page 11 of 48
DAA 22CSE52
Space used:
3. Time Efficiency
➤ Definition
Time efficiency measures the number of basic operations an algorithm performs with
respect to the size of the input.
➤ Time Complexity
➤ Definition
A basic operation is a single step in an algorithm that takes constant time to execute.
These are the most fundamental actions like:
➤ Example
max = arr[0]
for i = 1 to n-1
if arr[i] > max
max = arr[i]
• Runs (n − 1) times.
• Time complexity → O(n).
5. Order of Growth
➤ Definition
Order of growth describes how the running time or space requirement grows with input
size.
It helps to classify algorithms based on their efficiency rather than exact execution time.
Page 13 of 48
DAA 22CSE52
sum = 0
for i = 1 to n
sum = sum + i
Page 14 of 48
DAA 22CSE52
• Some algorithms may use more memory to reduce time complexity (e.g., caching
intermediate results).
• Others may optimize space at the cost of time by recalculating values instead of
storing them.
• Analyzing both helps find a balanced solution based on problem requirements.
• Searching problems
• Sorting problems
• String matching problems
• Graph traversal problems
• Pathfinding problems
• Dynamic programming problems
• Combinatorial problems
String Processing
Page 15 of 48
DAA 22CSE52
Graph Problems
Problems
Page 16 of 48
DAA 22CSE52
• Solving recursive relations by guessing the form of the solution and proving by
substitution.
• Example:
• T(n) = 2T(n/2) + n
• Guess: T(n) = O(n log n)
• Substitute and prove.
• Recursive analysis:
o Find recurrence relation.
o Solve it using substitution or tree method.
o Example: Merge sort → O(n log n)
• Non-recursive analysis:
o Count iterations in loops.
o Multiply the number of operations by input size.
o Example: Linear search → O(n)
• Comparisons:
Page 17 of 48
DAA 22CSE52
Recursive algorithms solve problems by breaking them into smaller sub-problems and
calling themselves with reduced input size.
Page 18 of 48
DAA 22CSE52
Example 1 – Factorial
Algorithm:
factorial(n):
if n == 0 or n == 1
return 1
else
return n * factorial(n-1)
Recurrence Relation
T(n) = T(n-1) + O(1)
Solution
T(n) = T(n-1) + 1
= T(n-2) + 1 + 1
= T(n-3) + 1 + 1 + 1
...
= T(1) + (n-1) × 1
= 1 + (n-1)
= O(n)
Page 19 of 48
DAA 22CSE52
Algorithm:
fibonacci(n):
if n == 0 or n == 1
return n
else
return fibonacci(n-1) + fibonacci(n-2)
✅ Recurrence Relation
Page 20 of 48
DAA 22CSE52
Algorithm:
✅ Recurrence Relation
• Level 0 → O(n)
• Level 1 → 2 × O(n/2) = O(n)
• Level 2 → 4 × O(n/4) = O(n)
• Level log n → O(n)
Page 21 of 48
DAA 22CSE52
1. Substitution Method
Example: For merge sort → all levels have work O(n), and log n levels → O(n log n).
✅ 3. Master Theorem
Where:
Page 22 of 48
DAA 22CSE52
Example:
Page 23 of 48
DAA 22CSE52
➤ Problem 1 – Factorial
Algorithm:
factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
Recurrence Relation:
Base Case:
T(1) = O(1)
Solution:
T(n) = T(n-1) + 1
= T(n-2) + 1 + 1
= ...
= T(1) + (n-1)
= O(n)
Page 24 of 48
DAA 22CSE52
➤ Problem 2 – Fibonacci
Algorithm:
fibonacci(n):
if n == 0 or n == 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
Recurrence Relation:
Base Case:
T(1) = O(1)
Solution (Observation):
It expands like a binary tree → grows exponentially → O(2ⁿ)
Algorithm:
Page 25 of 48
DAA 22CSE52
Recurrence Relation:
Base Case:
T(1) = O(1)
• Level 0 → O(n)
• Level 1 → 2 × O(n/2) = O(n)
• Level 2 → 4 × O(n/4) = O(n)
• ...
• Level log n → O(n)
T(n) = 2T(n/2) + n
= 2 [ (n/2) log(n/2) ] + n
= n log(n/2) + n
= n (log n − log 2) + n
= n log n − n + n
Page 26 of 48
DAA 22CSE52
= n log n
Result:
T(n) = O(n log n)
Step 1: Expand
T(n) = T(n-1) + n
= T(n-2) + (n-1) + n
= T(n-3) + (n-2) + (n-1) + n
...
= T(1) + 2 + 3 + ... + n
= O(1) + (n(n+1)/2 − 1)
= O(n²)
Result:
T(n) = O(n²)
Recursion Tree:
• Level 0 → O(n)
• Level 1 → 3 × O(n/3) = O(n)
• Level 2 → 9 × O(n/9) = O(n)
• ...
Page 27 of 48
DAA 22CSE52
Recursion Tree:
• Level 0 → O(1)
• Level 1 → O(1)
• Level 2 → O(1)
• ...
• Level log₂ n → O(1)
# Example Usage
n = 50
print(f"Fibonacci({n}) =", fibonacci(n))
Page 28 of 48
DAA 22CSE52
Time Complexity
# Example Usage
n = 100
print(f"Factorial({n}) =", factorial(n))
✔ Substitution and recursion tree methods are tools to solve these relations
Page 29 of 48
DAA 22CSE52
Algorithm:
Move n disks from source to destination using auxiliary rod.
Recurrence Relation:
Base Case:
T(1) = O(1)
Solution:
T(n) = 2(2T(n-2) + 1) + 1
= 4T(n-2) + 3
= 4(2T(n-3) + 1) + 3
= 8T(n-3) + 7
= ...
= 2ⁿ − 1
Page 30 of 48
DAA 22CSE52
Algorithm:
Search for a value in a sorted array.
Recurrence Relation:
Base Case:
T(1) = O(1)
Solution:
Page 31 of 48
DAA 22CSE52
➤ Problem 6 – Exponentiation
Algorithm:
Compute xⁿ.
power(x, n):
if n == 0:
return 1
if n is even:
return power(x, n/2) * power(x, n/2)
else:
return x * power(x, n-1)
Recurrence Relation:
Base Case:
T(0) = O(1)
Solution:
T(n) = O(log n)
Page 32 of 48
DAA 22CSE52
# Example Usage
n=3
moves = tower_of_hanoi(n, 'A', 'C', 'B')
print(f"Total moves required: {moves}")
# Example Usage
arr = [1, 3, 5, 7, 9, 11, 13]
x=7
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
Page 33 of 48
DAA 22CSE52
# Example Usage
x=2
n = 10
print(f"{x}^{n} =", power(x, n))
Page 34 of 48
DAA 22CSE52
Non-Recursive Algorithms
Definition:
A non-recursive algorithm is an algorithm that solves problems without calling itself. It
relies on loops such as for, while, or other control structures to perform repetitive tasks.
Page 35 of 48
DAA 22CSE52
Example 1 – Factorial
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
# Example
n=5
print(f"Factorial({n}) =", factorial(n))
def fibonacci(n):
a, b = 0, 1
Page 36 of 48
DAA 22CSE52
for _ in range(n):
a, b = b, a + b
return a
# Example
n = 10
print(f"Fibonacci({n}) =", fibonacci(n))
# Example
Page 37 of 48
DAA 22CSE52
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
# Example
n = 29
print(f"{n} is prime:", is_prime(n))
def array_sum(arr):
Page 38 of 48
DAA 22CSE52
total = 0
for num in arr:
total += num
return total
# Example
arr = [1, 2, 3, 4, 5]
print(f"Sum of array elements: {array_sum(arr)}")
Page 39 of 48
DAA 22CSE52
Recursive Non-Recursive
Algorithm Notes
Version (Iterative) Version
Efficient iterative
Iterative with a loop or
Fibonacci Recursion approach avoids
array
repeated calls
Page 40 of 48
DAA 22CSE52
Recursive Non-Recursive
Algorithm Notes
Version (Iterative) Version
Page 41 of 48
DAA 22CSE52
Recursive Problems
Example Use
# Problem Description Base Case
Case
Multiply n with Mathematical
1 Factorial (n!) factorial(0) = 1
factorial of n-1 computations
nth term is sum of fibonacci(0) = 0, Sequence
2 Fibonacci Series
previous two fibonacci(1) = 1 generation
Recursively search
3 Binary Search low > high → not found Efficient searching
sorted array
Move disks between
4 Tower of Hanoi n = 1 → move one disk Puzzle solving
rods recursively
GCD (Euclidean Find greatest common
5 b = 0 → return a Math problems
Algorithm) divisor
Sum of Array Sum elements by
6 n = 0 → return 0 Array calculations
Elements reducing size
Check if string reads
7 Palindrome Check start >= end → True String validation
same both ends
Swap ends and reverse String
8 String Reversal start >= end → return
inside manipulation
Calculate base^exp by Power
9 Exponentiation exp = 0 → return 1
recursive multiplication calculations
Combinations Pascal’s triangle Probability
10 r = 0 or n = r → 1
(nCr) identity problems
Count number of digits
11 Count Digits n = 0 → return 0 Digit operations
by dividing by 10
Find Largest Compare elements to
12 n = 1 → return arr[0] Array analysis
Element find max
Check Armstrong Sum of powered digits Special number
13 n = 0 → return 0
Number equals number checks
Explore paths Reached destination Game or AI
14 Maze Solving
recursively to find exit → return problems
Graph Traversal Visit nodes deeply via All neighbors visited Network
15
(DFS) neighbors → return exploration
Generate Swap and rearrange Reached end of
16 Combinatorics
Permutations elements arrangement → print
Out of bounds or
Flood Fill Fill connected cells
17 different color → Image processing
Algorithm recursively
return
Page 42 of 48
DAA 22CSE52
Example Use
# Problem Description Base Case
Case
Convert decimal to Number
18 Decimal to Binary n = 0 → return ""
binary conversion
Print Numbers 1 to Recursively print Learning
19 n = 0 → return
N sequence recursion flow
Tower of Hanoi Explore advanced Algorithm
20 n = 1 → move disk
Variants Hanoi puzzles optimization
Example Use
# Problem Description Iterative Approach
Case
Multiply numbers from Use a loop to Mathematical
1 Factorial (n!)
n down to 1 multiply calculations
Tower of Hanoi Simulate moves using Use iterative
2 Puzzle solving
(Iterative) stack or loop simulation
Generate terms by Use loop and Sequence
3 Fibonacci Series
adding previous two temporary variables generation
Sum of Array Sum all elements in Loop through
4 Array processing
Elements array elements
Use while loop with Efficient
5 Binary Search Search in sorted array
boundaries searching
Prime Number Verify if a number has
6 Loop from 2 to √n Math validations
Check divisors
GCD (Euclidean Find GCD using While loop until
7 Number theory
Algorithm) repeated division remainder is 0
LCM (Least Common Find LCM using formula Find using GCD or Fraction
8
Multiple) or loop search multiples operations
Check if string or
Two-pointer
9 Palindrome Check number reads same String analysis
scanning loop
both ways
Reverse characters in a
10 String Reversal Swap using loop Text processing
string
Page 43 of 48
DAA 22CSE52
Example Use
# Problem Description Iterative Approach
Case
Multiply repeatedly Power
11 Exponentiation Calculate base^exp
in loop calculations
Calculate using factorial Use iterative Probability
12 Combinations (nCr)
formula factorials problems
Count how many digits Divide by 10 in a
13 Count Digits Number analysis
in a number loop
Find Largest Identify maximum in Scan with loop and
14 Array operations
Element array compare
Check Armstrong Sum powered digits and Loop through digits Special number
15
Number compare and sum check
Visit all cells row-wise Image and data
16 Matrix Traversal Use nested loops
or column-wise processing
Use temporary
Array
17 Array Rotation Shift elements k steps storage or swap in
manipulation
loop
Sort array using simple Bubble sort,
18 Sorting Algorithms Data organization
methods selection sort
Decimal to Binary Convert number into Loop with division Number system
19
Conversion binary and remainder conversion
Print Numbers 1 to Print sequence of Learning iteration
20 Simple loop
N numbers flow
Page 44 of 48
DAA 22CSE52
Question Bank
1. Algorithm Introduction
1. Define an algorithm.
2. What are the characteristics of a good algorithm?
3. Explain the differences between algorithm and program with examples.
4. Why is it important to analyze an algorithm?
5. Write an algorithm to find the largest element in an array.
6. Explain flowchart representation of algorithms.
3. Asymptotic Notations
19. List commonly used functions and classify them by growth rate.
Page 45 of 48
DAA 22CSE52
20. Explain O(1), O(n), O(n²), O(log n), and O(n log n) with examples.
21. Write examples where O(1) time complexity applies.
22. Compare exponential and polynomial complexities.
23. Explain space complexity with an example algorithm.
7. Graph Problems
8. Combinatorial Problems
Page 46 of 48
DAA 22CSE52
54. Draw recursion trees for merge sort and binary search.
55. How does recursion tree help in determining time complexity?
56. Explain how to sum the cost at each level of a recursion tree.
57. Solve the recurrence T(n) = 3T(n/3) + n using recursion tree method.
58. Compare recursion tree method with substitution method.
59. Solve T(n) = T(n/2) + O(1) using recursion tree.
Page 47 of 48
DAA 22CSE52
Page 48 of 48