"Algorithm Diagram"--eighth chapter greedy algorithm

Source: Internet
Author: User

The eighth chapter greedy algorithm

1 Simple greedy algorithm

Each step takes the best approach and selects the local optimal solution at each step.

2 knapsack problem

In some cases, perfection is an excellent enemy. If you only need to find an approximate algorithm to solve the problem, the greedy algorithm is very good, because the implementation is easy, the result is quite close to the correct result.

Practice
8.1 You work in a furniture company, you need to send furniture to all parts of the country, you need to put the box on the truck. The size of each box is different, you need to make every possible use of the space of each truck, so how do you choose to load the truck box? Please design a greedy algorithm. Can the optimal solution be obtained by using this algorithm?

Choose the largest box that can be loaded into the truck, repeating it until it is no longer available, and the algorithm does not get the optimal solution.

8.2 You want to travel to Europe, the total itinerary is 7 days. For every tourist destination, you assign a value to it-how much you want to go there and estimate how long it will take. How do you maximize the value of this trip? Please design a greedy algorithm. Can the optimal solution be obtained by using this algorithm?

Continue to pick the most valuable activities that can be done in the rest of the time, knowing that the rest of the time will not be able to complete any activity. Similarly, this algorithm is not optimal for the solution.

3 Collection Overlay Issues

Let's say you've got a radio program for listeners in 50 states across the United States. To do this, you need to decide on which radio stations to broadcast. There is a cost to broadcast on every radio station, so you try to broadcast it on as few radio stations as possible. Each radio station covers a specific area, and the coverage area of the different radio stations may overlap.

Here's how:

① lists each possible set of radio stations, which is called the power set . A subset of the possible 2**n.

② in these sets, the smallest collection covering all 50 states of the United States is chosen.

The run time is O (2**n) because a possible subset has 2**n.

A greedy algorithm can be used to get a very close solution:

① elected such a radio station, which covers the largest number of non-covered states. It doesn't matter if there are duplicate states.

② repeat the first step until all the states are covered.

This is an approximate algorithm. The criteria for judging the approximate algorithm are as follows:

How fast is the ① speed?

The approximate solution obtained by ② is close to the optimal solution. In this example, the greedy algorithm runs at O (n**2)

"Algorithm Diagram"--eighth chapter greedy algorithm

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.