2.3.1 Algorithms Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Algorithms

A

A series of instructions that are used to solve a problem

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

Properties of a good algorithm

A
  1. Produce the correct output for valid inputs
  2. Should account for invalid inputs
  3. Must always terminate at some point
  4. Executes efficiently in as few steps as possible
  5. Should be designed such that other people can easily understand and modify it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Big O notation

A

The time complexity of an algorithm, how changing the size of the units affects the runtime
Remove coefficients and write the highest power of n only

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

Big O process

A
  1. Calculate how many times each line runs
  2. Simplify
  3. Remove coefficients and put O(n^highest power)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sequential big O

A

O(1)

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

Single loop big O

A

O(n)

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

Loops inside each other big O

A

O(n^2)

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

Recursive functions big O

A

O(numberofrecursions^n)

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

Divide and conquer big O

A

O(log n)

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

Permutations

A

The permutation of n items is the number of ways n items should be arranged

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

Permutation where values can be repeated big O

A

O(a^n)

Where a is the amount of possible values for each

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

Permutation where values can’t be repeated big O

A

O(n!)

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

Linear search

A

Iterates through each item in turn until you find the item or have checked every item

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

Linear search pseudocode

A

Iterating through an array until the value is found, at which point the index is returned or if the loop is completed the value has not been found

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

Linear search big O

A

O(n)

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

Binary search big O

A

O(log n)

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

Binary search process

A

Order the items and examine the middle item of the list. if that is the item it is found, else repeat using the items bigger or smaller than the value depending if it is bigger or smaller than the middle value

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

Binary search pseudocode

A

The lowerbound starts at 0 and the upperbound 1 less than the list length
The midpoint is (LB + UB) DIV 2
If the midpoint is the value return the index
If the midpoint is less than the value LB is midpoint + 1
If the midpoint is more than the value UB is midpoint - 1
If lowerbound > upperbound it is not found

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

Bubble sort big O

A

O(n^2)

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

Insertion sort big O

A

O(n^2)

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

Bubble sort pseudocode

A
for (i = 0 to length - 2)
for (j = 0 to length - i - 1)
if list[i] > list [i + 1]
temp = list[i]
list[i] = list[i + 1]
list [i + 1] = temp
end if
next j
next i
end procedure
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Insertion sort pseudocode

A
for (j = 1 to length - 1)
nextItem = list[j]
i = j - 1
while i>=0 and list[i] > nextItem
list [i+1] = list[i]
i--
end while
list{i+1] = nextItem
next j
end procedure
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Merge Sort Process

A
  1. Divide the unsorted list into n sublists, each containing one element, halving the length each time
  2. Order consecutive pairs of sublists and combine
  3. Repeat to have 4 items in each ordered list
  4. Repeat until there is one ordered list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Merge Sort Big O

A

n log n

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

Quick Sort Process

A
  1. The first value becomes the pivot, the left pointer starts at the second-left and the right pointer the rightmost
  2. Move the left pointer right until it lands on a value greater than the pivot and the right left until it lands on a value one less
  3. When both are on said values, swap the values and repeat
  4. When the left pointer is to the right of the right pointer, the right pointer swaps with the pivot if it is smaller and the pivot is in position
  5. Repeat with each list until it is in order
26
Q

Quick Sort Big O

A

n log n

worst case: O(n^2)

27
Q

Stable

A

Keeps identical elements in the same order as in the input

28
Q

Bubble sort advantages and disadvantages

A

+ easy implementation, stable, in place

- large time complexity, outperformed by other sorting algorithms

29
Q

Insertion sort advantages and disadvantages

A

+ easy implementation, in place

- unstable, large time complexity

30
Q

Merge sort advantages and disadvantages

A

+ low time complexity, stable

- hard to implement, slower than quick

31
Q

Quick sort advantages and disadvantages

A

+ low time complexity, usually the fastest sort

- hard to implement, unstable

32
Q

Dijkstra’s Algorithm Process

