The "Big-Oh" Notation (Data Structure and Algorithms in Python #1002)

The “Big-Oh” Notation

If f(n) and g(n) are two functions that map positive integers to positive real numbers. We could say that f(n) is O(g(n)) if there is a real constant c > 0 and an integer constant n0 > 1 that:
f(n) ≤ cg(n), for n > n0
The f(n) is O(g(n)) is usually read as “f(n) is big-Oh of g(n)”. The following graph illustrates the Big-Oh notation.
Example 1.1 Justify the function f(n) = 5n + 3 is O(n)
Answer:
By the definition of “Big-Oh” notation, there must exist a constant c > 0 such that:
f(n) ≤ cg(n) or 5n + 3 ≤ cn for all n > n
5n + 3 ≤ (5 + 3)n = 8n and n0 = 1, so 5n + 3 is O(n)

Example 1.2 Justify 5n2 + 2nlogn + 3n + 6 is O(n2) (logn ≤ n for n ≥ 1, base 2)
Answer: 5n2 + 2nlogn + 3n + 6 ≤ (5 + 2 + 3 + 6)n2 = 16n2 for c = 16, when n ≥ n0 = 1

Example 1.3 Justify 2logn + 2 is O(logn) (logn ≤ n for n ≥ 1, base 2)

Answer: 2logn + 2 ≤ (2 + 2)logn = 4logn for c = 4, when n ≥ n0 = 2 for log1 = 0

Comments

Popular posts from this blog

How to write a slide puzzle game with Python and Pygame (2020 tutorial)

How to create a memory puzzle game with Python and Pygame (#005)

Introduction to multitasking with Python #001 multithreading (2020 tutorial)