Algorithms Flashcards

1
Q

Algorithm for removing from a queue?

A

Found in photos

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

Algorithm for adding to a queue?

A

Found in photos

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

Algorithm for removing from a stack?

A

If stack pointer minimum then report stack empty
Else
Set data to stack(stack pointer)
Set stack pointer to stack pointer -1
Endif

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

Algorithm for adding to a stack?

A

If stack pointer maximum then report stack full
Else
Set stack pointer to stack pointer +1
Set stack(stack pointer) to data
Endif

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

What are the two main types of algorithmic operations?

A
  • sequential operations = instructions executed in order
  • control operations = instructions not executed in order. include conditional (if statements) and iterative (loops) operations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two approaches used by sorting algorithms?

A
  • incremental approach (go through line by line)

- divide and conquer approach (breaks down list into smaller lists and reassemble into correct order)

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

What is bubble sort? +/-

A

-uses incremental approach
-Each value is compared to adjacent values and bubbled up the list if it is greater than its adjacent value
-Repeated for each value until in correct order
-very slow and inefficient so only used for small lists
+most simple to understand

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

How are variable values swapped in programming?

A
  • When two variables have their values swapped a third variable is created allowing switch to be possible
  • eg: p=5 q=79 to switch we would set new variable temp =p then p=q and finally q=temp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is insertion sort?

A
  • incremental approach
  • goes through each item one by one and places them in correct order in list
  • needs partially ordered lists
  • faster than bubble but still not quick
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is linear search?

A

-start at beginning of list comparing your value to this value. if not the same move on if it is the same your finished. Repeat until value found
-simple loop with incrementing values used to represent
+list does not have to be ordered
+efficient for long lists
-only returns value once so if it represented multiple times it will still only be outputted once

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

What is binary search?

A
  • uses divide and conquer approach
  • sets an upper and lower bound then finds the midpoint by ub+lb/2
  • We check if the value we want is at this midpoint. If not we check if it is smaller or bigger than midpoint. if smaller we repeat process for values smaller than mid point. vice versa for larger.
  • must be sorted
  • better for more complex data searches
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is complexity?

A
  • not a reflection of how quickly an algorithm runs
  • instead it tells us how well the algorithm scales given increasing sizes of data
  • how quickly the runtime grows relative to input as the input gets arbitrary large
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the basis of big O notation?

A
  • the language we use to articulate how efficient an algorithm is
  • called Big O because we express it as O(x) where x is the worst case complexity of the algorithm
  • only care about how the algorithm scales with increased data not the exact time taken, therefore we simplify the number of steps in the algorithm
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the time complexity in big O notation of 7n^3+n^2+4n+1?

A

O(n^3)

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

What is constant complexity? Draw the graph

A
  • graph on paper
  • O(1)
  • algorithm that will ALWAYS execute in the same time regardless of the size of input data
  • eg; pushing an item onto, or popping an item from a stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is linear complexity? Draw the graph

A

-graph on paper
-O(n)
-if the input size doubles, the time taken for the algorithm to complete also doubles
Eg; average time to find an element using linear search

17
Q

What is polynomial complexity? Draw the graph

A
  • graph on paper
  • O(n^k)
  • time taken as the input size increases can be expressed as n^k where k is a constant
  • constant and linear are also polynomial as n^0=1 and n^1=n
  • examples are quadratic O(n^2) and cubic O(n^3)
  • Karmarkers algorithm for linear programming
18
Q

What exponential complexity? Draw the graph

A
  • graph on paper
  • O(k^n)
  • as the input n gets larger the time taken increases at a rate of k^n
  • do not scale well
  • eg; recursive calculation of Fibonacci numbers
  • Eg; solving matrix chain multiplication via brute force search
19
Q

What is logarithmic complexity? Draw the graph

A
  • graph on paper
  • O(log n)
  • the inverse of exponentiation
  • scale very well
  • The rate at which their execution time increases, decreases as the data set increases
  • Eg; binary search, as the data doubles the number of items to be checked only increases by 1
20
Q

What is Dijkstra’s shortest path algorithm used for? Algorithm?

A
  • finds shortest path between two points
  • marks the start node as a distance of 0 from itself and all others as an infinite distance from this node
  • start at start node. Visit all nodes connected. Mark down total distance. Close current node. Visit all nodes connected to current shortest path. Repeat.
  • algorithm in book
21
Q

What is the A* algorithm?

A
  • also goes through every node like Dijkstra’s but is faster than as it utilises heuristic values
  • heuristic is when existing experience is used to form a judgement
  • heuristic must NEVER be an overestimate
  • speed depends of efficiency of heuristic
  • A* refines search into a direct area whereas DJK’s searches all areas even if destination is not there
22
Q

Look over 15 puzzle

A

In book

23
Q

What is merge sort?

A
  • if we have two ordered lists we can merge them into a single ordered list
  • principle; spit n items into a list of 1 item, While there is more than 1 list recursively pair up the lists and merge each pair into a singe list twice the size
  • if one list is emptied before the other, add the remaining items into the new list
  • example of divide and conquer
23
Q

What is quicksort?

A
  • divide and conquer algorithm
  • involves pivots and sub lists. Pointer can be any value to start with, sub lists are not ordered
  • its powerful and fairly quick
  • due to its recursive nature it can be an issue when being applied to large data sets causing a stack overflow error
24
Q

Why is the ‘in place’ version of Quicksort sometimes used?

A
  • Goes through the same process as the original but on a single list without the need for recursive calls
  • uses pointers
25
Q

Algorithm for getting output of linked list

A

In notes

26
Q

Algorithm for searching a linked list?

A

In notes

27
Q

Algorithm for preorder traversal of trees?

A

In notes

28
Q

Algorithm for in-order traversal of trees?

A

In notes

29
Q

Algorithm for postorder traversal of trees?

A

In notes

30
Q

Algorithm for depth first traversal of graphs? When is this used?

A
  • stacks

- in notes

31
Q

Algorithm for breadth first traversal of graphs? When is this used?

A
  • quests

- in notes