Introduction To Algorithms
Introduction To Algorithms
Error removal
Guarantee no new errors generated
Notes
Select a proven correct algorithm is important
Initial tests focus on verifying that a program runs correctly, then
reduce the running time
What is the module all about?
What is an Algorithm?
What is the module all about?
The theoretical study of design and analysis of
computer algorithms
integers.
Step I - Concept
From those integers that are currently unsorted, find the
Theorem
Function sort(a, n) correctly sorts a set of n>= 1
Approaches:
theoretical analysis
empirical analysis
16
Running Time
Most algorithms transform best case
Running Time
80
with the input size.
60
Average case time is often
difficult to determine. 40
running time. 0
1000 2000 3000 4000
Easier to analyze Input Size
Crucial to applications such as
games, finance and robotics
Analysis of Algorithms 17
Experimental Studies
Write a program 9000
algorithm 7000
Time (ms)
inputs of varying size and 5000
composition 4000
Use a method like 3000
System.currentTimeMillis() to 2000
get an accurate measure
1000
of the actual running time
0
Plot the results 0 50 100
Input Size
Analysis of Algorithms 18
Limitations of Experiments
It is necessary to implement the
algorithm, which may be difficult
Results may not be indicative of the
running time on other inputs not included
in the experiment.
In order to compare two algorithms, the
same hardware and software
environments must be used
Analysis of Algorithms 19
Theoretical Analysis
Uses a high-level description of the
algorithm instead of an implementation
Characterizes running time as a function
of the input size, n.
Takes into account all possible inputs
Allows us to evaluate the speed of an
algorithm independent of the
hardware/software environment
Analysis of Algorithms 20
Theoretical analysis of time
efficiency
Time efficiency is analyzed by determining
the number of repetitions of the basic
operation as a function of input size
Basic operation: the operation that
contributes most towards the running time
of the algorithm
input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
executed 21
Input size and basic operation examples
22
Input size and basic operation examples
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
23
Empirical analysis of time efficiency
24
Best-case, average-case, worst-case
For some algorithms efficiency depends on form of input:
Worst case: Cworst(n) – maximum over inputs of size n
Analysis of Algorithms 26
Pseudocode Details
Control flow Method call
if … then … [else …] var.method (arg [, arg…])
while … do … Return value
repeat … until … return expression
for … do … Expressions
Indentation replaces braces Assignment
(like in Java)
Method declaration Equality testing
Algorithm method (arg [, arg…]) (like in Java)
Input … n2 Superscripts and other
Output … mathematical
formatting allowed
Analysis of Algorithms 27
The Random Access Memory
(RAM) Model
A CPU
An potentially unbounded
bank of memory cells, 2
1
each of which can hold an 0
arbitrary number or
character
Memory cells are numbered and accessing
any cell in memory takes unit time.
Analysis of Algorithms 28
Primitive Operations
Basic computations
Examples:
performed by an algorithm Evaluating an
Identifiable in pseudocode expression
Largely independent from the Assigning a value
to a variable
programming language Indexing into an
Assumed to take a constant array
amount of time in the RAM Calling a method
model Returning from a
method
Analysis of Algorithms 30
Growth Rate of Running Time
Changing the hardware/ software
environment
Affects T(n) by a constant factor, but
Does not alter the growth rate of T(n)
The linear growth rate of the running
time T(n) is an intrinsic property of
algorithm arrayMax
Analysis of Algorithms 31
Seven Important Functions
Seven functions that
often appear in 1E+29
algorithm analysis: 1E+27 Cubic
Constant 1 1E+25 Quadratic
1E+23
Logarithmic log n 1E+21 Linear
Linear n 1E+19
1E+17
N-Log-N n log n
T(n)
1E+15
Quadratic n2 1E+13
Cubic n3 1E+11
1E+9
Exponential 2n 1E+7
1E+5
In a log-log chart, the 1E+3
1E+1
slope of the line 1E-1
corresponds to the 1E-1 1E+2 1E+5 1E+8
Analysis of Algorithms 33
Math you need to Review
Summations
Logarithms and Exponents
properties of logarithms:
logb(xy) = logbx + logby
logb (x/y) = logbx - logby
logbxa = alogbx
logba = logxa/logxb
properties of exponentials:
a(b+c) = aba c
Proof techniques abc = (ab)c
Basic probability ab /ac = a(b-c)
b = a logab
bc = a c*logab
Analysis of Algorithms 34