Professional Documents
Culture Documents
Complexity
Complexity
Nicolò Felicioni1
1
Mostly based on Nicholas Mainardi’s material.
Complexity analysis Turing Machines RAM Machine
Asymptotic Bounds
Examples
n
f (n) 2log2 (e )
f (n) = e n , g (n) = n2n : limn→∞ g (n) = limn→∞ 2log2 (n2n ) =
2n log2 (e)
limn→∞ 2n log2 (2)+log2 (n)
= limn→∞ 2n log2 (e)−n−log2 (n) = 2∞ = ∞
f (n)
f (n) = loge (n), g (n) = log2 (n) : limn→∞ g (n) =
log2 (n)
loge (n) log2 (e) 1
limn→∞ log2 (n) = limn→∞ log2 (n) = log2 (e) = loge (2)
Complexity analysis Turing Machines RAM Machine
A classic: TM recognizing an b n
Counting in unary
We consider a k-tapes TM recognizing L = an b n , n ≥ 0: how
many steps does it take to do so?
Consider the common algorithm which employs a memory
tape as a stack and emulates a DPDA (writes one marker
symbol for each a, deletes one for each b)
It performs an action for each letter of the string, and, when
the input has been scanned, the computation either accepts or
rejects
The complexity is O(2n) (considering n as the number of a in
the string)
Consequently, dropping the constant we get O(n) as the
asymptotic complexity
Complexity analysis Turing Machines RAM Machine
Still an b n
A not-so-abstract machine
Memory only machine: all the memory cells can be accessed
in O(1)/O(log (n)) (linear/logarithmic cost criterion)
By default the first operand is the one stored in memory
location 0 when the instruction is executed
Minimal RISC assembly with conditional jumps, halt
instruction stops computation
Operations employing an immediate value are denoted by an
= symbol (e.g. add=,sub= )
Complexity analysis Turing Machines RAM Machine
Single tape TM
Input: n, a binary encoded number; output: 2n binary
encoded
To double a number in binary, it is sufficient to add an extra 0
as the least significant digit
If the input head starts on the least significant digit: O(1):
it’s just “move by one place and write 0”
If the input head starts on the most significant digit:
O(log2 (n)): reach the least significant digit, and then add the
extra 0
Does it change anything if the TM has k-tapes? In the former
case, we need to write the output, thus O(log2 (n)) complexity
Complexity analysis Turing Machines RAM Machine
RAM Machine
The program on a RAM machine is quite straightforward:
READ 0
MUL= 2
WRITE 0
Sorting Quickly
Sorting Quickly
RAM Program
READ 1 STORE 2
LOAD 1 LOAD 3
DIV= 2 MUL 1
MUL= 2 STORE 3
SUB 1 LOAD 2
JLZ ODD JUMP LOOP
LOAD 1 ODD: LOAD 1
STORE 3 MUL 1
LOAD 1 MUL 1
DIV= 2 STORE 3
LOOP: SUB= 1 END: WRITE 3
JZ END HALT
Complexity analysis Turing Machines RAM Machine
√
Efficiently Computing n
√
Let’s focus on the computation of b nc + 1:
READ 1 LOAD 2
LOAD= 1 ADD= 1
LOOP: STORE 2 JUMP LOOP
MUL 2 COMPUTED: WRITE 2
SUB 1 HALT
JGZ COMPUTED
√
The loop is executed O( n) times
Due to the multiplication, the i-.th iteration costs O(log2 (i)),
P√n
thus the overall complexity is i=1 log2 (i)
Let’s estimate upper and lower bounds for this summation . . .
Complexity analysis Turing Machines RAM Machine
√
Efficiently Computing n
P√n P√n √
Upper bound: i=1 log2 (i) < i=1 log2 ( n) =
√ √ √ √
n log2 ( n) = 14 n log2 (n) = O( n log2 (n))
Lower bound: √
P√n 2 P 2n −1 2 P√n 2
√
n
i=1 log (i) > i=1 log (i) + √ log (
2 )>
i= 2n
P√n 2
√
n
√
n 2
√
n √ 2
√ log ( ) = log (
n
i= 2 2 2 ) = Ω( n log (n))
2
In conclusion:
P√n 2 √ 2
i=1 log (i) = O( n log (n))
1
√
P n √
2 2
i=1 log (i) = Ω( n log (n))
2
⇓
√ √
Computing b nc + 1 with this strategy costs Θ( n log2 (n))
Complexity analysis Turing Machines RAM Machine
√
Efficiently Computing n
√
Can we do better to compute b nc + 1?
√
The previous algorithm computes i 2 , i = 1, . . . b nc + 1 with
a multiplication
Key property of perfect squares: ∀i(i + 2)2 − (i + 1)2 =
(i + 1)2 − i 2 + 2 =⇒ (i + 2)2 = (i + 1)2 + (i + 1)2 − i 2 + 2
High-level algorithm based on this property:
sqrt(n)
1 inc ← 1
2 acc ← 1
3 res ← 1
4 while acc ≤ n
5 do inc ← inc + 2
6 acc ← acc + inc
7 res + +
8 return res
Complexity analysis Turing Machines RAM Machine
RAM Program
READ 1 STORE 2
LOAD= 1 LOAD 3
STORE 2 ADD= 2
STORE 3 STORE 3
LOOP: STORE 4 ADD 4
SUB 1 JUMP LOOP
JGZ END END: WRITE 2
LOAD 2 HALT
ADD= 1
Computational complexity:
√
O( n) iterations which execute only LOAD, ADD and SUB
P√n √
of numbers as big as i ⇒ i=1 log(i 2 ) = O( n log(n))
2
Quick questions
Quick questions