Understanding Massive O Notation in Python – DZone – Uplaza

On the planet of programming, understanding the effectivity of your code is essential. That is the place ideas like time and house complexity come into play. On this weblog put up, we’ll discover these ideas intimately, specializing in find out how to calculate and interpret time complexity utilizing Massive O Notation. We may also take a look at sensible examples in Python.

What Is Time Complexity?

Time complexity measures the effectivity of your code because the size of the enter will increase. It gives an estimate of the time an algorithm takes to run relative to the scale of the enter.

What Is Area Complexity?

Area complexity refers back to the extra house taken by your code because the size of the enter will increase. It helps to know the reminiscence necessities of an algorithm.

Instance: Time Complexity in Python

As an example we’ve an inventory of 1000 numbers, and we have to print every quantity with an additional prefix or sentence earlier than the weather:

numbers = [i for i in range(1000)]
for quantity in numbers:
    print(f"Number: {number}")

On this instance, suppose, if printing every ingredient takes 1 second, printing all 1000 components would take 1000 seconds. If there’s only one ingredient, it takes 1 second. Subsequently, the time taken is straight proportional to the scale of the enter.

Massive O, Theta, and Omega Notations

  • Massive O Notation: Describes the worst-case situation.
  • Theta Notation: Describes the average-case situation.
  • Omega Notation: Describes the best-case situation.

Massive O Notation is essentially the most extensively used because it offers a transparent understanding of the worst-case time and house complexity.

Sensible Examples With Python Code

Let’s dive into examples with code to know these ideas higher.

Instance 1: Fixed Time Complexity — O(1)

Within the following perform demo, the record is of dimension 3. We have to calculate the time complexity by way of the record dimension. Right here, we’re printing the primary ingredient of the record. So, whether or not the record dimension is 3 or 3000, we’re simply printing the 0th ingredient.

def demo(lst):
    print(lst[0])

demo([1, 2, 3])

The time complexity of this operation is O(1), which is fixed time. As the scale will increase, the time stays fixed.

Instance 2: Linear Time Complexity — O(n)

On this code, the loop runs n occasions, making the time complexity O(n). This is named linear complexity. Because the enter will increase, the complexity will increase linearly.

def print_elements(lst):
    for ingredient in lst:
        print(ingredient)

print_elements([1, 2, 3])

Instance 3: Quadratic Time Complexity — O(n^2)

When there are two nested loops, the time complexity turns into quadratic, O(n^2). The outer loop runs n occasions and the interior loop runs m occasions.

def print_pairs(lst):
    for i in vary(len(lst)):
        for j in vary(len(lst)):
            print(lst[i], lst[j])

print_pairs([1, 2, 3])

Instance 4: Cubic Time Complexity — O(n^3)

When there are three nested loops, the time complexity is cubic, O(n^3).

def print_triplets(lst):
    for i in vary(len(lst)):
        for j in vary(len(lst)):
            for ok in vary(len(lst)):
                print(lst[i], lst[j], lst[k])

print_triplets([1, 2, 3])

Instance 5: Dominating Time period

In a perform with a number of complexities, we think about the time period with the very best development charge (dominating time period).

def complex_function(lst):
    for i in vary(len(lst)):  # O(n)
        print(lst[i])
    for i in vary(len(lst)):  # O(n)
        for j in vary(len(lst)):  # O(n^2)
            print(lst[i], lst[j])
    for i in vary(len(lst)):  # O(n)
        for j in vary(len(lst)):  # O(n)
            for ok in vary(len(lst)):  # O(n^3)
                print(lst[i], lst[j], lst[k])

complex_function([1, 2, 3])

The dominating time period right here is O(n^3).

Area Complexity in Python

Let’s additionally perceive house complexity with a sensible instance.

Instance: Area Complexity

Take into account the next perform that creates an inventory of n components.

def create_list(n):
    new_list = []
    for i in vary(n):
        new_list.append(i)
    return new_list

create_list(1000)

On this instance, the house complexity is O(n) as a result of the house required to retailer the new_list grows linearly with the scale of the enter n. For each new ingredient added to the record, we’d like extra house.

Complexity Graph

Understanding time and house complexity helps in optimizing code. With the next time/house complexity vs. enter dimension graph, we will perceive the completely different complexities. Fixed time complexity is the perfect, and cubic time complexity is the worst. Whereas optimizing code, the objective is to reduce complexity.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version