Lecture 22
Lecture 22
• Homework 6 is posted
– Due next Wednesday
– No partners
Algorithm Design Techniques
• Greedy
– Shortest path, minimum spanning tree, …
• Divide and Conquer
– Divide the problem into smaller subproblems,
solve them, and combine into the overall solution
– Often done recursively
– Quick sort, merge sort are great examples
• Dynamic Programming
– Brute force through all possible solutions, storing solutions to
subproblems to avoid repeat computation
• Backtracking
– A clever form of exhaustive search
Backtracking: Idea
• Backtracking is a technique used to solve problems with a large
search space, by systematically trying and eliminating possibilities.
• A standard example of backtracking would be going through a
maze.
– At some point, you might have two options of which direction to go:
Portion A
Junction
Portion B
Backtracking
o n
ti
One strategy would be to try going through Junc
Portion A of the maze.
If you get stuck before you find your Portion B
way out, then you "backtrack" to the
junction.
Portion A
At this point in time you know that Portion A
will NOT lead you out of the maze,
so you then start searching in Portion B
Backtracking
• Clearly, at a single junction you
could have even more than 2
choices.
dead end
?
dead end
dead end
?
start ? ? dead end
dead end
?
success!
Backtracking
• Dealing with the maze:
– From your start point, you will iterate through each possible
starting move.
– From there, you recursively move forward.
– If you ever get stuck, the recursion takes you back to where
you were, and you try the next possible move.
O(nlog n) ?
O(2n) ? non-polynomial
time
O(n!) ?
The Class P (polynomial time)
Binary Search
Dijkstra’s Algorithm
Breadth-First Search
P Sorting Algorithms
NP (Nondeterministic Polynomial Time)
Hamilton Cycle
NP Sudoku
Binary Search
SAT
P Dijkstra’s Algorithm
Breadth-First Search …
Sorting Algorithms
…
The P versus NP problem
3x3x3
Sudoku
3x3x3
Sudoku
4x4x4
Sudoku
4x4x4
Sudoku
nxnxn
Sudoku
P vs NP problem
nxnxn
The P versus NP problem (informally)
Breadth-First Search
P Sorting Algorithms
NP
The class of all sets that
can be verified in
Hamilton Cycle polynomial time.
NP Sudoku
Binary Search
SAT
P Dijkstra’s Algorithm
Breadth-First Search …
Sorting Algorithms
…
Sudoku
Input: n x n x n sudoku instance
AND NOT
The Set “SAT”
SAT = { all satisfiable circuits C }
AND
Verifying Membership
G HAM?
G Sudoku?
G SAT?
Fact: P NP
Recall: The algorithm doesn’t have to find the proof; it just needs to be
able to verify that it is a “correct” proof.
Summary: P versus NP
NP: “proof of membership” in a set can be verified in
polynomial time.
Fact: P NP
Question: Does NP P ?
i.e. Does P = NP?
People generally believe P ≠ NP, but no proof yet
Why Care?
NP Contains Lots of Problems
We Don’t Know to be in P
Classroom Scheduling
Packing objects into bins
Scheduling jobs on machines
Finding cheap tours visiting a subset of cities
Finding good packet routings in networks
Decryption
…
How do I do that?
It may take a long time!
Also, what if I forgot one of the sets in NP?
How could we prove that NP = P?
Sudoku Clique
SAT Independent-Set
3-Colorability HAM
These problems are all “polynomial-time equivalent”
i.e., each of these can be reduced to any of the others
in polynomial time