Big O

Determining efficiency of an Algorithm

Created by Kapil Sachdeva / @ksachdeva

Efficiency of an Algorithm

  • How much time it takes to run ?
  • How much memory it consumes or require ?

Time it takes to run ?

  • Number of cores/processors on machine
  • Read/Write speed to memory
  • 32-bit vs 64-bit
  • Input

Big O

Rate of growth of time of an algorithm with respect to the input

Why 'O' ?, Why not 'X' ?

O = Order of function

Define a Machine Model

  • 1 unit of time for arithmetic and logical operation
  • 1 unit of time for assignment
  • 1 unit of time for return

Example

                  
function sum(a, b) {
  return a + b;
}
                  
                

Unit of work here is 2

Time taken will always be constant irrespective of input

Example 2

                
function total(list) {
  let total = 0;
  for(let i = 0; i < list.length; i++) {
    total = total + list[i];
  }
  return total;
}
                
              

Time taken depends on size of input

Example 3

                
function totalStopIf6(list) {
  let total = 0;
  for(let i = 0; i < list.length; i++) {
    if (list[i] === 6) {
      break;
    }
    total = total + list[i];
  }
  return total;
}
                
              

What is the growth of rate here ?

Big O

Always is about the worst case

Think if 6 was at the last index

Big O rules

Always is about the worst case

Ignore the constants

Example 4

                  
  function MinMax1(list) {
    const n = list.length;
    let min = 0;
    let max = 1000;
    for(let i=0; i< n; i++) {
      if (list[i] < min) min = list[i];
      if (list[i] > max) max = list[i];
    }
  }

  function MinMax2(list) {
    const n = list.length;
    let min = 0;
    let max = 1000;
    for(let i=0; i< n; i++) {
      if (list[i] < min) min = list[i];
    }
    for(let i=0;i< n; i++) {
      if (list[i] > max) max = list[i];
    }
  }
                  
                

What is the growth of rate here ? n and 2n respectively ?

We will still say both of them are O(n)

Example 5

                  
  function nestedLoop(list) {
    const n = list.length;
    for(let i=0; i< n; i++) {
      for(let j=0; j< n; j++) {
        console.log(i*j);
      }
    }
  }

                  
                

What is the growth of rate here ?

O(\(n^2)\)

Example 6

                  
  function nestedLoop(list1, list2) {
    const a = list1.length;
    const b = list2.lenght;
    for(let i=0; i< a; i++) {
      for(let j=0; j< b; j++) {
        console.log(i*j);
      }
    }
  }

                  
                

What is the growth of rate here ?

O(a*b)

Example 7

                  
  function nestedLoop(list1, list2) {
    const n = list.length;
    for(let i=0; i< n; i++) {
      console.log(i+1);
    }

    for(let i=0; i< n; i++) {
      for(let j=0; j< n; j++) {
        console.log(i*j);
      }
    }
  }

                  
                

What is the growth of rate here ? O(n + \(n^2)\) ?

O(\(n^2)\)

Drop the non-dominant terms

Math Recap - Exponents and Lograthims

(\(2^4)\) = 2 * 2 * 2 * 2 = 16

Lograthims are opposite of exponentiation ..

We can ask ourselves, given

(\(2^x)\) = 16

What is the value of 'x' ?

(\(log_2 16)\) = x

O(logn)

Divide and Conquer methods still have an increase in growth rate but is not linear

Phone book is a good example

We will discuss this more when we will do Binary Search

Description O
Constant O(1)
Logarithmic O(log n)
Linear O(n)
n log n nO(log n)
quadratic O(\(n^2)\)
polynomial O(\(n^k)\) where k >= 1
exponential O(\(a^n)\) where a > 1

Space Complexity

An array of size n will have space complexity of O(n)

A 2D array will have - O(\(n^2)\)

Example 8

                    
    function sum(n) {
      if (n <= 0) {
        return 0;
      }
      return n + sum(n-1);
    }

                    
                  

What is the growth of rate here ?

O(n) and O(n)

>