Introduction to algorithms-Chapter 2-Greedy Algorithms

Source: Internet
Author: User

Chapter 2 greedy Algorithms

 

Conclusion: This chapter introduces the greedy algorithm based on activity selection, describes the basic idea of greedy policy, and introduces the Heman Encoding Based on greedy policy.

 

1.Basic Idea of greedy strategy

The greedy algorithm can be used to solve some optimization problems. The greedy algorithm expects to generate a global optimal solution through the local optimal selection. However, not all optimization problems can be solved through greedy algorithms.

Greedy Algorithm steps:

1) transform the optimization problem into a problem, that is, first make a choice, and then solve the remaining sub-problem.

2) prove that there is always an optimal solution for the original problem, which is obtained by greedy choice, thus demonstrating the security of greedy choice.

3) It indicates that, after making greedy choices, the remaining sub-problems have such a nature. That is, if we combine the optimal solution of the sub-problem with our greedy choice, we can get an optimal solution of the original problem.

 

Greedy choice: a global optimal solution can be achieved through local optimal (Greedy) selection. That is, when considering the selection, we only consider the best choice for the current problem rather than the results of the subproblem.

 

Two proofs are required to use the greedy algorithm:

1) prove that the greedy choice made by each step can generate a global optimal solution. In the proof, first examine a global optimal solution, and then prove to modify the solution, so that it uses greedy choice, this choice turns the original problem into a similar but smaller problem.

2) Assume that a greedy choice is made in the original problem and a subproblem is obtained. It is proved that, after combining the optimal solution of this subproblem with the greedy choice, an optimal solution of the original problem can be obtained.

 

Greedy algorithms and dynamic planning:

In dynamic planning, you must make choices at each step, but these choices depend on the subproblem solution. Therefore, dynamic planning is generally from bottom to bottom, from dealing with minor problems to major subproblems.

In the greedy algorithm, what we do is always the current seemingly best choice, and then solve the sub-problems that arise after the selection. The greedy policy is top-down, and the given problem instances are constantly reduced.

 

2.Heman Encoding

Prefix encoding: No encoding is the prefix of another encoding.

Huffman encoding is the greedy algorithm for optimal prefixes. In all possible merging of each step, select a merge with the smallest cost.

 

Set C to a set of n characters. For each character C in C, F [c] indicates the occurrence frequency of C. Obviously, the encoding length of a character with a high frequency should be as small as possible.

 

Pseudocode

Huffman (c)

N <-| c |

Q <-C

For I <-1 to n

Do allocate a new code Z

Left [Z] <-x <-EXTRACT-MIN (q)

Right [Z] <-Y <-EXTRACT-MIN (q)

F [Z] <-f [x] + F [y]

Insert (Q, Z)

Return EXTRACT-MIN (q)

 

Q is a minimum priority queue with the keyword F. It can be implemented using a minimal binary heap.

Complexity Analysis: O (nlgn)

 

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.