Algorithms Flashcards
What is an algorithm?
An algorithm is a sequence of steps for performing a task in a finite amount of time.
What is a data structure?
For computational algorithms, we use data structures to store, access, and manipulate data.
Questions to decide which search to use
- How is the list stored?
- Is it sorted or not?
- It is it not sorted, should we sort it first?
- Are we updating the list?
Types of algorithm analysis
- Running time
- Space usage
- Optimality of the output solution
Experimental Analysis
Interested in dependency of the running time on size of the input.
Size could mean amount of numbers of bits or amount of vertices and edges in a graph.
Therefore we perform experiments to empirically observe running time.
Drawbacks of experimental analysis
- Limited test of inputs
- Requires all tests to be performed hardware and software
- Requires implementation and execution of algorithm
Theoretical Analysis
More theory based approach of finding running time of algorithms.
Theoretical Analysis Benefits
- Can take all possible inputs into account.
- Involves studying high-level descriptions of algorithms
- Can compare efficiency of two algorithms
Theoretical Analysis Functions
When doing theoretical analysis, we typically assign a function f(n) to the algorithm, where f(n) characterises the running time for an input size n.
Theoretical Analysis Requirements
- a language to describe algorithms
- computational model in which algorithms are executed
- metric for running time
- a way of characterising running time
Pseudo-code
A high level description language for algorithms.
Mixture of a natural language and a high-level programming language, describing a generic implementation of data structures or algorithms.
Correctness
An algorithm is correct with respect to a specification if it behaves as specified.
Loop Invariant
A way to show correctness of an algorithm.
For example, if we had a for loop, we could state what the expected value is at the end of the for loop, aka specifying what the for loop is meant to do.
Loop Invariant Properties
- intialisation
- maintenance
- terminantion
Initalisation (Loop Invariant)
Remains true prior to the first iteration of the loop.