0% found this document useful (0 votes)
128 views

Introduction To Design Analysis & Algorithms

Yes, it will stop and return the correct output of 27. Numbers= {-13, -4, -12, -27} Is it working??????
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Introduction To Design Analysis & Algorithms

Yes, it will stop and return the correct output of 27. Numbers= {-13, -4, -12, -27} Is it working??????
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

By:

Dr. Rimjhim Singh


Asst. Professor (Sr.Gr.)
Department of Computer Science & Engg.
Dr. Rimjhim Singh 8/26/2022 1
Amrita Vishwa Vidyapeetham
 This course aims
◦ To provide the fundamentals of algorithm design and analysis.
◦ Specifically in terms of algorithm design techniques
◦ Application of these design techniques for real-world problem
solving.
◦ Analysis of complexity and correctness of algorithms to provide
the best solution to complex tasks in terms of simpler tasks.

Dr. Rimjhim Singh 8/26/2022 2


 CO1: Evaluate the correctness and analyze complexity of algorithms.

 CO2: Understand and implement various algorithmic design techniques


and solve classical problems

 CO3: Design solutions for real world problems by identifying, applying


and implementing appropriate design techniques.

 CO4: Design solutions for real world problem by mapping to classical


problems

 CO5: Analyze the impact of various implementation choices on the


algorithm complexity

Dr. Rimjhim Singh 8/26/2022 3


 Unit 1 Introduction and Review- Algorithms vs. programs. Flow charts and pseudo code, Rate of growth of
functions. Review of Asymptotic notation: motivation and types of notations. Recurrence relations and methods to
solve them: Recursion tree, substitution, Master Method. Review of Sorting: Bubble –Insertion – Selection –
Bucket – Heap, Comparison of sorting algorithms, Applications. Graph Algorithms – Graph Traversal:
Applications of BFS: distance, connectivity and connected components and cycles in undirected graphs.
Applications of DFS: Topological sort, cycles in directed graphs, Biconnected Components and Strong
Connectivity. Path algorithms: Shortest path algorithms (along with analysis) SSSP: Bellman Ford. APSP: Floyd
Warshall’s. Review of Minimum Spanning Tree (with analysis and applications).

 Unit 2 Divide and Conquer: Merge sort and Binary search type strategies, Pivot based strategies – Long integer
multiplication – Maximum sub array sum - Closest Pair problem etc as examples. Greedy Algorithm -
Introduction to the method, Fractional Knapsack problem, Task Scheduling Problem, Huffman coding etc as
examples. Dynamic Programming: Introduction to the method, Fibonacci numbers, 0-1 Knapsack problem, Matrix
chain multiplication problem, Longest Common Subsequence, and other problems including problems
incorporating combinatorics as examples.

 Unit 3 Backtracking, Branch and Bound 0-1 Knapsack, N- Queen problem, subset sum as some examples. String
Matching: Rabin Karp, Boyer Moore, KMP. Network Flow and Matching: Flow Algorithms Maximum Flow –
Cuts Maximum Bipartite Matching. Introduction to NP class: Definitions P, NP, NP complete, NP hard, Examples
of P and NP.

Dr. Rimjhim Singh 8/26/2022 4


 TEXTBOOK:
Michael T Goodrich and Roberto Tamassia, “Algorithm Design Foundations -
Analysis and Internet Examples”, John Wiley and Sons, 2007.

 REFERENCES:
1. Thomas H Cormen, Charles E Leiserson, Ronald L Rivest and Clifford Stein,
“Introduction to Algorithms”, Third Edition, Prentice Hall of India Private Limited,
2009
2. Dasgupta S, Papadimitriou C and Vazirani U, “Algorithms”, Tata McGraw-Hill,
2009.
3. Jon Kleinberg, Eva Tardos. Algorithm Design. First Edition, Pearson Education
India; 2013.

Dr. Rimjhim Singh 8/26/2022 5


 Evaluation: 65:35

 Periodical -1
 Periodical – 2
 Continuous Theory

 Lab tests : 2 (Tentative)


 Case Study

 End Semester: 35

Dr. Rimjhim Singh 8/26/2022 6


 Identify problems in real world solvable by computers
 Understand the problem
◦ Understand the inputs
◦ Output requirements
◦ Constraints under which the problem must operate
 Identify potential solutions
 Select best solution
◦ Fastest
◦ Most accurate

Dr. Rimjhim Singh 8/26/2022 7


