CE100 Algorithms and Programming II

CE100 Algorithms and Programming II

Week-1 (Introduction to Analysis of Algorithms)

Spring Semester, 2021-2022

Download DOC-PDF, DOC-DOCX, SLIDE, PPTX

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Brief Description of Course and Rules

We will first talk about,

  1. Course Plan and Communication

  2. Grading System, Homeworks, and Exams

please read the syllabus carefully.

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Outline (1)

  • Introduction to Analysis of Algorithms
    • Algorithm Basics
    • Flowgorithm
    • Pseudocode
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Outline (2)

  • RAM (Random Access Machine Model)
  • Sorting Problem
  • Insertion Sort Analysis
  • Algorithm Cost Calculation for Time Complexity
  • Worst, Average, and Best Case Summary
  • Merge Sort Analysis
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Outline (3)

  • Asymptotic Notation
    • Big O Notation
    • Big Teta Notation
    • Big Omega Notation
    • Small o Notation
    • Small omega Notation
RTEU CE100 Week-1
CE100 Algorithms and Programming II

We Need Mathematical Proofs (1)

  • Direct proof
  • Proof by mathematical induction
  • Proof by contraposition
  • Proof by contradiction
  • Proof by construction
  • Proof by exhaustion
RTEU CE100 Week-1
CE100 Algorithms and Programming II

We Need Mathematical Proofs (2)

  • Probabilistic proof
  • Combinatorial proof
  • Nonconstructive proof
    • Statistical proofs in pure mathematics
    • Computer-assisted proofs

Mathematical proof - Wikipedia

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Introduction to Analysis of Algorithms

  • Study two sorting algorithms as examples

    • Insertion sort: Incremental algorithm
    • Merge sort: Divide-and-conquer
  • Introduction to runtime analysis

    • Best vs. worst vs. average case
    • Asymptotic analysis
RTEU CE100 Week-1
CE100 Algorithms and Programming II

What is Algorithm

Algorithm: A sequence of computational steps that transform the input to the desired output

Procedure vs. algorithm
An algorithm must halt within finite time with the right output

We Need to Measure Performance Metrics

  • Processing Time
  • Allocated Memory
  • Network Congestion
  • Power Usage etc.
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Example Sorting Algorithms

Input: a sequence of n numbers

a1,a2,...,an\langle a_1,a_2,...,a_n \rangle

Algorithm: Sorting / Permutation

=(1),(2),...,(n)\prod = \langle \prod_{(1)},\prod_{(2)},...,\prod_{(n)} \rangle

Output: sorted permutation of the input sequence

a(1)a(2),...,a(n)\langle a_{\prod_{(1)}} \leqslant a_{\prod_{(2)}} \leqslant,...,a_{\prod_{(n)}} \rangle

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Pseudo-code notation (1)

  • Objective: Express algorithms to humans in a clear and concise way

  • Liberal use of English

  • Indentation for block structures

  • Omission of error handling and other details (needed in real programs)

You can use Flowgorithm application to understand concept easily.

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Pseudo-code notation (2)

Links and Examples

Wikipedia

CS50

University of North Florida

GeeksforGeeks

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Correctness (1)

We often use a loop invariant to help us to understand why an algorithm gives the correct answer.

Example: (Insertion Sort) at the start of each iteration of the "outer" for loop - the loop indexed by jj - the subarray A[1j1]A[1 \dots j-1] consist of the elements originally in A[1j1]A[1\dots j-1] but in sorted order.

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Correctness (2)

To use a loop invariant to prove correctness, we must show 3 things about it.

  • Initialization: It is true to the first iteration of the loop.
  • Maintaince: If it is true before an iteration of the loop, it remains true before the next iteration.
  • Termination: When the loop terminates, the invariant - usually along with the reason that the loop terminated - gives us a usefull property that helps show that the algorithm is correct.
RTEU CE100 Week-1
CE100 Algorithms and Programming II

RAM (Random Access Machine Model) Θ(1)\Longrightarrow \Theta(1) (1)

  • Operations
    • Single Step
    • Sequential
    • No Concurrent
    • Arithmetic
      • add, subtract, multiply, divide, remainder, floor, ceiling,
      • shift left/shift right (good by multiply/dividing 2k2^k)
RTEU CE100 Week-1
CE100 Algorithms and Programming II

RAM (Random Access Machine Model) Θ(1)\Longrightarrow \Theta(1) (2)

  • Data Movement
    • load, store, copy
  • Control
    • conditional / unconditional branch
    • subroutine calls
    • returns
RTEU CE100 Week-1
CE100 Algorithms and Programming II

RAM (Random Access Machine Model) Θ(1)\Longrightarrow \Theta(1) (3)

  • Each instruction take a constant amount of time
  • Integer will be represented by clognclogn c1c \geq 1
  • T(n)T(n) the running time of the algorithm:

all statement(cost of statement)(number of times statement is executed)=T(n)\sum \limits_{\text{all statement}}^{}(\text{cost of statement})*(\text{number of times statement is executed}) = T(n)

RTEU CE100 Week-1
CE100 Algorithms and Programming II

What is the processing time ?

alt:"processing time map" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Algorithm (1)

Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands

The array is virtually split into a sorted and an unsorted part

Values from the unsorted part are picked and placed at the correct position in the sorted part.

alt:"playing cards" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II
  • Assume input array : A[1..n]A[1..n]
  • Iterate jj from 22 to nn

alt:"insertion sort movement" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Algorithm (2)

alt:"insertion sort algorithm" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Algorithm (Pseudo-Code) (3)

Insertion-Sort(A)
1. for j=2 to A.length
2.     key = A[j]
3.     //insert A[j] into the sorted sequence A[1...j-1]
4.     i = j - 1
5.     while i>0 and A[i]>key
6.         A[i+1] = A[i]
7.         i = i - 1
8.     A[i+1] = key
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-By-Step Description (1)

alt:"insertion sort description-1" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-By-Step Description (2)

alt:"insertion sort description-2" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-By-Step Description (3)

