You are on page 1of 33

Time Complexity

1
Consider a decidable (recursive) language L
which is decided by a Turing Machine M
For any string w the computation of M
terminates (in an accept of reject state)
in a finite amount of transitions
Accept
or Reject

Time = #transitions followed
2
TM (n )

1 2 3 4  k 

{ Max time to accept a string


of length k }
3
Language class: TIME (T (n ))

TIME (T (n ))
L1 L3
L2

Languages for which there is


a deterministic Turing Machine
that decides each string of the
language within time T (n )
4
Examples:
TIME (n )

{a n : n  0 }

TIME (n ) 2

{a n b n : n  0 }

5
Example: The membership problem
for context free languages

L  { G ,w : w is generated by grammar G }

L TIME (n 3 ) (CYK - algorithm)

Polynomial time

6
Theorem: TIME (n k 1
) TIME (n )
k

k 1
DTIME (n )

k
DTIME (n )

7
Polynomial time algorithms: TIME (n ) k

For every constant k 0

Represent tractable algorithms:


For small k we can compute the
result fast

8
The class P

P   TIME (n ) k
for all k  0
k 0

•Polynomial time

•All tractable problems


9
P
CYK-algorithm n n
{a b }

 {ww}

10
nk
Exponential time algorithms: TIME (2 )

Represent intractable algorithms:


Some problem instances
may take centuries to solve

11
Example: the Hamiltonian Path Problem

s t

Question: is there a Hamiltonian path


from s to t? 12
s t

YES!

13
A solution: search exhaustively all paths

L = {<G,s,t>: there is a Hamiltonian path


in G from s to t}

nk
L TIME (n ! )  TIME (2 )

Exponential time

Intractable problem
14
The clique problem

15
The clique problem

16
Example: The Satisfiability Problem

Boolean expressions in
Conjunctive Normal Form:
t1  t2  t3    tk

ti  x1  x2  x3    x p
Variables

Question: is the expression satisfiable?


17
Example: ( x1  x2 )  ( x1  x3 )

Satisfiable: x1  0, x2  1, x3  1

( x1  x2 )  ( x1  x3 )  1

18
Example: ( x1  x2 )  x1  x2

Not satisfiable

19
L  {w : expression w is satisfiable}

nk
L TIME (2 )
exponential

Algorithm:
search exhaustively all the possible
binary values of the variables
20
Non-Determinism
Language class: NTIME (n)

NTIME (n)
L1 L3
L2

A Non-Deterministic Turing Machine


accepts each string of length n
in time O (n)
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 22


L  {ww}
Time needed:

•Use a two-tape Turing machine

•Guess the middle of the string O (| w |)


and copy w on the second tape

O (| w |)
•Compare the two tapes
Total time: O (| w |)
23
NTIME (n)

L  {ww}

24
In a similar way we define the class

NTIME (T (n))

for any time function: T (n)

2 3
Examples: NTIME (n ), NTIME (n ),...

25
Non-Deterministic Polynomial time algorithms:

k
L  NTIME ( n )

26
The class NP

k
NP   NTIME (n ) for all k

Non-Deterministic Polynomial time

27
Example: The satisfiability problem

L  {w : expression w is satisfiable}

Non-Deterministic algorithm:
•Guess an assignment of the variables

•Check if this is a satisfying assignment

28
L  {w : expression w is satisfiable}

Time for n variables:

•Guess an assignment of the variables O (n)

•Check if this is a satisfying assignment O (n)

Total time: O (n)


29
L  {w : expression w is satisfiable}

L  NP

The satisfiability problem is an NP - Problem

30
Observation:

P  NP

Deterministic Non-Deterministic
Polynomial Polynomial

31
Open Problem: P  NP ?

WE DO NOT KNOW THE ANSWER

32
Open Problem: P  NP ?

Example: Does the Satisfiability problem


have a polynomial time
deterministic algorithm?

WE DO NOT KNOW THE ANSWER


33

You might also like