A finite sequence of

Precise and well Computer Solves a Finite amount


defined steps implementable specific of time
instructions task/
problem

Definition:
“A finite sequence of unambiguous, executable steps or instructions, which, if followed
would ultimately terminate and give the solution of the problem”.

Dr. Rimjhim Singh 8/26/2022 8


• Algorithm
•Abstract description of a process
• Just like a method to solve the problem
• Does not deal with machine specific details

• Program
•Implementations of algorithms
• For specific system and platform

Dr. Rimjhim Singh 8/26/2022 9


Classification of Algorithms

Implementation Design Optimization Complexity


-Iterative -Brute fore - Linear -Linear
-Recursive -DAC programming -Logarithmic
-Parallel -Randomized -Dynamic -Quadratic
-Deterministic -Backtracking Programming -Polynomial
-Approximate -Greedy -Exponential
approach

Dr. Rimjhim Singh 8/26/2022 10


● High level description • ‘ ‘ for assignment and ‘=‘ for equality
● Intended for humans Expression • Mathematical expressions are allowed
● More structured than
prose Method • Algorithm name (param1, param2,...)
● Hides declaration • Defines new method ‘name’ with arguments

implementation /
design details Decision • If condition then Action1 else Action 2
● Less structured than structure • Use indentations

programs
While/ for/ • While condition do Action-1
• Repeat Action-1 until condition
repeat loops

• A[i] indexes to the ith element of array A


Array indexing

• Object.method(param1, param2,...)
Method calls • Object is optional

• Return value
Method returns

Dr. Rimjhim Singh 8/29/2022 11


Example: Maximum element in an Array

Algorithm arrayMax
Input: N number of integers
Output: Maximum number
Step-1: Create a local variable max to store the maximum
among the list
Step-2: Initialize max with the first element initially, to
start the comparison.
Step-3: Then traverse the given array from second
element till end, and for each element:
Step-4: Compare the current element with max
Step-5: If the current element is greater than max,
then replace the value of max with the current
element.
Step-6: At the end, return and print the value of the
largest element of array stored in max
Pseudocode
Algorithm
Dr. Rimjhim Singh 8/29/2022 12
Dr. Rimjhim Singh 8/29/2022 13
Algorithms can be analyzed based on the following
parameters:

● Correctness
●Amount of Work done
● Space used
● Simplicity, clarity
● Optimality

Dr. Rimjhim Singh 8/29/2022 14


Understand what correctness means
◦ Define the characteristics of the input an algorithm is
expected to work on
◦ The results that each input must produce
◦ Prove the statement about the relationship between input and
output

 Prove Correctness of algorithm

Dr. Rimjhim Singh 8/29/2022 15


• Specify the task to be performed by the algorithm
• Specification consists of:
 Name of algorithm and list of its arguments
 Initial condition (it specifies what is correct input data to
the problem)
 Final condition (it specifies what is the desired result of the
algorithm)

• An algorithm is called totally correct for the given


specification if and only if for any correct input data it:
 stops
 returns correct output

Dr. Rimjhim Singh 8/29/2022 16


Numbers= {13, 4 12, 27}
Is it working??????
Will it stop????
Numbers= {-13, -4, -12, -27}
Is it working??????

Is the Result correct??

Dr. Rimjhim Singh 8/29/2022 17


Change this to maxNum=numbers[0]

Numbers= {13, 4 12, 27}


Is it working??????
Will it stop????
Numbers= {-13, -4, -12, -27}
Is it working??????

Is the Result correct??

Dr. Rimjhim Singh 8/29/2022 18


 What is the largest integer?
INPUT: All the integers { … -2, -1, 0, 1, 2, … }
OUTPUT: The largest integer
Algorithm:
 Arrange all the integers in a list in decreasing order;
 MAX = first number in the list;
 Print out MAX;

Will it stop????

Is the Result correct??

Dr. Rimjhim Singh 8/29/2022 19


Analysis can be done for :
Resources required.
Memory / space required
Runtime requirements

• An efficient algorithm requires less time, space, resources and


computations

• Analyzing runtime requirements are of utmost importance

• Obvious way to measure the efficiency of an algorithm is to run it


and measure how much processor time is needed

• Is it correct
?????? Dr. Rimjhim Singh 8/29/2022 20
Factors:
• Hardware
• Operating
System
• Compiler
• Size of input
• Nature of Input

