4.3 Algorithms Flashcards
1
Q
Graph Traversal (definition)
A
visiting all the nodes on the graph
2
Q
Example of:
- Breadth First Search
- Depth First Search
A
- Breadth First: shortest path for an unweighted graph
- Depth First: navigating a maze
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)
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)
5
Q
Pre Order: acronym, dot placement
A
- NLR (node, left , right)
- dot to the left
6
Q
In Order: acronym, dot placement
A
- LNR (left, node, right)
- dot underneath
7
Q
Post Order: acronym, dot placement
A
- LRN (left, right, node)
- dot to the right
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
9
Q
Convert Infix to RPN: 3 + (4 * 2) - 1
A
3, 4, 2, *, +, 1, -
10
Q
Advantages of RPN over Infix Notation (2)
A
- simpler for a machine to evaluate
- RPN doesn’t require brackets, unlike infix notation
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
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
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
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)
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)