Section 12: Algorithms Flashcards

1
Q

Define what an algorithm with constant complexity is.

A

An algorithm with constant complexity is one that always executes in the same time regardless of the size of the data set.

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

What is the big O notation for constant complexity?

A

Constant complexity:

O(1)

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

Define what an algorithm with logarithmic complexity is.

A

An algorithm with logarithmic complexity is an algorithm that halves the data set in each pass (opposite to exponential).

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

What is the big O notation for logarithmic complexity?

A

Logarithmic complexity:

O(log n)

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

Define what an algorithm with linear complexity is.

A

An algorithm with linear complexity is an algorithm thats performance is proportional to the size of the data set and declines as the data set grows.

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

What is the big O notation for linear complexity?

A

Linear complexity:

O(n)

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

Define what an algorithm with polynomial complexity is.

A

An algorithm with polynomial complexity is an algorithm thats performance is proportional to the square of the size of the data set. It becomes significantly more inefficient as the size of the data set grows.

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

What is the big O notation for polynomial complexity?

A

Polynomial complexity:

O(n^k) where k is the number of nested loops.

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

Define what an algorithm with exponential complexity is.

A

An algorithm with exponential complexity is an algorithm thats time complexity doubles with each addition to the data set (opposite to logarithmic).

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

What is the big O notation for exponential complexity?

A

Exponential complexity:

O(2^n)

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

What is the worst case time complexity for a linear search?

A

Linear search big O:

O(n)

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

What is the worst case time complexity for a binary search?

A

Binary search big O:

O(log n)

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

What is the worst case time complexity for traversing a binary tree?

A

Binary tree traversal big O:

O(n)

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

What is the worst case time complexity for a bubble sort?

A

Bubble sort big O:

O(n^2)

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

What is the worst case time complexity for an insertion sort?

A

Insertion sort big O:

O(n^2)

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

What is the time complexity for a recursive function calling itself twice?

A

Recursive function big O:

O(2^n)

17
Q

How does a merge sort work?

A

A merge sort uses a divide and conquer approach, by splitting a list into sublists until each sublist contains only one element. The smaller sublists are then repeatedly merged together in the correct order, until finally there is a completely sorted list of the same size as the original one.

18
Q

What is the time complexity for a merge sort?

A

Merge sort big O:

O(n log n)

19
Q

What is the space complexity for a merge sort?

A

A merge sort requires 2n of memory, where n is the size of the list, since the two halves require additional space.

20
Q

How does a quick sort work?

A

A quick sort works using a divide and conquer method.
A pivot value is chosen which is then compared with the rest of the items in the list using a left and right pointer.
The list is then divided into two partitions once the two pointers cross - the first containing all values less than the pivot value and the second containing all values greater than the pivot value.
This process is then repeated recursively until the list is sorted.

21
Q

Why can the quick sort be advantageous over the merge sort?

A

The quick sort is as fast, i.e. O(n logn), but does not require the additional memory that a merge sort does.

22
Q

What is a potential disadvantage of the quick sort?

A

Quick sorts can be very slow if the split point is not in a useful place. If the split point is very close to the start or the end of the list, there will be a lot more recursion and the time complexity could be O(n^2)

23
Q

What is Dijkstra’s shortest path algorithm?

A

Dijkstra’s algorithm is an algorithm designed to find the shortest possible path between one particular start node and all other nodes in a weighted graph.

24
Q

How does Dijkstra’s algorithm work?

A

Firstly, it assigns a temporary distance value to each of the nodes, with the starting node being 0 and the rest being infinity.
Then, all of the nodes are added to a priority queue in order of distance with the starting node being first and the rest in a random order.
After this, each of the nodes are iteratively analysed and their current distance value is compared to the distance between the 2 nodes to check if it is smaller.
This is done until all the nodes have been checked (in a brute force type manner) and the shortest path has been found.

25
Q

How does the A* algorithm differ from Dijkstra’s algorithm?

A

The crucial difference between the A* and Dijkstra’s path-finding algorithms is that the A* algorithm has an extra cost function - that of the approximate cost of moving from the source node to the goal node. Dijkstra’s algorithm, alternatively, only takes into account the cost function from the source node to any given node.