Understanding Massive O Notation in Python – DZone – Uplaza

On this 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 submit, we are going to discover these ideas intimately, specializing in calculate and interpret time complexity utilizing Massive O Notation. We will even 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

For instance we’ve got a listing 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 aspect takes 1 second, printing all 1000 components would take 1000 seconds. If there may be just one aspect, it takes 1 second. Subsequently, the time taken is immediately proportional to the scale of the enter.

Massive O, Theta, and Omega Notations

  • Massive O Notation: Describes the worst-case state of affairs.
  • Theta Notation: Describes the average-case state of affairs.
  • Omega Notation: Describes the best-case state of affairs.

Massive O Notation is essentially the most extensively used because it provides 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 when it comes to the record dimension. Right here, we’re printing the primary aspect of the record. So, whether or not the record dimension is 3 or 3000, we’re simply printing the 0th aspect.

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 aspect in lst:
        print(aspect)

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 internal 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 okay 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 contemplate the time period with the very best development price (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 okay 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

Contemplate the next perform that creates a listing 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 aspect 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 totally different complexities. Fixed time complexity is one of the best, and cubic time complexity is the worst. Whereas optimizing code, the purpose is to attenuate complexity.

Share This Article
Leave a comment

Leave a Reply

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

Exit mobile version