alt:"insertion sort description-3" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Example

Insertion Sort Step-1 (initial)

alt:"insertion sort step-1" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-2 (j=2)

alt:"insertion sort step-2" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-3 (j=3)

alt:"insertion sort step-3" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-4 (j=3)

alt:"insertion sort step-4" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-5 (j=4)

alt:"insertion sort step-5" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-6 (j=5)

alt:"insertion sort step-6" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-7 (j=5)

alt:"insertion sort step-7" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Step-8 (j=6)

alt:"insertion sort step-8" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Review (1)

  • Items sorted in-place

    • Elements are rearranged within the array.

    • At a most constant number of items stored outside the array at any time (e.,g. the variable key)

    • Input array AA contains a sorted output sequence when the algorithm ends

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort Review (2)

  • Incremental approach

    • Having sorted A[1..j1]A[1..j-1] , place A[j]A[j] correctly so that A[1..j]A[1..j] is sorted
  • Running Time

    • It depends on Input Size (5 elements or 5 billion elements) and Input Itself (partially sorted)
  • Algorithm approach to upper bound of overall performance analysis

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Visualization of Insertion Sort

Sorting (Bubble, Selection, Insertion, Merge, Quick, Counting, Radix) - VisuAlgo

https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

https://algorithm-visualizer.org/

HMvHTs - Online C++ Compiler & Debugging Tool - Ideone.com

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Kinds of Running Time Analysis (Time Complexity)

  • Worst Case (Big-O Notation)
    • T(n)T(n) = maximum processing time of any input nn
    • Presentation of Big-O : O(n)O(n)
  • Average Case (Teta Notation)
    • T(n)T(n) = average time over all inputs of size nn, inputs can have a uniform distribution
    • Presentation of Big-Theta : Θ(n)\Theta(n)
  • Best Case (Omega Notation)
    • T(n)T(n) = min time on any input of size nn, for example sorted array
    • Presentation of Big-Omega : Ω(n)\Omega(n)
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Array Sorting Algorithms Time and Space Complexity

alt:"array sorting algorithms" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Comparison of Time Analysis Cases

For insertion sort, worst-case time depends on the speed of primitive operations such as

  • Relative Speed (on the same machine)

  • Absolute Speed (on different machines)

  • Asymptotic Analysis

    • Ignore machine-dependent constants

    • Look at the growth of T(n)nT(n) | n\rightarrow\infty

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Analysis (1)

alt:"algorithm analysis comparisons" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Analysis (2)

Theta-Notation (Average-Case)

  • Drop low order terms

  • Ignore leading constants

e.g

2n2+5n+3=Θ(n2)3n3+90n22n+5=Θ(n3)\begin{align*} 2n^2+5n+3 &= \Theta(n^2) \\ 3n^3+90n^2-2n+5 &= \Theta(n^3) \end{align*}

  • As nn gets large, a Θ(n2)\Theta(n^2) algorithm runs faster than a Θ(n3)\Theta(n^3) algorithm
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Analysis (3)

For both algorithms, we can see a minimum item size in the following chart. After this point, we can see performance differences. Some algorithms for small item size can be run faster than others but if you increase item size you will see a reference point that notation proof performance metrics.

alt:"T(n) and n change graph" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort - Runtime Analysis (1)

Cost   Times   Insertion-Sort(A)
----   -----   ---------------------
c1     n       1. for j=2 to A.length
c2     n-1     2.     key = A[j]
c3     n-1     3.     //insert A[j] into the sorted sequence A[1...j-1]
c4     n-1     4.     i = j - 1
c5     k5      5.     while i>0 and A[i]>key do 
c6     k6      6.         A[i+1] = A[i]
c7     k6      7.         i = i - 1
c8     n-1     8.     A[i+1] = key

we have two loops here, if we sum up costs as follow we can see big-O worst case notation.

k5=j=2ntjk_5 = \sum \limits_{j=2}^n{t_j} and k6=j=2nti1k_6 = \sum \limits_{j=2}^n{t_i-1}

for operation counts

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort - Runtime Analysis (2)

cost function can be evaluated as follow;

T(n)=c1n+c2(n1)+0(n1)+c4(n1)+c5j=2ntj+c6j=2nti1+c7j=2nti1+c8(n1)\begin{align*} T(n) &=c_1n+c_2(n-1)+ 0(n-1)+c_4(n-1) \\ & +c_5\sum \limits_{j=2}^n{t_j}+c_6\sum \limits_{j=2}^n{t_i-1} \\ & +c_7\sum \limits_{j=2}^n{t_i-1}+c_8(n-1) \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort - Runtime Analysis (3)

j=2nj=(n(n+1)/2)1 and j=2nj1=n(n1)/2\begin{align*} \sum \limits_{j=2}^n j &= (n(n+1)/2)- 1 \\ & \text{ and } \\ \sum \limits_{j=2}^n {j-1} &= n(n-1)/2 \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort - Runtime Analysis (4)

T(n)=(c5/2+c6/2+c7/2)n2+(c1+c2+c4+c5/2c6/2c7/2+c8)n(c2+c4+c5+c6)\begin{align*} T(n) & =(c_5/2 + c_6/2 + c_7/2)n^2 \\ & + (c_1+c_2+c_4+c_5/2-c_6/2-c_7/2+c_8)n \\ & -(c_2 + c_4 + c_5 + c_6) \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion Sort - Runtime Analysis (5)

T(n)=an2+bn+c=O(n2)\begin{align*} T(n) &= an^2 + bn + c \\ &=O(n^2) \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Best-Case Scenario (Sorted Array) (1)

Problem-1, If A[1...j]A[1...j] is already sorted, what will be tj=?t_j=?

alt:"Insertion Sort Best-Case Scenario (Sorted Array)" center

tj=1t_j=1

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Best-Case Scenario (Sorted Array) (2)

Parameters are taken from image

