"Introduction to Algorithms" using dynamic programming to solve active selection problems

Source: Internet
Author: User

in a previous article on the greedy algorithm to solve the problem of active selection ("Introduction to the algorithm" Greedy algorithm activity selection problem ), found that there is a practice 16.1-1 is to use dynamic programming to solve the problem of active selection. In fact, the matrix chain is similar to the previous multiplication, but also consider the partition of the activity is which, and two-dimensional data to record Sij---the maximum number of compatible sets, and another two-dimensional data to record the maximum Sij the active partition point K. Then the boundary problem is considered, and the optimal solution of dynamic programming is obtained by using recursive return.


The code annotations are more detailed:


#include <iostream> #include <algorithm>using namespace std; #define N 11/* * Active struct, with an ID, a start time, an end time */ struct activity {int Id;int start_time;int end_time;};/ /0 Initialize int s[n + 2][n + 2] = {0};/* * record Sij can achieve the best of a segmented activity subscript K. Greedy algorithm here is at the end of the order after ascending, take A1, and dynamic planning to pursue the global optimal, so to traverse all possible k values, and then * to find an optimal solution, where the K does not represent the first node decomposition method *//* Here the reason for the data to take n+2, is because to insert a start and end time is 0 of the 0 node in, so it is 11 nodes, it becomes a 11+1 node, * * Insert 0 node reason is because when a[i].start_time=0 still need to compare with A[i-1].end_time, Otherwise there is no way to determine the boundary, so add a 0 node * and then due to i=n time, also need to consider a[i].end_time * So to compare with a[i+1].start_time, so to take to i+1=n+1, so the scope of the data subscript if 0~n+ 2, here Add the N+1 node in order to let it never select, so * Let it start time and end time is the maximum 0x7fffffff, if in Java, may directly take Integer.max_value */int Cut_point[n + 2][n + 2] = {0};activity a[n + 2];/* * Dynamic Programming solution activity Problem */void Dynamic_programme () {int step;//step, for j-i, Difference int i, J, K;int result;/* * Due The SIJ represents the set of activities that precede the end of the active AI and the start of AJ, so when the j<=i+1 is clear, the Sij is empty, so here the step starts at 2 with the loop */for (step = 2; step <= N + 1; step++) {//at each step Long, traverse for all I for (i = 0; I <= N; i++) {j = step + i;//here if J is larger than n+1, it is not considered because the array only goes to the n+1 subscriptif (j <= N + 1) {//According to the definition of SIJ, there must be an interval between AI and AJ two activities, otherwise sij is empty, here because i=n time, also need to consider a[i].end_time//so to compare with A[i+1].start_time, So to get to the i+1=n+1, so the data subscript range if 0~n+2if (a[i].end_time <= a[j].start_time) {//In the eligible K, here K between I+1 and J: [I+1,j]; to find the k that can make the problem best , which makes Sij contains the most active solution for (k = i + 1; k < J; k++) {//here K also needs to be judged, need to be AI and AJ compatible if (A[k].end_time > A[j].start_time| | A[k].start_time < A[i].end_time) {continue;} According to "calculated third Edition 16.2 Formula" result = S[i][k] + s[k][j] + 1;if (Result > S[i][j]) {s[i][j] = result;//record the maximum number of activities included in the current split cut_point[i] [j] = k; And record the split point subscript}}}}}}cout << s[0][n + 1] << endl;//Returns the number of activities in the maximum compatible activity set in Sij}//print specifically which activities are in Sij void print (int Start, int end) {int k = cut_point[start][end];//Think k=0 is a case where there is no suitable activity in Sij, not a split point, so omit the IF (k! = 0) {Print (start, k); cout <& Lt K << '; Print (k, end);}} BOOL CMP (activity A, activity B) {return a.end_time < b.end_time;} int main () {int i = 0;//Let a[0] is 0 value a[i].start_time = 0; A[i].end_time = 0; a[i].id = 0; A[1].start_time = 1; A[1].end_time = 4; A[2].start_time = 3;A[2].end_time = 5; A[3].start_time = 0; A[3].end_time = 6; A[4].start_time = 5; A[4].end_time = 7; A[5].start_time = 3; A[5].end_time = 8; A[6].start_time = 5; A[6].end_time = 9; A[7].start_time = 6; A[7].end_time = 10; A[8].start_time = 8; A[8].end_time = 11; A[9].start_time = 8; A[9].end_time = 12; A[10].start_time = 2; A[10].end_time = 13; A[11].start_time = 12; A[11].end_time = 14;for (i = 1; I <= N; i++) {a[i].id = i;//is a manual input assignment, which, for the sake of simplicity, simply kills the value and can be restored to manual, commenting out the section above from a[1]~a[11]//cin >> a[i].start_time >> A[i].end_time;} Increase the active end node a[i].id = i; A[i].start_time = A[i].end_time = 0x7fffffff;//by end time from small to large sort sort (a, a + N + 2, CMP);D ynamic_programme (); Print (0, N + 1); return 0;}


"Introduction to Algorithms" using dynamic programming to solve active selection problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.