Algorithm Complexity Flashcards

1
Q

How do we measure the run-time of an algorithm ?

A

Empirical analysis (benchmarking on representative set of inputs)

Analyse the (time) complexity

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

Empirical analysis

A

Implement the algorithm (a program) and time it

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

How can we time a program?

A

Manual - using a stopwatch

Automatic - using some timer function

Run the program for various input sizes and measure run time

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

Time Complexity

A

the number of operations that an algorithm requires to execute in terms of the size of the input or problem.

usually focusing on worst-case analysis

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

O(1)

A

Constant time - Input size does not matter
Usually better than linear

On a graph this will show up as a straight line

Extremely fast

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

O(log n)

A

Logarithmic

Binary search

As the data size increases this algorithm becomes more and more efficient compared to the early stages with the smaller data set.

Get the middle element of the array when looking for a value see if it is smaller of bigger and look to the relevant half accordingly

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

O(n log2 n)

A

Log Linear

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

O(n^2)

A

Quadratic time complexity
the runtime is proportional to the square of the input size

Common in nested loops

As the amount of data increases it is going to take increasingly more and more time to complete anything that has a runtime

Extremely slow with large data (slower than linear) but can be faster in the case of a smaller data set

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

O(n3) =

A

Cubic time complexity

The runtime is proportional to the cube of the input size

Common in algorithms with three nested loops

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

O(^2n) =

A

Exponential time complexity; the runtime doubles with each additional input sizes

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

Look up a value v in a sorted array

A

Search from the middle, if the number is smaller we focus on the right hand side of the array

This is a way to get rid of half of the array

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

Big O notation

A

Simplified analysis of an algorithm’s efficiency

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

Types of measurement for an algorithm’s efficiency

A

Worst-case
Best-case
Average-case

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

0(1)

A

Constant

Input size does not affect the time of completion

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

0(n)

A

Linear time

As the amount of data increases, the amount of steps increases proportionally

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

What is the purpose of analysing Big O Notation?

A

To predict the scalability and efficiency of an algorithm as the input grows

17
Q

What is the difference between best case and worse case complexity

A

Best-case = the minimum time complexity for any input

Worst-case = the maximum

18
Q

Logarithmic Time Complexity
Why is it considered more efficient?

A

Runtime increases very slowly even for large input sizes

Common in divide-and-conquer algorithms

19
Q

What is the Big O complexity for finding the maximum value in an unsorted array?

A

O(n) linear complexity

20
Q

Exponential time

A

O(2n)

Runtime doubles with each additional input element

Extremely rapid

Impractical for large inputs, typically avoided

Grows much faster than O(n3) and O(n2)

T(n)=2^n or K^n where K>1

21
Q

Cubic Time

A

O(n3)

Slower than exponential but faster than quadratics

Can handle moderately large inputs but is inefficient for very large inputs

T(n)=n^3

22
Q

Quadratic Time

A

O(n2) grows at a manageable rate and is often feasible for medium to large data sets

T(n)=n^2

23
Q

Linear Time

A

Runtime grows directly proportional to n

Slow growth

T(n)=n

24
Q

Constant time

A

Runtime remains constant regardless of input size

No growth, constant for all data

T(n)=c (constant value)

25
Q

K>1

A

K is the base in exponential growth K^n

K>1 ensures the true exponential growth occurs

K=1 would lead to a constant function

K<1 would result in an exponential decay

26
Q
A