Time Complexity

33
Fall 2004 COMP 335 1 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

description

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. Example:. Algorithm to accept a string :. Use a two-tape Turing machine Copy the on the second tape Compare the and. Time needed:. - PowerPoint PPT Presentation

Transcript of Time Complexity

Page 1: Time Complexity

Fall 2004 COMP 335 1

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

Page 2: Time Complexity

Fall 2004 COMP 335 2

Example: }0:{ nbaL nn

Algorithm to accept a string :w

•Use a two-tape Turing machine

•Copy the on the second tape

•Compare the and

a

a b

Page 3: Time Complexity

Fall 2004 COMP 335 3

|)(|wO•Copy the on the second tape

•Compare the and

a

a b

Time needed:

|)(|wO

Total time: |)(|wO

}0:{ nbaL nn

Page 4: Time Complexity

Fall 2004 COMP 335 4

For string of length

time needed for acceptance:

}0:{ nbaL nn

n

)(nO

Page 5: Time Complexity

Fall 2004 COMP 335 5

Language class: )(nDTIME

A Deterministic Turing Machine accepts each string of length in time

)(nDTIME

1L2L 3L

)(nOn

Page 6: Time Complexity

Fall 2004 COMP 335 6

)(nDTIME

}0:{ nba nn

}{ww

Page 7: Time Complexity

Fall 2004 COMP 335 7

In a similar way we define the class

))(( nTDTIME

)(nTfor any time function:

Examples: ),...(),( 32 nDTIMEnDTIME

Page 8: Time Complexity

Fall 2004 COMP 335 8

Example: The membership problem for context free languages

}grammar by generated is :{ GwwL

)( 3nDTIMEL (CYK - algorithm)

Polynomial time

Page 9: Time Complexity

Fall 2004 COMP 335 9

Theorem:

)( knDTIME

)( 1knDTIME

)()( 1 kk nDTIMEnDTIME

Page 10: Time Complexity

Fall 2004 COMP 335 10

Polynomial time algorithms: )( knDTIME

Represent tractable algorithms:

For small we can compute theresult fast

k

Page 11: Time Complexity

Fall 2004 COMP 335 11

)( knDTIMEP for all k

The class P

•All tractable problems

•Polynomial time

Page 12: Time Complexity

Fall 2004 COMP 335 12

P

CYK-algorithm}{ nnba

}{ww

Page 13: Time Complexity

Fall 2004 COMP 335 13

Exponential time algorithms: )2( nDTIME

Represent intractable algorithms:

Some problem instancesmay take centuries to solve

Page 14: Time Complexity

Fall 2004 COMP 335 14

Example: the Hamiltonian Problem

Question: is there a Hamiltonian path from s to t?

s t

Page 15: Time Complexity

Fall 2004 COMP 335 15

s t

YES!

Page 16: Time Complexity

Fall 2004 COMP 335 16

)2()!( nDTIMEnDTIMEL

Exponential time

Intractable problem

A solution: search exhaustively all paths

L = {<G,s,t>: there is a Hamiltonian path in G from s to t}

Page 17: Time Complexity

Fall 2004 COMP 335 17

Example: The Satisfiability Problem

Boolean expressions inConjunctive Normal Form:

ktttt 321

pi xxxxt 321

Variables

Question: is expression satisfiable?

Page 18: Time Complexity

Fall 2004 COMP 335 18

)()( 3121 xxxx

Satisfiable: 1,1,0 321 xxx

1)()( 3121 xxxx

Example:

Page 19: Time Complexity

Fall 2004 COMP 335 19

2121 )( xxxx

Not satisfiable

Example:

Page 20: Time Complexity

Fall 2004 COMP 335 20

e}satisfiabl is expression :{ wwL

)2( nDTIMELFor variables: n

Algorithm: search exhaustively all the possible binary values of the variables

exponential

Page 21: Time Complexity

Fall 2004 COMP 335 21

Non-Determinism

Language class: )(nNTIME

A Non-Deterministic Turing Machine accepts each string of length in time

)(nNTIME

1L2L 3L

)(nOn

Page 22: Time Complexity

Fall 2004 COMP 335 22

Example: }{wwL

Non-Deterministic Algorithmto accept a string :ww

•Use a two-tape Turing machine

•Guess the middle of the string and copy on the second tape

•Compare the two tapes

w

Page 23: Time Complexity

Fall 2004 COMP 335 23

|)(|wO

Time needed:

|)(|wO

Total time: |)(|wO

}{wwL

•Use a two-tape Turing machine

•Guess the middle of the string and copy on the second tape

•Compare the two tapes

w

Page 24: Time Complexity

Fall 2004 COMP 335 24

)(nNTIME

}{wwL

Page 25: Time Complexity

Fall 2004 COMP 335 25

In a similar way we define the class

))(( nTNTIME

)(nTfor any time function:

Examples: ),...(),( 32 nNTIMEnNTIME

Page 26: Time Complexity

Fall 2004 COMP 335 26

Non-Deterministic Polynomial time algorithms:

)( knNTIMEL

Page 27: Time Complexity

Fall 2004 COMP 335 27

)( knNTIMENP for all k

The class NP

Non-Deterministic Polynomial time

Page 28: Time Complexity

Fall 2004 COMP 335 28

Example: The satisfiability problem

Non-Deterministic algorithm:

•Guess an assignment of the variables

e}satisfiabl is expression :{ wwL

•Check if this is a satisfying assignment

Page 29: Time Complexity

Fall 2004 COMP 335 29

Time for variables: n

)(nO

e}satisfiabl is expression :{ wwL

Total time:

•Guess an assignment of the variables

•Check if this is a satisfying assignment )(nO

)(nO

Page 30: Time Complexity

Fall 2004 COMP 335 30

e}satisfiabl is expression :{ wwL

NPL

The satisfiability problem is an - ProblemNP

Page 31: Time Complexity

Fall 2004 COMP 335 31

Observation:

NPP

DeterministicPolynomial

Non-DeterministicPolynomial

Page 32: Time Complexity

Fall 2004 COMP 335 32

Open Problem: ?NPP

WE DO NOT KNOW THE ANSWER

Page 33: Time Complexity

Fall 2004 COMP 335 33

Example: Does the Satisfiability problem have a polynomial time deterministic algorithm?

WE DO NOT KNOW THE ANSWER

Open Problem: ?NPP