Discussion on algorithms (1) how to prove that greedy algorithms are the optimal using exchange argument

Source: Internet
Author: User

Liu zitao

Here we mainly introduce a method to prove that the greedy algorithm is the best: Exchange Argument (I do not know how to translate it to Chinese and Exchange parameters? It sounds awkward, not like the name of a method ~ O (Clerk □clerk) o)

The main idea of Exchange Argument is to assume that an optimal algorithm is the closest to our greedy algorithm, and then Exchange one step (or element) of the two algorithms ), A new optimal algorithm is obtained. At the same time, this algorithm is closer to our greedy algorithm than the previous optimal algorithm, so as to obtain contradictions. The original proposition is true.

Here is a more formal explanation:

Steps:

Step 0: Describe greedy algorithm.

Step 1: Assume that O is the most similar to A (assuming that the first k Steps of O and A are the same, and the first k + 1 is different, usually this critical element is the most important) optimal Algorithm

Step 2: [Key] modify the algorithm O (Exchange Argument to Exchange an element in A and O) to obtain the new algorithm O'

Step 3: prove that o' is feasible, that is, O' is right.

Step 4: prove that o' is at least the same as O, that is, O' is also the best

Step 5: Get the contradiction, because o' is more similar to A than O.

Pass.

Of course, the above step also has a variant, as shown below:

Step 0: Describe greedy algorithm.

Step 1: Assume that O is an optimal algorithm (arbitrary)

Step 2: Find A difference between O and. (Of course, the difference here can be that an element is no longer A in O, or the order of A pair is different from that in O. Based on the specific question)

Step 3: Exchange, and then the algorithm O obtained by argue does not have to be O-poor.

Step 4: there are a total of Polynomial entries such as Argue. Then, I use exchange Polynomial to eliminate all the differences and ensure that the quality of the algorithm is no worse than that of O. This means that A is as good as an O. Because O is selected by arbitrary, A is optimal.

Certificate completion

The following are examples:

Maximum Cardinality Disjoint Interval Problem

Problem description: give some time segment set T = {(a1, b1) (a2, b2 ),..., (an, bn)} to find the subset S with the largest number of elements. The time segment of each element in the subset is not crossover.

Greedy Algorithm: select the smallest bi in all interval values, add (ai, bi) to S, and delete (ai, bi) from T, at the same time, delete the interval with (ai, bi) in T, find the smallest bj in T, and loop through the above operations until nothing can be added.

Prove that the Greedy Algorithm mentioned above is optimal.

Next we will use the first proof step to prove it.

Our Greedy Algorithm is recorded as A. If A is not optimal, there must be an O, and O is the optimal Algorithm closest to, the most similar is that it is the same as the previous K-1 of O and A, and the K is different.

Assume that for A, the k option of A is (ai, bi), and the K option of O is (aj, bj ). From the definition of A, we can continue to bi <= bj.

Now we construct an O, O = O-(aj, bj) + (ai, bi ).

1) Obviously, O is a solution to this problem, that is, intervals in O does not overlap.

In O, the intervals before (ai, bi) is the same as that in A, so the previous part does not overlap. The intervals after (ai, bi) is the same as that in O, so there is no overlap. At the same time, bi <= bj, so (ai, bi) will not overlap adjacent to it, so all intervals in o do not overlap.

2) O is an optimal solution because the number of intervals is the same as that of O.

To sum up, we have found an optimal solution O, which has K intervals with A, which is in conflict with our premise that there is A maximum of K-1, A is optimal. Pass.

Example: Scheduling with Deadline

Problem description: there are a series of tasks {a1, a2 ,..., an}, each task needs to run {p1, p2,... on the cpu ,..., pn} units time, the cpu is not preemptible, that is, once the task gets the cpu, it runs until it completes, {c1, c2 ,..., cn} is the completion time of each task. What we need to minimize now is (c1 + c2 +... + Cn)/n. All tasks are release together.

Greedy Algorithm: select the task with the smallest running time to run each time.

Prove that the Greedy Algorithm mentioned above is optimal.

We also use the first verification step.

Assuming that A is not optimal, there must be an optimal O, which is the closest to A, and the tasks they selected are the same.