T(n)=c1n+c2(n1)+c3(n1)+c4j=2ntj+c5j=2n(tj1)+c6j=2n(tj1)+c7(n1)\begin{align*} T(n) &=c_1n+c_2(n-1)+c_3(n-1) \\ & +c_4\sum \limits_{j=2}^nt_j+c_5\sum \limits_{j=2}^n(t_j-1) \\ & +c_6\sum \limits_{j=2}^n(t_j-1)+c_7(n-1) \end{align*}

tj=1t_j=1 for all jj

T(n)=(c1+c2+c3+c4+c7)n(c2+c3+c4+c7)T(n)=anb=Ω(n)\begin{align*} T(n) &= (c_1+c_2+c_3+c_4+c_7)n \\ &-(c_2+c_3+c_4+c_7) \\ T(n) &=an-b \\ &=\Omega(n) \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Worst-Case Scenario (Reversed Array) (1)

Problem-2 If A[j]A[j] is smaller than every entry in A[1...j1]A[1...j-1], what will be tj=?t_j=?

alt:"Insertion Sort Worst-Case Scenario (Reversed Array)" center

tj=?t_j=?

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Worst-Case Scenario (Reversed Array) (2)

The input array is reverse sorted tj=jt_j=j for all jj after calculation worst case runtime will be

T(n)=1/2(c4+c5+c6)n2+(c1+c2+c3+1/2(c4c5c6)+c7)n(c2+c3+c4+c7)T(n)=1/2an2+bnc=O(n2)\begin{align*} T(n) &=1/2(c_4+c_5+c_6)n^2 \\ & +(c_1+c_2+c_3+1/2(c_4-c_5-c_6)+c_7)n -(c_2+c_3+c_4+c_7) \\ T(n) &=1/2an^2+bn-c \\ &= O(n^2) \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Runtime Analysis of Insertion-Sort

alt:"Asymptotic Runtime Analysis of Insertion-Sort" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion-Sort Worst-case (input reverse sorted)

Inner Loop is Θ(j)\Theta(j)

T(n)=j=2nΘ(j)=Θ(j=2nj)=Θ(n2)\begin{align*} T(n) &=\sum \limits_{j=2}^n\Theta(j) \\ &=\Theta(\sum \limits_{j=2}^nj) \\ &=\Theta(n^2) \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Insertion-Sort Average-case (all permutations uniformly distributed)

Inner Loop is Θ(j/2)\Theta(j/2)

T(n)=j=2nΘ(j/2)=j=2nΘ(j)=Θ(n2)\begin{align*} T(n) &=\sum \limits_{j=2}^n\Theta(j/2) \\ &=\sum \limits_{j=2}^n\Theta(j) \\ &=\Theta(n^2) \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Array Sorting Algorithms Time/Space Complexities

To compare this sorting algorithm please check the following map again.

alt:"array sorting algorithms" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort : Divide / Conquer / Combine (1)

alt:"Merge Sort : Divide / Conquer / Combine" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort : Divide / Conquer / Combine (2)

Divide: we divide the problem into a number of subproblems

Conquer: We solve the subproblems recursively

Base-Case: Solve by Brute-Force

Combine: Subproblem solutions to the original problem

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Example

alt:"Merge Sort Example" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Algorithm (initial setup)

Merge Sort is a recursive sorting algorithm, for initial case we need to call Merge-Sort(A,1,n) for sorting A[1..n]A[1..n]

initial case

A : Array
p : 1 (offset)
r : n (length)
Merge-Sort(A,1,n)
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Algorithm (internal iterations)

internal iterations

A : Array
p : offset
r : length
Merge-Sort(A,p,r)
    if p=r then                (CHECK FOR BASE-CASE)
        return
    else
        q = floor((p+r)/2)    (DIVIDE)
        Merge-Sort(A,p,q)     (CONQUER)
        Merge-Sort(A,q+1,r)   (CONQUER)
        Merge(A,p,q,r)        (COMBINE)
    endif
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Algorithm (Combine-1)

p=startpointp = start-point
q=midpointq = mid-point
r=endpointr = end-point

alt:"Merge Sort Algorithm (Combine-1)" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Algorithm (Combine-2)

brute-force task, merging two sorted subarrays

The pseudo-code in the textbook (Sec. 2.3.1)

alt:"Merge Sort Algorithm (Combine-2)" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Combine Algorithm (1)

Merge(A,p,q,r)
    n1 = q-p+1
    n2 = r-q

    //allocate left and right arrays 
    //increment will be from left to right 
    //left part will be bigger than right part

    L[1...n1+1] //left array
    R[1...n2+1] //right array

    //copy left part of array
    for i=1 to n1
        L[i]=A[p+i-1]

    //copy right part of array
    for j=1 to n2
        R[j]=A[q+j]

    //put end items maximum values for termination
    L[n1+1]=inf
    R[n2+1]=inf

    i=1,j=1
    for k=p to r
        if L[i]<=R[j]
            A[k]=L[i]
            i=i+1
        else
            A[k]=R[j]
            j=j+1
RTEU CE100 Week-1
CE100 Algorithms and Programming II

What is the complexity of merge operation?

You can find by counting loops will provide you base constant nested level will provide you exponent of this constant, if you drop constants you will have complexity

we have 3 for loops

it will look like 3n3n and Θ(n)\Theta(n) will be merge complexity

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Correctness

  • Base case

    • p=rp = r (Trivially correct)
  • Inductive hypothesis

    • MERGE-SORT is correct for any subarray that is a strict (smaller) subset of A[p,q]A[p, q].
  • General Case

    • MERGE-SORT is correct for A[p,q]A[p, q]. From inductive hypothesis and correctness of Merge.
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Algorithm (Pseudo-Code)

A : Array
p : offset
r : length
Merge-Sort(A,p,r)
    if p=r then                (CHECK FOR BASE-CASE)
        return
    else
        q = floor((p+r)/2)    (DIVIDE)
        Merge-Sort(A,p,q)     (CONQUER)
        Merge-Sort(A,q+1,r)   (CONQUER)
        Merge(A,p,q,r)        (COMBINE)
    endif
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Algorithm Complexity

