unit 12 Flashcards

(69 cards)

1
Q

what are the features of a good algorithm?

A
  • has clear stated steps that produce correct output for valid input
  • should allow for invalid inputs
  • must always terminate at some point
  • should execute efficiently in as few steps as possible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what determines if an algorithm is efficient?

A

how many calculations are being executed
number of calculations relative to how many inputs there are
not speed
not amount of lines

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

what is a function?

A

a set of rules that connects the input to the output

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

what is the form of a linear function?

A

f(n) = an + b

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

what is the order of magnitude for a linear function?

A

O(n)

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

what is the form of a quadratic function?

A

f(n) = an^2 +bn + c

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

what is the order of magnitude of a quadratic function?

A

O(n^2)

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

what term is compared in algorithms?

A

the dominant term

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

what is the form of a logarithmic function?

A

f(n) = alog2n

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

what is the order of magnitude of a logarithmic function?

A

O(log n)

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

what is the behaviour of a quadratic function?

A

as n becomes large the n^2 term increases much faster than other terms

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

what is the behaviour of a logarithmic function?

A

f(n) increases very slowly in relation to n

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

what is big o notation?

A

the measure of the time or space complexity of an algorithm

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

what is the largest time complexity?

A

O(n!)

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

what are the different types of sort?

A

merge, bubble, insertion, quick

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

how does a bubble sort work?

A

passes are made through the array with each item being compared with the adjacent item and swapped if necessary

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

what is the time complexity for a bubble sort?

A

O(n^2)

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

what is an insertion sort?

A

takes each element and inserts into correct position until all are sorted

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

what is the time complexity for an insertion sort?

A

O(n^2) on average

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

how does merge sort work?

A

-divide unsorted list into n sublists each containing one element
-repeatedly merge sublists to produce new sublists until only one sublist left

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

what is the average time complexity for merge sort?

A

O(n log n)

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

how does quick sort?

A

-chooses a pivot point which divides list into 2 sub lists
-has left and right pointers
-move left pointer until value is larger than pointer
-move right pointer until value is smaller than pointer
-swap pointer values

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

what is the time complexity of a quick sort?

A

O(n log n)

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

why is the time complexity of quick sort O(n log n)?

A

length n, pivot in middle of list so n divisions
each of n items needs to be checked against pivot values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
what is a breadth first traversal?
explore all neighbours of the current node then the neighbours of each of those nodes and so on
26
what is a depth first traversal?
traverse as far as down one path as possible and backtrack
27
what data structure does a breadth first use?
queue
28
what data structure does a depth first traversal use?
stack
29
what are some applications of a depth first traversal?
job scheduling where some jobs are prioritised finding a path between two vertices solving maze puzzles
30
what are some applications of a breadth first traversal?
finding shortest path between two points a web crawler uses bdf to analyse sites
31
what is the time complexity of a traversal?
sparse graph = O(n) graph with max edges = O(n^2)
32
what are the optimisation algorithms?
shortest path A* and Dijkstra
33
what is Dijkstra's algorithm?
designed to find the shortest path between a start node and every node in a weighted graph
34
what could the weights represent in Dijkstra's algorithm?
represent distances, time, cost,
35
how does Dijkstra's algorithm work?
uses a priority queue to keep record of which node to visit next starts by setting temporary distance of 0 to start node and infinity to every other node
36
what is a computable problem?
a problem is defined as computable if there is an algorithm that can solve every instance of it in a finite number of steps
37
what is an intractable problem?
problems for which there exist no efficient algorithms to solve them have no polynomial time solution
38
what does insoluble mean?
limits of computation for example cannot be solved due to time or speed
39
what are the limits of computation?
algorithmic complexity hardware
40
what is the brute force method?
all possible routes tested
41
what is a tractable problem?
a problem with polynomial time solution or better
42
how do you overcome intractable problems?
use heuristics
43
what are heuristics?
using an estimate or rule of thumb to find a solution
44
what are the 2 cost functions in heuristics?
g(x) - real cost from source to a given node h(x) - the approximate cost from node(x) to goal node
45
what is the a* algorithm function?
f(x) = g(x) + h(x)
46
what is the rule of the functions in the a* algorithm?
h(x) should never be greater than g(x) h(x)
47
what are the differences between A* and Dijkstra's?
A* focuses only on reaching end node, DJ finds shortest path to end node A* uses heuristics
48
what are the similarities of A* and Dj?
both optimisation algorithms
49
what is the pseudocode for bubble sort?
array = [] i = 0 while i < len.array - 1 for j = 0 to len.array - i - 2 if array[j] > array[j+1] temp = arrat[j] array[j] = array[j+1] next j i += 1
50
what is the pseudocode for insertion sort?
procedure insert(list) n = len(list_ for i = 1 to n -1 item = list[i] position = i while position > 0 and list[position -1] > item list[position] = list[position - 1] list[position- = item
51
what is the algorithm for checking if a stack is empty?
is top pointer < 0? isEmpty() if top < o return True else return false
52
what is the algorithm for checking if a stack is full?
is top pointer = len stack?
53
what is the algorithm for peeking in a stack?
peek() if isEmpty() return error else return stack[top]
54
what is the algorithm for pushing an element onto a stack?
push(item) top+=1 stack[top] = item
55
what is the algorithm for popping an item off a stack?
pop () if isEmpty(): return error else index = stack[top] stack[top] = " " top -= 1 return index
56
what are the different operations for a queue?
size() isEmpty() peek() enqueue() dequeue()
57
what is the algorithm for checking if a queue is empty?
isEmpty() if front == back return True else return False
58
what is the algorithm for peeking a queue?
peek() return queue[front]
59
what is the algorithm for enqueuing an item?
enqueue(item) queue[back] = item back += 1
60
what is the algorithm for dequeueing an item?
dequeue() if isEmpty() return Error else index = queue[front] queue[front] = " " front += 1 return index
61
how to enqueue for a circular queue algorithm?
array Queue = [] front = -1 rear = -1 is full () FUNCTION enqueue(queue, front, rear, data) IF isFull() return null ELSE rear = rear + 1 MOD max queue[rear] = "" IF front = -1 THEN front = 0
62
how to dequeue in a circular queue algorithm?
FUNCTION dequeue(queue, front, rear) IF is empty() return null ELSE dequeued = queue[front] IF front == rear front = -1, rear = -1 ELSE front = front +1 MOD maxsize
63
what is the pseudocode for traversing a linked list?
current = node1 while current is not NONE: print(current.data) current = current.next
64
how to add an item to a linked list pseudocode?
new_node = Node() if node1 is None node1 = new_node else current = node1 while current.next is not None current = current.next current.next = new_node
65
how do you remove item from linked list in pseudocode?
while current is not None if current.data == "" if previous is None node1 = current.next else previous.next = current.next previous = current current - current.next
66
what is the pseudocode for a pre order traversal?
PROCEDURE pre_order_traversal(node) PRINT(node.data) IF node.left != Null THEN pre_order_traversal(node.left) ENDIF IF node.right != Null THEN pre_order_traversal(node.right) ENDIF ENDPROCEDURE
67
what is the pseudocode for an in order traversal?
PROCEDURE in_order_traversal(node) IF node.left != Null THEN in_order_traversal(node.left) ENDIF PRINT(node.data) IF node.right != Null THEN in_order_traversal(node.right) ENDIF ENDPROCEDURE
68
what is the pseudocode for a post order traversal?
PROCEDURE post_order_traversal(node) IF node.left != Null THEN post_order_traversal(node.left) ENDIF IF node.right != Null THEN post_order_traversal(node.right) ENDIF PRINT(node.data) ENDPROCEDURE
69