Theory of Algorithms and Data Structures Lecture 2: Complexity Theory Dr William Smith wsmithcs'york - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Theory of Algorithms and Data Structures Lecture 2: Complexity Theory Dr William Smith wsmithcs'york

Description:

Unfair test: what if one of the algorithms just happens to be faster on this particular input? ... Measuring resource usage. Theory of Algorithms and Data ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 57
Provided by: wsm26
Category:

less

Transcript and Presenter's Notes

Title: Theory of Algorithms and Data Structures Lecture 2: Complexity Theory Dr William Smith wsmithcs'york


1
Theory of Algorithms and Data StructuresLecture
2 Complexity TheoryDr William
Smithwsmith_at_cs.york.ac.uk
2
Theory of Algorithms and Data Structures
Todays Topics
Where we are
  • We know what an algorithm is
  • We know how to specify a problem

Todays topics
  • How do we analyse an algorithm?
  • How do we compare several algorithms that solve
    the same problem?
  • The BIG idea machine/programmer/language/input
    independent way to compare algorithms

Lecture 2
3
Theory of Algorithms and Data Structures
Algorithm Performance
Q How might we establish whether algorithm A is
faster than algorithm B?
Lecture 2
4
Theory of Algorithms and Data Structures
Algorithm Performance
Q How might we establish whether algorithm A is
faster than algorithm B?
A1 We could implement both of them, run them on
the same input and time how long each of them
takes
Lecture 2
5
Theory of Algorithms and Data Structures
Algorithm Performance
Q How might we establish whether algorithm A is
faster than algorithm B?
A1 We could implement both of them, run them on
the same input and time how long each of them
takes
Unfair test what if one of the algorithms just
happens to be faster on this particular input?
Lecture 2
6
Theory of Algorithms and Data Structures
Algorithm Performance
Q How might we establish whether algorithm A is
faster than algorithm B?
A2 We could implement both of them, run them on
lots of different inputs and time how long each
of them takes on each input
Lecture 2
7
Theory of Algorithms and Data Structures
Algorithm Performance
Q How might we establish whether algorithm A is
faster than algorithm B?
A2 We could implement both of them, run them on
lots of different inputs and time how long each
of them takes on each input
Assuming we can try every input of a particular
size, this would give us best, worst and average
running times for this particular implementation
on this particular computer for this particular
input size Still an unfair test what if one
algorithm just happens to be faster on this size
of input? What if we want a more general answer?
Not tied to one computer or implementation.
Lecture 2
8
Theory of Algorithms and Data Structures
Algorithm Performance
Lets generalise things slightly The function
is a mapping from the set of all inputs to the
time taken on that input
For any problem instance
T(i) is the running time on i
  • Computing the running time for every possible
    problem instance is overwhelming
  • Instead, group together similar inputs
  • Gives us running time as a function of a class
    of instances
  • How shall we group inputs?

Lecture 2
9
Theory of Algorithms and Data Structures
Grouping inputs by size
Grouping inputs together of equal size is
generally the most useful Bigger problems are
harder to solve
Q What do we mean by the size of an input?
Lecture 2
10
Theory of Algorithms and Data Structures
Grouping inputs by size
Grouping inputs together of equal size is
generally the most useful Bigger problems are
harder to solve
Q What do we mean by the size of an input? A It
depends on the problem
Lecture 2
11
Theory of Algorithms and Data Structures
Grouping inputs by size
Grouping inputs together of equal size is
generally the most useful Bigger problems are
harder to solve
Q What do we mean by the size of an input? A It
depends on the problem
  • Integer input ? number of digits
  • Set input ? number of elements in set
  • Text string ? number of characters
  • Generally obvious

Lecture 2
12
Theory of Algorithms and Data Structures
Grouping inputs by size
Grouping inputs together of equal size is
generally the most useful Bigger problems are
harder to solve
Q What do we mean by the size of an input? A It
depends on the problem
  • Integer input ? number of digits
  • Set input ? number of elements in set
  • Text string ? number of characters
  • Generally obvious

Not always so neat what if the input was a
graph? (Think back to ICM) May need more than one
size parameter graph size ( vertices, edges)
Lecture 2
13
Theory of Algorithms and Data Structures
Types of performance analysis
We denote the set of all instances of size We can
define three measures of performance
as In
Lecture 2
14
Theory of Algorithms and Data Structures
Types of performance analysis
We denote the set of all instances of size We can
define three measures of performance
as In
  • Worst-case
  • T(n) maximum time of algorithm on any input of
    size n.

Lecture 2
15
Theory of Algorithms and Data Structures
Types of performance analysis
We denote the set of all instances of size We can
define three measures of performance
as In
  • Worst-case
  • T(n) maximum time of algorithm on any input of
    size n.
  • Best-case
  • T(n) minimum time of algorithm on any input of
    size n.

