Grokking algorithms (Chapter 8) greedy algorithms

Greedy algorithm can get an approximate optimal solution. Approximate means: close to the most perfect value, but not the most perfect value

Give an example: The knapsack problem

Suppose you have a bag that can hold 35lbs of goods. From the items in the picture below, pick out the most valuable items and put them in your bag. At the same time, the weight cannot exceed 35lbs.

The steps to use the greedy algorithm are as follows

 If you use the greedy algorithm, you can only hold stereo in your bag, which is worth 3000, but the optimal solution is a laptop and a guitar, worth 3500. It can be seen that the greedy algorithm is not the optimal solution, but the most Optimal solution approach

The following are scenarios commonly used by greedy algorithms ( travel related)

 

In a word, when the cost of solving the optimal solution is very large, the greedy algorithm can be used to approximate the solution

As an example

First, let's look at the function of set. Set is to remove duplicate content in a list. From the figure below, we can see that duplicates 2 and 3 are removed.

The situation is described in the following figure:

The following code is used to complete this selection work, because 50 states are too many, so select a subset of states (area), each station in it covers a part of the area (states), you need to use the least station to cover all the states (area) )

states_needed = set(['mt', 'wa', 'or', 'id', 'nv', 'ut',
                    'ca', 'az'])
stations = {}
stations['kone'] = set(['id', 'nv', 'ut'])
stations['ktwo'] = set(['wa', 'id', 'mt'])
stations['kthree'] = set(['or', 'nv', 'ca'])
stations['kfour'] = set(['nv', 'ut'])
stations['kfve'] = set(['ca', 'az'])
 
final_stations = set()

while states_needed:
    best_station = None
    best_states = set()
    for station, states in stations.items():
        covered_states = states_needed & states
        if len(covered_states) > len(best_states):
            best_states = covered_states
            best_station = station
    final_stations.add(best_station)
    states_needed = states_needed-best_states
print (final_stations)

You can see that what is printed out is {'ktwo','kone','kthree','kfve'}

NP-complete problems

In the case of finding the station above, you calculate every possible solution and pick the smallest/shortest one. This situation is called NP-complete. In short, the problem with very high computational cost is called NP-complete.

to sum up

Guess you like

Origin blog.csdn.net/Gussss/article/details/96152797