SlideShare une entreprise Scribd logo
1  sur  6
Télécharger pour lire hors ligne
1
CS216: Program and Data Representation
University of Virginia Computer Science
Spring 2006 David Evans
Lecture 8:
Crash Course
in
Computational
Complexity
http://www.cs.virginia.edu/cs216 2UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Procedures and Problems
• So far we have been talking about
procedures (how much work is our
brute force subset sum algorithm?)
• We can also talk about problems:
how much work is the subset sum
problem?
What is a problem?
What does it mean to describe the
work of a problem?
3UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Problems and Solutions
• A problem defines a desired output
for a given input.
• A solution to a problem is a
procedure for finding the correct
output for all possible inputs.
• The time complexity of a problem
is the running time of the best
possible solution to the problem
4UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Subset Sum Problem
• Input: set of n positive integers, {w0,
…, wn-1}, maximum weight W
• Output: a subset S of the input set
such that the sum of the elements of
S ≤ W and there is no subset of the
input set whose sum is greater than
the sum of S and ≤ W
What is the time complexity of the
subset sum problem?
5UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Brute Force Subset Sum
Solution
def subsetsum (items, maxweight):
best = {}
for s in allPossibleSubsets (items):
if sum (s) <= maxweight 
and sum (s) > sum (best)
best = s
return best
Running time ∈ Θ(n2n)
What does this tell us about the time
complexity of the subset sum problem?
6UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Problems and Procedures
• If we know a procedure that is that is
Θ(f (n)) that solves a problem then we
know the problem is O (f(n)).
• The subset sum problem is in Θ(n2n)
since we know a procedure that
solves it in Θ(n2n)
• Is the subset sum problem in Θ(n2n)?
No, we would need to prove there is no
better procedure.
2
7UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Lower Bound
• Can we find an bound for the
subset sum problem?
• It is in (n) since we know we need
to at least look at every input
element
• Getting a higher lower bound is
tough
8UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
How much work is the
Subset Sum Problem?
• Upper bound: O (2n)
Try all possible subsets
• Lower bound: Ω (n)
Must at least look at every element
• Tight bound: Θ (?)
No one knows!
9UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Tractable/Intractable
0
200000
400000
600000
800000
1000000
1200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
logn n
nlogn n^2
n^3 2^n
Sequence Alignment
Subset Sum
“tractable”
“intractable”
I do nothing that a man of unlimited funds, superb physical
endurance, and maximum scientific knowledge could not do.
– Batman (may be able to solve intractable problems, but
computer scientists can only solve tractable ones for large n)
10UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Complexity Class P
“Tractable”
Class P: problems that can be
solved in polynomial time
O (nk) for some constant k.
Easy problems like sorting,
sequence alignment, simulating
the universe are all in P.
11UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Complexity Class NP
Class NP: problems that can be solved
in nondeterministic polynomial time
If we could try all possible solutions at
once, we could identify the solution in
polynomial time.
Alternately: If we had a magic guess-
correctly procedure that makes every
decision correctly, we could devise a
procedure that solves the problem in
polynomial time.
12UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Complexity Classes
Class P: problems that can be solved in
polynomial time (O(nk) for some constant
k): “myopic” problems like sequence
alignment, interval scheduling are all in P.
Class NP: problems that can be solved in
polynomial time by a nondeterministic
machine: includes all problems in P and
some problems possibly outside P like
subset sum
3
13UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Complexity Classes: Possible View
P
NP
Interval
Scheduling:
O(n log n)
Sequence
Alignment: O(n2)
Subset Sum:
O(2n) and Ω(n)
O(n)
How many problems
are in the O(n) class?
How many problems
are in P but not
in the O(n) class?
How many problems
are in NP but not
in P?
infinite
Either 0 or infinite!
infinite
14UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
P = NP?
• Is P different from NP: is there a
problem in NP that is not also in P
– If there is one, there are infinitely many
• Is the “hardest” problem in NP also in P
– If it is, then every problem in NP is also in P
• No one knows the answer!
• The most famous unsolved problem in
computer science and math
– Listed first on Millennium Prize Problems
15UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Problem Classes if P ⊂ NP:
P
NP
Interval
Scheduling:
O(n log n)
Sequence
Alignment: O(n2)
O(n)
Subset Sum:
O(2n) and Ω(n)
How many problems
are in NP but not
in P?
Infinite!
16UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Problem Classes if P = NP:
P
Interval
Scheduling:
O(n log n)
Sequence
Alignment: O(n2)
Subset Sum:
O(nk)
O(n)
How many problems
are in NP but not
in P?
0
17UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Distinguishing P and NP
• Suppose we identify the hardest
problem in NP - let’s call it Super
Arduous Task (SAT)
• Then deciding is P = NP should be
easy:
– Find a P-time solution to SAT ⇒ P = NP
– Prove there is no P-time solution to SAT
⇒ P ⊂ NP
18UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
The Satisfiability Problem (SAT)
• Input: a sentence in propositional
grammar
• Output: Either a mapping from
names to values that satisfies the
input sentence or no way
(meaning there is no possible
assignment that satisfies the input
sentence)
4
19UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
SAT
Example
SAT (a ∨ (b ∧ c) ∨ ¬b ∧ c)
→ { a: true, b: false, c: true }
→ { a: true, b: true, c: false }
SAT (a ∧ ¬a)
→ no way
Sentence ::= Clause
Clause ::= Clause1 ∨ Clause2 (or)
Clause ::= Clause1 ∧ Clause2 (and)
Clause ::= ¬Clause (not)
Clause ::= ( Clause )
Clause ::= Name
20UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
The 3SAT Problem
• Input: a sentence in propositional
grammar, where each clause is a
disjunction of 3 names which may be
negated.
• Output: Either a mapping from names
to values that satisfies the input
sentence or no way (meaning there
is no possible assignment that
satisfies the input sentence)
21UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
3SAT / SAT
Is 3SAT easier or harder than
SAT?
It is definitely not harder than
SAT, since all 3SAT problems
are also SAT problems. Some
SAT problems are not 3SAT
problems.
22UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
3SAT
Example
3SAT ( (a ∨ b ∨ ¬ c)
∧ (¬a ∨ ¬ b ∨ d)
∧ (¬a ∨ b ∨ ¬ d)
∧ (b ∨ ¬ c ∨ d ) )
→ { a: true, b: false, c: false, d: false}
Sentence ::= Clause
Clause ::= Clause1 ∨ Clause2 (or)
Clause ::= Clause1 ∧ Clause2 (and)
Clause ::= ¬Clause (not)
Clause ::= ( Clause )
Clause ::= Name
23UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
NP Completeness
• Cook and Levin proved that 3SAT was
NP-Complete (1971): as hard as the
hardest problem in NP
• If any 3SAT problem can be
transformed into an instance of
problem Q in polynomial time, than
that problem must be no harder than
3SAT: Problem Q is NP-hard
• Need to show in NP also to prove Q is
NP-complete.
24UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Subset Sum is NP-Complete
• Subset Sum is in NP
–Easy to check a solution is correct?
• 3SAT can be transformed into
Subset Sum
–Transformation is complicated, but
still polynomial time.
–A fast Subset Sum solution could
be used to solve 3SAT problems
5
25UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Problem Classes if P ≠ NP:
P
Interval
Scheduling:
Θ(n log n)
Sequence
Alignment: O(n2)
Subset Sum
O(n)
How many problems
are in the Θ(n) class?
How many problems
are in P but not
in the Θ(n) class?
How many problems
are in NP but not
in P?
infinite
infinite
infinite
NP
3SAT
NP-Complete
Note the
NP-
Complete
class is a
ring – others
are circles
26UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
NP-Complete Problems
• Easy way to solve by trying all
possible guesses
• If given the “yes” answer, quick (in P)
way to check if it is right
– Assignments of values to names (evaluate
logical proposition in linear time)
– Subset – check if it has correct sum
• If given the “no” answer, no quick way
to check if it is right
– No solution (can’t tell there isn’t one)
27UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Traveling Salesperson Problem
–Input: a graph of cities and roads
with distance connecting them and a
minimum total distant
–Output: either a path that visits each
with a cost less than the minimum,
or “no”.
• If given a path, easy to check if it
visits every city with less than
minimum distance traveled
28UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Graph (Map) Coloring Problem
–Input: a graph of nodes with edges
connecting them and a minimum
number of colors
–Output: either a coloring of the nodes
such that no connected nodes have
the same color, or “no”.
If given a coloring, easy to check if it no
connected nodes have the same color,
and the number of colors used.
29UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Minesweeper Consistency
Problem
–Input: a position of n
squares in the game
Minesweeper
–Output: either a
assignment of bombs to
squares, or “no”.
• If given a bomb assignment,
easy to check if it is consistent.
30UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Pegboard Problem
6
31UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Pegboard Problem
- Input: a configuration of n pegs on
a cracker barrel style pegboard
- Output: if there is a sequence of
jumps that leaves a single peg,
output that sequence of jumps.
Otherwise, output false.
If given the sequence of jumps, easy
(O(n)) to check it is correct. If not,
hard to know if there is a solution.
32UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Drug Discovery Problem
–Input: a set of
proteins, a desired
3D shape
–Output: a sequence
of proteins that
produces the shape
(or impossible)
If given a sequence, easy (not really –
this may actually be NP-Complete too!) to
check if sequence has the right shape.
Caffeine
33UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Is it ever useful to be
confident that a problem is
hard?
34UVa CS216 Spring 2006 - Lecture 8: Computational Complexity
Charge
• PS3 can be turned in up till 4:50pm
Friday: turn in to Brenda Perkins in
CS office (she has folders for each
section)
• Exam 2 will be handed out
Wednesday
– Send me email questions you want
reviewed

Contenu connexe

Tendances

Noisy optimization --- (theory oriented) Survey
Noisy optimization --- (theory oriented) SurveyNoisy optimization --- (theory oriented) Survey
Noisy optimization --- (theory oriented) SurveyOlivier Teytaud
 
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's ClassesIIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's ClassesSOURAV DAS
 
4 litvak
4 litvak4 litvak
4 litvakYandex
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian Rebedea
 
Orthogonal Vector Spaces
Orthogonal Vector Spaces Orthogonal Vector Spaces
Orthogonal Vector Spaces Sohaib H. Khan
 
A Szemeredi-type theorem for subsets of the unit cube
A Szemeredi-type theorem for subsets of the unit cubeA Szemeredi-type theorem for subsets of the unit cube
A Szemeredi-type theorem for subsets of the unit cubeVjekoslavKovac1
 
What are free particles in quantum mechanics
What are free particles in quantum mechanicsWhat are free particles in quantum mechanics
What are free particles in quantum mechanicsbhaskar chatterjee
 
Density theorems for anisotropic point configurations
Density theorems for anisotropic point configurationsDensity theorems for anisotropic point configurations
Density theorems for anisotropic point configurationsVjekoslavKovac1
 
A Szemerédi-type theorem for subsets of the unit cube
A Szemerédi-type theorem for subsets of the unit cubeA Szemerédi-type theorem for subsets of the unit cube
A Szemerédi-type theorem for subsets of the unit cubeVjekoslavKovac1
 
