0% found this document useful (0 votes)
9 views15 pages

L1 Time and Space Complexity concept 1

The document provides an overview of time and space complexity in algorithms, defining key concepts such as best, worst, and average case scenarios. It outlines how to calculate these complexities and introduces asymptotic notation, which describes algorithm behavior as input size increases. Additionally, it compares time and space complexity, highlighting trade-offs and includes examples like linear and binary search algorithms.

Uploaded by

apollo3122003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

L1 Time and Space Complexity concept 1

The document provides an overview of time and space complexity in algorithms, defining key concepts such as best, worst, and average case scenarios. It outlines how to calculate these complexities and introduces asymptotic notation, which describes algorithm behavior as input size increases. Additionally, it compares time and space complexity, highlighting trade-offs and includes examples like linear and binary search algorithms.

Uploaded by

apollo3122003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Complexity Analysis

Time complexity

Space complexity

asymptotic notation
What is Time Complexity?
• Definition: Time Complexity quantifies the amount of time an algorithm takes to
complete as a function of the size of its input.
• Why is it important?
• • Helps compare algorithm efficiency.
• • Predicts scalability for large inputs.
• Time Complexity Sequence
• Basic Time Complexities in Ascending Order (Best to Worst):
• 1. O(1): Constant Time – Fastest
• 2. O(log n): Logarithmic Time
• 3. O(n): Linear Time
• 4. O(n log n): Log-linear Time
• 5. O(n^2): Quadratic Time
• 6. O(2^n): Exponential Time
• 7. O(n!): Factorial Time – Slowest
• Example:
Types of Time Complexity?
•Definitions:
• Best Case: The scenario where the algorithm performs the least number of operations.

o Example: Searching for an element in an array, and it is the first element.

o Notation: Best-case complexity is represented using Ω (Omega).

• Worst Case: The scenario where the algorithm performs the maximum number of operations.

o Example: Searching for an element in an array, and it is the last element or not present.

o Notation: Worst-case complexity is represented using O (Big-O).

• Average Case: The scenario where the algorithm performs an average number of operations over all possible
inputs.

o Example: Searching for an element in an array where the element is equally likely to be at any
position.
Why Are These Important?
 Helps understand algorithm behavior under different conditions.

 Worst-case analysis ensures reliability.

 Average-case analysis predicts typical performance.

•Example: Linear Search


 Best Case: O(1) – Element found at the first position.

 Worst Case: O(n) – Element not found or at the last position.

 Average Case: O(n) – Element is in the middle on average.


How to Calculate Time Complexity
Steps to Calculate Time Complexity:

•Analyze Each Operation: Determine how many times each statement in the algorithm executes.

•Example: Loops, nested loops, and recursive calls.

•Express Execution Count as a Function of Input Size (n):

•Example: A single loop iterating n times has O(n) complexity.

•Keep the Dominant Term: Focus on the term that grows the fastest as input size increases.

•Example: For n^2 + n, the dominant term is n^2, so complexity is O(n^2).

•Ignore Constants: Drop constant coefficients and less significant terms.

•Example: 5n + 3 simplifies to O(n).


Example Calculation:
Example Calculation:
Example Calculation:
What is Space Complexity?
Definition: Space Complexity measures the amount of memory an algorithm requires relative to the size of

its input.

Key Components:

•Fixed Part: Memory used by variables, constants, and program instructions.

•Variable Part: Memory dependent on input size (e.g., arrays, recursion stack).

Why is it important?

•Ensures efficient memory usage.

•Avoids memory overflow for large inputs.

Example: Storing an array of size n takes O(n) space.


How to Calculate Space Complexity
Steps to Calculate Space Complexity:

•Fixed Memory Usage: Account for variables, constants, and program instructions.

•Example: A single integer variable requires O(1) space.

•Dynamic Memory Usage: Evaluate memory required by data structures and recursive calls.

•Example: An array of size n requires O(n) space.

•Include Recursion Stack: For recursive algorithms, add the space used by function calls.

•Example: A recursive function with depth n has O(n) stack space.


Example Calculation:
Example Calculation:
Asymptotic Notation
•Definition: Asymptotic Notation describes the behavior of an algorithm's complexity as the input size
grows infinitely large.

•Types of Asymptotic Notation:


 Big-O (O): Upper bound; worst-case scenario.

 Big-Θ (Θ): Tight bound; average-case scenario.

 Big-Ω (Ω): Lower bound; best-case scenario.

•Why is it useful?
 Standardizes complexity analysis.

 Focuses on significant terms while ignoring constants.


Comparison of Time and Space Complexity
Aspect Time Complexity Space Complexity
Focus Time taken to execute Memory required to execute
Optimizations Faster execution Efficient memory usage
Sacrificing memory for speed or
Trade-offs
vice versa

Real-World Example
Problem: Searching for an element in a sorted array.
 Binary Search Algorithm
o Time Complexity: O(log n)
o Space Complexity: O(1)
 Recursive Binary Search Algorithm
o Time Complexity: O(log n)
o Space Complexity: O(log n) (due to recursion stack)
Thank
s

You might also like