L1_Welcome Lecture
L1_Welcome Lecture
4 Hours Practical
Evaluation Policy
Components of Course Evaluation Marks
Mid Semester Examination 20
End Semester Examination 40
Lab (Continuous Evaluation) * 20
Hackathon 05
Competitive programming (Leetcode)** 10
Certification*** 5
Total 100
Foundation of the course (Prerequisite)
Other Resources:
Geekforgeeks for
Google practising questions Class Lectures
and assignments.
Lab Assignments
Dynamic
Greedy algorithms. Graph algorithms.
programming.
Computational
classes.
Introduction to algorithm
Definition :
➢ It is a combination of sequence of finite steps to
solve a problem.
Properties :
➢ It should take zero or more input.
➢ It should produce at least one output.
➢ It should terminate after finite time.
➢ Every state in the algorithm should be
deterministic.
➢ It should be programming language
independent.
Steps Required to Construct Algorithm
Problem Definition
Design the Algorithm
Flow Chart
Verification (Testing)
Implementation (Coding)
Analysis
Analysis
Using time complexity and space Complexity we can analyze the
algorithm.
Time Complexity:
T(A) = C(A) + R(A)
2. Answer will change system to system 2. Answer will not change system to system
Example 1:
main()
{ O(1)
x=y+z;
}
If any program requires a fixed amount of time for all input values then its time complexity is
said to be Constant Time Complexity.
Example 2:
main()
{
x=y+z; // Step 1: Constant time operation, O(1)
for (i=1; i<=n; i++) // Loop runs n times
{
x=y+z; // Step 2: Constant time operation inside loop,
O(1), But executed n times. i.e. O(n).
}
} //overall time complexity of the given program
is O(n).
Example 3:
main()
{
x=y+z;
for (i=1; i<=n; i++)
x=y+z;
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
x=y+z;
}
}
}
main()
{
x = y + z; // Line 1: O(1)
}
Total number of iterations is n × (n / 2) = n² / 2.
Since constant factors are ignored in Big-O notation, this gives O(n²).
Example 5:
main()
{
//The outer loop runs n times, with each iteration containing
for (i =1; i<=n; i++) the middle loop.
{
for (j=1; j<=i; j++) // The middle loop runs from 1 to i, so for each iteration of i, it runs i
{ times.
for (k=1; k<=133; k++) //This loop runs exactly 133 times, regardless of i or j. i.e. O(1)
{
x=y+z;
}
}
}
}
1. Total Number of Iterations:
• For i = 1, the middle loop runs once, with the innermost loop running 133 times.
• For i = 2, the middle loop runs twice, and each iteration contains the innermost loop running 133 times.
• Therefore, the total iterations follow this pattern:
• Simplifying, we have:
• Ignoring constants (133 and the factor of 1/2), the time complexity simplifies to O(n²).
Example 6:
main()
{
while(n>=1) // The loop continues as long as n >= 1
{
n=n/2; // In each iteration, n is divided by 2. This reduces the size of n exponentially.
}
Number of Iterations:
Let’s calculate how many times the loop runs before n becomes less than 1.
The sequence of values for n is :
n, n/2, n/4, ….
This is a geometric progression. After k iterations, n=n/2k
The loop stops when n /2k <1
Solving for k, 2k = n ⇒ k=log2(n) , Hence, total time complexity O(log n).
Example 7:
main() Number of Iterations:
{ • Let’s determine how many iterations are needed before i >
i=1;
n.
while(i<=n)
{ • In the k-th iteration, i = 2^k.
i=2*i; • The loop stops when 2^k > n.
}
} • Solving for k,
• 𝑘=log2(𝑛)
int main()
{
cout << fact(5);
return 0;
}
Thank You