Algorithms Flashcards

1
Q

What is an algorithm?

A

An algorithm is a sequence of steps for performing a task in a finite amount of time.

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

What is a data structure?

A

For computational algorithms, we use data structures to store, access, and manipulate data.

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

Questions to decide which search to use

A
  • How is the list stored?
  • Is it sorted or not?
  • It is it not sorted, should we sort it first?
  • Are we updating the list?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Types of algorithm analysis

A
  • Running time
  • Space usage
  • Optimality of the output solution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Experimental Analysis

A

Interested in dependency of the running time on size of the input.
Size could mean amount of numbers of bits or amount of vertices and edges in a graph.
Therefore we perform experiments to empirically observe running time.

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

Drawbacks of experimental analysis

A
  • Limited test of inputs
  • Requires all tests to be performed hardware and software
  • Requires implementation and execution of algorithm
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Theoretical Analysis

A

More theory based approach of finding running time of algorithms.

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

Theoretical Analysis Benefits

A
  • Can take all possible inputs into account.
  • Involves studying high-level descriptions of algorithms
  • Can compare efficiency of two algorithms
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Theoretical Analysis Functions

A

When doing theoretical analysis, we typically assign a function f(n) to the algorithm, where f(n) characterises the running time for an input size n.

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

Theoretical Analysis Requirements

A
  • a language to describe algorithms
  • computational model in which algorithms are executed
  • metric for running time
  • a way of characterising running time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Pseudo-code

A

A high level description language for algorithms.
Mixture of a natural language and a high-level programming language, describing a generic implementation of data structures or algorithms.

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

Correctness

A

An algorithm is correct with respect to a specification if it behaves as specified.

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

Loop Invariant

A

A way to show correctness of an algorithm.
For example, if we had a for loop, we could state what the expected value is at the end of the for loop, aka specifying what the for loop is meant to do.

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

Loop Invariant Properties

A
  • intialisation
  • maintenance
  • terminantion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Initalisation (Loop Invariant)

A

Remains true prior to the first iteration of the loop.

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

Maintenance

A

If it remains true before an iteration of the loop, then it remains true before the next iteration.

17
Q

Termination

A

When the loop is terminated, the invariant gives if a property which helps determine the correctness of the algorithm.

18
Q

Computational Model

A

CPU connected to a bank of memory cells, with each cell being able to store a number, address or character.
We use a set of high-level primitive operations, like assignment, method invocation, arithmetic operation, etc.

19
Q

Assumption of primitive operations

A

Primitive operations require constant time to perform.
So when analysing the running time of an algorithm, we count the number of operations executed during the course of the algorithm.

20
Q

Average-case Complexity

A

Running time as an average taken over all inputs of the same size.

21
Q

Worst-case Complexity

A

Running time as the maximum over all inputs of the same size.

22
Q

Worst Case Explanation

A

f(n) - runtime
g(n) - any arbitrary number

f(n) in O(g(n)) if, for constants c and place n_0:
f(n) <= c x g(n) for all n >= n_0

Essentially, for any point after n_0, the runtime of f(n) is always smaller than g(n).

23
Q

Common Functions in Big O Notation

A

O(log n)
O(n)
O(n^2)
O(n^k)
O(a^n)

24
Q

O(log n)

A

Logarithmic

25
Q

O(n)

A

Linear

26
Q

O(n^2)

A

Quadratic

27
Q

O(n^k)

A

Polynomial

28
Q

O(a^n)

A

Exponential

29
Q

Ω(g(n

A

f(n) - maps to runtime
g(n) - maps to any arbitrary number
f(n) in Ω(g(n)) if, for all constants c and place n_0:
f(n) >= c x g(n) for all n >= n_0

30
Q

Θ(g(n))

A

If f(n) in O(g(n)) and Ω(g(n)) then f(n) is also in Θ(g(n)).

31
Q

Asymptotic Notation

A

Allows characterisation of the main factor affecting running time.

32
Q

NP Completeness problem

A

Class P - problems that are efficiently solvable.
Class NP - problems that are efficiently verifiable.

33
Q

Proof by Mathematical Induction

A

1) Show hypothesis true for smallest value n
2) Assume hypothesis to be true for n = k and show it is true for n = k + 1

34
Q

Proof by Contradiction

A

Essentially, prove two different things which fall under the same law and how they contradict each other.

35
Q

Proof by Contrapositive

A

“If not-B then not-A” is the contrapositive of “if A then B”.
Essentially, proving the complete opposite statement.

36
Q

(Dis)Proof by Counterexample

A

Proving the statement is false by giving an opposite statement which is true.
“All dogs are hairy” can be disproved by finding one non-hairy dog.