Time Complexity
-
Upload
hamish-medina -
Category
Documents
-
view
20 -
download
0
description
Transcript of 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
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
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
Fall 2004 COMP 335 4
For string of length
time needed for acceptance:
}0:{ nbaL nn
n
)(nO
Fall 2004 COMP 335 5
Language class: )(nDTIME
A Deterministic Turing Machine accepts each string of length in time
)(nDTIME
1L2L 3L
)(nOn
Fall 2004 COMP 335 6
)(nDTIME
}0:{ nba nn
}{ww
Fall 2004 COMP 335 7
In a similar way we define the class
))(( nTDTIME
)(nTfor any time function:
Examples: ),...(),( 32 nDTIMEnDTIME
Fall 2004 COMP 335 8
Example: The membership problem for context free languages
}grammar by generated is :{ GwwL
)( 3nDTIMEL (CYK - algorithm)
Polynomial time
Fall 2004 COMP 335 9
Theorem:
)( knDTIME
)( 1knDTIME
)()( 1 kk nDTIMEnDTIME
Fall 2004 COMP 335 10
Polynomial time algorithms: )( knDTIME
Represent tractable algorithms:
For small we can compute theresult fast
k
Fall 2004 COMP 335 11
)( knDTIMEP for all k
The class P
•All tractable problems
•Polynomial time
Fall 2004 COMP 335 12
P
CYK-algorithm}{ nnba
}{ww
Fall 2004 COMP 335 13
Exponential time algorithms: )2( nDTIME
Represent intractable algorithms:
Some problem instancesmay take centuries to solve
Fall 2004 COMP 335 14
Example: the Hamiltonian Problem
Question: is there a Hamiltonian path from s to t?
s t
Fall 2004 COMP 335 15
s t
YES!
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}
Fall 2004 COMP 335 17
Example: The Satisfiability Problem
Boolean expressions inConjunctive Normal Form:
ktttt 321
pi xxxxt 321
Variables
Question: is expression satisfiable?
Fall 2004 COMP 335 18
)()( 3121 xxxx
Satisfiable: 1,1,0 321 xxx
1)()( 3121 xxxx
Example:
Fall 2004 COMP 335 19
2121 )( xxxx
Not satisfiable
Example:
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
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
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
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
Fall 2004 COMP 335 24
)(nNTIME
}{wwL
Fall 2004 COMP 335 25
In a similar way we define the class
))(( nTNTIME
)(nTfor any time function:
Examples: ),...(),( 32 nNTIMEnNTIME
Fall 2004 COMP 335 26
Non-Deterministic Polynomial time algorithms:
)( knNTIMEL
Fall 2004 COMP 335 27
)( knNTIMENP for all k
The class NP
Non-Deterministic Polynomial time
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
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
Fall 2004 COMP 335 30
e}satisfiabl is expression :{ wwL
NPL
The satisfiability problem is an - ProblemNP
Fall 2004 COMP 335 31
Observation:
NPP
DeterministicPolynomial
Non-DeterministicPolynomial
Fall 2004 COMP 335 32
Open Problem: ?NPP
WE DO NOT KNOW THE ANSWER
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