what is recursion
the ability of a subroutine to call itselff
characteristics of a recursive subroutine
what is the algorithm for an in order traversal
algorithm for post order traversal
algorithm for pre order traversal
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
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
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
what is the objective of dijkstras algorithm
to find the shortest distance between a starting vertex and any other vertex in a graph
how does dijkstras algorithm work and what are its uses
Used in GPS/navigation
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
FSMs consist of:
States: These are the individual steps that the machine can be in at any time.
Transitions: These are the changes between states based on an input symbol.
Start State: The state where the FSM begins.
accept States: The states that represent successful completion of the process.
dead states - a state from which there is no possible transition to an accepting or final state.
uses of FSMs and mealy machines
FSM - Digital circuits: FSMs help design systems that react to inputs
Compilers: FSMs help parse regular expressions and check whether sequences of characters are valid.
Mealy machines - Embedded systems: Devices that need to respond to external stimuli (like sensors)
- Telecommunication protocols: Machines that need to send signals or receive them
what is bubble sort
a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
bubble sort example - Sorting the list [4, 3, 2, 1]
First pass: [3, 2, 1, 4] (4 moves to the end)
Second pass: [2, 1, 3, 4]
Third pass: [1, 2, 3, 4]
bubble sort time complexity
merge sort Example: Sorting the list [8, 4, 6, 1]:
Divide into [8, 4] and [6, 1].
Recursively sort both halves:
Sort [8, 4] into [4, 8]
Sort [6, 1] into [1, 6]
Merge the two sorted halves:
[4, 8] and [1, 6] → [1, 4, 6, 8]
summarise merge sort