Solution set 3
Solution set 3Solution set 3
Solution set 3慧环 赵
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy methodMaryJacob24
 
On an Optimal control Problem for Parabolic Equations
On an Optimal control Problem for Parabolic EquationsOn an Optimal control Problem for Parabolic Equations
On an Optimal control Problem for Parabolic Equationsijceronline
 

Tendances (17)

Noisy optimization --- (theory oriented) Survey
Noisy optimization --- (theory oriented) SurveyNoisy optimization --- (theory oriented) Survey
Noisy optimization --- (theory oriented) Survey
 
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's ClassesIIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
 
Lecture5
Lecture5Lecture5
Lecture5
 
4 litvak
4 litvak4 litvak
4 litvak
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
Orthogonal Vector Spaces
Orthogonal Vector Spaces Orthogonal Vector Spaces
Orthogonal Vector Spaces
 
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
 
Kuhn munkres algorithm
Kuhn munkres algorithmKuhn munkres algorithm
Kuhn munkres algorithm
 
A Szemeredi-type theorem for subsets of the unit cube
A Szemeredi-type theorem for subsets of the unit cubeA Szemeredi-type theorem for subsets of the unit cube
A Szemeredi-type theorem for subsets of the unit cube
 
What are free particles in quantum mechanics
What are free particles in quantum mechanicsWhat are free particles in quantum mechanics
What are free particles in quantum mechanics
 
