BU CS 332 – Theory of Computation
Lecture 18:
• Time Complexity
Reading:
Sipser Ch 7.1‐7.2
• Complexity Class P
Mark Bun April 6, 2020
Where we are in CS 332
Automata & Formal Languages Computability Complexity
Previous unit: Computability theory
What kinds of problems can / can’t computers solve?
Final unit: Complexity theory
What kinds of problems can / can’t computers solve under constraints on their computational resources?
4/6/2020 CS332 ‐ Theory of Computation 2
First topic: Time complexity
Today: Answering the basic questions
1. How do we measure complexity? (as in CS 330)
2. Asymptotic notation (as in CS 330)
3. How robust is the TM model when we care about measuring complexity?
4. How do we mathematically capture our intuitive notion of “efficient algorithms”?
4/6/2020 CS332 ‐ Theory of Computation 3
Running time analysis
Time complexity of a TM (algorithm) = maximum number of steps it takes on a worst‐case input
Formally: Let ∗ . A TM runs in time if on every input , halts on within at most steps
‐ Focus on worst‐case running time: Upper bound of must hold for all inputs of length
‐ Exact running time does not translate well between computational models / real computers. Instead focus on asymptotic complexity.
4/6/2020 CS332 ‐ Theory of Computation 4
Example
How much time does it take for a basic single‐tape TM to decide ?
Let’s analyze one particular TM :
= “On input :
1. Scan input and reject if not of the form 2. While input contains both ’s and ’s:
∗∗
Cross off one and one
3. Accept if no 0’s and no 1’s left. Otherwise, reject.”
4/6/2020 CS332 ‐ Theory of Computation 5
Review of asymptotic notation
‐notation (upper bounds)
means:
There exist constants
such that
Example:
4/6/2020 CS332 ‐ Theory of Computation
( , )
6
for every
Caution: does not mean “equals” Not reflexive:
Example:
does not mean
Alternative (better) notation:
4/6/2020 CS332 ‐ Theory of Computation 7
Examples
•
632
• • •
4/6/2020
CS332 ‐ Theory of Computation 8
Review of asymptotic notation
‐notation (lower bounds)
Example:
( , )
9
means:
There exist constants
such that
4/6/2020 CS332 ‐ Theory of Computation
for every
When should we use vs. ?
Upper bounds: Use
“The merge‐sort algorithm uses at most
comparisons in the worst case”
Lower bounds: Use
“Every comparison‐based sorting algorithm requires at
least comparisons in the worst case”
4/6/2020 CS332 ‐ Theory of Computation 10
Review of asymptotic notation
‐notation (tight bounds)
Example:
Generally, polynomials are easy:
means:
AND
𝑑𝑑 1 0𝑑 4/6/2020 CS332 ‐ Theory of Computation 11
Little‐oh and little‐omega
‐notation and ‐notation are like and ; ‐notation and ‐notation are like and
means:
For every constant there exists
such that
for every
Example: ( ) 4/6/2020 CS332 ‐ Theory of Computation
12
A handy‐dandy chart
Notation f(n)=O(g(n))
… means … ∃c>0,n0>0,∀n>n0 :
Think… Upper bound
Example 100n2
lim ←
f(n)=(g(n)) f(n)=(g(n))
cg(n) < f(n)
both of the above:
Lower bound
= (n100) log(n!)
If it exists, it is > 0
f(n)=o(g(n)) f(n)=(g(n))
f(n) < cg(n)
Strict upper bound
Limit exists, =0
4/6/2020
CS332 ‐ Theory of Computation
13
f(n) < cg(n) ∃c>0,n0>0,∀n>n0 :
= O(n3) 2n
If it exists, it is < ∞
f=(g)andf=O(g) ∀c>0,∃n0>0,∀n>n0 :
Tight bound
=(nlogn) n2 = o(2n)
If it exists, it is>0and< ∞
∀c>0,∃n0>0,∀n>n0 : cg(n) < f(n)
Strict lower bound
n2
= (log n)
Limit exists, =∞
Asymptotic notation within expressions
Asymptotic notation within an expression is shorthand for an unspecified function satisfying the statement
Examples:
•
• •
4/6/2020
CS332 ‐ Theory of Computation 14
FAABs: Frequently asked asymptotic bounds
•Polynomials. 0 1 • Logarithms. 𝑎
𝑑 𝑑 is 𝑑 if 𝑑 𝑏 for all constants
For every • Exponentials. For all
,
and all
,
𝑑𝑛
• Factorial.
By Stirling’s formula,
4/6/2020
CS332 ‐ Theory of Computation
15
Time Complexity
4/6/2020 CS332 ‐ Theory of Computation 16
Time complexity classes
Let
is a class (i.e., set) of languages:
A language if there exists a basic single‐ tape (deterministic) TM that
1) Decides , and 2) Runs in time
4/6/2020 CS332 ‐ Theory of Computation 17
Example
𝐴01 𝑚0 𝑀 = “On input 𝑤:
1. Scan input and reject if not of the form 0∗1∗ 2. While input contains both 0’s and 1’s:
Cross off one 0 and one 1
3. Accept if no 0’s and no 1’s left. Otherwise, reject.”
• runs in time
• Is there a faster algorithm?
4/6/2020 CS332 ‐ Theory of Computation 18
Example
𝐴01 𝑚0 𝑀′ = “On input 𝑤:
1. Scan input and reject if not of the form 0∗1∗
2. While input contains both 0’s and 1’s:
• Reject if the total number of 0’s and 1’s remaining is odd
• Cross off every other 0 and every other 1
3. Accept if no 0’s and no 1’s left. Otherwise, reject.” • Running time of :
• Is there a faster algorithm?
4/6/2020 CS332 ‐ Theory of Computation 19
Example
Running time of :
Theorem (Sipser, Problem 7.49): If can be decided in
time on a 1‐tape TM, then is regular
4/6/2020 CS332 ‐ Theory of Computation
20
Does it matter that we’re using the 1‐tape model for this result?
It matters: 2‐tape TMs can decide faster
𝑀′′ = “On input 𝑤:
1. Scan input and reject if not of the form 0∗1∗
2. Copy 0’s to tape 2
3. Scan tape 1. For each 1 read, cross of a 0 on tape 2
4. If 0’s on tape 2 finish at same time as 1’s on tape 1, accept. Otherwise, reject.”
Analysis: is decided in time on a 2‐tape TM Moral of the story (part 1): Unlike decidability, time
complexity depends on the TM model
4/6/2020 CS332 ‐ Theory of Computation 21
How much does the model matter?
Theorem: Let be a function. Every multi‐tape TM running in time has an equivalent single‐tape TM running in time
Proof idea:
We already saw how to simulate a multi‐tape TM with a
single‐tape TM
Need a runtime analysis of this construction
Moral of the story (part 2): Time complexity doesn’t depend too much on the TM model (as long as it’s deterministic, sequential)
4/6/2020 CS332 ‐ Theory of Computation 22
Simulating Multiple Tapes
Implementation‐Level Description
On input
1. Format tape into
2. For each move of :
Scan left‐to‐right, finding current symbols Scan left‐to‐right, writing new symbols, Scan left‐to‐right, moving each tape head
If a tape head goes off the right end, insert blank If a tape head goes off left end, move back right
4/6/2020 CS332 ‐ Theory of Computation 23
How much does the model matter?
Theorem: Let be a function. Every multi‐tape TM running in time has an equivalent single‐tape TM running in time
Proof: Time analysis of simulation
• Time to initialize (i.e., format tape):
• Time to simulate one step of multi‐tape TM: • Number of steps to simulate:
=> Total time:
4/6/2020 CS332 ‐ Theory of Computation 24
Extended Church‐Turing Thesis
Every “reasonable” model of computation can be simulated by a basic, single‐tape TM with only a polynomial slowdown.
E.g., doubly infinite TMs, multi‐tape TMs, RAM TMs Does not include nondeterministic TMs (not reasonable)
Possible counterexamples? Randomized computation, parallel computation, DNA computing, quantum computation
4/6/2020 CS332 ‐ Theory of Computation 25
Complexity Class
4/6/2020 CS332 ‐ Theory of Computation 26
Complexity class
Definition: is the class of languages decidable in polynomial time on a basic single‐tape (deterministic) TM
• Class doesn’t change if we substitute in another reasonable deterministic model (Extended Church‐Turing)
• Cobham‐Edmonds Thesis: Captures class of problems that are feasible to solve on computers
4/6/2020 CS332 ‐ Theory of Computation 27
Examples of languages in
• 𝑃𝐴𝑇𝐻
𝐺, 𝑠, 𝑡 𝐺 is a directed graph with a directed path from 𝑠 to 𝑡
• 𝐴 𝐷,𝑤 • 𝑅𝐸𝐿𝑃𝑅𝐼𝑀𝐸
𝐷 is a DFA that accepts input 𝑤
𝑥, 𝑦 𝑥 and 𝑦 are relatively prime
• 𝑃𝑅𝐼𝑀𝐸𝑆
𝑥 𝑥 is prime
2006 Gödel Prize citation
• Every context‐free language (section tomorrow)
4/6/2020 CS332 ‐ Theory of Computation 28
The 2006 Gödel Prize for outstanding articles in theoretical computer science is awarded to Manindra Agrawal, Neeraj Kayal, and Nitin Saxena for their paper “PRIMES is in P.”
In August 2002 one of the most ancient computational problems was finally solved….