4.3 Algorithms Flashcards

1
Q

Graph Traversal (definition)

A

visiting all the nodes on the graph

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

Example of:
- Breadth First Search
- Depth First Search

A
  • Breadth First: shortest path for an unweighted graph
  • Depth First: navigating a maze
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Breadth First Search Process (5)

A
  • enqueue root and add to visited list
  • are there any nodes adjacent to the node at the front of the queue that haven’t been visited?
    • if so: enqueue node and add to visited list
    • if not: dequeue
  • repeat process until queue is empty (all nodes have been visited)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Depth First Search Process (5)

A
  • push root node onto stack and add to visited list
  • is there a node adjacent to the node at the top of the stack that has not been visited?
    • if so: push node onto stack and add to visited list
    • is not: pop off stack
  • repeat process until stack is empty (all nodes have been visited)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Pre Order: acronym, dot placement

A
  • NLR (node, left , right)
  • dot to the left
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In Order: acronym, dot placement

A
  • LNR (left, node, right)
  • dot underneath
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Post Order: acronym, dot placement

A
  • LRN (left, right, node)
  • dot to the right
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Examples Using Tree Traversal Algorithms

A
  • Post Order: converts infix notation to RPN
  • In Order: outputs contents of a binary search tree in ascending order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Convert Infix to RPN: 3 + (4 * 2) - 1

A

3, 4, 2, *, +, 1, -

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

Advantages of RPN over Infix Notation (2)

A
  • simpler for a machine to evaluate
  • RPN doesn’t require brackets, unlike infix notation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Using a Stack to Evaluate RPN (5)

A
  • if character in expression is a number: push value onto stack
  • if character in expression is an operator:
    • pop last two values off the stack
    • apply the operator to the two values
    • push result onto stack
  • repeat process until end of expression is reached
  • pop top value off stack to give result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Linear Search
- process (2)
- time complexity
- advantage

A
  • iterates through each item in list and compares it to condition element
  • only breaks if condition is met or program reaches end of the list
  • time complexity: O(n)
  • adv: data doesn’t need to be in order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Binary Search
- process (1)
- time complexity
- disadvantage

A
  • when searching, the index of the middle of the set is calculated by adding the leftmost and rightmost indexes and dividing by 2, discarding any remainder
  • time complexity: O(log n)
  • dis: data needs to be in order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Binary Tree Search
- time complexity
- use
- advantage
- disadvantage

A
  • time complexity: O(log n)
  • rapid search of data
  • adv: data does not need to be in order
  • dis: it takes more memory to store the data (3 times the amount of data)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Bubble Sort
- process (1)
- time complexity

A
  • if swapped is still false at the end of a pass through the data then no swaps were made, which means the data is now in order and the sort can finish
  • time complexity: O(n^2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Merge Sort
- process (2)
- time complexity

A
  • follows a ‘divide and conquer’ approach
  • uses a recursive technique
  • average time complexity: O(n log n)
17
Q

Dijkstra’s Algorithm
- process (3)
- examples (2)

A
  • finds the shortest path between a given node and all other nodes (graph must be weighted)
  • stores nodes in a priority queue
    • rule: the node with smallest distance from start is dequeued
  • e.g. use with GPS devices
  • e.g. solving a Rubik’s cube