Very similar problems can have different complexity. Recall:
- P: a class of problems solvable in polynomial time. O( ) for some constant k.
- NP: a class of problems verifiable in polynomial time.
Hamiltonian cycle in a directed graph G(V, E) is a simple cycle that contains each vertex in V.
Determining whether a graph has a Hamiltonian cycle is NP-complete but verifying that a cycle is hamiltonian is easy.
- NP-complete: the problem is in NP and is as hard as any problem in NP.
If any NPC problem can be solved in polynomial time, then every problem in NP has a polynomial time solution.
Interval Scheduling
Requests 1, 2, . . . , n, single resource
s(i) start time, f(i) finish time, s(i) < f(i) (start time must be less than finish time for a request)
Two requests i and j are compatible if they don’t overlap, i.e., f(i) ≤ s(j) or f(j) ≤ s(i).
Goal: Select a compatible subset of requests of maximum size.
Claim: We can solve this by using a greedy algorithm. A greedy algorithm is a myopic algorithm that processes the input one piece at a time with no apparent look ahead.
Greedy Interval Scheduling
- Use a simple rule to select a request i
- Reject all requests incompatible with i.
- Repeat until all requests are processed.
Weighted Interval Scheduling
Each request i has weight w(i). Schedule subset of requests that are non-overlapping with maximum weight
Dynamic Programming
We can define our sub-problems as
Rx = {j ∈ R|s(j) ≥ x}
Here, R is the set of all requests.
If we set x = f(i), then Rx is the set of requests later than request i.
Total number of sub-problems = n (one for each request)
Only need to solve each subproblem once and memorize.
We try each request i as a possible first. If we pick a request as the first, then the remaining requests are .
Note that even though there may be requests compatible with i that are not in Rf(i), we are picking i as the first request, i.e., we are going in order.
opt(R) = max (w(i) + opt(Rf(i) )) 1≤i≤n Total running time is O(n2) since we need O(n) time to solve each sub-problem.