0% found this document useful (0 votes)
7 views24 pages

L1_Welcome Lecture

The document outlines the course structure for 'Design and Analysis of Algorithms (CSET244)', including evaluation components, prerequisites, and references. It covers various algorithmic concepts such as sorting, dynamic programming, and time complexity analysis, with examples illustrating different complexities. Additionally, it emphasizes the importance of understanding algorithm properties and provides a programming language specification (C++) for lab assignments.

Uploaded by

Jay Wardhan Suri
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)
7 views24 pages

L1_Welcome Lecture

The document outlines the course structure for 'Design and Analysis of Algorithms (CSET244)', including evaluation components, prerequisites, and references. It covers various algorithmic concepts such as sorting, dynamic programming, and time complexity analysis, with examples illustrating different complexities. Additionally, it emphasizes the importance of understanding algorithm properties and provides a programming language specification (C++) for lab assignments.

Uploaded by

Jay Wardhan Suri
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/ 24

Design and Analysis of

Lecture 1 Algorithms (CSET244)


3 Hours Lectures

Course 1 Hour Tutorial


Structure

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)

Data Structures Programming


References
Books:
S.no Book Title Authors Press
1. Introduction to Thomas H. Cormen, MIT Press
Algorithms Charles E. Leiserson,
Ronald L. Rivest, Clifford
Lab Continuous Evaluation
Stein
2. Algorithm Design Jon Kleinberg & Eva Pearson
Tardos
3. Computer Algorithms Ellis Horowitz, Sartaj Computer
Sahni, Sanguthevar Science Press
Rajasekaran
References

Other Resources:

Geekforgeeks for
Google practising questions Class Lectures
and assignments.
Lab Assignments

Programming language to follow: C++


Broad list of topics to be covered in the course

Basic of Divide and


Sorting algorithms.
algorithms. Conquer paradigm.

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)

C(A)- Compile time (Depend on Compiler)


R(A) - Run Time (Depend on Processor)
Types of Analysis
Posteriori Analysis (Relative Analysis) Priori Analysis (Absolute Analysis)

1. It is programming language of compiler and 1. It is programming language of compiler and


type of processor dependent analysis type of processor independent analysis

2. Answer will change system to system 2. Answer will not change system to system

3. Exact answer 3. Approximate Analysis


Priori Analysis (Absolute Analysis)
“It is determination of order of magnitude of a statement.”

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)

for (i = 1; i <= n; i++) // Outer Loop 1


x = y + z; // Line 2: O(n)

for (i = 1; i <= n; i++) // Outer Loop 2


{
for (j = 1; j <= n; j++) // Inner Loop
{ Overall Time Complexity
x = y + z; // Line 3: O(n^2) Summing up the individual complexities:
} • O(1) + O(n) + O(n²).
} • The dominant term is O(n²), so the overall time
complexity is O(n²).
}
Example 4:
main()
{
for (i=1; i<=n; i++) //This loop runs n times
{
for (j=1; j<=n/2; j++) //For each iteration of the outer loop, the inner loop runs n / 2
{ times.
x=y+z; // It takes O(1) time.
}
}

}
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(𝑛)

Time complexity is O(log n).


The Complexity of a recursive function

int factorial(int n) Time Complexity:


{ • The function calls itself recursively until it reaches the base case.
// Base case • For factorial(n), the number of recursive calls is `n` (e.g.,
if (n == 0) { factorial(n), factorial(n-1), ..., factorial(1), and finally factorial(0)).
return 1; • Therefore, the time complexity is O(n).
}
Space Complexity:
// Recursive case - Each function call consumes stack space, leading to a maximum
return n * factorial(n - 1); depth of recursion equal to n.
} - Thus, the space complexity due to the call stack is also O(n).
Space Optimization

unsigned factTR(unsigned int n, unsigned int a)


{
if (n <= 1)
return a;
Time Complexity: O(n)
return factTR(n - 1, n * a); Auxiliary Space: O(1)
}

unsigned int fact(unsigned int n) { return factTR(n, 1); }

int main()
{
cout << fact(5);
return 0;
}
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy