Algorithms Flashcards
what is recursion
the ability of a subroutine to call itselff
characteristics of a recursive subroutine
- a stopping condition/base case must be included, which when met, the routine will not call itself
- for input values other than the stopping condition, the routine must call itself
- system keeps pending calls on a stack
- parameters are also passed on a stack
- system needs a lot of memory to handle recursion
- the stopping condition must be reached after a finite number of calls
what is the algorithm for an in order traversal
- traverse the left subtree
- visit the root node
- traverse the right subtree
algorithm for post order traversal
- traverse the left subtree
- traverse the right subtree
- visit the root node
algorithm for pre order traversal
- visit the root node
- traverse the left subtree
- traverse the right subtree
why is time complexity important when coding
helps design algorithms that will run quickly while taking up the minimal amount of resources such as memory
how does depth first traversal work
- uses a stack
- starts at root, follows one branch as far as it’ll go, then backtracks
how does breadth first search work
Start from the source node and mark it as visited.
Add the source node to a queue.
While the queue is not empty:
Remove the node from the front of the queue.
Visit all its unvisited neighbors and add them to the queue.
Mark them as visited.
how to do depth first traversal
- PUSH the first vertex onto the stack
- Mark vertex as visited
- REPEAT
- visit next unvisited vertex to the one on top of the stack
- mark as visited
If no vertex to visit, POP vertex off stack - Until stack is empty
simplified:
Start from the source node.
Visit a neighbor and recursively continue the search as far as possible along this path.
When you reach a dead-end (no unvisited neighbors), backtrack and explore other paths.
how to do breadth first search
- PUSH the first vertex onto the queue
- mark vertex as visited
- Visit unvisited vertex’s connected to the first vertex
- PUSH vertex’s onto queue
- Until all vertex’s have been visited
- Repeat
- Visit the unvisited vertex’s
- PUSH vertex onto queue
- Until all vertex’s visited
- Until queue is empty
what is the objective of dijkstras algorithm
to find the shortest path between a starting vertex and any other vertex in a graph
how does dijkstras algorithm work
- we use 2 lists : one to keep track of the vertices we have visited, and one to keep track of unvisited vertices
1. visit the unvisited vertex with the smallest known distance from the start vertex (would be the start vertex)
2. for the current vertex, examine it’s unvisited neighbours
3. for the current vertex, calculate the distance of each neighbour from the starting vertex
4. update the distance in the table
5. update the previous vertex for each of the updated distances
6. add the previous vertex to the list of visited vertex
7. repeat until all vertices are visited
what do we label the distances from the starting vertex in dijkstras algorithm and why
infinity, as the distances are unknown
what is an algorithm
a finite sequence of instructions that can be followed to complete a task and that always terminates
what is a mealy machine
an FSM with an output
- the outputs are determined by both its current state and the current input
- there can only be one possible transition
what are FSMs
a computational model that consists of a finite number of states.
how do finite state machines operate
Having a set of states (one state at a time).
Starting in an initial state.
Transitioning between states based on input symbols.
Reaching either a final/accepting state