A couple of test cases (written in C++11 for GoogleTest) should clarify the problem:
typedef std::pair<int, int> Activity; typedef std::vector<Activity> Activities; TEST(ActSel, Simple) { Activities input { {1, 2}, {5, 9}, {0, 6}, {8, 9}, {3, 4}, {5, 7} }; Activities output = selectMax(input); ASSERT_EQ(4, output.size()); for(unsigned i = 1; i < output.size(); ++i) ASSERT_LE(output[i-1].second, output[i].first); } TEST(ActSel, Simple2) { Activities input { {1, 4}, {3, 5}, {0, 6}, {3, 9}, {5, 9}, {5, 7}, {6, 10}, {8, 11}, {8, 12}, {2, 14}, {12, 16} }; Activities output = selectMax(input); ASSERT_EQ(4, output.size()); for(unsigned i = 1; i < output.size(); ++i) ASSERT_LE(output[i-1].second, output[i].first); }In both cases I expect a selection of four Activity objects in output. In the first case these elements: (1, 2) (3, 4) (5, 7) (8, 9), in the second one (1, 4) (5, 7) (8, 12) (12, 16), or maybe (8, 11) instead of (8, 12). As you can see, there could be more solutions, and the problem doesn't require you to be particolary choosy. Once you maximize the number of selected items, the actual value of each of them is not an issue.
Still, I want to ensure in my test cases that I peak a valid solution, so I check, through ASSERT_LE, that all the elements in the extracted sequence are ordered as expected.
As said above, this problem has a greedy optimal solution. What we have to do is just sorting the input elements by their second component (the end time), and then greedily accepting all the elements we can. As in this implementation:
Activities selectMax(Activities& input) // 1 { std::sort(input.begin(), input.end(), [](Activity a, Activity b) { return a.second < b.second; }); // 2 Activities output; output.push_back(input[0]); // 3 for(unsigned i = 0, j = 1; j < input.size(); ++j) // 4 { if(input[j].first >= input[i].second) // 5 { output.push_back(input[j]); i = j; } } return output; }1. We don't mind if this function modify the input parameter, so it is passed as non-constant reference. Beware that this should be know and accepted by the callers.
2. The "normal" STL sort() function would order the passed sequence by its first component. So we need to use the overloaded version that let as pass a predicate to be used as comparator. Using a C++11 lambda function, as shown here, makes it simple and elegant.
3. The first element is always selected.
4. Now we are ready to loop on all the other elements in the sequence. The real looping variable is j, while i is used to keep track of the last accepted element.
5. The first element after the last accepted one that starts not before the end of it, is pushed in the output sequence.
No comments:
Post a Comment