Big-O Flashcards

1
Q

What is “Big-O” ?

A

The language and metric we use to describe the efficiency of algorithms.

Also called “Asymptotic Runtime”

Refers to how much time or space an algorithm will use, generally based on an input size “n”

It is a rough upper bound for worst cases and large inputs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Common Big-O Runtimes

A
  • O(1) - Constant
  • O( log N ) - Logarithmic
  • O( N ) - Linear
  • O( N log N ) - Log Linear
  • O( N2 ) - Quadratic
  • O( N3 ) - Cubic
  • O( 2N ) - Exponential
  • O( N! ) - Factorial
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which has the higher complexity?

O(N)

or

O( 1 )

A

O( N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which has the higher complexity?

O(N)

or

O( log N )

A

O( N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which has the higher complexity?

O(N)

or

O( N log N )

A

O( N log N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which has the higher complexity?

O(N log N)

or

O( 1 )

A

O( N log N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which has the higher complexity?

O(N2)

or

O( 1 )

A

O( N2 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which has the higher complexity?

O( N3 )

or

O( 1 )

A

O( N3 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which has the higher complexity?

O( 1 )

or

O( 2N )

A

O( 2N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which has the higher complexity?

O( N! )

or

O( 1 )

A

O( N! )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which has the higher complexity?

O( log N )

or

O( N log N )

A

O( N log N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which has the higher complexity?

O( N2 )

or

O( log N )

A

O( N2 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which has the higher complexity?

O( log N )

or

O( N3 )

A

O( N3 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which has the higher complexity?

O( 2N )

or

O( log N )

A

O( 2N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which has the higher complexity?

O( log N )

or

O( N! )

A

O( N! )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which has the higher complexity?

O( N )

or

O( N2 )

A

O( N2 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which has the higher complexity?

O( N3 )

or

O( N )

A

O( N3 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which has the higher complexity?

O( N )

or

O( 2N )

A

O( 2N )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which has the higher complexity?

O( N! )

or

O( N )

A

O( N! )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Which has the higher complexity?

O( N log N )

or

O( N2 )

A

O( N2 )

21
Q

Which has the higher complexity?

O( N3 )

or

O( N log N )

A

O( N3 )

22
Q

Which has the higher complexity?

O( N log N )

or

O( 2N )

A

O( 2N )

23
Q

Which has the higher complexity?

O( N log N )

or

O( N! )

A

O( N! )

24
Q

Which has the higher complexity?

O( N3 )

or

O( N2 )

A

O( N3 )

25
Q

Which has the higher complexity?

O( N2 )

or

O( 2N )

A

O( 2N )

26
Q

Which has the higher complexity?

O( N! )

or

O( N2 )

A

O( N! )

27
Q

Which has the higher complexity?

O( 2N )

or

O( N3 )

A

O( 2N )

28
Q

Which has the higher complexity?

O( N3 )

or

O( N! )

A

O( N! )

29
Q

Which has the higher complexity?

O( 2N )

or

O( N! )

A

O( N! )

30
Q

Three Major

Complexity Notations

A

Big O

  • O( f(n) )
  • Asymptotic Upper bounds
  • Algorithm AT LEAST that fast

Big Omega

  • Ω( f(n) )
  • Asymptotic Lower Bounds
  • Algorithm will run AT MOST that fast

Big Theta

  • 𝚯( f(n) )
  • Combines O and Theta
  • Indicates that algorithm is O( f(n) ) AND Ω( f(n) )
  • Casual use of Big O often really means Big Theta
31
Q

Three general

Input Cases

A
  • Best Case
    • Input is organized in a way that the algorithm runs at the fastest possible speed, with the lowest possible complexity
    • Example: Already sorted array into sorting algorithm
  • Worst Case
    • Input organized so that algorithm runs at the slowest speed possible, with the highest complexity
    • Example: Reverse sorted array into sorting algorithm
  • Expected/General Case
    • What the vast majority of real world input will look like
32
Q

Two Types

of

Complexity

A

Time Complexity

Space Complexity

33
Q

Space Complexity

A
  • The space (typically memory) used by an algorithm
  • Also described with Big O notation

Example:

  • Array size n -> O(n)
  • 2D Array size n x n -> O(n2)
  • Call Stack: n recursive calls → O(n)
34
Q

Space Complexity:

Recursion

vs

Iterative(loops)

A

Space Complexity only increases when memory is being used simultaneously.

  • Recursive calls increase complexity, because they add to the stack
  • Loops(Iterative) do not increase complexity, memory in the scope of the loop is destroyed each iteration
35
Q

General Formulation

of

Big O

(steps)

A
  1. Describe the Worst Case Input
  2. Step through the algorithm, add or multiply runtime of sections
  3. Simplify by dropping constants:
    1. O(2n) = O(n)
  4. Further simplify by only using the most dominant(fastest growing) term:
    1. O(n2 + n) = O(n2)
  5. Consider for each type of operation
  6. Simplify amortized cost
36
Q

Types of Code Sections

and their Complexity

A

Code Section Type

Complexity

Sequential Instruction

O( 1 )

Single Pass through Array/Set

O( n )

Distinct Sequential Loops (e.g. two separate arrays)

O(A + B)

Nested Loops

O(A * B)

Elements Halved (eg Binary Search)

O( log n )

Recursive Function with Multiple calls

O(branchesdepth)

37
Q

Complexity of Code Section Types:

Sequential Instruction

A

O(1)

Constant

38
Q

Complexity of Code Section Types:

Single Pass

through Array/Set

A

O( n )

Linear

39
Q

Complexity of Code Section Types:

Distinct Sequential Loops

A

O( A + B)

Where A and B are the number of iterations in two distinct loops

40
Q

Complexity of Code Section Types:

Nested Loops

A

O( A * B)

Where B is the loop nested inside of loop A

41
Q

Complexity of Code Section Types:

Elements halved

(eg Binary Search)

A

O( log N)

Logarithmic

42
Q

Complexity of Code Section Types:

Recursive Function

w/ multiple calls

A

O( branchesdepth)

Ex: O( 2N )

43
Q

Multipart Algorithms:

What is the complexity of this code:

for( a : array A) {}

for( b : array B) {}

A

In this case, Add the complexity:

O( A + B)

44
Q

Multipart Algorithms:

What is the complexity of this code:

for( a : array A) {

for( b : array B) {}

}

A

Nested Loop: Multiply

O( A * B)

45
Q

Special Case:

Amortized Time

A

It allows us to describe that a worst case happens every once in a while. But once it happens, it won’t happen again for so long that the cost is “amortized”.

A costly operation that happens infrequently can effectively be ignored, especially if it becomes less frequent over time.

eg

A Dynamic ArrayList doubles it’s capacity when capacity is reached. Normal insertions are O(1), but insertion with growth is O(N).

The Amortized Time is O(1)

46
Q

O( log N )

Runtimes:

Explanation

A

Log N comes from repeatedly halving an array or set.

One example is Binary Search.

The total runtime is then a matter of how many steps (dividing N by 2 each time) we can take until N becomes 1.

N/2 + N/4 +…+N/2k

At one element: 2k = N

and k is our number of steps

So, k = log2N

In Big O, the base of log doesn’t matter as the growth rate is similar, so it’s just O(log N)

47
Q

Recursive Runtimes:

Explanation

A

The runtime is the sum of each level of recursion.

For a function:

f(n) { return f(n-1) + f(n-1) }

Each level branches twice:

2n + 2n-1 + … + 21 + 20 = 2n+1 + 1

Applying reductions: O(2n+1 + 1) = O(2n)

48
Q

Recursive Runtimes:

Call Tree

A

A technique for modeling recursive functions,

each node on the tree represents a call or stack frame.

Used with a table to count the number of calls per node.

Runtime is the sum of each level of recursion