Algorithms Flashcards

1
Q

Master’s method

A

Method of analysis that solves recurrence relations of the form

T(n) = aT(n/b) + f(n)

where
a and b are constants with a >= 1 and b >1

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

Amortized analysis

A

Worst case analysis of a sequence of operations rather than an entire algorithm

For example, worst case of a hash table is O(n) but we consider the average sequence of operations, which does not include reallocating memory for the table. This leads us to O(1) in the average case.

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

Different methods of amortized analysis

A

Aggregate method - typical analysis on the aggregation of all operations that should be analyzed

Accounting method - Paying for each operation on the data structure used to help perform the operations

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

Decision trees

A

Models decisions that the algorithm will take

Example:
Insertion sort
Nodes contain elements that will be compared
Leaves contain an answer (ordered set of elements)
Edges contain decisions (<= or >)

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

Merge sort

A

Time complexity
Worst - O(n lg(n))
Average - O(n lg(n))
Best - O(n lg(n))

Space complexity
O(n)

Divide the list into the smallest unit possible, compare adjacent lists until you have original list but sorted.

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

Heap sort

A

Time complexity
Worst - O(n lg(n))
Average - O(n lg(n))
Best - O(n lg(n))

Space complexity
O(1)

Iteratively pull out an object based on some attribute (for example, minimum/maximum) and add to the sorted part of the data structure. Do this until the unsorted part contains 0 objects, and the sorted part contains n objects, equal to the original number of elements in the heap.

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

Binary sort

A

Time complexity
Worst - O(n)
Average - O(n)
Best - O(n)

Space complexity
O(1)

In-order traversal of a tree. Must be BST.

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

Bucket sort

A

Time complexity
Worst - O(n^2)
Average - O(n + k)
Best - O(n + k)

Space complexity
O(n)

Go through the entire n elements, placing them in buckets. In the best case, the buckets are considered sorted and you only need to go through k buckets to get the sorted outcome. Worst case, all elements to go into the same bucket, so you must again go through n elements to get the sorted collection.

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

Selection sort

A

Time complexity
Worst - O(n^2)
Average - O(n^2)
Best - O(n^2)

Space complexity
O(1)

Comparison-based, iteratively progress through the the unsorted side of the list until it is empty and the sorted side contains all of the original elements in sorted order.

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

Bubble sort

A

Time complexity
Worst - O(n^2)
Average - O(n^2)
Best - O(n)

Space complexity
O(1)

Traverse the collection while floating the current object to its sorted position.

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

Quicksort

A

Time complexity
Worst - O(n^2)
Average - O(n lg(n))
Best - O(n lg(n))

Space complexity
O(lg(n))

Choose a pivot, move it to the end of the collection. Have 2 pointers, move the left one to the right until you find a value greater than the pivot. Move the right one to the left until you find a value less than the pivot. Swap those elements. Repeat until the bounds of those pointers overlap. Repeat the quicksort on the remaining partition.

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

What is Single Source Shortest Path (SSSP)?

A

The shortest path from a selected node to all other nodes.

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

Shortest path for unweighted graph

A

BFS, find the single source shortest path from a starting node to all other nodes

O(|E| + |V|)

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

Shortest path for weighted graph

A

Floyd-Warshall
Best for negative edge weights
Worst case of O(V^3)

Bellman-Ford
Can have negative edge weights
Worst case of O(VE)
If graph is dense it can devolve into O(V^3)

Dijkstra
Can't have negative edge weights
Similar to Prim's 
Greedy
Min priority queue - O(V^2)
Fibonacci heap - O(E + Vlg(V))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Minimal spanning trees

A

Prim’s
Greedy
Given a starting node, select the minimum edge that is not in the tree so far. Repeat until all nodes are in the tree. Cannot add an edge that makes a cycle.

Kruskal’s
Sort the edges of the tree
Add the shortest edge to the MST (that does not make a cycle)
Repeat until all vertices are in the tree

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

Ways to store a graph in memory

A

Adjacency list
List of nodes with each node containing a list of nodes that it is connected to

Adjacency matrix
Matrix of all nodes in the tree. If a node is connected to a given node in its row, place a 1. If not, place a 0.

17
Q

Proof types

A

Direct
No further assumptions, just using established facts
Induction

Indirect
Makes assumptions
Contrapositive - if something, then something ([p implies q] if [~q implies ~p])
Contradiction - p^~q implies ~p

18
Q

Loop invariant

A

Property of a loop that is true before and after each iteration

19
Q

Divide and conquer

A

Divide a large problem into a subset of smaller problems. Solve the smaller problems recursively until the larger problem is solved
Merge sort, binary search

20
Q

Dynamic programming

A

Store the results for small subproblems and look them up instead of recomputing. Usually associated with optimization
LCS - what is it and how long

21
Q

Greedy

A

Choose best approach based on arbitrary attribute (smallest, biggest, shortest, etc.) Continuously make that choice until problem is solved

22
Q

Approximation methods

A
Returns near optimal solutions
Vertex cover can be determined with this
Example:
approximation of 10 with 6 being optimal
10 <= 2*6, yes, that is a 2-approximation
23
Q

N-approximation

A

Is your guess within n times the optimal?

Approximation <= n * optimal

24
Q

DFS

A

Perform exhaustive search in the forward direction, then backtrack. Do this until every node is visited.

25
Q

Binomial Heap

A

Insert
Worst: O(lg(n)) - may have to merge all separate trees together
Amortized: O(1)

Delete
Worst: O(lg(n))
Amortized: O(lg(n))
May need to separate trees in lg(n) time

Search
Worst: O(n) - not finding the minimum
Amortized: O(lg(n)) - finding the minimum

26
Q

Fibonacci Heap

A

Insert
O(1)

Delete
O(lg(n))

Search
Worst: O(n) - not the minimum
Amortized: O(1) - the minimum

27
Q

Binary Search Tree

A

Insert
Worst: O(n)
Amortized: O(lg(n))

Delete
Worst: O(n)
Amortized: O(lg(n))

Search
Worst: O(n)
Amortized: O(lg(n))

Devolves to linked list in worst case

28
Q

NP Complete Hierarchy

A
Circuit SAT
SAT
3-CNF SAT
Clique, Subset
Vertex Cover
Hamiltonian Cycle
Travelling Salesman
29
Q

What is a decision problem?

A

Something that is not simply a yes or no answer. Typically an answer associated with an optimization where the answer is very difficult to find.

30
Q

What is P, NP, and NP complete?

A

P - can be determined in polynomial time
NP - non-deterministic polynomial, can be verified in polynomial time
NP complete - is NP, can be used to solve NP problems