Introduction to algorithms-greedyactivityselector

Source: Internet
Author: User
/*** Introduction to algorithms, second edition * 16.1 an activity-selection problem * @ author potato dad **/import Java. util. arraylist; import Java. util. list; public class greedyactivityselector {/*** activity */static class activity {int start; // start time int finish; // result time public activity (INT start, int finish) {This. start = start; this. finish = finish ;}} /*** obtain the maximum subset of mutual compatibility * @ Param Activities Activity array in ascending order at the end * @ return maximum subset of mutual compatibility */public static list <activity> select (activity [] activities) {list <activity> List = new arraylist <activity> (); list. add (activities [0]); // obtain 1st active int I = 0; // record the index of the last selected activity for (INT m = 0; m <activities. length; m ++) {If (activities [M]. start> activities [I]. finish) {// obtain the next compatible activity list. add (activities [m]); I = m ;}} return list ;}import Java. util. list; import JUnit. framework. testcase; public class greedyactivityselectortest extends testcase {public void testgreedyactivityselector () {greedyactivityselector. activity [] activities = {New greedyactivityselector. activity (1, 4), new greedyactivityselector. activity (3, 5), new greedyactivityselector. activity (0, 6), new greedyactivityselector. activity (5, 7), new greedyactivityselector. activity (3, 8), new greedyactivityselector. activity (5, 9), new greedyactivityselector. activity (6, 10), new greedyactivityselector. activity (8, 11), new greedyactivityselector. activity (8, 12), new greedyactivityselector. activity (2, 13), new greedyactivityselector. activity (12, 14)}; List <greedyactivityselector. activity> List = greedyactivityselector. select (activities); int [] Answer = {0, 3, 7, 10}; For (INT I = 0; I <list. size (); I ++) {greedyactivityselector. activity A = activities [answer [I]; assertequals (A, list. get (I ));}}}

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.