Greedy & Dynamic Programming of Data Structures and Algorithms

        One: Thinking

        1. One morning, the company leader comes to you to solve a problem. Tomorrow, the company will have N meetings of the same level that need to use the same conference room. Now I give you the start and end times of these N meetings. How can you arrange it so that the conference room can be used? Maximum use? That is, arrange the most meetings? As for movies, they are definitely the ones with the highest ticket price and admission rate. Comprehensive algorithm

        2. Double Eleven is coming soon. The goddess in Little C’s mind added N items to the shopping cart. Suddenly she won a prize and could clear out 5,000 yuan of items in the shopping cart (no change allowed). Each item was only If she can buy one, how should she choose the items to maximize the use of the winning amount? If there are multiple optimal combinations, you only need to give one. Hey, now the goddess comes to ask you, what should you do? (dynamic programming)

        2: Greedy Algorithm

        Concept: Greedy algorithm is also called greedy algorithm. When solving a certain problem, it always makes the greatest benefit at hand. In other words, it only cares about the immediate situation and ignores the overall situation, so it is a local optimal solution. Core point: deducing the global optimum through local optimum .

        Example:

        1. One morning, the company leader comes to you to solve a problem. Tomorrow, the company will have N meetings of the same level that need to use the same conference room. Now I give you the start and end times of these N meetings. How can you arrange it so that the conference room can be used? Maximum use? That is, arrange the most meetings?

         How can we be greedy now? This is the greedy strategy we choose:,

         1.1 Select the shortest time: 1-3, 2~4, 3~5, 4~6

        1.2 Sort by end time from small to large : first add the first one to the list of meetings we can have. After that, as long as the start time is greater than our previous end time, we can open it (the code is as follows)

package tx;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 贪心算法:1.某天早上公司领导找你解决一个问题,明天公司有N个同等级的会议需要使用同一个会议室,
 * 	现在给你这个N个会议的开始和结束时间,你怎么样安排才能使会议室最大利用?即安排最多场次的会议?
 *
 * 	策略:按结束时间从小到大排序:首先把第一个加入我们可以开会的列表。之后只要开始时间是大于我们上一个的结束时间的就可以开
 * 	核心:排序
 */
class Metting implements Comparable<Metting> {

	int meNum; // 编号
	int startTime; // 开始时间
	int endTime; // 结束时间
	
	public Metting(int meNum, int startTime, int endTime) {
		super();
		this.meNum = meNum;
		this.startTime = startTime;
		this.endTime = endTime;
	}

	public int compareTo(Metting o) {
		if (this.endTime > o.endTime)
			return 1;
		return -1;
	}

	@Override
	public String toString() {
		return "Metting [meNum=" + meNum + ", startTime=" + startTime
				+ ", endTime=" + endTime + "]";
	}

}

public class MettingTest {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		List<Metting> mettings = new ArrayList<Metting>();
		int n = cin.nextInt();	//n个会议
		for (int i = 0 ;i < n; i++){
			int start = cin.nextInt();
			int end = cin.nextInt();
			Metting metting = new Metting(i+1, start, end);
			mettings.add(metting);
		}

		mettings.sort(null);
		int curTime = 0;		//当前的时间,从一天的0点开始,如果领导要求从8点开始 那curTime=8
		for(int i = 0 ; i < n; i ++){
			Metting metting = mettings.get(i);
			if(metting.startTime >= curTime){		//会议的开始时间比我们当前的要大 表示可以开
				System.out.println(metting.toString());
				curTime = metting.endTime;
			}
		}
	}
}

         2.1 The core idea of ​​greedy algorithm

        The routine of the greedy algorithm: there must be a sorting . Huffman coding, greedy algorithm, compression algorithm. shortest path

        The greedy algorithm does not obtain the overall optimal solution for all problems. The key is the selection of the greedy strategy . The selected greedy strategy must have no aftereffects, that is, the previous process of a certain state will not affect the subsequent state, but only affects the current state. related.

         The two most important points of the greedy algorithm are: greedy strategy and sorting

The global optimal solution can be obtained through the local optimal solution

Generally, the following problems can be solved by greedy algorithm:

                1. There is a limit value for a certain problem, and there is an expected best result. Usually some of them are selected from certain data to achieve the best result.

                2. Generally there will be a sorting to find out the ones with the greatest contribution.

                3. Give an example to see if greed can be solved. Generally used in task scheduling, teacher scheduling and other systems. In fact, using greedy algorithms to solve problems does not always give the optimal solution.

        Three: Dynamic programming

        Classic question

        Backpack problem A thief went to a store to steal. He had a backpack with a capacity of 5kg. Now he has the following items (the items cannot be divided into pieces, and there is only one item). How should the thief get the maximum value?

