Time Complexity
•We use a multitape Turing machine
•We count the number of steps until
a string is accepted
•We use the O(k) notation
Fall 2004
COMP 335
1
Example:
n n
L {a b : n 0}
Algorithm to accept a string w :
•Use a two-tape Turing machine
•Copy the a on the second tape
•Compare the a and b
Fall 2004
COMP 335
2
n n
L {a b : n 0}
Time needed:
•Copy the a on the second tape
O(| w |)
•Compare the a and b
O(| w |)
Total time:
Fall 2004
COMP 335
O(| w |)
3
n n
L {a b : n 0}
For string of length
n
time needed for acceptance: O(n)
Fall 2004
COMP 335
4
Language class:
DTIME(n)
DTIME(n)
L1
L2
L3
A Deterministic Turing Machine
accepts each string of length n
in time O(n)
Fall 2004
COMP 335
5
DTIME(n)
n n
{a b : n 0}
Fall 2004
{ww}
COMP 335
6
In a similar way we define the class
DTIME(T (n))
for any time function:
T (n)
Examples:
2
Fall 2004
3
DTIME(n ), DTIME(n ),...
COMP 335
7
Example: The membership problem
for context free languages
L {w : w is generated by grammar G}
3
L DTIME (n )
(CYK - algorithm)
Polynomial time
Fall 2004
COMP 335
8
Theorem:
DTIME (n
k 1
k
) DTIME (n )
DTIME (n
k 1
)
k
DTIME (n )
Fall 2004
COMP 335
9
Polynomial time algorithms:
k
DTIME (n )
Represent tractable algorithms:
For small k we can compute the
result fast
Fall 2004
COMP 335
10
The class
P
k
P DTIME (n )
for all k
•Polynomial time
•All tractable problems
Fall 2004
COMP 335
11
P
CYK-algorithm
Fall 2004
n n
{a b }
{ww}
COMP 335
12
Exponential time algorithms:
n
DTIME (2 )
Represent intractable algorithms:
Some problem instances
may take centuries to solve
Fall 2004
COMP 335
13
Example: the Hamiltonian Problem
s
t
Question: is there a Hamiltonian path
from s toCOMP
t?335
Fall 2004
14
s
t
YES!
Fall 2004
COMP 335
15
A solution: search exhaustively all paths
L = {<G,s,t>: there is a Hamiltonian path
in G from s to t}
n
L DTIME(n!) DTIME (2 )
Exponential time
Intractable problem
Fall 2004
COMP 335
16
Example: The Satisfiability Problem
Boolean expressions in
Conjunctive Normal Form:
t1 t2 t3 tk
ti x1 x2 x3 x p
Variables
Question: is expression satisfiable?
Fall 2004
COMP 335
17
Example:
Satisfiable:
( x1 x2 ) ( x1 x3 )
x1 0, x2 1, x3 1
( x1 x2 ) ( x1 x3 ) 1
Fall 2004
COMP 335
18
Example:
( x1 x2 ) x1 x2
Not satisfiable
Fall 2004
COMP 335
19
L {w : expression w is satisfiabl e}
For n variables:
n
L DTIME (2 )
exponential
Algorithm:
search exhaustively all the possible
binary values of the variables
Fall 2004
COMP 335
20
Non-Determinism
Language class:
NTIME (n)
NTIME (n)
L1
L2
L3
A Non-Deterministic Turing Machine
accepts each string of length n
in time O(n)
Fall 2004
COMP 335
21
Example: L {ww}
Non-Deterministic Algorithm
to accept a string ww :
•Use a two-tape Turing machine
•Guess the middle of the string
and copy w on the second tape
•Compare the two tapes
Fall 2004
COMP 335
22
L {ww}
Time needed:
•Use a two-tape Turing machine
•Guess the middle of the string
and copy w on the second tape
•Compare the two tapes
Total time:
Fall 2004
COMP 335
O(| w |)
O(| w |)
O(| w |)
23
NTIME (n)
L {ww}
Fall 2004
COMP 335
24
In a similar way we define the class
NTIME (T (n))
for any time function:
Examples:
Fall 2004
T (n)
2
3
NTIME (n ), NTIME (n ),...
COMP 335
25
Non-Deterministic Polynomial time algorithms:
k
L NTIME (n )
Fall 2004
COMP 335
26
The class
NP
NP NTIME(n )
k
for all k
Non-Deterministic Polynomial time
Fall 2004
COMP 335
27
Example:
The satisfiability problem
L {w : expression w is satisfiabl e}
Non-Deterministic algorithm:
•Guess an assignment of the variables
•Check if this is a satisfying assignment
Fall 2004
COMP 335
28
L {w : expression w is satisfiabl e}
Time for n variables:
•Guess an assignment of the variables O(n)
•Check if this is a satisfying assignment O(n)
Total time:
Fall 2004
COMP 335
O(n)
29
L {w : expression w is satisfiabl e}
L NP
The satisfiability problem is an NP - Problem
Fall 2004
COMP 335
30
Observation:
P NP
Deterministic
Polynomial
Fall 2004
Non-Deterministic
Polynomial
COMP 335
31
Open Problem:
P NP ?
WE DO NOT KNOW THE ANSWER
Fall 2004
COMP 335
32
Open Problem:
P NP ?
Example: Does the Satisfiability problem
have a polynomial time
deterministic algorithm?
WE DO NOT KNOW THE ANSWER
Fall 2004
COMP 335
33
© Copyright 2026 Paperzz