• Solution
Theoretical approximation is preferred
- It characterizes the execution time of an algorithm
independently from the machine, the language and compiler.
- Analyzing variation in runtime requirements with
changing size of input data
- Comparisons of different algorithms.
Dr. Rimjhim Singh 8/29/2022 21
Analyzing Algorithms

 Runtime complexity of algorithm


◦ Varies with input size
◦ Nature of input data

 Model of Computation:
◦ Mathematical model
◦ Asymptotic Notations

Dr. Rimjhim Singh 8/29/2022 22


 It is of three types:
(a) Best-case:
- Describes an algorithm's behavior under optimal conditions.
- Searching an element present at first position
(b) Average-case:
- Provides the cost of algorithm for average case of data.
- Analyzing average data and cost is difficult.
(c) Worst-case:
- Provides upper-bound or maximum cost of algorithm.
- Safe analysis of over-all behaviour.
Mostly worst-case analysis of the algorithms is computed for
comparisons.

Dr. Rimjhim Singh 8/29/2022 23


Difference in complexity matters!!!!!

Dr. Rimjhim Singh 8/29/2022 24


 Random Access Machine Model:
 Counts the primitive operations / computations
 Assumes that CPU performs single primitive operation in 1
unit time
 Primitive operations are identified and counted to analyze cost
–Assigning a value to a variable
–Performing an arithmetic operation
–Calling a method
–Comparing two numbers
–Indexing into an array
–Following an object reference
–Returning from a method

Dr. Rimjhim Singh 8/29/2022 25


Counting primitive operations:
Algorithm FindMax(S, n)
Input : An array S storing n numbers,n>=1
Output: Max Element in S

S1: curMax ← A[0]


S2: i←1
S3: while i <= (n-1) do
S4: if curMax < A[i] then
S5: curMax ← A[i]
S6: i ← i+1;
S7: return curmax
Complexity ?????

Dr. Rimjhim Singh 8/29/2022 26


 Counting primitive operations:
Algorithm FindMax(S, n)
Input : An array S storing n numbers,n>=1
Output: Max Element in S

S1: curMax ← A[0] 2 op (ind & assign)


S2: i←1 1 op (assign)
S3: while i<= n-1 do 2n op (n comp+n sub)
S4: if curMax < A[i] then 2(n-1) op (n-1 [] & n-1 <)
S5: curMax ← A[i] 0 to 2(n-1) op
S6: i ← i+1; 2(n-1) op (+ & ->)
S7: return curmax 1 op
Complexity between 6n and 8n-2

Dr. Rimjhim Singh 8/29/2022 27


Dr. Rimjhim Singh 8/29/2022 28
Think how to analyze ?????

Dr. Rimjhim Singh 8/29/2022 29


Dr. Rimjhim Singh 8/29/2022 30
Dr. Rimjhim Singh 8/29/2022 31
Constant factor
can be dropped
n

Constant factor
can be dropped
n2

Dr. Rimjhim Singh 8/29/2022 32


Constant factor
can be dropped
O(n)

Constant factor
can be dropped
O(n2)

Dr. Rimjhim Singh 8/29/2022 33


int a = 0, b = 0;
for (i = 0; i < N; i++)
{
a = a + rand();
}
for (j = 0; j < M; j++)
{
b = b + rand();
}

Time complexity=??
Space Complexity=??

Dr. Rimjhim Singh 8/29/2022 34


function isPrime(n) {

for (for i = 2; i <= sqrt(n); ++i)


{

if (n % i === 0)
{
return false;
}
}
return true;
}

Time complexity=

Dr. Rimjhim Singh 8/29/2022 35


int a = 0, i = N;
while (i > 0)
{
a += i;
i /= 2;
}

Time complexity=

Dr. Rimjhim Singh 8/29/2022 36


for(var i=0;i<n;i++)
{
i*=k
}

Time complexity=???

Dr. Rimjhim Singh 8/29/2022 37


let a = 0, b = 0;
for (let i = 0; i < n; ++i)
{
for (let j = 0; j < n; ++j) {
a = a + j;
}
}
for (let k = 0; k < n; ++k)
{
b = b + k;
}
Time complexity=????

Dr. Rimjhim Singh 8/29/2022 38


int i, j, k = 0;
for (i = n / 2; i <= n; i++)
{
for (j = 2; j <= n; j = j * 2)
{
k = k + n / 2;
}
}

Time complexity=????

Dr. Rimjhim Singh 8/29/2022 39