As follows:

No. 1 2 3... K .........

A ........... Ai .. Aj .....

O ........... Aj .... Ai...

According to the definition of A, we know that pi <= pj.

Now let's construct O. Of course, O is to exchange the ai and aj elements in O, that is

No. 1 2 3... K ..... T...

A ........... Ai .. Aj .....

O ........... Aj .... Ai...

O ........... Ai .... Aj...

For the entire completion time, we can give the calculation formula, such as b1, b2 ,..., bn is a scheduling sequence (bi is the index of the task, that is, {bi} is a permutation of the index of all tasks), that is, the task execution sequence is a _ {bi }, a _ {b2 },..., a _ {bn }. For example, if there are only five tasks, a1, a2, a3, a4, and a5. {Bi} = {2, 1, 3, 5, 4}, that is, the execution sequence of the task is a2, a1, a3, a5, a4. (It is uncomfortable to enter mathematical formulas in a blog ~)

We can still use the five examples above. For a2, a1, a3, a5, and a4, we know that the total time is actually the time of five a2, that is, the time of p2 and four A_1, that is, p1, and so on. Therefore, the total time is 5 * p2 + 4 * p1 + 3 * p3 + 2 * p5 + p4.

Well, with the concept of total computing time above, we will calculate the total time of O and O respectively. In fact, we need to compare who is big and who is small, generally, you only need to compare their differences, that is, Time (O)-Time (O ). At the same time, it is clear that their difference is only caused by aj and ai, while others have not changed. We know that in O, aj is the k-th choice, ai is the t-choice, and in O, ai is the k-choice, aj is t selected, so Time (O)-Time (O) = (n-k + 1) * pj + (n-t + 1) * pi-(n-k + 1) * pi-(n-t + 1) * pj = k * (pj-pi)-t * (pj-pi) = (pi-pj) * (k-t)> = 0, that is, we get Time (O)> = Time (O), which means we find an O, he is optimal. At the same time, O and A have K common elements, so they come to A conflict. Pass.

Example: Kruskal Algorithm for Minimum Spanning Tree (Minimum Spanning Tree)

Problem description: For the edge set E, first sort the cost of each edge, from small to large, and then construct an MST using the following method. Each time you select the smallest side of the cost, add the edge to our MST. If an edge causes a loop in our existing MST, discard the edge and repeat the above operation. (I feel like my expression is getting more and more awkward. I don't know what's going on. Let's look at it here.Http://en.wikipedia.org/wiki/Kruskals_algorithm)

Next we will use the second proof method to prove that the Kruskal Algorithm is optimal.

Assume that our graph is G (V, E) and there is an optimal algorithm O. The MST generated by graph is T1, and the tree generated by Kruskal is T2. In this case, because T2! = T1 (if it is equal to our Kruskal, it will be the best, so we don't need to prove it), so at least one side e, e is in T1, and e is not in T2. E is our starting point. As you can imagine, if we remove e from T1, T1 will become two parts: Ta and Tb, we know that vertices in {Ta} V in {Tb} = V (that is, vertices in Ta and vertices in the upper Tb are the point set V in G), so in T2, there must be an edge f (f! = E), one point of f is in {Ta point}, and the other point of f is in {Tb point }. According to our Greedy algorithm Kruskal, we did not select e because e caused a loop in our figure. For further consideration, there is a loop because we chose f first, which means cost (f) <= cost (e ). (Well, this is our focus. Laugh at the O ~)

Obviously, we are considering creating a new tree, T3. For T3 = E (T1)-e + f, that is, the edge in T3 is all the edges in T1 except e, and edge f is added.

1) Obviously, we know that T3 is a spanning tree, and f connects two parts separated by removing e.

2) Obviously, T3 cost is <= T1 cost. Because cost (f) <= cost (e), and others remain unchanged. That is, T3 is the MST.

Therefore, the same structure above is used to construct a tree that is MST and is closer to T2 than T1. (A common edge is added)

We know that T1 and T2 have n different sides. Through the above steps, we can perform n transformations to obtain T2 and cost <= T1. Therefore, t2 is the MST. Pass.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.