Density theorems for anisotropic point configurations
Density theorems for anisotropic point configurationsDensity theorems for anisotropic point configurations
Density theorems for anisotropic point configurations
 
Back tracking
Back trackingBack tracking
Back tracking
 
A Szemerédi-type theorem for subsets of the unit cube
A Szemerédi-type theorem for subsets of the unit cubeA Szemerédi-type theorem for subsets of the unit cube
A Szemerédi-type theorem for subsets of the unit cube
 
Solution set 3
Solution set 3Solution set 3
Solution set 3
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
 
Ma2002 1.19 rm
Ma2002 1.19 rmMa2002 1.19 rm
Ma2002 1.19 rm
 
On an Optimal control Problem for Parabolic Equations
On an Optimal control Problem for Parabolic EquationsOn an Optimal control Problem for Parabolic Equations
On an Optimal control Problem for Parabolic Equations
 

En vedette

open messenger-messaging-for-livelihood-development
open messenger-messaging-for-livelihood-developmentopen messenger-messaging-for-livelihood-development
open messenger-messaging-for-livelihood-developmentdigitalvisionoxfam
 
Class website
Class websiteClass website
Class websiteMHReunion
 
Matkailu, vastuullisuus ja yhteistyötalous
Matkailu, vastuullisuus ja yhteistyötalousMatkailu, vastuullisuus ja yhteistyötalous
Matkailu, vastuullisuus ja yhteistyötalousMinna-Maari Harmaala
 
Vídeo director nuevas herram. 2011_revisado
Vídeo director nuevas herram. 2011_revisadoVídeo director nuevas herram. 2011_revisado
Vídeo director nuevas herram. 2011_revisadoaropaifo
 
Alumnae Five-Year Plan
Alumnae Five-Year PlanAlumnae Five-Year Plan
Alumnae Five-Year PlanMHReunion
 
Orca Yachts 34 m and Tiira 500 gt
Orca Yachts 34 m and Tiira 500 gtOrca Yachts 34 m and Tiira 500 gt
Orca Yachts 34 m and Tiira 500 gtBrunobouckaert
 
Communications
CommunicationsCommunications
CommunicationsMHReunion
 

En vedette (8)

open messenger-messaging-for-livelihood-development
open messenger-messaging-for-livelihood-developmentopen messenger-messaging-for-livelihood-development
open messenger-messaging-for-livelihood-development
 
Plant parts
Plant partsPlant parts
Plant parts
 
Class website
Class websiteClass website
Class website
 
Matkailu, vastuullisuus ja yhteistyötalous
Matkailu, vastuullisuus ja yhteistyötalousMatkailu, vastuullisuus ja yhteistyötalous
Matkailu, vastuullisuus ja yhteistyötalous
 
Vídeo director nuevas herram. 2011_revisado
Vídeo director nuevas herram. 2011_revisadoVídeo director nuevas herram. 2011_revisado
Vídeo director nuevas herram. 2011_revisado
 
Alumnae Five-Year Plan
Alumnae Five-Year PlanAlumnae Five-Year Plan
Alumnae Five-Year Plan
 
Orca Yachts 34 m and Tiira 500 gt
Orca Yachts 34 m and Tiira 500 gtOrca Yachts 34 m and Tiira 500 gt
Orca Yachts 34 m and Tiira 500 gt
 
Communications
CommunicationsCommunications
Communications
 

