First, greedy algorithm
Definition: An algorithm is a greedy algorithm, if it is through some small steps to a solution, and at each step according to local circumstances select a decision, so that some of the main indicators are optimized.
Second, interval scheduling problem
1. Question: We have a set of requirements {,......, N}, the first requirement corresponds to a start time s (i), and an end time F (i). If no two requirements overlap in time, we say that the subset of requirements is compatible.
2. Objective: To find a maximum compatible subset O.
3. Algorithm:
The initial order R is a set of all requirements, set A to null
while (| r| > 0)
Select an early-ending requirement
Add I to a
Remove all requirements that are incompatible with I in R
End
Returns collection A as a collection of accepted requirements
Algorithm rule: The rule of this greedy algorithm is that each step chooses an early-ending demand and releases resources as soon as possible.
4. Verify the correctness of the algorithm:
Suppose O is the optimal solution, because the optimal solution may have multiple, so we just need to prove | a|=| O|, which proves that set a and set o contain the same number of requirements. Obviously, A,o are compatible, and neither of the two needs in a,o will overlap.
Algorithm thought: The proof is mainly to find out such a recognition, our greedy algorithm "lead" in this optimal solution O. We compare the partial solution of the greedy algorithm construction with the optimal solution o initial section, and prove that the greedy algorithm is better in one step at a time.
Suppose that a = {i1,i2,......, ik}, o = {j1,j2,......, JM}, we want to prove k=m, and we assume that the requirements in a are sorted by the order in which they are added to a, and that the requirements in O are sorted in the natural order of the corresponding intervals. Existing:
For any I, there is f (IR) <= s (ir+1)
For any J, there is F (JR) <= S (jr+1)
The present mathematical inductive method proves k=m
1) R=1, apparently with F (I1) <= F (J1)
Because, the greedy algorithm each step is chooses the earliest end interval, therefore has F (I1) <= F (J1)
2) Assuming that step r-1 has, F (ir-1) <= F (jr-1)
3) Step R, Proof f (ir-1) <= F (jr-1)
Because O is compatible, therefore: F (jr-1) <= S (JR)
Also because: F (ir-1) <= F (jr-1)
So: F (ir-1) <= f (jr-1) <= S (JR)
Both: F (ir-1) <= S (JR)
According to the above greedy algorithm, in the section R step of the JR is still in R, and the greedy algorithm of step R is to select an early end of the interval IR in R, so there is: f (IR) <= F (JR)
Summary: For any R, there is f (IR) <= F (JR)
Now with contradiction proof m=k, both a is the optimal solution:
Assuming that a is not an optimal solution, there is a requirement jk+1 in O, which begins after JK, and because F (IK) <= f (JK), so jk+1 is compatible with I1,i2,......, IK, since the requirement also begins after IK, so jk+1 in R And the greedy algorithm knows that r is empty at the end of the algorithm and therefore contradictory. So a is one of the best solutions.
The proof of the solution of the greedy algorithm-interval scheduling problem