int count = 0;
for (int i = N; i > 0; i /= 2)
{
for (int j = 0; j < i; j++)
{
count += 1;
}
}
Time complexity=???

Dr. Rimjhim Singh 8/29/2022 40


void counter(int n)
{
for(int i = 1 ; i < n ; i++)
{
for(int j = 1 ; j<n ; j += i )
{
cout<<i<<” ”<<j;
}
cout<<endl;
}
}

Time complexity=????

Dr. Rimjhim Singh 8/29/2022 41


 Important factor to be considered when estimating
complexity
 When experimental setup (hardware/software) changes
◦ Running time/memory is affected by a constant factor
◦ 2n or 3n or 100n is still linear
◦ Growth rate of the running time/memory is not affected
 Growth rates of functions
◦ Linear
◦ Quadratic
◦ Exponential
◦ Logarithmic
◦ Polynomial

Dr. Rimjhim Singh 8/29/2022 42


 A method to characterize the execution time of an algorithm
 Provides the upper bound or worst-case complexity analysis
 Adding two square matrices is Quadratic
 Searching in an array (linear search) is Linear
 Searching in an array (binary search) is Logarithmic
 Multiplying two square matrices is Cubic

 The O notation only uses the dominating terms of the execution


time. Constants are disregarded.

Dr. Rimjhim Singh 8/29/2022 43


 Let f(n) and g(n) be functions mapping nonnegative integers to real numbers. We say
that f(n) is O(g(n)) if there is a real constant c > 0 and an integer constant
n0 ≥ 1 such that f(n) ≤ cg(n) for every integer n ≥ n0.
 This definition is often pronounced as “f(n) is big-Oh of g(n)” or “f(n) is order g(n).”
 Example: 7n − 2 is O(n).

Mostly analysis is done


for larger ‘n’
Dr. Rimjhim Singh 8/29/2022 44
 Let f(n) and g(n) be functions mapping nonnegative integers to real numbers. We say
that f(n) is O(g(n)) if there is a real constant c > 0 and an integer constant
n0 ≥ 1 such that f(n) ≤ cg(n) for every integer n ≥ n0.
 This definition is often pronounced as “f(n) is big-Oh of g(n)” or “f(n) is order g(n).”
 Example: 7n − 2 is O(n).

Mostly analysis is done


for larger ‘n’
Dr. Rimjhim Singh 8/29/2022 45
 Let f(n) and g(n) be functions mapping nonnegative
integers to real numbers. We say that f(n) is O(g(n)) if
there is a real constant c > 0 and an integer constant
n0 ≥ 1 such that f(n) ≤ cg(n) for every integer n ≥ n0.
 This definition is often pronounced as “f(n) is big-Oh
of g(n)” or “f(n) is order g(n).”
 Example: 7n − 2 is O(n).

Dr. Rimjhim Singh 8/29/2022 46


Example
f(n) => O(g(n)) if
•Show 7n-2 is O(n)
f(n) <= c.g(n) when n> n0

Here f(n)= 7n-2


g(n)=n

–need c > 0 and n0 >= 1 such that


7n-2 <= cn for n >= n0
–this is true for c = 7 and n0 = 1

Multiple values of C and n0 are


possible
Dr. Rimjhim Singh 8/29/2022 47
Algorithm FindMax(S, n)
Input : An array S storing n numbers,n>=1
Output: Max Element in S
S1: curMax ← A[0]
S2: i←1
S3: while i<= n-1 do
S4: if curMax < A[i] then
S5: curMax ← A[i]
S6: i ← i+1;
S7: return curmax
Complexity between 6n and 8n-2
show T(n) = 8n-2 is O(n)
c=8 n0=1
Prove T(n) = 8n-2 is O(n)

Dr. Rimjhim Singh 8/29/2022 48


Example f(n) => O(g(n)) if
• Show 3n3 + 20n2 + 5 is O(n3) f(n) <= c.g(n) when n> n0
Sol
–need c > 0 and n0 >= 1 such that
3n3 + 20n2 + 5 <= cn3 for n >= n0
–this is true for c = 4 and n0 = 21
Another way to prove

3n3 < (3n3 + 20n2 + 5) <= (28) n3

C=28 ; Constant term.


Hence proved

Dr. Rimjhim Singh 8/29/2022 49


Example
• Prove n2 is not O(n)

Example
• Show 3 log n + log log n is O(log n)

