You are on page 1of 35

Time Complexity

Instructor: Dr. Safdar Abbas

Book: Prof. Sipser-MIT


Slides: Prof. Busch - LSU 1
Consider a deterministic Turing Machine M
which decides a language L

2
For any string w the computation of M
terminates in a finite amount of transitions


Initial Accept
state or Reject w

3
Decision Time = #transitions


Initial Accept
state or Reject w

4
Consider now all strings of length n

TM (n ) = maximum time required to decide


any string of length n

5
TIME TM (n )

1 2 3 4  n 
STRING LENGTH

Max time to accept a string of length n


6
Time Complexity Class: TIME (T (n ))

All Languages decidable by a


deterministic Turing Machine
in time O (T (n ))

L1 L2
L3
7
Example: L1  {a b : n  0}
n

This can be decided in O (n ) time

TIME (n )

L1  {a b : n  0}
n

8
Other example problems in the same class

TIME (n )

L1  {a b : n  0}
n

{ab aba : n , k  0}
n

{b : n is even }
n

{b : n  3k }
n

9
Examples in class:

TIME (n ) 2

{a b : n  0}
n n

{ww : w  {a , b }}

10
Examples in class:
TIME (n ) 3

CYK algorithm
L2  { G ,w : w is generated by
context - free grammar G }

Matrix multiplication
L3  { M1 , M2, M3 : n  n matrices
and M1  M2  M3 }

11
Polynomial time algorithms: TIME (n )
k

constant k  0

Represents tractable algorithms:


for small k we can decide
the result fast

12
It can be shown:

TIME (n k 1 )

TIME (n ) k

13
The Time Complexity Class P

P  TIME (n k
)
k 0

Represents:
•polynomial time algorithms

•“tractable” problems
14
Class P
{a b }
n

{a b }
n n
{ww }

CYK-algorithm

Matrix multiplication

15
nk
Exponential time algorithms: TIME (2 )

Represent intractable algorithms:


Some problem instances
may take centuries to solve

16
Example: the Hamiltonian Path Problem

s t

Question: is there a Hamiltonian path


from s to t?

17
s t

YES!

18
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

19
The clique problem

Does there exist a clique of size 5?

20
The clique problem

Does there exist a clique of size 5?

21
Example: The Satisfiability Problem

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

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

Question: is the expression satisfiable?


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

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

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

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

Not satisfiable

24
L  {w : expression w is satisfiabl e}

nk
L TIME (2 )
exponential

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

L1
L3
L2

A Non-Deterministic Turing Machine


decides each string of length n
in time O (T (n ))
26
Non-Deterministic Polynomial time algorithms:

k
L  NTIME (n )

27
The class NP

NP  NTIME (n k
)
k 0

Non-Deterministic Polynomial time

28
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

29
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: O(n)


30
L  {w : expression w is satisfiabl e}

L  NP

The satisfiability problem is an NP - Problem

31
Observation:

P  NP

Deterministic Non-Deterministic
Polynomial Polynomial

32
Open Problem: P  NP ?

Example: Does the Satisfiability problem


have a polynomial time
deterministic algorithm?

WE DO NOT KNOW THE ANSWER


33
Is P = NP ?

Get US $ 1,000,000 prize for the first


correct solution.

It is one of the seven Millennium Prize


Problems selected by the Clay
Mathematics Institute

34
Merci Beaucoup!

35

You might also like