First, greedy algorithm introduction
The basic idea of the algorithm: from one of the initial solution of the problem to proceed step by step, according to an optimization measure, each step must ensure that the local optimal solution can be obtained. Each step considers only one data, and his selection should satisfy the conditions of local optimization. If the next data and part of the optimal solution are no longer feasible, the data is not added to the partial solution until all the data is enumerated, or the algorithm can no longer be added to stop. (Excerpt from Greedy algorithm _ Baidu Encyclopedia)
The simple and direct description means that each step chooses the local optimal solution, and the result is the global optimal solution.
II. Introduction: Set Coverage issues
Let's say you have a radio program that you want to hear from all the American States, so you need to decide which radio stations to broadcast. There is a cost to broadcast on every radio station, so you try to broadcast on as few radio stations as possible. The list of existing radio stations is as follows:
Each radio station covers a specific area, and the coverage area of the different radio stations may overlap.
How do you find the most broadcast Taiwan collection that covers all states? Here are the steps to resolve:
- Lists each possible set of radio stations, which is called the power set. There are 2n possible subsets.
- In these sets, the smallest collection covering all 50 states of the United States is chosen.
So the problem is, it takes a long time to compute a subset of each possible broadcast station.
We can try to use greedy algorithms.
Third, the implementation of the algorithm
Algorithm steps
- Elect such a radio station, that is, it covers the most non-covered states. Even if the radio station covers some of the covered states (that is, overlapping overrides), it doesn't matter.
- Repeat the first step until you have covered all the states.
This is a neighbor algorithm (approximation algorithm). The approximate algorithm can be considered when it takes too long to obtain the exact solution. The criteria for judging the approximate algorithm are as follows:
- How fast is the speed;
- The approximate solution obtained is close to the optimal solution.
So the greedy algorithm is a good choice, they are not only simple, but usually run fast. In this example, the greedy algorithm runs at O (N2), where n is the number of broadcast stations.
The code is as follows
#Create a list that contains the states you want to overwritestates_needed = Set (["MT","WA","or","ID","NV","ut","CA","AZ"])#passing in an array, being converted to a collectionStations={}stations["Kone"] = Set (["ID","NV","ut"]) stations["KTwo"] = Set (["WA","ID","MT"]) stations["Kthree"] = Set (["or","NV","CA"]) stations["Kfour"] = Set (["NV","ut"]) stations["kfive"] = Set (["CA","AZ"]) final_stations= Set ()#use a collection to store the final selected broadcast station whilestates_needed:best_station= None#store the broadcast stations that cover the largest number of non-covered statesstates_covered = set ()#A collection that contains all the non-covered states covered by the broadcast station forStation, statesinchStations.items ():#Iterate over each radio station and determine if it is the best broadcast stationCovered = states_needed & states#Calculate intersection ifLen (covered) > Len (states_covered):#Check whether the state of the broadcast station is more than Best_stationBest_station = Station#if more, set the Best_station to the current broadcast stationstates_covered =covered states_needed-= states_covered#Update states_neededFinal_stations.add (best_station)#add best_station to the final list of broadcast stations after the For loop endsPrint(final_stations)#Print Final_stations
Iv. Summary
- The greedy algorithm finds the local optimal solution and attempts to obtain the global optimal solution in this way.
- Greedy algorithm is easy to implement and run fast, it is a good approximate algorithm.
- Breadth-First search and Dijkstra algorithm are greedy algorithms.
V. Interpretation of the Code part
In the above algorithm, a piece of code is interesting
# Calculate intersection
It is used to perform related calculations between sets, which are described in the following set, intersection, and difference sets
- The union means merging the collections;
- Intersection means finding the elements in all two sets;
- A difference means that an element that appears in another collection is removed from one collection.
An example is shown below
>>> fruits = Set (["Avocado","Tomato","Banana"])>>> vegetables = Set (["Beets","Carrots","Tomato"])>>> Fruits | Vegetables#Compute and set{'Carrots','Tomato','Avocado','Beets','Banana'}>>> Fruits & Vegetables#Calculate intersection{'Tomato'}>>> Fruits-vegetables#Calculate Difference Sets{'Avocado','Banana'}>>> Vegetables-fruits{'Carrots','Beets'}
From this we can see that the collection is similar to a list, but cannot contain duplicate elements .
"Algorithm Diagram" the eighth chapter _ greedy Algorithm _ Set coverage problem