5kg bag

thing:

Money: 6 10 12

Kg:1  2   4

Idea: We divide the 5kg bag into 1kg, and calculate 1kg like this. The table inside shows the maximum amount of money that can be contained under the current weight . The number series in the table represent the items to be loaded.

1kg

2kg

3 kg

4kg

5kg

Add item 1

6

6

6

6

6

Added item 2

6

10

10+6=16

10+6=16

16

Added item 3

6

10

16

16

18

The first item : The bag can only hold 1kg of items, so the price is 6

Second item: 

        When the bag currently has a capacity of 1kg, we found that item 2 could not be loaded. So how much should we take? Is it just a maximum charge of 1kg when picking up items?

        When the bag is 2kg, we find that item 2 can be packed in, and we can get 10 yuan at this time. When item 1 came in before, the maximum 2kg was 6, then we must choose the larger one of 10, not 6. At this time, the bag There are 0kg left to load.

        When the bag is 3kg, we can still hold this item 2, get 10 pieces, and there is still 1kg left in the bag.

10+1kg can be loaded.

Third item:

        When the bag is 4kg, item 3 can be transferred in and 12 yuan is obtained, leaving 0kg in the bag.

        I found that I can still get 16 without loading item 3.

        When the bag is 5kg, item 3 can be transferred in, and 12 yuan is obtained, leaving 1kg left in the bag. Then if you install item 3, you can get 12+6=18 yuan.

        I found that if I didn't install item 3, I could get 16, which was smaller than 18, so I decided to install it.

                                (Illustration: Dividing the value by 10 is the question above)

                Code

package tx;

public class Dp {

	public static void main(String[] args) {
		
		int value [] ={60,100,120};
		int weigth[] = {10,20,40};	//购物车那个问题 只需要一个价值就行了,重量都都没有。
		
		int w = 50;  //代表我可以装的数量
		int n = 3; //代表三个物品
		int dp[][] = new int[n+1][w+1];		//n表示是物品,w表示重量,初始化全是0
		
		for(int i = 1; i<= n; i++){	//每次加的物品
			for(int cw = 1 ; cw <= w ; cw ++){		//分割的背包
				if(weigth[i - 1] <= cw){		//表示这个物品可以装进去
					dp[i][cw] = Math.max(
							value[i-1] + dp[i-1][cw-weigth[i-1]],
							dp[i-1][cw]
							);
				}else{
					dp[i][cw] = dp[i-1][cw];	//不能装
				}
			}
		}
		System.out.println(dp[n][w]);
		
	}
}

Four: Comparison between Donggui and Greedy        

        Greedy only cares about the current situation and does not care about the future, but Donggui is different. Each iteration is based on the previous optimal solution. Therefore, the optimal solution can always be found by dynamic regression, but not necessarily greedy. This is also the shortcoming of the greedy algorithm, but everyone has seen that the time complexity of dynamic regression is O(n*m) and greedy is O(nlogn ), so the greedy algorithm is efficient. If there are too many sub-problems in dynamic regression, it is easy to fail to calculate the result, and problems that can be used in dynamic regression can often be solved partly, or even a large part, by greedy. Therefore, if the requirements in the actual project are not particularly strict, I suggest using the greedy algorithm to find the optimal solution. In fact, we do not need to guarantee 100% accuracy in many cases, as long as we can be as accurate as possible. Greedy is exactly in line with this rule.

        Five: Shopping cart code implementation

package tx;

public class CardDp {

	public static void main(String[] args) {
		
		int weigth[] = {1,2,3,4,5,9};	//购物车那个问题 只需要一个价值就行了,重量都都没有。
		
		int w = 8;
		int n = 6;
		int dp[][] = new int[n+1][w+1];		//n表示是物品,w表示重量,初始化全是0
		
		for(int i = 1; i<= n; i++){	//每次加的物品
			for(int cw = 1 ; cw <= w ; cw ++){		//分割的背包
				if(weigth[i - 1] <= cw){		//表示这个物品可以装进去
					dp[i][cw] = Math.max(
							weigth[i-1] + dp[i-1][cw-weigth[i-1]],
							dp[i-1][cw]
							);
				}else{
					dp[i][cw] = dp[i-1][cw];	//不能装
				}
			}
		}
		System.out.println(dp[n][w]);
		
	}
}

Guess you like

Origin blog.csdn.net/qq_67801847/article/details/132721949