[Speaking algorithm small class] Greedy method-activity arrangement problem (proof of correctness)

Insert picture description hereAnother proof method
a_k represents the k-th activity, and s_k and f_k represent the start time and end time of activity k.
Insert picture description here

Event scheduling problem
There are n events, numbered 1,...,n respectively. During each activity, an item (resource) must always be occupied.
The start and end times of activity k are b_k and e_k respectively, and they are both non-negative integers.
Once the activity starts, it is not allowed to be interrupted until the end of the activity.
It is required to give an activity arrangement plan to maximize the number of activities that can be carried out. If there are multiple optimal solutions, output any one.
Solution
[1] Sort the activities {a_n} in ascending order of end time.
[2] Let E be the end time of the last selected activity, and its initial value is E=0.
[3] Starting from the first activity after sorting, loop the following parts:
For the i-th activity:
[3.1] If the start time of the activity b_i<E, then its time conflicts with the selected activity, so do not select.
[3.2] If the start time of the activity b_i≥E, select the activity and update E=e_i.
Prove that
this method is greedy.
Suppose the unexplored activities are A^'={a_m,a_(m+1),…,a_n }, m=1,2,…,n. With the operation of the above algorithm, a solution X will be constructed gradually. That is to say:
if X is to be the optimal solution, the next selected activity must be a_k, where m≤k≤n, a_k represents one of several activities with the same ending time, and its progress time is the same as that of the selected activity. The activities are not conflicting, and the end time is as early as possible.
Proof by contradiction. If a_k is not selected, then:
[1] Activities that ended earlier than a_k cannot be selected because there is a conflict with the time of the selected activity.
[2] The activity a_l that ends later than a_k can be selected. But:
let X'Is the set from the solution X [excluding the selected activities before a_l (excluding a_l)], then as the end time of a_l becomes later, |X ' | will decrease. In order to arrange as many activities as possible, even if X is the optimal solution, you can only choose a_k.

It is easy to find: the optimal solution obtained by using this algorithm will always select activity a_1, because a_1 is the first selected activity. Under the condition of examining and selecting in the order of ending time from morning to evening, it is impossible to compare with subsequent selections. Conflict of activities.

Guess you like

Origin blog.csdn.net/COFACTOR/article/details/110577930