# Algorithm of generating round robin schedule by recursive and exhaustive method __ algorithm

Source: Internet
Author: User
``` writing a program recently requires the functionality generated by the cycle schedule.

If the number of teams is 2 n times, the use of separate treatment is relatively simple, but like 6, 20 of such numbers, the decomposition of the number of 3, 5 is more difficult.

The team number 0...n-1, draw a matrix to discharge the table, thinking that can be programmed to achieve this intuitive way to arrange the schedule. 6 teams have been taken for example to form 6x6 matrices. In the first round, start with the first line, fill in the upper-left corner of the Matrix, A to B fill 1, and then line 1th, column 1th, row 2nd, 2nd column are not selectable. So row B is no longer processed, and then to C, you can try the first empty C pair d. In case C to D is occupied, the rest of E and F can not match (for example, has been against) (of course, this example is not the case, but we assume), then C to D, not set up, continue to try C to E.                                                     (see below for a description of the following figure) a B c d E f a b c D E F a      A--1 b B--X                                                     x x x C C--D                                   D--E E                                         --F F

--a total of n-1 round, each round of N/2 game, according to the way, you can use recursion to achieve a poor lift. Code in: ```
```Import java.io.IOException;
Import java.util.ArrayList;

Import java.util.List;
public class Matchschedule {private int table[][];
private int roundnum;
private int roundmatchnum;

private int teamcount;
public boolean generate (int teamcount) {if (0!= (teamcount% 2)) {return false;
} this.teamcount = Teamcount;
Roundnum = teamCount-1;

Roundmatchnum = TEAMCOUNT/2;

Table = new Int[teamcount][teamcount]; for (int i = 0; i < Teamcount. i++) {for (int j = 0; J < Teamcount; J +) {Table[i][j] =
-1;
} return Genround (1); public void Printschedule () {for (int i = 1; I <= roundnum; i++) {System.out.println ("* * *
**************");

System.out.println ("First" + i + "round");
for (int m = 0; m < teamcount. m++) {for (int n = m+1; n < teamcount; n++) {     if (table[m][n] = = i) {System.out.println (M + "VS" + N); '}}}} private Boolean genround (int round) {list<integer> OC
Cupies = new arraylist<integer> ();
if (Genmatch (round, 1, occupies)) {return true;
return false; Private Boolean genmatch (int round, match int, list<integer> occupies) {System.out.println ("Generate
Round "+ round + match" + match);
for (int i = 0; i < Teamcount i++) {if (Find (occupies, i)) {continue; for (int j = i + 1; j < Teamcount; J + +) {if (Find (occupies, j)) {con
Tinue;
} if ( -1!= table[i][j]) {continue;

} Table[i][j] = round; if (match = = Roundmatchnum) && (round = = Roundnum)) {return true;
Boolean Nextsteprst;
if (match = = Roundmatchnum) {Nextsteprst = Genround (round+1);
}else{Nextsteprst = Genmatch (round, match+1, occupies);
} if (true = = Nextsteprst) {return true;
}else{Occupies.remove (Occupies.size ()-1);
Occupies.remove (Occupies.size ()-1);
TABLE[I][J] =-1;
}} return false;
Private Boolean find (list<integer> occupies, int v) {for (int i = 0; i < occupies.size (); i++) {
if (occupies.get (i) = = V) {return true;
return false; public static void Main (String args[]) {System.out. Print ("team number:");

int input = Integer.parseint (args[0]);

Matchschedule schedule = new Matchschedule ();
if (false = = Schedule.generate (input)) {System.out.println (input + "team can not generate the schedule");
}else{System.out.println (input + "The schedule of the detachment is as follows");
Schedule.printschedule ();
}
}
}```

When n is large, the time performance of this algorithm and the footprint of the stack space are problematic.
in real life, n is often fixed, can actually prepare a schedule, for a, B, C, D corresponding team, can be used for a long time.

Related Keywords:

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.