Dr. Rimjhim Singh 8/29/2022 50


 The big-Oh notation gives an upper bound on the growth rate of
a function

 The statement “f(n) is O(g(n))” means that the growth rate of f(n)
is no more than the growth rate of g(n)

 We are guaranteeing that f(n) grows at a rate no faster than g(n)


 Both can grow at the same rate
◦ Though 1000n is larger than n2 , n2 grows at a faster rate
◦ n2 will be larger function after n = 1000
◦ Hence 1000n = O(n)

 The big-Oh notation can be used to rank functions according to


their growth rate
Dr. Rimjhim Singh 8/29/2022 51
Growth rate analysis of different functions

Dr. Rimjhim Singh 8/29/2022 52


4. What does it mean when we say that an
algorithm X is asymptotically more efficient than
Y?
Options:
a. X will always be a better choice for small inputs
b. X will always be a better choice for large inputs
c. Y will always be a better choice for small inputs
d. X will always be a better choice for all inputs

Dr. Rimjhim Singh 8/29/2022 53


• If is f(n) a polynomial of degree d, then f(n) is O(nd), i.e.,

• Drop lower-order terms


•Drop constant factors

•Use the smallest possible class of functions to represent in


big Oh

•“2n is O(n)”
• Use the simplest expression of the class
•“3n+ 5 is O(n)” instead of “3n + 5 is O(3n)”

Dr. Rimjhim Singh 8/29/2022 54


Algorithm A and B have a worst-case running time complexity,
respectively. Therefore, algorithm B always runs faster than the
algorithm A.

TRUE / FALSE

Dr. Rimjhim Singh 8/29/2022 55


● Show that 8n+5 is O(n)
●Show that 20n3 +10n2+5 is O(n3)
●Show that 3logn+2 is O(logn).

Arrange the following functions in order of their


complexities.
5n2, log3n, 20n, log2n, n2/3, 2n, 2100,n1000

Plot graph for the fucntions and verify your results.

Dr. Rimjhim Singh 8/29/2022 56


Calculate problems for the following
the following

Dr. Rimjhim Singh 8/29/2022 57


 Let d(n), e(n), f(n), and g(n) be functions mapping nonnegative integers to
non-negative reals.

1. If d(n) is O(f(n)), then ad(n) is O(f(n)), for any constant a > 0.


2. If d(n) is O(f(n)) and e(n) is O(g(n)), then d(n)+e(n) is O(f(n)+g(n)).
3. If d(n) is O(f(n)) and e(n) is O(g(n)), then d(n)e(n) is O(f(n)g(n)).
4. If d(n) is O(f(n)) and f(n) is O(g(n)), then d(n) is O(g(n)).
5. If f(n) is a polynomial of degree d (that is, f(n) = a0 +a1n+・ ・ ・+ad nd),
then f(n) is O(nd).
6. nx is O(an) for any fixed x > 0 and a > 1.
7. log nx is O(log n) for any fixed x > 0.
8. logx n is O(ny) for any fixed constants x > 0 and y > 0.

Dr. Rimjhim Singh 8/29/2022 58


Dr. Rimjhim Singh 8/29/2022 59
????

????

Own understanding:
Analyze complexity of bubble sort and
merge sort by counting primitive
functions.

Dr. Rimjhim Singh 8/29/2022 60


 Let f(n) and g(n) be functions mapping nonnegative integers to real numbers. We
say that f(n) is Ω (g(n)) if there is a real constant c > 0 and an integer constant
n0 ≥ 1 such that f(n) ≥ cg(n) for every integer n ≥ n0.
 This definition is often pronounced as “f(n) is big-Omega of g(n)”
 Example: 7n − 2 is Ω (n).

Dr. Rimjhim Singh 8/30/2022 61


Likewise, we say that f(n) is Θ(g(n)) (pronounced “f(n) is big-Theta of g(n)”)
if f(n) is O(g(n)) and f(n) is Ω(g(n)); that is, there are real constants c1 > 0 and c2 > 0, and
an integer constant n0 ≥ 1 such that c1g(n) ≤ f(n) ≤ c2 g(n), for n ≥ n0.

NOTE:
g(n) here is same on
both the sides.

Left side represents Ω while


right side represents θ

Inference:

Θ exists only when


