What Will You Learn?
- What are asymptotic notations?
- What are the use cases for asymptotic notations?
- Why learn asymptotic notations?
- What are the common asymptotic notations?
- How can I use asymptotic notations in the real world?
- Where can I learn more about asymptotic notations?
The Basics
Asymptotic notations can improve algorithm efficiency and performance analysis.
Asymptotic notation simplifies algorithm processing time estimation even with uncertain variables.
We use asymptotic notations to estimate computer algorithm processing time and to communicate the tradeoffs.
Let’s build a solid foundation for learning asymptotic notations with use cases, key topics, and study materials.
Primary Use Cases
Algorithm Efficiency Comparison — Asymptotic notations help us choose efficient algorithms for problem-solving by analyzing their complexities.
Predicting Performance — Asymptotic notations estimate the performance of algorithms for significant inputs by modeling their runtime and space requirements.
Algorithm Design — Asymptotic notations lead to efficient algorithms needed for problem-solving.
Less Suitable Use Cases
While asymptotic notations are powerful tools, they have limitations.
Here are a few examples:
Ignoring Constant Factors — Asymptotic notations ignore constant factors impacting performance because they describe the long-term growth rate of functions rather than absolute magnitude.
Measuring Small Inputs — Asymptotic notations assume input size grows infinitely. As a result, they may not show accurate performance for smaller inputs. Higher complexity algorithms may outperform lower complexity ones for small inputs.
Precise Performance Measurement — Asymptotic notations do not consider data structure, hardware, and other factors.
Why Learn Asymptotic Notations?
To communicate and reason about algorithm efficiency and performance.
By implementing efficient algorithms, one can drastically decrease the expenses linked to computing resources.
Reasons to learn asymptotic notations:
Understanding Performance — Asymptotic notations help predict algorithm performance with increasing input data size.
Compare Algorithms — Asymptotic notations compare algorithm efficiency by analyzing Big O, Ω, or Θ. This helps determine which algorithm performs better as the input size increases.
Optimization — Knowing an algorithm’s time and space complexity helps optimize it. High time complexity suggests a need for increased efficiency.
Interviews and Exams — An understanding of asymptotic notations is helpful for computer science students and professionals.
Communication — Asymptotic notations help developers communicate code performance, especially when working in teams or reading technical docs.
Common Asymptotic Notations
Asymptotic notations describe the behavior of functions as inputs approach infinity. O, Ω, and Θ are ways to represent this behavior.
Each notation applies to a different scenario. For example, Big O describes the upper bound of an algorithm’s complexity, while Big Ω describes the lower bound.
Big O (O) Notation
Big O notation sets an upper bound on an algorithm’s runtime or space complexity. O(n) means the complexity grows linearly with input size.
⬆️
It describes worst-case performance.
- Big O Notation (O) is introduced by Programmer 1.
- It’s explained that Big O Notation sets an upper bound on runtime or space complexity.
- Programmer 1 states that bigger input sizes mean more complexity, so be cautious.
Imagine your mom telling you no matter how messy your room is, it won’t take more than an hour. This is an upper bound or worst-case scenario. If your room is dirty, it might take an hour. But if it’s manageable, you might finish faster.
def clean_room(room):
"""
This function represents cleaning a room, where each item takes some time.
Its time complexity is O(n), where n is the number of items in the room.
"""
total_time = 0
for item in room:
total_time += item['cleaning_time_minutes']
return total_time
# Let's define a room with some items.
# Each item has a 'cleaning_time_minutes' property representing minutes to clean.
room = [
{'item': 'bed', 'cleaning_time_minutes': 10},
{'item': 'desk', 'cleaning_time_minutes': 15},
{'item': 'floor', 'cleaning_time_minutes': 20},
{'item': 'closet', 'cleaning_time_minutes': 15},
]
print(clean_room(room)) # Output: 60
Omega (Ω) Notation
Omega notation defines the lower bound of an algorithm’s growth rate, such as Ω(n), denoting the function grows linearly with the input size.
⬇️
It describes best-case performance.
- Omega Notation (Ω) is introduced by Programmer 2.
- It’s explained that Omega Notation defines the lower bound of an algorithm’s growth rate.
- Programmer 2 states that this example denotes a function that grows linearly with the input size.
Imagine your dad telling you that even if you’re super fast, cleaning your room will take at least 15 minutes. So even if you rush and clean quickly, you can only finish in 15 minutes. This is a lower bound or the best-case scenario.
def clean_room(room):
"""
This function represents cleaning a room, where each item takes time.
Its time complexity is Ω(n), with 'n' being the total number of items in the room.
"""
total_time = 0
for item in room:
total_time += item['cleaning_time_minutes']
return max(total_time, 15)
# The cleaning time will never be less than 15 minutes
# Let's define a room with some items.
# Each item has a 'cleaning_time_minutes' property representing minutes to clean.
room = [
{'item': 'bed', 'cleaning_time_minutes': 5},
{'item': 'desk', 'cleaning_time_minutes': 5},
{'item': 'floor', 'cleaning_time_minutes': 5},
{'item': 'closet', 'cleaning_time_minutes': 0},
]
print(clean_room(room)) # Output: 15
Theta (Θ) Notation
Theta notation (Θ) provides both an upper and lower bound on the asymptotic growth of an algorithm, indicating that the growth rate is within a constant factor above and below for large inputs.
🔄
It describes both best and worst-case performance with the highest precision.
- Theta Notation (Θ) is introduced by Programmer 1.
- It’s explained that Theta Notation provides upper and lower bound for an algorithm’s complexity.
- Programmer 1 states that the growth rate is within a constant factor above and below for large inputs.
Imagine your mom and dad agreeing that cleaning your room will take precisely 30 minutes, no more or less. This means they’ve figured out the exact time it will take, not just the fastest or slowest you could do it.
def clean_room(room):
"""
This function represents cleaning a room where each item takes time to clean.
Regardless of the number of items present, it will always take 30 minutes to clean the room.
"""
total_time = 0
for item in room:
total_time += item['cleaning_time_minutes']
return 30
# The cleaning time will always be 30 minutes, no matter how many items there are
# Let's define a room with some items.
# Each item has a 'cleaning_time_minutes' property that represents minutes to clean.
room = [
{'item': 'bed', 'cleaning_time_minutes': 5},
{'item': 'desk', 'cleaning_time_minutes': 5},
{'item': 'floor', 'cleaning_time_minutes': 5},
{'item': 'closet', 'cleaning_time_minutes': 5},
]
print(clean_room(room)) # Output: 30
Using Asymptotic Notations In The Real World
Let’s review a workflow between two programmers using asymptotic notations in the real world.
- Programmer 1 submits function A with O(n) to Programmer 2.
- Programmer 1 explains the asymptotic notation of the function A to Programmer 2.
- Programmer 2 proposes a function B with Θ(n) to Programmer 1.
- Programmer 2 explains the asymptotic notation of the function B to Programmer 1.
- Programmer 1 compares and discusses the pros and cons of function A with Programmer 2.
- Programmer 2 compares and discusses the pros and cons of function B with Programmer 1.
- The programmers pair up and implement the preferred function.
Asymptotic notations help programmers communicate and reason about efficient and performant algorithms through an iterative process.
Here’s what this workflow might look like in detail.
# Step 1: Programmer 1 submits function A with O(n) to Programmer 2
def linear_search(lst, item):
for i in range(len(lst)):
if lst[i] == item:
return i
return -1
print(linear_search([1, 2, 3, 4, 5], 3)) # Output: 2
# Step 2: Programmer 1 explains the asymptotic notation of the function A to Programmer 2
"""
Linear search has a time complexity of O(n) where n is the number of elements in the list.
This is because in the worst case, we might have to look at each element in the list once.
"""
# Step 3: Programmer 2 proposes a function B with Θ(log n) to Programmer 1
def binary_search(lst, item):
lst.sort() # Ensure the list is sorted
low = 0
high = len(lst) - 1
while low <= high:
mid = (low + high) // 2
guess = lst[mid]
if guess == item:
return mid
if guess > item:
high = mid - 1
else:
low = mid + 1
return -1
print(binary_search([1, 2, 3, 4, 5], 3)) # Output: 2
# Step 4: Programmer 2 explains the asymptotic notation of the function B to Programmer 1
"""
Binary search has a time complexity of Θ(log n) where n is the number of elements in the list.
This is because with each comparison, binary search cuts the number of elements to search by half.
"""
# Step 5: Programmer 1 compares and discusses the pros and cons of function A with Programmer 2
"""
Pros of Linear Search (function A):
- Simple to understand and implement.
- Doesn't require the list to be sorted.
Cons of Linear Search:
- Inefficient for large lists as it has a time complexity of O(n).
"""
# Step 6: Programmer 2 compares and discusses the pros and cons of function B with Programmer 1
"""
Pros of Binary Search (function B):
- Much more efficient for large lists as it has a time complexity of Θ(log n).
Cons of Binary Search:
- Requires the list to be sorted.
- More complex to understand and implement than linear search.
"""
# Step 7: Both programmers pair up and implement the chosen function
"""
Given that Binary Search is more efficient for large lists, they decide to implement Binary Search in their project,
provided the list of elements is always sorted.
"""
Happy coding! 🎉
Learn Asymptotic Notations - Beyond the Basics
- Books
- Videos
Related Content
- Learn X in Y Minutes: Asymptotic Notation
- Learn Python
- An Introduction to Big-O, Big-Omega, and Big-Theta
Other Learning
- How Do I Learn Effectively?
- Learn 3D Graphics
- Learn Amazon Athena
- Learn Asymptotic Notations
- Learn AWS Amplify
- Learn Color Theory
- Learn Data Visualization
- Learn Design Systems
- Learn ECS
- Learn GitHub
- Learn How To Prioritize
- Learn Java
- Learn Java Coding Challenges
- Learn JavaScript
- Learn jq
- Learn Kubernetes
- Learn Python
- Learn Rendanheyi
- Learn SignalFx
- Learn Software Architecture
- Learn Software Design Patterns
- Learn Structurizr
- Learn systemd
- Learn Terraform