Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Algorithm
Problem:
ques=on
+
input(s)
Algorithm:
eec0ve
procedure
mapping
input
to
output
5/25/10
Ulam's
problem
f(n):
if
(n==1)
return
1
else
if
(odd(n))
return
f(3*n+1)
else
return
f(n/2)
Ulam's
problem
f(n):
if
(n==1)
return
1
else
if
(odd(n))
return
f(3*n+1)
else
return
f(n/2)
Ulam's
problem
f(n):
if
(n==1)
return
1
else
if
(odd(n))
return
f(3*n+1)
else
return
f(n/2)
-Nobody
has
ever
found
an
n
for
which
f
does
not
stop
-Nobody
has
ever
found
a
proof:
n : f (n) stops
(so
there
can
be
no
algorithm
deciding
this.)
5/25/10
Undecidability
Verica=on/equivalence
undecidable
Given
any
specica=on
S
and
any
program
P,
there
is
no
algorithm
that
decides
whether
P
executes
according
to
S
Given
any
two
programs
P1
and
P2,
there
is
no
algorithm
that
decides
x:
P1(x)=P2(x)
Does
this
mean
we
should
not
build
program
veriers?
Intractability
Suppose
we
have
a
program,
does
it
execute
a
in
a
reasonable
=me?
Eg,
towers
of
Hanoi.
Three
pegs,
one
with
n
smaller
and
smaller
disks,
move
(1
disk
at
the
=me)
to
another
peg
without
ever
placing
a
larger
disk
on
a
smaller
f(n) = # moves for tower of size n Monk: before a tower of Hanoi of size 100 is moved, the world will have vanished
5/25/10
Example: Tower of Hanoi, move all disks to third peg without ever
Example: Tower of Hanoi, move all disks to third peg without ever
13
14
Recursive
Algorithms
Example:
Tower
of
Hanoi,
move
all
disks
to
third
peg
without
ever
Example: Tower of Hanoi, move all disks to third peg without ever
f(n) = f(n-1) +
f(n) = f(n-1) + 1 +
15
16
5/25/10
f(n):
#moves
Example:
Tower
of
Hanoi,
move
all
disks
to
third
peg
without
ever
f(n) = 2f(n-1) + 1, f(1)=1 f(1) = 1, f(2) = 3, f(3) = 7, f(4) = 15 f(n) = 2n-1 How can you show that?
5/25/10
Algorithm
complexity
Measures
in
units
of
0me
and
space
Linear
Search
X
in
dic=onary
D
i=1;
while
(not
at
end
and
X!=
D[i])
{i++}
We
don't
know
if
X
is
in
D,
and
we
don't
know
where
it
is,
so
we
can
only
give
worst
or
average
=me
bounds
We
don't
know
the
=me
for
atomic
ac=ons,
so
we
only
determine
Orders
of
Magnitude
=me
complexity
In
the
worst
case
we
search
all
of
D,
so
the
loop
body
is
executed
n
=mes
In
the
average
case
the
loop
body
is
executed
about
n/2
=mes.
Why?
5/25/10
=me
complexity
In
the
worst
case
we
search
all
of
D,
so
the
loop
body
is
executed
n
=mes
In
the
average
case
the
loop
body
is
executed
about
n/2
=mes.
In
average
case
analysis
we
sum
the
products
of
the
probability
of
each
outcome
and
the
cost
of
that
outcome,
here
n n
Units
of
=me
1
microsecond
?
1
machine
instruc=on?
#code
fragments
that
take
constant
=me?
Units
of
=me
1
microsecond
?
no,
too
specic
and
machine
dependent
1
machine
instruc=on?
no,
s0ll
too
specic
#code
fragments
that
take
constant
=me?
yes
bit?
unit of space
int?
5/25/10
unit
of
space
bit?
very
detailed
but
some0mes
necessary
int?
nicer,
but
dangerous:
we
can
code
a
whole
program
or
array
(or
disk)
in
one
arbitrary
int,
so
we
have
to
be
careful
with
space
analysis
(take
value
ranges
into
account
when
needed)
Orders
of
magnitude
If
an
algorithm
with
input
size
n
takes
less
than
c.n2
steps
to
execute,
we
say
it
is
an
order
n
squared
algorithm,
nota=on
O(n2)
In
general
g(n)
=
O(f(n))
if
there
is
a
constant
c
such
that
g(n)
<=
c.f(n)
for
all
but
a
nite
number
of
values
for
n.
In
other
words,
there
is
a
n0
such
that
g(n)
<=
c.f(n)
for
all
n>n0
P .C
i iI n
Pi : probability input i occurs Ci : complexity given input i In : all possible inputs of size n
5/25/10
Binary
Search
Because
dic=onaries
are
sorted
BS(x,lo,hi):
if
(lo>hi)
not
found
else
{
m
=
(lo+hi)/2;
if
(x==D[m])
found
else
if
(x<D[m])
BS(x,lo,m-1)
else
BS(x,m+1,hi)
}
Binary
Search
Because
dic=onaries
are
sorted
BS(x,lo,hi):
if
(lo>hi)
not
found
else
{
m
=
(lo+hi)/2;
if
(x==D[m])
found
else
if
(x<D[m])
BS(x,lo,m-1)
else
BS(x,m+1,hi)
}
why
m-1
and
m+1?
5/25/10
lower bound is minimal worst case complexity of any algorithm, using only the allowed steps, solving it
10
5/25/10
For some input a path through the tree to a par=cular leaf is taken
11
5/25/10
NP
Complete
problems
NPC:
a
certain
class
of
problems
with
algorithmic
gaps
upper
bound:
exponen=al
lower
bound:
polynomial
Trial
and
error
type
algorithms
are
the
only
ones
we
have
so
far
to
nd
an
exact
solu=on
12
5/25/10
Back
tracking
Back
tracking
searches
(walks)
a
state
space,
at
each
choice
point
it
guesses
a
choice.
In
a
leaf
(no
further
choices)
if
solu=on
found
OK,
else
go
back
to
last
choice
point
and
pick
another
move.
A
Non
Determinis=c
algorithm
is
one
with
the
ability
to
select
the
sequence
of
right
guesses
(avoiding
back
track)
P vs NP
NP
problems
become
intractable
quickly
NP
is
the
class
of
problems
solvable
in
TSP
for
100
ci0es?
How
would
you
polynomial
=me
by
a
non
determinis=c
enumerate
all
possible
tours?
How
many?
algorithm
Coping
with
intractability:
P
is
the
class
of
problems
solvable
in
polynomial
=me
by
a
determinis=c
algorithm
Approxima=on:
Find
a
nearly
op=mal
tour
Randomiza=on:
use
a
probabilis=c
algorithm
using
the
$1,000,000
ques0on:
is
P
=
NP
?
"coin
tosses"
(eg
prime
witnesses)
a
$1
ques=on:
is
Hanoi
in
NP?
13