A
  1. Start with the distance to the start node 0 and infinity to all the others
  2. Mark the latest node with the next number in the visited column and for all adjacent nodes where the current weight + distance between is less than the weight, write the sum in the weight and the vertex in the last visited
  3. Repeat until you have checked the destination node
  4. Trace back through last visited for the route
33
Q

Dijkstra’s Algorithm

A

Vertex | Order visited | Shortest distance | Previous vertex

34
Q

Computable problem

A

A problem for which an algorithm can solve every instance of it in a finite number of steps

35
Q

Insoluble problem

A

A problem that is theoretically solvable by a computer but takes an impractical length of time

36
Q

TSP time complexity

A

O(n!)

37
Q

Tractable problem

A

A problem with polynomial or better time complexity

38
Q

Limitations of computation

A

Algorithmic complexity and hardware

39
Q

Heuristic method

A

A method to find a reasonable solution to an intractable problem within a reasonable time frame, even if it isn’t the optimal solution

40
Q

A* table

A

Vertex | Order visited | Shortest distance (g) | Heuristic (h) | f=g+h | Previous vertex

41
Q

A* method

A

Similar to Dijkstra, mark the lowest f as visited (shortest path if equal) and add the path to the distance for new paths (only add the heuristic of the destination)

42
Q

Showing how an insertion sort is used

A

Show the first list with each item in turn used as a pivot and correctly positioned in the growing second list which gains the pivot as a new item each time

43
Q

In place

A

All sorting happens in the memory space taken up by the actual data

44
Q

Depth first data structures used in algorithm

A

Uses a list to store the nodes that have been visited

Uses a stack to show the path followed, push on when you go to a child, pop off when you backtrack back to the node

45
Q

Breadth first data structures used in algorithm

A

Uses a list to store the nodes that have been visited

Uses a queue that the children of the current node are enqueued to when the node is visited, removes and visits in FIFO

46
Q

Queue isFull() pseudocode

A
function isFull()
if size == maxSize then
return True
else:
return False
end if
end function
47
Q

Queue isEmpty() pseudocode

A
function isEmpty()
if size == 0 then
return True:
else: 
return False
end if
end function
48
Q

enQueue() pseudocode

A
procedure enQueue(item)
if size < maxSize then
rear = (rear + 1) MOD maxSize
Q[rear] = item
size ++
else:
print (queue is full)
end if
end procedure
49
Q

deQueue() pseudocode

A
function deQueue()
if size  == 0 then
item = null
print (queue is empty)
else:
item = Q[front]
front = (front + 1) MOD maxSize
size --
end if
return item
end function
50
Q

Stack isEmpty() pseudocode

A
function isEmpty()
if top == -1 then
return True
else:
return False
end if
end function
51
Q

Stack isFull() pseudocode

A
function isFull()
if (top + 1) == maxSize then
return True
else:
return False
end if
end function
52
Q

Stack push() pseudocode

A
procedure push(newItem)
if (top + 1) == maxSize then
print (stack is full)
else:
top ++
stack[top] = newItem
end if
end procedure
53
Q

Stack pop() pseudocode

A
function pop()
if top == -1 then
print (stack is empty)
item = null
else:
item = stack[top]
top --
end if
return item
end function
54
Q

In order traversal

A

Follow the left subtree then visit the root then follow the right

55
Q

Pre-order traversal

A

Visit the root then follow the left subtree then the right

56
Q

Post-order traversal

A

Traverse the left subtree then the right then visit the root node

57
Q

Deleting a leaf node

A

Just remove the node

58
Q

Deleting a node with one child

A

The child replaces the deleted parent

59
Q

Deleting a node with two children

A

Repeatedly add 1 to the value of the node until you find the value of another node, which will replace the deleted node

60
Q

Breadth-first traversal

A

Traverses the root then the children of the root left to right then their children left to right

61
Q

Depth-first (post-order) traversal

A

Starts at the bottom left and at each leaf it will move up to the latest decision point and go right where it hasn’t been done before