Lecture 2
16
Theory of Algorithms and Data Structures
Types of performance analysis
We denote the set of all instances of size We can
define three measures of performance
as In
  • Worst-case
  • T(n) maximum time of algorithm on any input of
    size n.
  • Best-case
  • T(n) minimum time of algorithm on any input of
    size n.

Average-case
  • T(n) expected time of algorithm over all
    inputs of size n.

Lecture 2
17
Theory of Algorithms and Data Structures
Types of performance analysis
We denote the set of all instances of size We can
define three measures of performance
as In
  • Worst-case
  • T(n) maximum time of algorithm on any input of
    size n.
  • Best-case
  • T(n) minimum time of algorithm on any input of
    size n.

Average-case
  • T(n) expected time of algorithm over all
    inputs of size n.

Q What assumption is being made here?
Lecture 2
18
Theory of Algorithms and Data Structures
Types of performance analysis
We denote the set of all instances of size We can
define three measures of performance
as In
  • Worst-case
  • T(n) maximum time of algorithm on any input of
    size n.
  • Best-case
  • T(n) minimum time of algorithm on any input of
    size n.

Average-case
  • T(n) expected time of algorithm over all
    inputs of size n.

Q What assumption is being made here? All inputs
equally likely if not we need to know the
probability distribution
Lecture 2
19
Theory of Algorithms and Data Structures
Types of performance analysis
We denote the set of all instances of size We can
define three measures of performance
as In
Q Which is most useful? Q How can we modify
almost any algorithm tohave a good best-case
running time?
Lecture 2
20
Theory of Algorithms and Data Structures
Types of performance analysis
Q Which is most useful? A Generally concentrate
on worst-case execution time strongest
performance guarantee

Lecture 2
21
Theory of Algorithms and Data Structures
Types of performance analysis
Q Which is most useful? A Generally concentrate
on worst-case execution time strongest
performance guarantee Q How can we modify almost
any algorithm to have a good best-case running
time? A Find a solution for one particular input
and store it. When that input is encountered,
return our precomputed answer immediately. Other
more subtle ways of improving best-case
performance. Best-case is generally bogus!

Lecture 2
22
Theory of Algorithms and Data Structures
Measuring resource usage
Example Summing the first n positive integers
Two solutions
Lecture 2
23
Theory of Algorithms and Data Structures
Measuring resource usage
Example Summing the first n positive integers
Two solutions
Both algorithms are correct Q Which is better?
Lecture 2
24
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Lecture 2
25
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 1
Cost
No. Times
Lecture 2
26
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 1
Cost
No. Times
s
1
Lecture 2
27
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 1
Cost
No. Times
s
1
ti
n 1
Lecture 2
28
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 1
Cost
No. Times
s
1
ti
n 1
as
n
Lecture 2
29
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 1
Cost
No. Times
s
1
ti
n 1
as
n
Lecture 2
30
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 2
Cost
No. Times
Lecture 2
31
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 2
Cost
No. Times
imds
1
Lecture 2
32
Theory of Algorithms and Data Structures
Measuring resource usage
  • Define some constants
  • i is the time to increment by 1
  • a is the time to perform an addition
  • t is the time to perform the loop test
  • m is the time to multiply two numbers
  • d is the time to divide by 2
  • s is the time to perform an assignment

Version 2
Cost
No. Times
imds
1
Lecture 2
33
Theory of Algorithms and Data Structures
Measuring resource usage
Which is better?
Time
n
Lecture 2
34
Theory of Algorithms and Data Structures
Measuring resource usage
Which is better?
Time
n
Depends on size of input Beyond intersection, T2
will always win
Lecture 2
35
Theory of Algorithms and Data Structures
The RAM Model of Computation
  • The above analysis made some implicit
    assumptions
  • Modern hardware is hugely complex (pipelines,
    multiple cores, caches etc)
  • We need to abstract away from this
  • We require a model of computation that is simple
    and machine independent
  • Typically use a variant of a model developed by
    John von Neumann in 1945
  • Programs written with his model in mind run
    efficiently on modern hardware

Lecture 2
36
Theory of Algorithms and Data Structures
The RAM Model of Computation
  • The above analysis made some implicit
    assumptions
  • Modern hardware is hugely complex (pipelines,
    multiple cores, caches etc)
  • We need to abstract away from this
  • We require a model of computation that is simple
    and machine independent
  • Typically use a variant of a model developed by
    John von Neumann in 1945
  • Programs written with his model in mind run
    efficiently on modern hardware

The Random Access Machine(see Mehlhorn 2.2 for
detailed description)
Lecture 2
37
Theory of Algorithms and Data Structures
The RAM Model of Computation
  • Each simple operation (, , -, , if,
    assignment) takes exactly one time step
  • Loops and subroutine calls not considered simple
    operations
  • We have a finite, but always sufficiently large,
    amount of memory
  • Each memory access takes exactly one time step
  • Instructions are executed one after another
  • Time ? number of instructions

