Algorithm concepts v2 Flashcards

simple editor

1
Q

Recurrence

A

aT(N/b) + D(N) + C(N) , N ≥ c

T(N/b) is the total time to solve a subproblem

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

Lomuto partitioning

A

compare all elements to last element in array

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

Hoare partitioning

A

partition grows inward from the end

handles duplicates better

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

Three-way partitioning

A

includes a region for values equal to pivot

handles duplicates better

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

Median of 3 partition

A

choose pivot as median of three random elements

better balance between subproblems

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

Amortisation

A

amortized analysis considers a sequence of operations
cost of expensive operations is ‘amortized’ across the sequence…
can never “be in debt”

pick a value to ‘pay’ per operation - show that this value covers all costs (never in debt)

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

Amortized time

A

average time taken per operation to do many operations

worst-case run time per operation rather than per algorithm can be too pessimistic

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

Amortized analysis

A

considers both the costly and less costly operations together over the whole series of operations of the algorithm

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

Amortized analysis: aggregate analysis

A

determine the upper bound T(n) on the total cost of a sequence of n operations, then calculates amortized cost to be T(n) / n

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

Amortized analysis: accounting method

A

a form of aggregate analysis that assigns to each operation an amortized cost, which may differ from actual cost

early operations have an amortized cost higher than their actual cost, which accumulates a saved “credit” that pays for later operations having an amortized cost lower than their actual cost

because the credit begins at 0, the actual cost of a sequence of operations equals the amortized cost minus the accumulated credit… the amortized cost is an upper bound on the actual cost

usually many short-running operations accumulate such a credit in small increments, while rare long-running operations decrease it drastically

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

Arithmetic series

A

1 + 2 + … + (N - 1) + N

create a second series that is a reverse of the first and add together term by term

   1  +    2     + ... + (N - 1) + N
\+ N + (N - 1) + ... +     2    +  1
----------------------------------------
=(N + 1) + (N + 1) + ... + (N + 1) + (N + 1)
= N(N+1)
= 2s
2s = N(N +1)
s = N(N + 1)/2

a + (a + d) + (a + 2d) + … + (a + (k - 1)d)

Sk = a1 + ... + ak
Sk = k(a1 + ak)/2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Geometric series

A

𝑁𝑐+(𝑁/2)𝑐+(𝑁/4)𝑐+⋯+2𝑐+𝑐

create second series by multiplying by r (common ratio)
r = 1/2

Nc + (N/2)c + … + 2c + c
(N/2)c + … + 2c + c + c/2

Nc + (N/2)c + … + 2c + c
(N/2)c + … + 2c + c + c/2

all but two of the terms are identical
take the difference

Nc + (N/2)c + … + 2c + c
- (N/2)c + … + 2c + c + c/2
———————————————-
Nc - c/2

Nc - c/2 = s - s/2
Nc - c/2 = s/2
2Nc - c = s

𝑆𝑘 =𝑎+𝑎𝑟+𝑎𝑟^2 +⋯+𝑎𝑟^(𝑘−1)
𝑆𝑘 − 𝑟𝑆𝑘 = 𝑎 − 𝑎𝑟^𝑘
𝑆𝑘=𝑎(1−𝑟^𝑘) / (1−𝑟)

final form
𝑆𝑘=𝑎(𝑟𝑘−1) / (𝑟−1)
𝑆𝑘 = 𝑟𝑎𝑘 − 𝑎1 / (𝑟−1)

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

Arithmetic Series general form

A

Sk = a + (a + d) + (a + 2d) + … + (a + (k - 1)d)
so
Sk = a1 + … + ak
Sk = k(a1 + ak)/2

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

Geometric series general form

A

𝑆𝑘 =𝑎+𝑎𝑟+𝑎𝑟^2 +⋯+𝑎𝑟^(𝑘−1)
so
𝑆𝑘=𝑎(𝑟𝑘−1) / (𝑟−1)
𝑆𝑘 = 𝑟𝑎𝑘 − 𝑎1 / (𝑟−1)

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

RBT 5 properties

A
  1. all nodes (including nil) are either red or black
  2. the root node is black
  3. every leaf (all nil) is black
  4. both children of a red node are black
  5. all paths from a node to a descendant leaf contain the same number of black nodes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Simple Uniform Hashing Assumption

A

with reference to chaining

Given a hash table T with m buckets, using hash function h, the simple uniform hashing assumption (SUHA) states that each different key k is equally likely to hash into any of the buckets. So the probability that h(k) = I, for all 1 ≤ I ≤ m, is 1/m

most probable / expected outcome: same number in each bucket

17
Q

Memoisation

A

making a note for later

for dynamic programming

18
Q

Dynamic programming

A

makes a space-time tradeoff

conditions to apply it:

  • optimal substructure
  • overlapping subproblems

optimal substructure:
problem can be decomposed into subproblems
an optimal solution uses optimal solutions to the subproblems

overlapping subproblems
same problems are generated over and over
subproblems must still be independent
set of all subproblems is the subproblem space
subproblem space determines time complexity

19
Q

Top Down

A

set out to solve the biggest problem

20
Q

Bottom Up

A

complete the table in order

21
Q

Optimal substructure

A

problem can be decomposed into subproblems

an optimal solution uses optimal solutions to the subproblems

22
Q

Overlapping subproblems

A

same problems are generated over and over
subproblems must still be independent
set of all subproblems is the subproblem space
subproblem space determines time complexity

23
Q

Graph

A

A graph G is a pair (V, E) where V is a finite set (of objects) and E is a binary relation on V. Elements of V are called vertices and elements of E are called edges

24
Q

Breadth-first Search

A

search proceeds gradually down multiple paths at once
all vertices at depth I visited before any at depth I + 1
BFS maintains a set of vertices being searched

search from given s

use of FIFO queue characteristic of BFS
best-first search would use priority queue

25
Q

BFS shortest paths

A

shortest path to all vertices
record extra info during search when a vertex is visited
unreachable vertices have diet[v] = - 1

26
Q

Depth-first search

A

when vertex is visited, immediately use it to look for more vertices. only visit others found with v when this search is over

can use call stack instead of explicit queue
restart until whole graph searched

27
Q

Kruskal’s algorithm

A

set of edges is iterated over in weight order

if the next edge connects two distinct components, it is added