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 j - the subarray A[1…j−1] consist of the elements originally in A[1…j−1] but in sorted order.
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.
T(n) = average time over all inputs of size n, inputs can have a uniform distribution
Presentation of Big-Theta : Θ(n)
Best Case (Omega Notation)
T(n) = min time on any input of size n, for example sorted array
Presentation of Big-Omega : Ω(n)
CE100 Algorithms and Programming II
Array Sorting Algorithms Time and Space Complexity
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)∣n→∞
CE100 Algorithms and Programming II
Asymptotic Analysis (1)
CE100 Algorithms and Programming II
Asymptotic Analysis (2)
Theta-Notation (Average-Case)
Drop low order terms
Ignore leading constants
e.g
2n2+5n+33n3+90n2−2n+5=Θ(n2)=Θ(n3)
As n gets large, a Θ(n2) algorithm runs faster than a Θ(n3) algorithm
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.
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-12. key = A[j]
c3 n-13. //insert A[j] into the sorted sequence A[1...j-1]
c4 n-14. 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-18. A[i+1] = key
we have two loops here, if we sum up costs as follow we can see big-O worst case notation.
To compare this sorting algorithm please check the following map again.
CE100 Algorithms and Programming II
Merge Sort : Divide / Conquer / Combine (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
CE100 Algorithms and Programming II
Merge Sort Example
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]
initial case
A : Array
p : 1 (offset)
r : n (length)
Merge-Sort(A,1,n)
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)
returnelse
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
CE100 Algorithms and Programming II
Merge Sort Algorithm (Combine-1)
p=start−point q=mid−point r=end−point
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)
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=1for k=p to r
if L[i]<=R[j]
A[k]=L[i]
i=i+1else
A[k]=R[j]
j=j+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 3n and Θ(n) will be merge complexity
CE100 Algorithms and Programming II
Merge Sort Correctness
Base case
p=r (Trivially correct)
Inductive hypothesis
MERGE-SORT is correct for any subarray that is a strict (smaller) subset of A[p,q].
General Case
MERGE-SORT is correct for A[p,q]. From inductive hypothesis and correctness of Merge.
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)
returnelse
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
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)
returnelse
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
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)2T(n/2)+Θ(n)if n=1otherwise
CE100 Algorithms and Programming II
How To Solve Recurrence (1)
T(n)={Θ(1)2T(n/2)+Θ(n)if n=1otherwise
CE100 Algorithms and Programming II
How To Solve Recurrence (2)
We will assume T(n)=Θ(1) for sufficiently small n to rewrite equation as
T(n)=2T(n/2)+Θ(n)
Solution for this equation will be Θ(nlgn) with following recursion tree.
CE100 Algorithms and Programming II
How To Solve Recurrence (3)
Multiply by height Θ(lgn) with each level cost Θ(n) we can found Θ(nlgn)
CE100 Algorithms and Programming II
How To Solve Recurrence (4)
This tree is binary-tree and binary-tree height is related with item size.
CE100 Algorithms and Programming II
How Height of a Binary Tree is Equal to logn ? (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.
CE100 Algorithms and Programming II
How Height of a Binary Tree is Equal to logn ? (2)
Let n be the number of nodes in the tree and let lk denote the number of nodes on level k. According to this;
lk=2lk−1 i.e. each level has exactly twice as many nodes as the previous level
l0=1 , i.e. on the first level we have only one node (the root node)
The leaves are at the last level, lh where h is the height of the tree.
CE100 Algorithms and Programming II
How Height of a Binary Tree is Equal to logn ? (3)
The total number of nodes in the tree is equal to the sum of the nodes on all the levels: nodes n
(Important) Analogy to compare of two real numbers (2)
O≈≤Θ≈=Ω≈≥ω≈>o≈<
CE100 Algorithms and Programming II
(Important) Trichotomy property for real numbers
For any two real numbers a and b, we have either
a<b, or a=b, or a>b
Trichotomy property does not hold for asymptotic notation, for two functions f(n) and g(n), it may be the case that neither f(n)=O(g(n)) nor f(n)=Ω(g(n)) holds.
e.g. n and n1+sin(n) cannot be compared asymptotically
CE100 Algorithms and Programming II
Examples
5n2=O(n2)
TRUE
n2lgn=O(n2)
FALSE
5n2=Ω(n2)
TRUE
n2lgn=Ω(n2)
TRUE
5n2=Θ(n2)
TRUE
n2lgn=Θ(n2)
FALSE
5n2=o(n2)
FALSE
n2lgn=o(n2)
FALSE
5n2=ω(n2)
FALSE
n2lgn=ω(n2)
TRUE
2n=O(3n)
TRUE
2n=Ω(3n)
FALSE
2n=o(3n)
TRUE
2n=Θ(3n)
FALSE
2n=ω(3n)
FALSE
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))
Reflexivity: holds for Θ,O,Ω
e.g. f(n)=O(f(n))
Symmetry: hold only for Θ
e.g. f(n)=Θ(g(n))⇔g(n)=Θ(f(n))
Transpose Symmetry: holds for (O↔Ω) and (o↔ω)
e.g. f(n)=O(g(n))⇔g(n)=Ω(f(n))
CE100 Algorithms and Programming II
Using O-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)
Note:
This O(n2) upper bound also applies to its running time on every input
Abuse to say “running time of insertion sort is O(n2)"
For a given n, the actual running time depends on the particular input of size n
i.e., running time is not only a function of n
However, worst-case running time is only a function of n
CE100 Algorithms and Programming II
Using O-Notation to Describe Running Times (2)
When we say:
Running time of insertion sort is O(n2)
What we really mean is
Worst-case running time of insertion sort is O(n2)
or equivalently
No matter what particular input of size n is chosen, the running time on that set of inputs is O(n2)
CE100 Algorithms and Programming II
Using Ω-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)
Note:
This Ω(n) lower bound also applies to its running time on every input
CE100 Algorithms and Programming II
Using Ω-Notation to Describe Running Times (2)
When we say
Running time of algorithm A is Ω(g(n))
What we mean is
For any input of size n, the runtime of A is at least a constant times g(n) for sufficiently large n
It’s not contradictory to say
worst-case running time of insertion sort is Ω(n2)
Because there exists an input that causes the algorithm to take Ω(n2)
CE100 Algorithms and Programming II
Using Θ-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 Θ-notation to bound worst-case and best-case runtimes separately
Case 2: Worst-case and best-case asymptotically equal
Use Θ-notation to bound the runtime for any input
CE100 Algorithms and Programming II
Using Θ-Notation to Describe Running Times (2)
Case 1: Worst-case and best-case not asymptotically equal
Use Θ-notation to bound the worst-case and best-case runtimes separately
We can say:
"The worst-case runtime of insertion sort is Θ(n2)"
"The best-case runtime of insertion sort is Θ(n)"
But, we can’t say:
"The runtime of insertion sort is Θ(n2) for every input"
A Θ-bound on worst/best-case running time does not apply to its running time on arbitrary inputs
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)
CE100 Algorithms and Programming II
Using Asymptotic Notation to Describe Runtimes Summary (1)
"The worst case runtime of Insertion Sort is O(n2)"
Also implies: "The runtime of Insertion Sort is O(n2)"
"The best-case runtime of Insertion Sort is Ω(n)"
Also implies: "The runtime of Insertion Sort is Ω(n)"
CE100 Algorithms and Programming II
Using Asymptotic Notation to Describe Runtimes Summary (2)
"The worst case runtime of Insertion Sort is Θ(n2)"
But: "The runtime of Insertion Sort is not Θ(n2)"
"The best case runtime of Insertion Sort is Θ(n)"
But: "The runtime of Insertion Sort is not Θ(n)"
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)"
FALSE "The best case runtime of Merge Sort is Θ(nlgn)"
TRUE "The runtime of Merge Sort is Θ(nlgn)"
This is true, because the best and worst case runtimes have asymptotically the same tight bound Θ(nlgn)
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) means n∈O(n2)
Asymptotic notation appears on the RHS of an equation
stands for some anonymous function in the set
e.g., 2n2+3n+1=2n2+Θ(n) means:
2n2+3n+1=2n2+h(n), for some h(n)∈Θ(n)
i.e., h(n)=3n+1
CE100 Algorithms and Programming II
Asymptotic Notation in Equations (LHS)
Asymptotic notation appears on the LHS of an equation: