"Algorithm" based on priority scheduling algorithm implementation

Source: Internet
Author: User

Scene: In the university, there are many organizations will be organized in the members on duty, of course, this shift is the time for students without classes will be scheduled on duty.

Assume the following requirements: There are 3 times a day to be on duty, every week from Monday to Friday to be on duty, is a total of 15 on duty, each time period of the people are not the same, the existing 40 students, the requirements of these students without a schedule to arrange on duty, require each duty section must have two people, Each person is only on duty once a week (if there is only one person on the duty section without classes, the shift can only be one person on duty).

Insert a question, do a combination of questions:

There are 10 of the same candy, awarded to 3 small people, asking everyone to have at least 2 sweets, how many kinds of different methods?

(Self-answer, 2014 Ali intern Recruit has a problem: there are 10 of the same candy, awarded to 3 villain, ask everyone to have at least 1 sweets, how many kinds of division? )

Scheduling algorithm design difficulties mainly in everyone's no schedule is not the same, if like the above problem, not according to everyone's no timetable, everyone in any one of the duty section can be on duty, that is simple.

The following is a personal algorithm design:

1. backtracking-based traversal : (Realization of the idea: when arranged to the nth duty section, found that there is no condition can be selected, back to the N-1 duty section rescheduled, if the first N-1 on the duty section of the people have tried, but the nth duty paragraph is still not satisfied with the conditions can be selected, Go back to the first N-2, and so on.

Now reduce the problem data to analyze the problem:

Suppose there are three duty segments, three people, according to these three people without a schedule to the duty section of the schedule of people on duty.

Ideas:

First on the first duty section of people who have no class to choose a person to schedule the shift on duty, arranged by the person is marked as scheduled on duty (flag 0 is not arranged, flag 1 indicates that has been arranged), can no longer be arranged on the other duty section on duty, and so on, Arrange the next duty section on a duty section.

Best case:

In the best case, the person who has not been placed on duty is arranged on every duty section, so that all of them are discharged at once, such as:


(1, 2, 3 indicates that the duty section, each on-duty section of the letter indicates that the duty section of the person without classes, such as the above 1th on the duty section has a and B two people without classes, can be arranged on the duty section on duty)

A is arranged to 1 duty section,

B is arranged to 2 duty section,

C is arranged to 3 duty section.

Worst case scenario:

As shown in the following:


Schedule to the third shift because a has been scheduled on duty, so to go back to the second shift to reschedule, to the second shift to schedule C on duty, but at this time the third shift is still no one to watch, and the second on duty section of the people have also traversed, so at this time to go back to the first shift to reschedule, at this time:

b is arranged to 1 duty section,

C is arranged to 2 duty section,

A is arranged to 3 duty section.

the above is the basic idea, but if on the 15 duty section, 40 people to schedule, consider the worst case, if just to the 15th duty segment when no one can be on duty, and finally back to the first duty segment re-traverse, the search comparison will greatly increase, so the final still did not take this method. However, the individual has not been able to implement the algorithm recursively, there is no time to study the study.

2. Priority-based selection:(Realize the idea: Scheduling on duty is not from the first shift in order to schedule to the 15th duty section, because in 15 on duty section, some of the people who have no class on the duty section more, at this time the choice is more, and there is no class on the duty section of the few people, Then this duty is to prioritize people on duty, such as there is a duty section only two people without class, it is necessary to arrange the two people on the duty section on duty, this is the first priority ; everyone has no class in a week is different, some people only one or two times a week without class, And some people have five or six times a week without a class, in a shift to choose who on the duty section of the first choice of the number of non-class, which is the second priority )

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >//on duty section public class Dutytime {private String time;  Duty time Private list<user> freeusers = new arraylist<user> ();  The time period without class of the person private list<user> dutyusers = new arraylist<user> ();  Public Dutytime () {}public dutytime (String time) {super (); this.time = times;} Omit Get set}</span>


<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >//staff public class User {private String name;private int num;  The person has no class time of week public user () {}public user (String name) {super (); this.name = name;} <span style= "White-space:pre" ></span>//omit get set}</span>

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >/** * Duty Schedule: * First for each duty section to arrange for a person on duty (first round), arrange a round after the second round, the third round * priority arrangement on duty: * 1. Priority on the number of people on duty on duty * 2. When selecting a person on a duty section, preference is given to those who have no class. * Note: After being arranged, the person will be removed from the no-class collection in the shift, so that no further traversal is required, and the order of each shift will be re-adjusted after the next round of arrangement (because the person has been removed) * */public class Schedule3 {  public static void Main (string[] args) {User A = new User ("A", 4); A There are 4 time periods a week without classes ...  Dutytime time1 = new Dutytime ("1"); First duty section ... time1.getfreeusers (). Add (A); .... list<dutytime> dutytimelist = new arraylist<> ();d utytimelist.add (time1);.. for (int w = 0; w < 3; w++) {// Sort, can be used for the number of people on duty on duty section in front, the following arrangements on duty is available for the number of people on duty on duty priority arrangement Dutytimelist = Dutytimesort (dutytimelist);//traverse the orderly shift section, Schedule a duty officer for each duty segment for (int i = 0; i < dutytimelist.size (); i++) {//If there is no class on the duty section, the person who has no classes is scheduled to be on duty if (Dutytimelist.get (i). Getfreeusers (). s ize () > 0) {//Get all the people who have not been scheduled to be on duty, good order, there is no class number of people in front of the platoon list<user> freeuserlist = Freeusersort (Dutytimelist.get (i). Getfreeusers ());//The person on duty will be assigned to the duty section on duty Dutytimelist.get (i). Getdutyusers (). Add (freeuserlist.Get (0)); System.out.println (Freeuserlist.get (0). GetName () + "arranged to" + Dutytimelist.get (i). GetTime () + "on duty; the duty section is existing" + Dutytimelist.get (i). Getdutyusers (). Size () + "Man on Duty," +freeuserlist.get (0). GetName () + "Total number of lessons a week:" +freeuserlist.get (0) . Getnum ());//The person to be arranged from the various shifts (with the person's duty section) removed, the next time no longer traverse user Deluser = Freeuserlist.get (0); for (int j = 0; J < dutytimelist.size (); J + +) {if (Dutytimelist.get (j). Getfreeusers (). Contains (Deluser)) {Dutytimelist.get (j). Getfreeusers (). Remove ( Deluser); SYSTEM.OUT.PRINTLN ("---" +deluser.getname () + "has been removed from the" +dutytimelist.get (j). GetTime () + "Duty section, the remaining number of people currently on the duty section is:" + Dutytimelist.get (j). Getfreeusers (). Size ());}}} else {System.out.println (Dutytimelist.get (i). GetTime () + "The duty segment is no longer available for scheduled duty");}} System.out.println ("---------------------arranged a round of--------------------");}} A person who does not have a class on a duty segment (a person who has no classes in the first week) public static list<user> Freeusersort (list<user> freeuserlist);// The order of the shift (can be used for the number of people on duty on duty section in front) public static list<dutytime> Dutytimesort (list<dutytime> dutytimelist);} </span>

Using this method to achieve a schedule can only generate a duty table, but in reality, according to the total no schedule is can be discharged from different plans of the duty table, the above algorithm is only to meet the needs of individual projects under the circumstances of the design, is not universal, small compiled a number of university professors published on the scheduling algorithm implementation of the paper, Those situations are even more complex, with various backtracking priorities.

The problem of course scheduling has been proved to be a NP -complete problem in the last years, that is, the computational time of the algorithm is increasing exponentially, which establishes the theoretical depth of the problem of course scheduling. For the complete problem of NP problem, there is not a general algorithm in mathematics to solve it well.



Author: Gu

Sign: Don't lose to who you used to be








"Algorithm" based on priority scheduling algorithm implementation

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.