Algorithm Diagram - Greedy Algorithm

content:

  • How to deal with impossible tasks; problems without fast algorithms (NP-complete problems)
  • Learning is NP-complete, so as not to waste time looking for fast algorithms to solve them
  • Learn approximate algorithms with which to quickly find approximate solutions to NP-complete problems
  • Learning a Greedy Strategy - A Very Simple Problem Solving Strategy

8.1 Classroom Scheduling Problems

  Greedy Algorithm: Each step takes a never-optimal solution, and the final one is the global optimal solution.

Greedy Sufan doesn't work in every situation, but it's easy to implement.

8.2 The knapsack problem

  The greedy algorithm does not get the optimal solution, but it is very close.

In some cases, perfection is the enemy of goodness. Sometimes, you just need to find a single algorithm to solve the problem, and this is where the greedy algorithm comes in handy, it is easy to implement, and the results obtained are quite close to the optimal results.

8.3 Approximation Algorithms

  The greedy algorithm is an approximation algorithm that is used when it takes too long to obtain an exact solution.

  The criteria for judging the pros and cons of approximate algorithms are as follows:

  • how fast
  • how close the approximated solution is to the optimal solution

  Solve the broadcast station coverage problem using a greedy algorithm.

 1 # You pass an array in, and it gets converted to a set.
 2 states_needed = set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"])
 3 
 4 stations = {}
 5 stations["kone"] = set(["id", "nv", "ut"])
 6 stations["ktwo"] = set(["wa", "id", "mt"])
 7 stations["kthree"] = set(["or", "nv", "ca"])
 8 stations["kfour"] = set(["nv", "ut"])
 9 stations["kfive"] = set(["ca", "az"])
10 
11 final_stations = set()
12 
13 for station, states in stations.items():
14 #  print "states: ", states
15   print "station: ", station
16 print "\n"
17 
18 while states_needed:
19   best_station = None
20   states_covered = set()
21   for station, states in stations.items():#station is index, states is element
22     covered = states_needed & states  # get intersection
23     print "covered: ", covered
24     print "states_covered: ", states_covered
25     print "states: ", states
26     print "station: ", station
27     print "***********\n"
28     
29     if len(covered) > len(states_covered):
30       best_station = station
31       states_covered = covered
32 
33   states_needed -= states_covered
34   final_stations.add(best_station)
35 
36 print final_stations
set_covering.py

illustrate:

  1. The for loop in the code traverses the broadcasting stations to find the broadcasting station that can cover the most states;
  2. After the for loop ends, delete the covered states from the set and update the set of final solutions;
  3. If there are still uncovered states, continue to step 1 (in the second part, the found radio stations should be deleted);
  4. The search ends until there are no states to cover.

8.4 NP-complete problems

  1. Definition: A simple definition of an NP-complete problem is a problem that is notoriously difficult to solve, such as the traveling salesman problem and the set covering problem. Many people believe that there is no algorithm that can solve these problems quickly.
  2. How to identify:
  • The algorithm runs very fast when there are few elements, but becomes very slow with more elements
  • Problems involving "all combinations" are usually NP-complete
  • The problem cannot be divided into small problems, all possible cases must be considered, which may be NP-complete
  • If sets are involved and difficult to solve, it may be NP-complete
  • If the problem involves sets and is difficult to solve, it may be NP-complete
  • If the problem can be transformed into a set covering problem or a traveling salesman problem, it must be NP-complete

8.5 Summary

  • The greedy algorithm finds the local optimal solution in an attempt to obtain the global optimal solution in this way
  • For NP-complete problems, no fast solution has been found
  • When faced with NP-complete problems, the best practice is to use an approximation algorithm
  • The greedy algorithm is easy to implement, runs fast, and is a good approximation algorithm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324665681&siteId=291194637
Recommended