Very similar problems can have very different complexity.
- P: class of problems solvable in polynomial time.
- NP: class of problems verifiable in polynomial time.
- NP-complete: problems is in NP and as hard as any problem in NP.
If any NP-complete problem can be solved in polynomial time, then every problem in NP has a polynomial time solution.
NP-complete problem is a subproblem of NP problem.
只可以用非确定性方法才能在多项式时间求解1。
Interval Scheduling
Request 1, 2, … , n, single resource
Goal: Select a compatible subset of requests of maximum size.
Claim: We can solve this using a greedy algorithm.
A greedy algorithm is a myopic algorithm that processes the input one piece at a time with no apprent look ahead.
Greedy Interval Scheduling
- Use a simple rule to select a request i i i.
- Reject all requests that are incompatible with i i i.
- Repeat until all requests are processed.
Rule: select request with earliest finish time, i.e., minimum f ( i ) f(i) f(i).
Prove
Skip
Weighted Interval Scheduling
Each request i i i has a weight w ( i ) w(i) w(i). Schedule subset of requests that are non-overlapping with maximum weight.
Dynamic Programming
We can define our subproblems as
R x = { j ∈ R ∣ s ( j ) ≥ x } \begin{aligned} R^x = \{j \in R|s(j) \geq x\} \end{aligned} Rx={
j∈R∣s(j)≥x}
Here, R R R is the set of all requests.
If we set x = f ( i ) x=f(i) x=f(i), then R x R^x Rx is the set of requests later than request i i i.
Total number of sub-problems = n = n =n (one for each request).
Only need to solve each sub-problem once and memorize.