Similaire à Lecture8

Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Ziyauddin Shaik
 
Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6Traian Rebedea
 
Np completeness
Np completenessNp completeness
Np completenessRajendran
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Techglyphs
 
A Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT ProblemA Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT ProblemValerie Felton
 
Skiena algorithm 2007 lecture19 introduction to np complete
Skiena algorithm 2007 lecture19 introduction to np completeSkiena algorithm 2007 lecture19 introduction to np complete
Skiena algorithm 2007 lecture19 introduction to np completezukun
 
2020 겨울방학 정기스터디 2주차
2020 겨울방학 정기스터디 2주차2020 겨울방학 정기스터디 2주차
2020 겨울방학 정기스터디 2주차Moonki Choi
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problemsJyotsna Suryadevara
 
teteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxm
teteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxmteteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxm
teteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxmzoobiarana76
 
lecture 27
lecture 27lecture 27
lecture 27sajinsc
 

Similaire à Lecture8 (20)

Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems
 
DAA.pdf
DAA.pdfDAA.pdf
DAA.pdf
 
DAA.pdf
DAA.pdfDAA.pdf
DAA.pdf
 
Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6
 
Teori pnp
Teori pnpTeori pnp
Teori pnp
 
Fine Grained Complexity
Fine Grained ComplexityFine Grained Complexity
Fine Grained Complexity
 
Np completeness
Np completenessNp completeness
Np completeness
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2
 
A Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT ProblemA Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT Problem
 
class23.ppt
class23.pptclass23.ppt
class23.ppt
 
Introduction
IntroductionIntroduction
Introduction
 
Skiena algorithm 2007 lecture19 introduction to np complete
Skiena algorithm 2007 lecture19 introduction to np completeSkiena algorithm 2007 lecture19 introduction to np complete
Skiena algorithm 2007 lecture19 introduction to np complete
 
2020 겨울방학 정기스터디 2주차
2020 겨울방학 정기스터디 2주차2020 겨울방학 정기스터디 2주차
2020 겨울방학 정기스터디 2주차
 
P versus NP
P versus NPP versus NP
P versus NP
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
AA ppt9107
AA ppt9107AA ppt9107
AA ppt9107
 
teteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxm
teteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxmteteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxm
teteuueieoeofhfhfjffkkkfkfflflflhshssnnvmvvmvv,v,v,nnxmxxm
 
Complexity theory
Complexity theory Complexity theory
Complexity theory
 
lecture 27
lecture 27lecture 27
lecture 27
 
NP completeness
NP completenessNP completeness
NP completeness
 

