Take-Away Notes for Module COMP2048.
Pseudocode, Running Time Analysis and Big-Oh(
- Proofread
- Reviewed
- Add Figures
- Complete Sample Proofs
Pseudocode
Method Declaration
1 | Algorithm method(arg [, arg ...] ) |
Control Flow
1 | if ... then ... [else ...] |
note: Use Indentation to replace Braces
Method Call
1 | method(arg [, arg ...]) |
Return Value
1 | return expression |
Expression Notation
Assignment:
Equality Testing:
note: Superscriptions and other mathematical formatting allowed
Running Time Analysis
- Focus on the relationship between running time of algorithm & size of its input
- Characterized as a function of the input size for an algorithm
Running Time
Affected by:
- Input Size
- Hardware Environment (e.g. Processor / Clock Rate / Memory / Disk)
- Software Environment (e.g. Operating System / Programming Language)
Experimental Analysis
- Write a program to implement the algorithm
- Run the program to perform independent experiments with inputs of varying size
- Plot the results of input size & running time
- (Extra) Apply statistical analysis that seeks to fit the best function of input size to the experimental data
Limitations:
- Same hardware & software must be applied to compare algorithms;
- Results may not be indicative of the running time on other inputs not included in the experiment;
- Implementation of algorithm is compulsory;
Theoretical Analysis
Advantages
- High Level Description
- Characterize Running Time as a function of the input size n
- Takes all possible(legal) inputs into account
- Evaluation independent of hardware / software environment
Primitive Operations
Definition:
- Basic computations performed by an algorithm
- Independent from programming languages
- Exact definition is unnecessary
- Assumed to take a constant amount of time in RAM(Random Access Machine) Model
Components & Examples:
- Assign a value to a variable
- Following an object reference
- Performing an arithmetic operation
- Comparing two numbers
- [!] Accessing a single element of an array by index
- [!] Calling a method
- [!] Return from a method
RAM(Random Access Machine)
Components:
- CPU
- (Potentially) unbounded & numbered memory cells
Estimating Theoretical Running Time
TBC. See also in the slides (Page 17-18) for more details.
Just take care: Treat loops / method calling / method returning CAUTIOUSLY!
Big-Oh Series
Concerns: Worst Case
Samples:
Usage:
- Rank functions / algorithms according to the growth rates of input size & running time;
- Gives an upper bound on the growth rate of a function;
Note:
Example Process:
e.g. For Algorithm arrayMax
(mentioned in slides
(Page 17-18)) with
Primitive operations are assumed to take a constant amount time in
the RAM model as mentioned before, which is denoted as
Let arrayMax
:
Thus
Thus arrayMax
runs in linear time;
Note:
e.g. hardware / software environment, lower-ordered terms of
but the growth rate of which is independent(an intrinsic property of the corresponding algorithm)
Big-Oh: General Proofs
Let
Thus we say
aka. the growth of the real running time is never under estimated.
f(n) is O(g(n)) | g(n) is O(f(n)) | |
---|---|---|
g(n) grows more | Yes | No |
f(n) grows more | No | Yes |
Same Growth |
Yes | Yes |
Note: Big-Oh Series are NOT related to derivative & differentiate! [!]Note: Proofs are ommited until later. -- 2022/09/28
Big-Oh Rules:
- Drop lower-order terms of
; - Drop constant factors of
;
e.g. "
Asymptotic Algorithm Analysis
Purpose: determines the running time in Big-Oh
Notation(
Perform / Deliver 1. Find the worst-case number of
primitive operations executed as a function of the input size; 2.
Express this function with Big-Oh notation; e.g. arrayMax
runs in
Big-Oh Series in A Nutshell
Prerequisites: Let
Big-Oh
asymptotically
less
than or equal to g(n).
i.e. We say that
if there is a real constant
such that for every
i.e.
Big-Omega
asymptotically
greater than or equal to g(n).
i.e. We say that
if there is a real constant
such that for every
i.e.
Big-Theta
asymptotically
equal
to g(n).
i.e. We say that
if there are real constants
such that for every
i.e.
Little-Oh
asymptotically
,
strictly
less than g(n).
i.e. We say that
if for all positive real constant
such that for every
i.e.