Lecture 2
38
Theory of Algorithms and Data Structures
Exact analysis is hard!
  • RAM model justifies counting number of
    operations in our algorithms to measure execution
    time
  • But, can only predict real execution times up to
    a constant factor
  • Precise details depend on uninteresting coding
    details
  • Constant speedups just reflect running code on a
    faster computer
  • We are really interested in machine independent
    growth rates
  • Why?
  • We are interested in performance for large n, we
    want to be able to solve difficult instances and
    start-up time dominates for small n
  • Known as asymptotic analysis
  • We can characterize and compare running times of
    algorithms with simple functions

Lecture 2
39
Theory of Algorithms and Data Structures
Asymptotic Notation
Consider two functions f(n) and g(n) with integer
inputs and numerical outputs We say f grows no
faster then g in the limit if
There exist positive constants c and n0 such that
We write this as
Read as f is Big Oh of g
We can also say f is asymptotically dominated by
g g is an upper bound on f f grows no faster
than g
Lecture 2
40
Theory of Algorithms and Data Structures
Definition of Big Oh
Formal definition
Breaking this up
Means we dont care about small n
Means we dont care about constant speedups
Unusual notation one way equality Really an
ordering relation (think of lt and gt)
definitely does not imply
Lecture 2
41
Theory of Algorithms and Data Structures
Definition of Big Oh
Might like to think in terms of sets
In this way, we can interpret
as
Sometimes read as f is in Big Oh of g
Lecture 2
42
Theory of Algorithms and Data Structures
Big Oh Example
- True or false?
How would we prove it?
Traditional to drop ?n. So we write
Go back to definition
To prove
we need
  • a witness (value) for x
  • a proof that P holds when witness substituted
    for x

Lecture 2
43
Theory of Algorithms and Data Structures
Big Oh Example
Lets choose c 2 Need to find an n0 such that
In this case, n0 1 or greater will do
By convention, always complex to simple
e.g.
Related operators follow from definition of Big
Oh
Lecture 2
44
Theory of Algorithms and Data Structures
Definition of Big Omega
If Big Oh is like then Big omega is like f
grows no slower than g
Read as f is big omega of g
Expressed as a set
Same as Big Oh, just reverse equality
e.g.
Lecture 2
45
Theory of Algorithms and Data Structures
Definition of Big Omega
Lecture 2
46
Theory of Algorithms and Data Structures
Definition of Big Theta
Big theta is like f grows at the same rate as
g
Read as f is big theta of g
Expressed as a set
e.g.
Lecture 2
47
Theory of Algorithms and Data Structures
Definition of Big Theta
Lecture 2
48
Theory of Algorithms and Data Structures
Definition of Little Oh
If Big Oh is like then Little Oh is like lt f
grows strictly slower than g
Read as f is little oh of g
Expressed as a set
Same as Big Oh, but existential becomes universal
e.g.
Lecture 2
49
Theory of Algorithms and Data Structures
Definition of Little Omega
If Big Omega is like then Little Omega is like
gt f grows strictly faster than g
Read as f is little omega of g
Expressed as a set
Same as Big Omega, but existential becomes
universal
e.g.
Lecture 2
50
Theory of Algorithms and Data Structures
Summary



lt
gt
An alternative limits-based interpretation
Q How might we use this to empirically test the
complexity of an algorithm implementation?
Lecture 2
51
Theory of Algorithms and Data Structures
Complexity Theory for Engineers
  • Properties of Big Oh and others leads to
    mechanical rules for simplification
  • Drop low order terms
  • Ignore leading constants

Lecture 2
52
Theory of Algorithms and Data Structures
Complexity Theory for Engineers
  • Properties of Big Oh and others leads to
    mechanical rules for simplification
  • Drop low order terms
  • Ignore leading constants

Lecture 2
53
Theory of Algorithms and Data Structures
Complexity Theory for Engineers
  • Properties of Big Oh and others leads to
    mechanical rules for simplification
  • Drop low order terms
  • Ignore leading constants

Lecture 2
54
Theory of Algorithms and Data Structures
Complexity Theory for Engineers
  • Properties of Big Oh and others leads to
    mechanical rules for simplification
  • Drop low order terms
  • Ignore leading constants

Lecture 2
55
Theory of Algorithms and Data Structures
Complexity Theory for Engineers
  • Properties of Big Oh and others leads to
    mechanical rules for simplification
  • Drop low order terms
  • Ignore leading constants
  • More tricks next time!

Lecture 2
56
Theory of Algorithms and Data Structures
Conclusions
We now have some tools for algorithm analysis
allowing us to talk abstractly about the
complexity of an algorithm. Next time
  • Applying these tools
  • Common classes of complexity
  • Limits what level of complexity is considered
    efficient or doable?

Recommended reading for this lecture
  • Mehlhorn Chapter 2 upto and including 2.4
  • Skiena 2.1 2.2
  • Cormen Chapter 3 (includes some useful basic
    maths revision)

Lecture 2
Write a Comment
User Comments (0)
About PowerShow.com