You are on page 1of 17

Algorithms and

complexity (1)
Data structures and algorithms in
Java

Parts used by kind permission:


Bruno Preiss, Data Structures and Algorithms with Object-Oriented Design Patterns in Java
David Watt and Deryck F. Brown, Java Collections, An Introduction to Abstract Data Types, Data Structures and Algorithms
Klaus Bothe, Humboldt University Berlin, course Praktische Informatik 1
Algorithms and complexity
General principles
Efficiency
Detailed model of a computer
Simplified model of a computer
Examples
General principles
An algorithm is a step-by-step procedure for
solving a stated problem.
The algorithm will be performed by a
processor (which may be human,
mechanical, or electronic).
The algorithm must be expressed in steps
that the processor is capable of performing.
The algorithm must eventually terminate.
More general principles
The algorithm must be expressed in some
language that the processor understands.
(But the underlying procedure is independent
of the particular language chosen.)
The stated problem must be solvable, i.e.,
capable of solution by a step-by-step
procedure.
Efficiency
Given several algorithms to solve the same
problem, which algorithm is best?
Given an algorithm, is it feasible to use it at all? In
other words, is it efficient enough to be usable in
practice?
How much time does the algorithm require?
How much space (memory) does the algorithm
require?
In general, both time and space requirements
depend on the algorithms input (typically the size
of the input).
Example
Hypothetical profile of two sorting algorithms:
4
Key:

3 Algorithm A
Algorithm B
time
(ms) 2

0
10 20 30 40 items to be sorted, n

Algorithm Bs time grows more slowly than As.


Measurements
In order to learn more about an algorithm (and the
possible program implementation), we can analyze
it. But what can we analyze? We can
determine the running time of a program as a function of its
inputs;
determine the total or maximum memory space needed for
program data;
determine the total size of the program code;
determine whether the program correctly computes the
desired result;
determine the complexity of the program--e.g., how easy is it
to read, understand, and modify; and,
determine the robustness of the program--e.g., how well does
it deal with unexpected or erroneous inputs?
More on the measuring
We are concerned primarily with the running time.
We also consider the memory space needed to execute the
program.
Factors that affect the running time of a program:
the algorithm itself,
the input data,
the computer system used to run the program.
the hardware:
processor used (type and speed),
memory available (cache and RAM), and
disk available;
the programming language in which the algorithm is specified;
the language compiler/interpreter used; and
the computer operating system software.
Measuring time
Measure time in seconds?
+ is useful in practice
depends on language, compiler, and processor.
Count algorithm steps?
+ does not depend on compiler or processor
depends on granularity of steps.
Count characteristic operations? (e.g.,
arithmetic ops in math algorithms, comparisons
in searching algorithms)
+ depends only on the algorithm itself
+ measures the algorithms intrinsic efficiency.
Define a model of a computer !!!!
Detailed model of a computer
Model independent of the actual hardware
and operating system
Analyzing the Java code on the Java Virtual
Machine
Loss of fidelity
Still enough details
Based on a set of basic axioms
Axiom 1
The time required to fetch an operand from memory
is a constant, fetch
The time required to store a result in memory is a
constant, store.
Examples
y = x fetch + store
y = 1 fetch + store
Axiom 2
The time required to perform elementary arithmetic
operations, such as addition, subtraction, multiplication,
division, and comparison, are all constants.
These times are denoted by +, -, x, /, and <, respectively.
Examples

y = y + 1; fetch + + + store

y++; fetch + + + store


Axiom 3
The time required to call a method is a constant, call
The time required to return from a method is a
constant, return.
Axiom 4
The time required to pass an argument to a method
is the same as the time required to store a value in
memory, store
Example

y = f(x); fetch + 2store + call + Tf(x)

where Tf(x) is the running time of


method f for the input x
n
Example i
i 1
Axiom 5
The time required for the address calculation implied by an array
subscripting operation, e.g., a[i], is a constant, []. This time does not
include the time to compute the subscript expression, nor does it
include the time to access (i.e., fetch or store) the array element.
Example

y = a[i]; fetch + [] + store

fetch -
Three operand fetches are required: the first to fetch a,
the base address of the array; the second to fetch i, the
index into the array; and, the third to fetch array element
a[i].
n
Example: Horners rule a x
i 1
i
i

You might also like