Lecture8

  • 1. 1 CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity http://www.cs.virginia.edu/cs216 2UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Procedures and Problems • So far we have been talking about procedures (how much work is our brute force subset sum algorithm?) • We can also talk about problems: how much work is the subset sum problem? What is a problem? What does it mean to describe the work of a problem? 3UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Problems and Solutions • A problem defines a desired output for a given input. • A solution to a problem is a procedure for finding the correct output for all possible inputs. • The time complexity of a problem is the running time of the best possible solution to the problem 4UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Subset Sum Problem • Input: set of n positive integers, {w0, …, wn-1}, maximum weight W • Output: a subset S of the input set such that the sum of the elements of S ≤ W and there is no subset of the input set whose sum is greater than the sum of S and ≤ W What is the time complexity of the subset sum problem? 5UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Brute Force Subset Sum Solution def subsetsum (items, maxweight): best = {} for s in allPossibleSubsets (items): if sum (s) <= maxweight and sum (s) > sum (best) best = s return best Running time ∈ Θ(n2n) What does this tell us about the time complexity of the subset sum problem? 6UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Problems and Procedures • If we know a procedure that is that is Θ(f (n)) that solves a problem then we know the problem is O (f(n)). • The subset sum problem is in Θ(n2n) since we know a procedure that solves it in Θ(n2n) • Is the subset sum problem in Θ(n2n)? No, we would need to prove there is no better procedure.
  • 2. 2 7UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Lower Bound • Can we find an bound for the subset sum problem? • It is in (n) since we know we need to at least look at every input element • Getting a higher lower bound is tough 8UVa CS216 Spring 2006 - Lecture 8: Computational Complexity How much work is the Subset Sum Problem? • Upper bound: O (2n) Try all possible subsets • Lower bound: Ω (n) Must at least look at every element • Tight bound: Θ (?) No one knows! 9UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Tractable/Intractable 0 200000 400000 600000 800000 1000000 1200000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 logn n nlogn n^2 n^3 2^n Sequence Alignment Subset Sum “tractable” “intractable” I do nothing that a man of unlimited funds, superb physical endurance, and maximum scientific knowledge could not do. – Batman (may be able to solve intractable problems, but computer scientists can only solve tractable ones for large n) 10UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Complexity Class P “Tractable” Class P: problems that can be solved in polynomial time O (nk) for some constant k. Easy problems like sorting, sequence alignment, simulating the universe are all in P. 11UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Complexity Class NP Class NP: problems that can be solved in nondeterministic polynomial time If we could try all possible solutions at once, we could identify the solution in polynomial time. Alternately: If we had a magic guess- correctly procedure that makes every decision correctly, we could devise a procedure that solves the problem in polynomial time. 12UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Complexity Classes Class P: problems that can be solved in polynomial time (O(nk) for some constant k): “myopic” problems like sequence alignment, interval scheduling are all in P. Class NP: problems that can be solved in polynomial time by a nondeterministic machine: includes all problems in P and some problems possibly outside P like subset sum
  • 3. 3 13UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Complexity Classes: Possible View P NP Interval Scheduling: O(n log n) Sequence Alignment: O(n2) Subset Sum: O(2n) and Ω(n) O(n) How many problems are in the O(n) class? How many problems are in P but not in the O(n) class? How many problems are in NP but not in P? infinite Either 0 or infinite! infinite 14UVa CS216 Spring 2006 - Lecture 8: Computational Complexity P = NP? • Is P different from NP: is there a problem in NP that is not also in P – If there is one, there are infinitely many • Is the “hardest” problem in NP also in P – If it is, then every problem in NP is also in P • No one knows the answer! • The most famous unsolved problem in computer science and math – Listed first on Millennium Prize Problems 15UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Problem Classes if P ⊂ NP: P NP Interval Scheduling: O(n log n) Sequence Alignment: O(n2) O(n) Subset Sum: O(2n) and Ω(n) How many problems are in NP but not in P? Infinite! 16UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Problem Classes if P = NP: P Interval Scheduling: O(n log n) Sequence Alignment: O(n2) Subset Sum: O(nk) O(n) How many problems are in NP but not in P? 0 17UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Distinguishing P and NP • Suppose we identify the hardest problem in NP - let’s call it Super Arduous Task (SAT) • Then deciding is P = NP should be easy: – Find a P-time solution to SAT ⇒ P = NP – Prove there is no P-time solution to SAT ⇒ P ⊂ NP 18UVa CS216 Spring 2006 - Lecture 8: Computational Complexity The Satisfiability Problem (SAT) • Input: a sentence in propositional grammar • Output: Either a mapping from names to values that satisfies the input sentence or no way (meaning there is no possible assignment that satisfies the input sentence)
  • 4. 4 19UVa CS216 Spring 2006 - Lecture 8: Computational Complexity SAT Example SAT (a ∨ (b ∧ c) ∨ ¬b ∧ c) → { a: true, b: false, c: true } → { a: true, b: true, c: false } SAT (a ∧ ¬a) → no way Sentence ::= Clause Clause ::= Clause1 ∨ Clause2 (or) Clause ::= Clause1 ∧ Clause2 (and) Clause ::= ¬Clause (not) Clause ::= ( Clause ) Clause ::= Name 20UVa CS216 Spring 2006 - Lecture 8: Computational Complexity The 3SAT Problem • Input: a sentence in propositional grammar, where each clause is a disjunction of 3 names which may be negated. • Output: Either a mapping from names to values that satisfies the input sentence or no way (meaning there is no possible assignment that satisfies the input sentence) 21UVa CS216 Spring 2006 - Lecture 8: Computational Complexity 3SAT / SAT Is 3SAT easier or harder than SAT? It is definitely not harder than SAT, since all 3SAT problems are also SAT problems. Some SAT problems are not 3SAT problems. 22UVa CS216 Spring 2006 - Lecture 8: Computational Complexity 3SAT Example 3SAT ( (a ∨ b ∨ ¬ c) ∧ (¬a ∨ ¬ b ∨ d) ∧ (¬a ∨ b ∨ ¬ d) ∧ (b ∨ ¬ c ∨ d ) ) → { a: true, b: false, c: false, d: false} Sentence ::= Clause Clause ::= Clause1 ∨ Clause2 (or) Clause ::= Clause1 ∧ Clause2 (and) Clause ::= ¬Clause (not) Clause ::= ( Clause ) Clause ::= Name 23UVa CS216 Spring 2006 - Lecture 8: Computational Complexity NP Completeness • Cook and Levin proved that 3SAT was NP-Complete (1971): as hard as the hardest problem in NP • If any 3SAT problem can be transformed into an instance of problem Q in polynomial time, than that problem must be no harder than 3SAT: Problem Q is NP-hard • Need to show in NP also to prove Q is NP-complete. 24UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Subset Sum is NP-Complete • Subset Sum is in NP –Easy to check a solution is correct? • 3SAT can be transformed into Subset Sum –Transformation is complicated, but still polynomial time. –A fast Subset Sum solution could be used to solve 3SAT problems
  • 5. 5 25UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Problem Classes if P ≠ NP: P Interval Scheduling: Θ(n log n) Sequence Alignment: O(n2) Subset Sum O(n) How many problems are in the Θ(n) class? How many problems are in P but not in the Θ(n) class? How many problems are in NP but not in P? infinite infinite infinite NP 3SAT NP-Complete Note the NP- Complete class is a ring – others are circles 26UVa CS216 Spring 2006 - Lecture 8: Computational Complexity NP-Complete Problems • Easy way to solve by trying all possible guesses • If given the “yes” answer, quick (in P) way to check if it is right – Assignments of values to names (evaluate logical proposition in linear time) – Subset – check if it has correct sum • If given the “no” answer, no quick way to check if it is right – No solution (can’t tell there isn’t one) 27UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Traveling Salesperson Problem –Input: a graph of cities and roads with distance connecting them and a minimum total distant –Output: either a path that visits each with a cost less than the minimum, or “no”. • If given a path, easy to check if it visits every city with less than minimum distance traveled 28UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Graph (Map) Coloring Problem –Input: a graph of nodes with edges connecting them and a minimum number of colors –Output: either a coloring of the nodes such that no connected nodes have the same color, or “no”. If given a coloring, easy to check if it no connected nodes have the same color, and the number of colors used. 29UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Minesweeper Consistency Problem –Input: a position of n squares in the game Minesweeper –Output: either a assignment of bombs to squares, or “no”. • If given a bomb assignment, easy to check if it is consistent. 30UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Pegboard Problem
  • 6. 6 31UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Pegboard Problem - Input: a configuration of n pegs on a cracker barrel style pegboard - Output: if there is a sequence of jumps that leaves a single peg, output that sequence of jumps. Otherwise, output false. If given the sequence of jumps, easy (O(n)) to check it is correct. If not, hard to know if there is a solution. 32UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Drug Discovery Problem –Input: a set of proteins, a desired 3D shape –Output: a sequence of proteins that produces the shape (or impossible) If given a sequence, easy (not really – this may actually be NP-Complete too!) to check if sequence has the right shape. Caffeine 33UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Is it ever useful to be confident that a problem is hard? 34UVa CS216 Spring 2006 - Lecture 8: Computational Complexity Charge • PS3 can be turned in up till 4:50pm Friday: turn in to Brenda Perkins in CS office (she has folders for each section) • Exam 2 will be handed out Wednesday – Send me email questions you want reviewed