3 - Fundamentals of Algorithms Flashcards
What is infix notation?
What is postfix notation (also known as Reverse Polish Notation)?
Infix notation is where the operator is between the operands eg. 5 + 3
Postfix notation is where the operators follow the operands eg. 5 3 +
How do you evaluate a postfix expression?
eg.4 2 3 5 1 - + * +
Work from left to right, applying each operator to the previous two operands.
eg.4 2 3 5 1 - + * + → 4 2 3 4 + * +
→ 4 2 7 * + → 4 14 + → 18
How do you convert from postfix to infix?
eg.3 4 + 2 *
Work from left to right, moving the operator between the previous two operands and encasing the operands in brackets. Make sure the operands remain in the same order.
eg.3 4 + 2 * → ( 3 + 4 ) 2 *
→ (( 3 + 4 ) * 2 ) → ( 3 + 4 ) * 2
How do you convert from infix to postfix?
eg.3 * ( 7 - 2 )
Add brackets to indicate which order to perform the operations. Move the operator to the end of each bracket, then remove the brackets. Make sure the operands are in the same order.
eg.3 * ( 7 - 2 ) → ( 3 * ( 7 2 ) - )
→ ( 3 ( 7 2 ) - ) * → 3 7 2 - *
Why is Reverse Polish Notation used?
- Because the order of evaluation of an expression is unambiguous, RPN expressions don’t need brackets.
- Expressions in RPN can be evaluated easily using a stack.
- There is no need for backtracking in evaluation as the operators appear in the order required for computation and can be evaluated from left to right.
Where is Reverse Polish Notation used?
RPN is used in interpreters based on a stack for example Postscript and bytecode.
What is meant by a recursive subroutine?
A subroutine that calls itself.
What are the advantages of Reverse Polish Notation over infix notation?
- Simpler for a computer to evaluate.
- Do not need brackets to show correct order of evaluation.
- Operators appear in the order required for computation.
- No need for order of precedence of operators.
- No need to backtrack when evaluating.
What is the complexity of merge sort?
O(nlogn)
What is the complexity of bubble sort?
Explain why this is.
O(n²)
In each pass through the list n items will be examine. There will be at most n passes through the list.
What is the complexity of binary search?
Explain why this is.
O(logn)
Each comparison halves the size of the list that has to be searched through.
What is the complexity of linear search?
Explain why this is.
O(n)
As the size of the list increases the time taken increases at the same rate.
What are the steps for Breadth-First Search?
- Mark all vertices as unvisited
- Choose a starting vertex
- Visit the starting vertex and mark it as visited
- Enqueue starting vertex in the queue
- Dequeue a vertex from the queue
- For each neighbouring vertex of this vertex, if it is univisited visit it, mark it as visited and enqueue it in the queue
- Repeat the processor of dequeuing vertices and visiting its neighbours until the queue is empty
What is a common use of Breadth-First Search?
To find the path containing the least number of nodes between two points in an unweighted graph.
What are the steps for Depth-First Search using a stack?
- Mark the starting vertex as discovered
- Push the starting node onto the stack
- Pop the top vertex off the stack and mark and visit it
- Push each of this vertex’s neighbours to the stack and mark them as discovered
- Repeat the processes of popping vertices and pushing their neighbours to the stack until the stack is empty