Ω(g(n) Ո O(g(n) ≠ φ

Dr. Rimjhim Singh 8/30/2022 62


Problem-1
Let T(n)=3n3 + 20n2 + 5 for an algorithm. Express in terms of Big(O), Big(Ω)
and Big (θ)

Problem-2
Let T(n) be running time for an algorithm and its lower bound is 2n2 + 1
and its upper bound is 20n2 + 5n+8.
Express in terms of Big(O), Big(Ω) and Big (θ)

Problem-3
Express complexity of Bubble sort in terms of Big(O), Big(Ω) and Big (θ)

Dr. Rimjhim Singh 8/30/2022 63


Comment on complexity of T(n)=1+2+3+.....+n
in terms of θ, Ω and O

Dr. Rimjhim Singh 8/30/2022 64


 Let f(n) and g(n) be functions mapping integers to real numbers. We say that
f(n) is o(g(n)) (pronounced “f(n) is little-oh of g(n)”) if, for any constant c > 0,
there is a constant n0 ≥ 1 such that f(n) < cg(n) for n ≥ n0. Eg: 7n + 8 ∈ o(n2)

 Likewise, we say that f(n) is ω(g(n)) (pronounced “f(n) is little-omega of g(n)”)


if g(n) is o(f(n)), that is, if, for any constant c > 0, there is a constant n0 ≥ 1
such that f(n) > cg(n) for n ≥ n0. Eg: 4n + 6 ∈ ω(1)

Note: Little-ο, (ο()) notation is used to describe an upper-bound that cannot be


tight.
Little ω, notation is used to denote a lower bound that is not
asymptotically tight.

65
 x2∈O(x2)
 x2∉o(x2)
 x2∈o(x3)

The following are true for Big-O, but would not be true if
you used little-o:
• x² ∈ O(x²)
• x² ∈ O(x² + x)
• x² ∈ O(200 * x²)

The following are true for little-o: For example, the


• x² ∈ o(x³) function f(n) = 3n is:
• x² ∈ o(x!) •in O(n²), o(n²), and O(n)
• ln(x) ∈ o(x) •not in O(lg n), o(lg n),
or o(n)

66
67
∞ = 1/0

 Example 1: 3x2+2x is O(x2)

68
 Example 2: 3n2+2n is o(n2)
 Example 3: 3n2+2n is o(n3)

69
 Example 4: 3n2+2n is Ω(n)
 Example 5: 3n2+2n is ω(n)

70
 Theta, commonly written as Θ, is an Asymptotic
Notation to denote the asymptotically tight
bound on the growth rate of runtime of an
algorithm.
 For example, if f(n) is O(g(n)), and f(n) is O(h(n))
and if g(n) is O(h(n)) then g(n) is called the
asymptotically tight notation.

71
 So for log(n) we could say:
log(n) is O(n^n) since log(n) grows asymptotically no faster than n^n
log(n) is O(n) since log(n) grows asymptotically no faster than n
log(n) is O(log(n)) since log(n) grows asymptotically no faster than log(n)
We went from loose upper bounds to a tight upper bound

 log(n) is Ω(1) since log(n) grows asymptotically no slower than 1


log(n) is Ω(log(log(n))) since log(n) grows asymptotically no slower than
log(log(n))
log(n) is Ω(log(n)) since log(n) grows asymptotically no slower than log(n)
We went from loose lower bounds to a tight lower bound
Since we have log(n) is O(log(n)) and Ω(log(n)) we can say that log(n) is Θ(log(n)).

72
73
74
75
 Let a, b, and c be positive real numbers. We have

76

77
Some other observations we can make are:
f(n) = Q(g(n)) ⇔ g(n) = Q(f(n))
f(n) = O(g(n)) ⇔ g(n) = W(f(n))
f(n) = o(g(n)) ⇔ g(n) = w(f(n))

Big-Q as an Equivalence Relation

If we look at the first relationship, we notice that


f(n) = Q(g(n)) seems to describe an equivalence relation:
1. f(n) = Q(g(n)) if and only if g(n) = Q(f(n))
2. f(n) = Q(f(n))
3. If f(n) = Q(g(n)) and g(n) = Q(h(n)), it follows that f(n) = Q(h(n))

Consequently, we can group all functions into equivalence


classes, where all functions within one class are big-theta Q of
each other

78
Examples:
O(n) + O(n2) + O(n4) = O(n + n2 + n4) = O(n4)
O(n) + Q(n2) = Q(n2)
O(n2) + Q(n) = O(n2)
O(n2) + Q(n2) = Q(n2)

79

You might also like