Python algorithm-greedy algorithm

Today, let's learn one of the five major python algorithms-greedy algorithm

basic introduction:

Greedy algorithm is also called greedy algorithm, which means that when solving the problem, first make the best choice currently, without considering the overall optimization, the algorithm obtains a local optimal solution in a certain sense

The greedy strategy used must be carefully analyzed whether it satisfies no aftereffect

The basic idea of ​​the greedy algorithm:
establish a mathematical model to describe the problem,
divide the problem to be solved into several sub
-problems, solve each sub-problem, and get the local optimal solution of the
sub-problem. Synthesize the local optimal solution of the sub-problem to a solution of the original problem

Problems with this algorithm: It is
not guaranteed that the final solution obtained is the best. It
cannot be used to find the maximum or minimum value. It
can only find the range of feasible solutions that meet certain constraints.

Problems with classroom scheduling:

Insert picture description here
When there is no conflict in class time, how to arrange more courses?
1. Choose the earliest class that ends, which is the first class in this classroom.
2. Next, you must choose the first class A class that starts after the end of a class. Similarly, if you choose to end the earliest class, this will be the second class in this classroom.
Repeating all the time will find the final answer.
Insert picture description here
In some cases, the greedy algorithm may not find the optimal solution, but it is very Close, sometimes you only need to find an algorithm that can roughly solve the problem.

set

A set is similar to a list, except that it cannot contain repeated elements.
Union means to merge the sets. Intersection means to find elements in both sets.
Difference means to remove elements from one set that appear in the other set.

NP complete problem

1. When the number of elements is small, the algorithm runs very fast, but as the number of elements increases, the speed becomes very slow
. 2. Problems involving "all combinations" are usually NP complete problems
. 3. The problem cannot be divided into small problems. All possible situations must be considered. This may be an NP complete problem
. 4. If the problem involves a sequence (such as the city sequence in the traveling salesman problem) and is difficult to solve, it may be an NP complete problem.
5. If the problem involves a collection (such as a collection of broadcast stations) and is difficult to solve, it may It is NP complete problem
6. If the problem can be converted to a set covering problem or traveling salesman problem, then it must be an NP complete problem

Guess you like

Origin blog.csdn.net/Layfolk_XK/article/details/108490468
Recommended