Lecture 23: P v. NP
CSE 373: Data Structures and Algorithms
1
Announcements
EX 6 due Tomorrow
P4 due Wednesday
EC Survey 3 released later this evening
UW Class Survey Out Now
CSE 373 21 SP – CHAMPION
2
tinyurl.com/Summer373L23
Warm Up
CSE 373 20 SP – CHAMPION & CHUN
3
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
No
Yes
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
Yes
Yes
Worst case runtime?
Best case runtime?
In-practice runtime?
Stable?
In-place?
No
Yes
Selection Sort
Insertion Sort
Heap Sort
0/1 Knapsack Problem
Given a set of objects which have both a value and a weight (vi, wi) what is the maximum value we can obtain by selecting a subset of these objects such that the sum of the weights does not exceed the knapsacks given capacity?
4
Brute Force Recursive Solution
public int knapsack(int n, int cap, int[] w, int[] v) {
if (n == 0 || cap == 0) {
result = 0;
} else if (w[n] > cap) {
result = knapsack(n-1, cap, w, v);
} else {
int temp1 = knapsack(n-1, cap, w, v);
int temp2 = v[n] + knapsack(n-1, cap - w[n-1], w, v);
result = Math.max(temp1, temp2);
return result;
}
}
5
O(2n)
Recursive Solution is Exponential
6
Memoized DP Solution
public int knapsack(int n, int cap, int[] w, int[] v, int[][] memo) {
int result = memo[n][cap];
if (memo[n][cap] == NULL) {
if (n == 0 || cap == 0) {
result = 0;
} else if (w[n] > c) {
result = knapsack(n-1, cap);
} else {
int temp1 = knapsack(n-1, cap);
int temp2 = v[n] + knapsack(n-1, cap - w[n-1]);
result = Math.max(temp1, temp2);
memo[n][cap] = result;
}
}
return result;
}
7
O(n)
Administrivia
CSE 373 WI19 - KASEY CHAMPION
8
The 2 Sat Solver
CSE 373 SP 18 - KASEY CHAMPION
9
Topological Sort
Perform a topological sort of the following DAG
CSE 373 19 SP - KASEY CHAMPION
10
A
B
C
E
D
If a vertex doesn’t have any edges going into it, we add it to the ordering
If the only incoming edges are from vertices already in the ordering, then add to ordering
0
1
2
1
1
A
C
B
D
E
0
1
0
0
0
Given: a directed graph G
Find: an ordering of the vertices so all edges go from left to right.
Topological Sort
A directed graph without any cycles.
Directed Acyclic Graph (DAG)
Strongly Connected Components
CSE 373 19 SP - KASEY CHAMPION
11
A subgraph C such that every pair of vertices in C is connected via some path in both directions, and there is no other vertex which is connected to every vertex of C in both directions.
Strongly Connected Component
D
B
C
A
E
Why Find SCCs?
CSE 373 SP 18 - KASEY CHAMPION
12
D
C
F
B
E
A
K
J
1
3
4
2
Why Find SCCs?
CSE 373 SP 18 - KASEY CHAMPION
13
D
C
F
B
E
A
K
J
1
3
4
2
Why Must H Be a DAG?
CSE 373 SP 18 - KASEY CHAMPION
14
Takeaways
CSE 373 SP 18 - KASEY CHAMPION
15
A Longer Example
CSE 373 SP 18 - KASEY CHAMPION
16
Example Problem: Final Review
CSE 373 SP 18 - KASEY CHAMPION
17
*This is NOT how Kasey is making the final ;)
Given: A list of 2 preferences per student.
Find: A set of questions so every student gets at least one of their preferences (or accurately report no such question set exists).
Final Creation Problem
Review Creation: Take 1
CSE 373 SP 18 - KASEY CHAMPION
18
Review Creation: Take 2
Each student introduces new relationships for data:
CSE 373 SP 18 - KASEY CHAMPION
19
If we don’t include a big-O proof, can you still be happy?
If we do include a recurrence can you still be happy?
Yes! Big-O
NO recurrence
Yes! recurrence
NO Graph
NO Big-O
Yes!�Graph
NO Heaps
Yes! Heaps
Problem | YES | NO |
Big-O | X | |
Recurrence | | X |
Graph | | |
Heaps | | |
Problem | YES | NO |
Big-O |
| |
Recurrence | X |
|
Graph | X | |
Heaps | | |
Review Creation: Take 2
CSE 373 SP 18 - KASEY CHAMPION
20
NO recurrence
NO Big-O
True
False
CSE 373 SP 18 - KASEY CHAMPION
21
NO C
Yes�A
NO B
Yes�B
NO E
Final Creation: SCCs
YES! Topological Sort
CSE 373 SP 18 - KASEY CHAMPION
22
CSE 373 SP 18 - KASEY CHAMPION
23
NO C
Yes�A
NO D
Yes�B
NO E
YesC
NOA
Yes D
NOB
YesE
NO F
Yes�F
Yes�H
Yes�G
NO H
NO G
CSE 373 SP 18 - KASEY CHAMPION
24
NO C
Yes�A
NO D
Yes�B
NO E
YesC
NOA
Yes D
NOB
YesE
NO F
Yes�F
Yes�H
Yes�G
NO H
NO G
CSE 373 SP 18 - KASEY CHAMPION
25
NO C
Yes�A
NO D
Yes�B
NO E
YesC
NOA
Yes D
NOB
YesE
NO F
Yes�F
Yes�H
Yes�G
NO H
NO G
1
6
5
2
3
4
CSE 373 SP 18 - KASEY CHAMPION
26
NO C
Yes�A
NO D
Yes�B
NO E
YesC
NOA
Yes D
NOB
YesE
NO F
Yes�F
Yes�H
Yes�G
NO H
NO G
1
6
5
2
3
4
True
False
True
False
True
False
Making the Final
CSE 373 SP 18 - KASEY CHAMPION
27
Some More Context
CSE 373 SP 18 - KASEY CHAMPION
28
Given: A set of Boolean variables, and a list of requirements, each of the form:
variable1==[True/False] || variable2==[True/False]
Find: A setting of variables to “true” and “false” so that all of the requirements evaluate to “true”
2-Satisfiability (“2-SAT”)
Reductions
CSE 373 SP 18 - KASEY CHAMPION
29
2-Coloring
CSE 373 SP 18 - KASEY CHAMPION
30
B
D
E
A
C
B
D
E
A
C
2-Coloring
2-Coloring
CSE 373 SP 18 - KASEY CHAMPION
31
B
D
E
A
C
B
D
E
A
C
What are we doing?
CSE 373 SP 18 - KASEY CHAMPION
32
Reductions: Take 2
Using an algorithm for Problem B to solve Problem A.
Reduction (informally)
Weighted Graphs: A Reduction
CSE 373 SP 18 - KASEY CHAMPION
14
s
u
v
t
2
2
2
1
1
s
u
v
t
s
u
v
t
2
s
u
v
t
2
2
2
1
1
2
Transform Input
Unweighted Shortest Path
Transform Output
Reductions
CSE 373 SP 18 - KASEY CHAMPION
35
2-Coloring
2-Coloring
CSE 373 SP 18 - KASEY CHAMPION
36
Use our 2-SAT algorithm
to solve 2-Coloring
A Reduction
CSE 373 SP 18 - KASEY CHAMPION
37
CSE 373 SP 18 - KASEY CHAMPION
38
AisRed = True
BisRed = False
CisRed = True
DisRed = False
EisRed = True
B
D
E
A
C
B
D
E
A
C
(AisRed||BisRed)&&(!AisRed||!BisRed)
(AisRed||DisRed)&&(!AisRed||!DisRed)
(BisRed||CisRed)&&(!BisRed||!CisRed)
(BisRed||EisRed)&&(!BisRed||!EisRed)
(DisRed||EisRed)&&(!DisRed||!EisRed)
Transform Input
2-SAT Algorithm
Transform Output
P vs. NP
CSE 373 SP 18 - KASEY CHAMPION
39
Efficient
CSE 373 - 18AU
40
Decision Problems
CSE 373 - 18AU
41
Running Times
CSE 373 WI19 - KASEY CHAMPION
42
P Complexity Class
CSE 373 WI19 - KASEY CHAMPION
43
The decision version of all problems we’ve solved in this class are in P.
P is an example of a “complexity class”
A set of problems that can be solved under some limitations (e.g. with some amount of memory or in some amount of time).
NP Complexity Class
CSE 373 19 SP - KASEY CHAMPION
44
2-Coloring:
Can you color vertices of a graph red and blue so every edge has differently colored endpoints?
2-SAT:
Given a set of variables and a list of requirements:
(variable==[T/F] || variable==[T/F])
Find a setting of the variables to make every requirement true.
The spanning tree itself.
Verify by checking it really connects every vertex and its weight.
The assignment of variables.�Verify by checking each requirement.
The coloring.�Verify by checking each edge.
The set of all decision problems such that if the answer is YES, there is a proof of that which can be verified in polynomial time.
NP (stands for “nondeterministic polynomial”)
Decision Problems such that:
P vs. NP
CSE 373 WI19 - KASEY CHAMPION
45
Are P and NP the same complexity class?
That is, can every problem that can be verified in polynomial time also be solved in polynomial time.
P vs. NP
Hard Problems
CSE 373 WI19 - KASEY CHAMPION
46
NP-Completeness
CSE 373 WI19 - KASEY CHAMPION
47
The problem B is NP-complete if B is in NP and �for all problems A in NP, A reduces to B.
NP-complete
NP-Completeness
CSE 373 WI19 - KASEY CHAMPION
48
3-SAT is NP-complete
Cook-Levin Theorem (1971)
This sentence (and the proof of it) won Cook the Turing Award.
2-SAT vs. 3-SAT
CSE 373 WI19 - KASEY CHAMPION
49
Given: A set of Boolean variables, and a list of requirements, each of the form:
variable1==[True/False] || variable2==[True/False]
Find: A setting of variables to “true” and “false” so that all of the requirements evaluate to “true”
2-Satisfiability (“2-SAT”)
Given: A set of Boolean variables, and a list of requirements, each of the form:
variable1==[True/False]||variable2==[True/False]||variable3==[True/False]
Find: A setting of variables to “true” and “false” so that all of the requirements evaluate to “true”
3-Satisfiability (“3-SAT”)
2-SAT vs. 3-SAT
CSE 373 WI19 - KASEY CHAMPION
50
Given: A set of Boolean variables, and a list of requirements, each of the form:
variable1==[True/False] || variable2==[True/False]
Find: A setting of variables to “true” and “false” so that all of the requirements evaluate to “true”
2-Satisfiability (“2-SAT”)
2-SAT vs. 3-SAT
CSE 373 WI19 - KASEY CHAMPION
51
Given: A set of Boolean variables, and a list of requirements, each of the form:
variable1==[True/False]||variable2==[True/False]||variable3==[True/False]
Find: A setting of variables to “true” and “false” so that all of the requirements evaluate to “true”
3-Satisfiability (“3-SAT”)
Can we do the same for 3-SAT?
NO recurrence
NO Big-O
NP-Complete Problems
CSE 373 WI19 - KASEY CHAMPION
52
A lot of problems are NP-complete
Karp’s Theorem (1972)
NP-Complete Problems
By 1979, at least 300 problems had been proven NP-complete.�
Garey and Johnson put a list of all the NP-complete problems they could find in this textbook.
Took almost 100 pages to just list them all.
No one has made a comprehensive list since.
CSE 373 WI19 - KASEY CHAMPION
53
NP-Complete Problems
CSE 373 WI19 - KASEY CHAMPION
54
Examples
CSE 373 WI19 - KASEY CHAMPION
55
In P
NP-Complete
There are literally thousands of NP-complete problems.
And some of them look weirdly similar to problems we do know efficient algorithms for.
Examples
CSE 373 WI19 - KASEY CHAMPION
56
The electric company just needs a greedy algorithm to lay its wires.
Amazon doesn’t know a way to optimally route its delivery trucks.
In P
NP-Complete
Given a weighted graph, find a tour (a walk that visits every vertex and returns to its start) of minimum weight.
Dealing with NP-Completeness
CSE 373 WI19 - KASEY CHAMPION
57
Dealing with NP-Completeness
CSE 373 WI19 - KASEY CHAMPION
58
Given a weighted graph, find a tour (a walk that visits every vertex and returns to its start) of weight at most k.
Optimization version of Traveling Salesperson
Algorithm:
Find a minimum spanning tree.
Have the tour follow the visitation order of a DFS of the spanning tree.
Theorem: This tour is at most twice as long as the best one.
Why should you care about P vs. NP
CSE 373 WI19 - KASEY CHAMPION
59
Most computer scientists are convinced that P≠NP.
Why should you care about this problem?
It’s your chance for:
$1,000,000. The Clay Mathematics Institute will give $1,000,000 to whoever solves P vs. NP (or any of the 5 remaining problems they listed)
To get a Turing Award
Why should you care about P vs. NP
CSE 373 WI19 - KASEY CHAMPION
60
Most computer scientists are convinced that P≠NP.
Why should you care about this problem?
It’s your chance for:
$1,000,000. The Clay Mathematics Institute will give $1,000,000 to whoever solves P vs. NP (or any of the 5 remaining problems they listed)
To get a Turing Award the Turing Award renamed after you.
Why Should You Care if P=NP?
CSE 373 WI19 - KASEY CHAMPION
61
Why Should You Care if P=NP?
A world where P=NP is a very very different place from the world we live in now.
CSE 373 WI19 - KASEY CHAMPION
62
CSE 373 WI19 - KASEY CHAMPION
63
Why Should You Care if P≠NP?
CSE 373 WI19 - KASEY CHAMPION
64
To prove P≠NP we need to better understand the differences between problems.
-Why do some problems allow easy solutions and others don’t?
-What is the structure of these problems?
We don’t care about P vs NP just because it has a huge effect about what the world looks like.
We will learn a lot about computation along the way.
Why Should You Care if P≠NP?