























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























Outline
Today:
P, NP, and NP-Complete
If there’s an algorithm to solve a problem that runs in polynomial time, the problem is said to be in the set P
If the outcome of an algorithm to solve a problem can be verified in polynomial time, the problem is said to be in the set NP (non-deterministic polynomial, the “non-determinism” refers to the outcome of the algorithm, not the verification)
There is a set of problems in NP for which if there’s a polynomial solution to one there will be a polynomial solution to all The set is called NP-Complete
Examples of NP-Complete Problems
Hamiltonian Cycle Problem
Traveling Salesman Problem
0/1 Knapsack Problem
Graph Coloring Problem : can you color a graph using k ≥ 3 colors such that no adjacent vertices have the same color?
A 2-Approximation for a Special Case of TSP
If distances satisfy the triangle inequality , i.e., for edges (u, v), (v, w), (u, w) in G : C(u, v) + C(v, w) ≥ C(u, w), and G is a complete graph , we can build a 2-approximate TSP
k-approximate means that the result is off by a factor of k from the optimal
2-approximate TSP:
Running time: O(|E| log |V |) for MST + O(|V |) for Euler = O(|E| log |V |) as opposed to O(|V | ∗ |V |!) by brute force
2-Approximate Proof
Let:
Claim: T is at most 2 times longer than Topt
Proof:
So C(T ) ≤ 2 C(Topt)
Dynamic Programming
Origin of name (Bellman 1957): programming : planning, decision making by a tabular method dynamic : multi-stage, time-varying process
Computing the Fibonacci Sequence (contd)
What is a Fibonacci sequence? f 0 = 0; f 1 = 1; fn = fn− 1 + fn− 2 , n ≥ 2
Recursive implementation:
int rfib(int n) { // assume n >= 0 return (n <= 1? n : rfib(n-1)+rfib(n-2)); } Running time: Ω((^32 )n) [Preiss 3.4.3]
Iterative version: int ifib(int n) { // assume n >= 2 int i, f[n]; f[0] = 0; f[1] = 1; for (i = 2 to n) { f[i] = f[i-1]+f[i-2]; } return f[n]; } Running time: Θ(n) [Preiss 14.3.2, 14.4.1]
Computing the Fibonacci Sequence (contd)
Why is the recursive version so slow? The number of computations grows exponentially! Each rfib(i), i < n − 1 computed more than once Tree size grows almost 2 n Actually the number of base case computations in computing fn is fn Since fn > (^32 )n−^1 (see Preiss Thm. 3.9), complexity is Ω((^32 )n)
Why is the iterative version so fast? Instead of recomputing duplicated subproblems, it saves their results in an array and simply looks them up as needed
Can we design a recursive algorithm that similary look up results of duplicated subproblem?
Example of Problems Solved Using Dynamic Programming
0/1 Knapsack Problem (Preiss 14.1.2, 14.2.5, handouts)
You are a poor Mayan trader, carying wares on your back, in a tattered knapsack, back and forth between Chichen Itza and Palenque
Each item has both a weight and a profit
The tattered knapsack cannot carry more than its capacity or it will rip
Choose a set of items that doesn’t rip the knapsack while maximizing your profit
How to Solve It?
Brute-Force:
Branch-and-Bound: what are the bounding factor(s)?
Greedy:
Dynamic-Programming:
0/1 Knapsack Brute Force Solution
Exhaustively enumerate all feasible solution and pick one with the highest total profit
Running time:
0/1 Knapsack Greedy Solution
Put item into knapsack one by one, no removal of item already chosen
Possible greedy criterion:
Optimal solution not guaranteed (see Preiss Tbl 14.1)
0/1 Knapsack Dynamic Programming Solution
Let n = 3, w = { 5 , 10 , 20 }, and p = { 50 , 60 , 140 }, C = 30
Solution table P [i][cj], cj step increments to C: pi wi i 0 5 10 15 20 25 30 50 5 1 0 50 50 50 50 50 50 60 10 2 0 50 110 110 110 110 140 20 3 0 50
Can the problem be decomposed into overlapping subproblems?
Is optimal solution to problem guaranteed to consist only of optimal solutions to subproblems?