A : Array
p : offset
r : length
Merge-Sort(A,p,r)-------------> T(n)
    if p=r then--------------->Theta(1)                
        return
    else
        q = floor((p+r)/2)---->Theta(1)
        Merge-Sort(A,p,q)-----> T(n/2)
        Merge-Sort(A,q+1,r)---> T(n/2)
        Merge(A,p,q,r)-------->Theta(n)
    endif
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Merge Sort Algorithm Recurrence

We can describe a function recursively in terms of itself, to analyze the performance of recursive algorithms

T(n)={Θ(1)if n=12T(n/2)+Θ(n)otherwiseT(n)=\begin{cases} \Theta(1)&\text{if n=1} \\ 2T(n/2)+\Theta(n)&otherwise \end{cases}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How To Solve Recurrence (1)

T(n)={Θ(1)if n=12T(n/2)+Θ(n)otherwiseT(n)=\begin{cases} \Theta(1)&\text{if n=1} \\ 2T(n/2)+\Theta(n)&otherwise \end{cases}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How To Solve Recurrence (2)

We will assume T(n)=Θ(1)T(n)= \Theta(1) for sufficiently small nn to rewrite equation as

T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)

Solution for this equation will be Θ(nlgn)\Theta(nlgn) with following recursion tree.

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How To Solve Recurrence (3)

Multiply by height Θ(lgn)\Theta(lgn) with each level cost Θ(n)\Theta(n) we can found Θ(nlgn)\Theta(nlgn)

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How To Solve Recurrence (4)

This tree is binary-tree and binary-tree height is related with item size.

alt:"Merge Sort Recursive Tree" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How Height of a Binary Tree is Equal to lognlogn ? (1)

Merge-Sort recursion tree is a perfect binary tree, a binary tree is a tree which every node has at most two children, A perfect binary tree is binary tree in which all internal nodes have exactly two children and all leaves are at the same level.

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How Height of a Binary Tree is Equal to lognlogn ? (2)

Let nn be the number of nodes in the tree and let lkl_k denote the number of nodes on level k. According to this;

  • lk=2lk1l_k = 2l_{k-1} i.e. each level has exactly twice as many nodes as the previous level

  • l0=1l_0 = 1 , i.e. on the first level we have only one node (the root node)

  • The leaves are at the last level, lhl_h where hh is the height of the tree.

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How Height of a Binary Tree is Equal to lognlogn ? (3)

The total number of nodes in the tree is equal to the sum of the nodes on all the levels: nodes nn

1+21+22+23+...+2h=n1+21+22+23+...+2h=2h+112h+11=n2h+1=n+1log22h+1=log2(n+1)h+1=log2(n+1)h=log2(n+1)1\begin{align*} 1+2^1+2^2+2^3+...+2^h &= n \\ 1+2^1+2^2+2^3+...+2^h &= 2^{h+1}-1 \\ 2^{h+1}-1 &= n\\ 2^{h+1} &= n+1\\ log_2{2^{h+1}} &= log_2{(n+1)} \\ h+1 &= log_2{(n+1)} \\ h &= log_2{(n+1)}-1 \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

How Height of a Binary Tree is Equal to lognlogn ? (3)

If we write it as asymptotic approach, we will have the following result

height of tree is h=log2(n+1)1=O(logn)\text{height of tree is }h = log_2{(n+1)}-1 = O(logn)

also

number of leaves is lh=(n+1)/2\text{number of leaves is } l_h = (n+1)/2

nearly half of the nodes are at the leaves

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Review

Θ(nlgn)\Theta(nlgn) grows more slowly than Θ(n2)\Theta(n^2)

Therefore Merge-Sort beats Insertion-Sort in the worst case

In practice Merge-Sort beats Insertion-Sort for n>30n>30 or so

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Notations

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (1)

f(n)=O(g(n))f(n)=O(g(n)) if \exists positive constants cc, n0n_0 such that

0f(n)cg(n),nn00 \leq f(n) \leq cg(n), \forall n \geq n_0

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (2)

alt:"Big-O Function-1" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (3)

Asymptotic running times of algorithms are usually defined by functions whose domain are N=0,1,2,N={0, 1, 2, …} (natural numbers)

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (4)

Example-1

Show that 2n2=O(n3)2n^2 = O(n^3)

we need to find two positive constant cc and n0n_0 such that:

02n2cn3 for all nn00 \leq 2n^2 \leq cn^3 \text{ for all } n \geq n_0

Choose c=2c=2 and n0=1n_0 = 1

2n22n3 for all n12n^2 \leq 2n^3 \text{ for all } n \geq 1

Or, choose c=1c=1 and n0=2n_0=2

2n2n3 for all n22n^2 \leq n^3 \text{ for all } n \geq 2

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (5)

Example-2

Show that 2n2+n=O(n2)2n^2 + n = O(n^2)

We need to find two positive constant cc and n0n_0 such that:

02n2+ncn2 for all nn00 \leq {2n^2+n} \leq cn^2 \text{ for all } n \geq n_0

2+(1/n)c for all nn02 + (1/n) \leq c \text{ for all } n \geq n_0

Choose c=3c=3 and n0=1n_0=1

2n2+n3n2 for all n12n^2 + n \leq 3n^2 \text{ for all } n \geq 1

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (6)

We can say the followings about f(n)=O(g(n))f(n)=O(g(n)) equation

The notation is a little sloppy

One-way equation, e.q. n2=O(n3)n^2 = O(n^3) but we cannot say O(n3)=n2O(n^3)=n^2

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (7)

O(g(n))O(g(n)) is in fact a set of functions as follow

O(g(n))={f(n): positive constant c,n0 such that 0f(n)cg(n),nn0}O(g(n)) = \{ f(n) : \exists \text{ positive constant } c, n_0 \text{ such that } 0 \leq f(n) \leq cg(n), \forall n \geq n_0 \}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (8)

In other words O(g(n))O(g(n)) is in fact, the set of functions that have asymptotic upper bound g(n)g(n)

e.q 2n2=O(n3)2n^2 = O(n^3) means 2n2O(n3)2n^2 \in O(n^3)

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (9)

Example-1

109n2=O(n2)10^9n^2 = O(n^2)

0109n2cn2 for nn00 \leq 10^9n^2 \leq cn^2 \text{ for } n \geq n_0

choose c=109c=10^9 and n0=1n_0=1

0109n2109n2 for n10 \leq 10^9n^2 \leq 10^9n^2 \text{ for } n \geq 1

CORRECT

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (10)

Example-2

100n1.9999=O(n2)100n^{1.9999}=O(n^2)

0100n1.9999cn2 for nn00 \leq 100n^{1.9999} \leq cn^2 \text{ for } n \geq n_0

choose c=100c=100 and n0=1n_0=1

0100n1.9999100n2 for n10 \leq 100n^{1.9999} \leq 100n^2 \text{ for } n \geq 1

CORRECT

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (11)

Example-3

109n2.0001=O(n2)10^{-9}n^{2.0001} = O(n^2)

0109n2.0001cn2 for nn00 \leq 10^{-9}n^{2.0001} \leq cn^2 \text{ for } n \geq n_0

109n0.0001c for nn010^{-9}n^{0.0001} \leq c \text{ for } n \geq n_0

INCORRECT (Contradiction)

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-O / OO- Notation : Asymptotic Upper Bound (Worst-Case) (12)

If we analysis O(n2)O(n^2) case, OO-notation is an upper bound notation and the runtime T(n)T(n) of algorithm A is at least O(n2)O(n^2 ).

O(n2)O(n^2): The set of functions with asymptotic upper bound n2n^2

T(n)O(n2)T(n) \geq O(n^2) means T(n)h(n)T(n) \geq h(n) for some h(n)O(n2)h(n) \in O(n^2)

h(n)=0h(n)=0 function is also in O(n2)O(n^2). Hence : T(n)0T(n) \geq 0 , runtime must be nonnegative.

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (1)

f(n)=Ω(g(n))f(n)=\Omega(g(n)) if \exists positive constants c,n0c,n_0 such that 0cg(n)f(n),nn00 \leq cg(n) \leq f(n) , \forall n \geq n_0

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (2)

alt:"Big-Omega Function-1" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (3)

Example-1

Show that 2n3=Ω(n2)2n^3 = \Omega(n^2)

We need to find two positive constants cc and n0n_0 such that:

0cn22n3 for all nn00 \leq cn^2 \leq 2n^3 \text{ for all } n \geq n_0

Choose c=1c=1 and n0=1n_0=1

n22n3 for all n1n^2 \leq 2n^3 \text{ for all } n \geq 1

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (4)

Example-4

Show that n=Ω(lgn)\sqrt{n}=\Omega(lgn)

We need to find two positive constants cc and n0n_0 such that:

clgnn for all nn0clgn \leq \sqrt{n} \text{ for all } n \geq n_0

Choose c=1c=1 and n0=16n_0=16

lgnn for all n16lgn \leq \sqrt{n} \text{ for all } n \geq 16

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (5)

Ω(g(n))\Omega(g(n)) is the set of functions that have asymptotic lower bound g(n)g(n)

Ω(g(n))={f(n): positive constants c,n0 such that 0cg(n)f(n),nn0}\begin{align*} \Omega(g(n)) &=\{ f(n):\exists \text{ positive constants } c,n_0 \text{ such that } \\ & 0 \leq cg(n) \leq f(n), \forall n \geq n_0 \} \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (6)

Example-1

109n2=Ω(n2)10^9n^2 = \Omega(n^2)

0cn2109n2 for nn00 \leq cn^2 \leq 10^9n^2 \text{ for } n\geq n_0

Choose c=109c=10^9 and n0=1n_0=1

0109n2109n2 for n10 \leq 10^9n^2 \leq 10^9n^2 \text{ for } n\geq 1

CORRECT

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (7)

Example-2

100n1.9999=Ω(n2)100n^{1.9999} = \Omega(n^2)

0cn2100n1.9999 for nn00 \leq cn^2 \leq 100n^{1.9999} \text{ for } n \geq n_0

n0.0001(100/c) for nn0n^{0.0001} \leq (100/c) \text{ for } n \geq n_0

INCORRECT(Contradiction)

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Omega / Ω\Omega-Notation : Asymptotic Lower Bound (Best-Case) (8)

Example-3

109n2.0001=Ω(n2)10^{-9}n^{2.0001} = \Omega(n^2)

0cn2109n2.0001 for nn00 \leq cn^2 \leq 10^{-9}n^{2.0001} \text{ for } n \geq n_0

Choose c=109c=10^{-9} and n0=1n_0=1

0109n2109n2.0001 for n10 \leq 10^{-9}n^2 \leq 10^{-9}n^{2.0001} \text{ for } n \geq 1

CORRECT

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Comparison of Notations (1)

alt:"Big-Omega Function for Comparison" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Comparison of Notations (2)

alt:"Comparison of Notations" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (1)

f(n)=Θ(g(n)) if  positive constants c1,c2,n0such that0c1g(n)f(n)c2g(n),nn0\begin{align*} f(n) &=\Theta(g(n)) \ if \ \exists \ \text{positive constants} \ c_1,c_2,n_0 \text{such that} \\ & 0 \leq c_1g(n) \leq f(n) \leq c_2g(n), \forall n \geq n_0 \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (2)

alt:"Big-Theta Function" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (3)

Example-1

Show that 2n2+n=Θ(n2)2n^2 + n = \Theta(n^2)

We need to find 3 positive constants c1,c2c_1,c_2 and n0n_0 such that:

0c1n22n2+nc2n20 \leq c_1n^2 \leq 2n^2+n \leq c_2n^2 for all nn0n \geq n_0

c12+(1/n)c2c_1 \leq 2 + (1/n) \leq c_2 for all nn0n \geq n_0

Choose c1=2,c2=3c_1=2, c_2=3 and n0=1n_0=1

2n22n2+n3n22n^2 \leq 2n^2+n \leq 3n^2 for all n1n \geq 1

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (4)

Example-2.1

Show that 1/2n22n=Θ(n2)1/2n^2-2n=\Theta(n^2)

We need to find 3 positive constants c1,c2c_1,c_2 and n0n_0 such that:

0c1n21/2n22nc2n2 for all nn00 \leq c_1n^2 \leq 1/2n^2-2n \leq c_2n^2 \text{ for all } n \geq n_0

c11/22/nc2 for all nn0c_1 \leq 1/2 - 2 / n \leq c_2 \text{ for all } n \geq n_0

Choose 3 positive constants c1,c2,n0c_1,c_2, n_0 that satisfy c11/22/nc2c_1 \leq 1/2 - 2/n \leq c_2 for all nn0n \geq n_0

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (5)

Example-2.2

alt:"Big-Theta Example" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (6)

Example-2.3

1/101/22/n for n51/10 \leq 1/2 - 2/n \text{ for } n \geq 5

1/22/n1/2 for n01/2 - 2/n \leq 1/2 \text{ for } n \geq 0

Therefore we can choose c1=1/10,c2=1/2,n0=5c_1 = 1/10, c_2=1/2, n_0=5

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (7)

Theorem: leading constants & low-order terms don’t matter

Justification: can choose the leading constant large enough to make high-order term dominate other terms

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (8)

Example-1

109n2=Θ(n2)10^9n^2 = \Theta(n^2) CORRECT

100n1.9999=Θ(n2)100n^{1.9999} = \Theta(n^2) INCORRECT

109n2.0001=Θ(n2)10^9n^{2.0001} = \Theta(n^2) INCORRECT

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (9)

Θ(g(n))\Theta(g(n)) is the set of functions that have asymptotically tight bound g(n)g(n)

Θ(g(n))={f(n):\Theta(g(n))=\{ f(n): \exists
positive constants c1,c2,n0c_1,c_2, n_0 such that
0c1g(n)f(n)c2g(n),nn0}0 \leq c_1g(n) \leq f(n) \leq c_2g(n), \forall n \geq n_0 \}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (10)

Theorem:

f(n)=Θ(g(n))f(n)=\Theta(g(n)) if and only if f(n)=O(g(n))f(n)=O(g(n)) and f(n)=Ω(g(n))f(n)=\Omega(g(n))

Θ\Theta is stronger than both OO and Ω\Omega

Θ(g(n))O(g(n)) and Θ(g(n))Ω(g(n))\Theta(g(n)) \subseteq O(g(n)) \text{ and } \Theta(g(n)) \subseteq \Omega(g(n))

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (11)

Example-1.1

Prove that 108n2Θ(n)10^{-8}n^2 \neq \Theta(n)

We can check that 108n2=Ω(n)10^{-8}n^2 = \Omega(n) and 108n2O(n)10^{-8}n^2 \neq O(n)

Proof by contradiction for O(n)O(n) notation

O(g(n))={f(n): positive constant c,n0 such that 0f(n)cg(n),nn0}\begin{align*} O(g(n)) &= \{ f(n) : \exists \text{ positive constant } c, n_0 \text{ such that } \\ & 0 \leq f(n) \leq cg(n), \forall n \geq n_0 \} \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Big-Theta /Θ\Theta-Notation : Asymptotically tight bound (Average Case) (12)

Example-1.2

Suppose positive constants c2c_2 and n0n_0 exist such that:

108n2c2n,nn010^{-8}n^2 \leq c_2n, \forall n \geq n_0

108nc2,nn010^{-8}n \leq c_2, \forall n \geq n_0

Contradiction: c2c_2 is a constant

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Summary of O,ΩO,\Omega and Θ\Theta notations (1)

O(g(n))O(g(n)) : The set of functions with asymptotic upper bound g(n)g(n)

Ω(g(n))\Omega(g(n)) : The set of functions with asymptotic lower bound g(n)g(n)

Θ(n)\Theta(n): The set of functions with asymptotically tight bound g(n)g(n)

f(n)=Θ(g(n))f(n)=O(g(n)) and f(n)=Ω(g(n))f(n)=\Theta(g(n)) \Leftrightarrow f(n)=O(g(n)) \text{ and } f(n)=\Omega(g(n))

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Summary of O,ΩO,\Omega and Θ\Theta notations (2)

alt:"Summary Asymptotic Analysis" center

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Small-o / oo-Notation : Asymptotic upper bound that is not tight (1)

Remember, upper bound provided by big- OO notation can be tight or not tight

Tight mean values are close the original function

e.g. followings are true

2n2=O(n2)2n^2 = O(n^2) is asymptotically tight

2n=O(n2)2n = O(n^2) is not asymptotically tight

According to this small-oo notation is an upper bound that is not asymptotically tight

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Small-o / oo-Notation : Asymptotic upper bound that is not tight (2)

Note that in equations equality is removed in small notations

o(g(n))={f(n): for any constantc>0, a constant n0>0, such that 0f(n)<cg(n),nn0}\begin{align*} o(g(n)) &=\{ f(n): \text{ for any constant} c > 0, \exists \text{ a constant } n_0 > 0, \\ & \text{ such that } 0 \leq f(n) < cg(n), \\ & \forall n \geq n_0 \} \end{align*}

limnf(n)g(n)=0\lim_{n \to \infty} \frac{f(n)}{g(n)} = 0

e.g 2n=o(n2)2n=o(n^2) any positive cc satisfies but 2n2o(n2)2n^2 \neq o(n^2) c=2c=2 does not satisfy

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Small-omega / ω\omega-Notation: Asymptotic lower bound that is not tight (1)

ω(g(n))={f(n): for any constant c>0, a constant n0>0, such that 0cg(n)<f(n),nn0\begin{align*} \omega(g(n)) &= \{ f(n): \text{ for any constant } c > 0, \exists \text{ a constant } n_0>0, \\ & \text{ such that } 0 \leq cg(n) < f(n), \\ & \forall n \geq n_0 \end{align*}

limnf(n)g(n)=\lim_{n \to \infty} \frac{f(n)}{g(n)} = \infty

e.g. n2/2=ω(n)n^2/2=\omega(n), any positive cc satisfies but n2/2ω(n2)n^2/2 \neq \omega(n^2), c=1/2c=1/2 does not satisfy

RTEU CE100 Week-1
CE100 Algorithms and Programming II

(Important) Analogy to compare of two real numbers (1)

f(n)=O(g(n))abf(n)=Ω(g(n))abf(n)=Θ(g(n))a=bf(n)=o(g(n))a<bf(n)=ω(g(n))a>b\begin{align*} f(n) &= O(g(n)) \leftrightarrow a \leq b \\ f(n) &= \Omega(g(n)) \leftrightarrow a \geq b \\ f(n) &= \Theta(g(n)) \leftrightarrow a = b \\ f(n) &= o(g(n)) \leftrightarrow a < b \\ f(n) &= \omega(g(n)) \leftrightarrow a > b \\ \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

(Important) Analogy to compare of two real numbers (2)

OΘ=Ωω>o<\begin{align*} O \approx \leq \\ \Theta \approx = \\ \Omega \approx \geq \\ \omega \approx > \\ o \approx < \end{align*}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

(Important) Trichotomy property for real numbers

For any two real numbers aa and bb, we have either

a<ba<b, or a=ba=b, or a>ba>b

Trichotomy property does not hold for asymptotic notation, for two functions f(n)f(n) and g(n)g(n), it may be the case that neither f(n)=O(g(n))f(n)=O(g(n)) nor f(n)=Ω(g(n))f(n)=\Omega(g(n)) holds.

e.g. nn and n1+sin(n)n^{1+sin(n)} cannot be compared asymptotically

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Examples

5n2=O(n2)5n^2=O(n^2) TRUE n2lgn=O(n2)n^2lgn = O(n^2) FALSE
5n2=Ω(n2)5n^2=\Omega(n^2) TRUE n2lgn=Ω(n2)n^2lgn = \Omega(n^2) TRUE
5n2=Θ(n2)5n^2=\Theta(n^2) TRUE n2lgn=Θ(n2)n^2lgn = \Theta(n^2) FALSE
5n2=o(n2)5n^2=o(n^2) FALSE n2lgn=o(n2)n^2lgn = o(n^2) FALSE
5n2=ω(n2)5n^2=\omega(n^2) FALSE n2lgn=ω(n2)n^2lgn = \omega(n^2) TRUE
2n=O(3n)2^n = O(3^n) TRUE
2n=Ω(3n)2^n = \Omega(3^n) FALSE 2n=o(3n)2^n=o(3^n) TRUE
2n=Θ(3n)2^n = \Theta(3^n) FALSE 2n=ω(3n)2^n = \omega(3^n) FALSE
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Function Properties

Transitivity: holds for all

e.g. f(n)=Θ(g(n))&g(n)=Θ(h(n))f(n)=Θ(h(n))f(n) = \Theta(g(n)) \& g(n)=\Theta(h(n)) \Rightarrow f(n)=\Theta(h(n))

Reflexivity: holds for Θ,O,Ω\Theta,O,\Omega

e.g. f(n)=O(f(n))f(n)=O(f(n))

Symmetry: hold only for Θ\Theta

e.g. f(n)=Θ(g(n))g(n)=Θ(f(n))f(n)=\Theta(g(n)) \Leftrightarrow g(n)=\Theta(f(n))

Transpose Symmetry: holds for (OΩ)(O \leftrightarrow \Omega) and (oω)(o \leftrightarrow \omega)

e.g. f(n)=O(g(n))g(n)=Ω(f(n))f(n)=O(g(n))\Leftrightarrow g(n)=\Omega(f(n))

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using OO-Notation to Describe Running Times (1)

Used to bound worst-case running times, Implies an upper bound runtime for arbitrary inputs as well

Example:

Insertion sort has worst-case runtime of O(n2)O(n^2 )

Note:

  • This O(n2)O(n^2) upper bound also applies to its running time on every input

    • Abuse to say “running time of insertion sort is O(n2)O(n^2)"
  • For a given nn, the actual running time depends on the particular input of size nn

    • i.e., running time is not only a function of nn
  • However, worst-case running time is only a function of nn

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using OO-Notation to Describe Running Times (2)

  • When we say:

    • Running time of insertion sort is O(n2)O(n^2)
  • What we really mean is

    • Worst-case running time of insertion sort is O(n2)O(n^2)
  • or equivalently

    • No matter what particular input of size n is chosen, the running time on that set of inputs is O(n2)O(n^2)
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using Ω\Omega-Notation to Describe Running Times (1)

Used to bound best-case running times, Implies a lower bound runtime for arbitrary inputs as well

Example:

Insertion sort has best-case runtime of Ω(n)\Omega(n)

Note:

  • This Ω(n)\Omega(n) lower bound also applies to its running time on every input
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using Ω\Omega-Notation to Describe Running Times (2)

  • When we say

    • Running time of algorithm A is Ω(g(n))\Omega(g(n))
  • What we mean is

    • For any input of size nn, the runtime of A is at least a constant times g(n)g(n) for sufficiently large nn
  • It’s not contradictory to say

    • worst-case running time of insertion sort is Ω(n2)\Omega(n^2)

    • Because there exists an input that causes the algorithm to take Ω(n2)\Omega(n^2)

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using Θ\Theta-Notation to Describe Running Times (1)

Consider 2 cases about the runtime of an algorithm

  • Case 1: Worst-case and best-case not asymptotically equal

    • Use Θ\Theta-notation to bound worst-case and best-case runtimes separately
  • Case 2: Worst-case and best-case asymptotically equal

    • Use Θ\Theta-notation to bound the runtime for any input
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using Θ\Theta-Notation to Describe Running Times (2)

  • Case 1: Worst-case and best-case not asymptotically equal
    • Use Θ\Theta-notation to bound the worst-case and best-case runtimes separately
    • We can say:
      • "The worst-case runtime of insertion sort is Θ(n2)\Theta(n^2)"
      • "The best-case runtime of insertion sort is Θ(n)\Theta(n)"
    • But, we can’t say:
      • "The runtime of insertion sort is Θ(n2)\Theta(n^2) for every input"
    • A Θ\Theta-bound on worst/best-case running time does not apply to its running time on arbitrary inputs
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Worst-Case and Best-Case Equation for Merge-Sort

e.g. for merge-sort, we have:

T(n)=Θ(nlgn){T(n)=O(nlgn)T(n)=Ω(nlgn)T(n)=\Theta(nlgn)\begin{cases} T(n)=O(nlgn)\\ T(n)=\Omega(nlgn)\end{cases}

RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using Asymptotic Notation to Describe Runtimes Summary (1)

  • "The worst case runtime of Insertion Sort is O(n2)O(n^2)"

    • Also implies: "The runtime of Insertion Sort is O(n2)O(n^2)"
  • "The best-case runtime of Insertion Sort is Ω(n)\Omega(n)"

    • Also implies: "The runtime of Insertion Sort is Ω(n)\Omega(n)"
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using Asymptotic Notation to Describe Runtimes Summary (2)

  • "The worst case runtime of Insertion Sort is Θ(n2)\Theta(n^2)"

    • But: "The runtime of Insertion Sort is not Θ(n2)\Theta(n^2)"
  • "The best case runtime of Insertion Sort is Θ(n)\Theta(n)"

    • But: "The runtime of Insertion Sort is not Θ(n)\Theta(n)"
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Using Asymptotic Notation to Describe Runtimes Summary (3)

Which one is true?

  • FALSE "The worst case runtime of Merge Sort is Θ(nlgn)\Theta(nlgn)"

  • FALSE "The best case runtime of Merge Sort is Θ(nlgn)\Theta(nlgn)"

  • TRUE "The runtime of Merge Sort is Θ(nlgn)\Theta(nlgn)"

    • This is true, because the best and worst case runtimes have asymptotically the same tight bound Θ(nlgn)\Theta(nlgn)
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Notation in Equations (RHS)

  • Asymptotic notation appears alone on the RHS of an equation:

    • implies set membership

      • e.g., n=O(n2)n = O(n^2) means nO(n2)n \in O(n^2)

Asymptotic notation appears on the RHS of an equation
stands for some anonymous function in the set

  • e.g., 2n2+3n+1=2n2+Θ(n)2n^2 + 3n + 1 = 2n^2 + \Theta(n) means:

  • 2n2+3n+1=2n2+h(n)2n^2 + 3n + 1 = 2n^2 + h(n), for some h(n)Θ(n)h(n) \in \Theta(n)

    • i.e., h(n)=3n+1h(n) = 3n + 1
RTEU CE100 Week-1
CE100 Algorithms and Programming II

Asymptotic Notation in Equations (LHS)

  • Asymptotic notation appears on the LHS of an equation:

    • stands for any anonymous function in the set

      • e.g., 2n2+Θ(n)=Θ(n2)2n^2 + \Theta(n) = \Theta(n^2) means:
    • for any function g(n)Θ(n)g(n) \in \Theta(n)

    • \exists some function h(n)Θ(n2)h(n)\in \Theta(n^2)

      • such that 2n2+g(n)=h(n)2n^2+g(n) = h(n)
  • RHS provides coarser level of detail than LHS

RTEU CE100 Week-1
CE100 Algorithms and Programming II

References

RTEU CE100 Week-1
CE100 Algorithms and Programming II

EndOfWeek1CourseModule-End-Of-Week-1-Course-Module-

RTEU CE100 Week-1

<iframe width="800" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code=//%20C%2B%2B%20program%20for%20insertion%20sort%0A%23include%20%3Cstring.h%3E%0A%23include%20%3Ciostream%3E%0A%0Ausing%20namespace%20std%3B%0A%0Avoid%20printArray%28int%20arr%5B%5D,%20int%20n%29%3B%0Avoid%20insertionSort%28int%20arr%5B%5D,%20int%20n%29%3B%0A%0A%20%0A/*%20Function%20to%20sort%20an%20array%20using%20insertion%20sort*/%0Avoid%20insertionSort%28int%20arr%5B%5D,%20int%20n%29%0A%7B%0A%20%20%20%20int%20i,%20key,%20j%3B%0A%20%20%20%20for%20%28i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B%29%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20key%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20j%20%3D%20i%20-%201%3B%0A%20%0A%20%20%20%20%20%20%20%20/*%20Move%20elements%20of%20arr%5B0..i-1%5D,%20that%20are%0A%20%20%20%20%20%20%20%20greater%20than%20key,%20to%20one%20position%20ahead%0A%20%20%20%20%20%20%20%20of%20their%20current%20position%20*/%0A%20%20%20%20%20%20%20%20while%20%28j%20%3E%3D%200%20%26%26%20arr%5Bj%5D%20%3E%20key%29%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%20%2B%201%5D%20%3D%20arr%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20j%20-%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20arr%5Bj%20%2B%201%5D%20%3D%20key%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A//%20A%20utility%20function%20to%20print%20an%20array%20of%20size%20n%0Avoid%20printArray%28int%20arr%5B%5D,%20int%20n%29%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20%28i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B%29%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20cout%20%3C%3C%20endl%3B%0A%7D%0A%20%0A/*%20Driver%20code%20*/%0Aint%20main%28%29%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B%2012,%2011,%2013,%205,%206%20%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof%28arr%29%20/%20sizeof%28arr%5B0%5D%29%3B%0A%20%0A%20%20%20%20insertionSort%28arr,%20n%29%3B%0A%20%20%20%20printArray%28arr,%20n%29%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=cpp_g%2B%2B9.3.0&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe>