3-way / 4-way round-robin tournament scheduling algorithm - java

I would like to create/implement a tournament scheduling algorithm which is able to deal with more than 2 participants per game.
The problem seems to be well known for 2 participants. See here for example: Round Robin Algorithm Implementation Java
Example of matchups with 6 teams (A, B, C, D, E, F):
(ABC)(DEF)
(ABD)(CEF)
(ABE)(CDF)
(ABF)(CDE)
(ACD)(BEF)
(ACE)(BDF)
(ACF)(BDE)
(ADE)(BEF)
(ADF)(BCE)
(AEF)(BCD)
In case of an odd number of teams (i.e. A, B, C, D, E), I would like to have a 3-way and a 2-way game per round: (ABC)(DE)
Once the 3-way problem is solved, I would like to do the same with 4-way games.
I am unable to create such an algorithm and unable to find a similar solution on the internet.
Could somebody point me in the right direction?

To choose K items from N, you need combinations.
Note that C(6,3)=20 but you do fixing one item (A) and have really C(5,2)=10 variants
There is a lot of implementations of combinations generation - the simplest is recursive, more effective is lexicographic ordered generation -simple C code

Related

Dynamic Programming (how to send a message in the most efficient way)

I'm having a hard time with dynamic programming, I'm new in this, so I hope you could help me with anything you can, the problem is this:
As the Communications Officer of the IKS B'Moth Klingon battle cruiser, your duty is to manage communications in the most efficient way. Assume you need to transmit a message S= s1...sm given as a string of m symbols. For this purpose, you have r different codes. Let b(ij) be the number of bits needed to enconde the i-th symbol of your message in the j-th code. Initially, the bridge transmitter is set to code #1, but you can freely change the code at any point within the message and as many times as you want. To do so, you need to send a control code which is composed of C(ij) bits if you want to switch from current code i to any other code j. Your goal is to determine how to send thee message in the most efficient way (using the least number of bits).
A) Prove the problem exhibits optimal substructure.
B) find a recurrence for the optimal number of bits required.
C) Build a bottom up dynamic programming algorithm to solve the problem and indicate its complexity.
You can make a 3 dimensional array, and use previousCode, newCode, and ithSymbol as indices. The array will store the least number of bits while scanned upto ithSymbol and when it switches code from previousCode to newCode.
The recursive formula will be:
dp(ithSymbol, previousCode, newCode)=min_(i=1 to r)(dp(ithSymbol-1,i,previousCode))+C(previousCode, newCode)+b(ithSymbol,newCode);
(Assumed, C(i,i)=0 for all i)
Now you can write the code for yourself.
N.B. This is a naive approach. You can further make it efficient by making the array 2-D as only ithSymbol-1 is used in any step.

Record Matching - Efficient Iteration

I have to preform record matching of 70K records in Java. One record size would be 200 bytes As record matching process all records compared against all records. My query is, how efficiently I can iterate and perform comparison.
First of all, you don't need compare all to each other. Once A - B is equal to B - A, you just need compare one with its successors. For example, you have { A, B, C, D }, then you compare A with B, C and D. Compare B with C and D, and compare C with D. This cut the amount of comparisons from n ^ 2 to n!.
You can optimize the algorithm by making search blocks. Put everyone with the same name and last name on the same block. Everyone with the same email on other block and so on. After all, you process each block comparing their records as described above. Depending on the amount of records you have, you will reduce dramatically the time of processing.
Use Duke [https://github.com/larsga/Duke].
Not perfect, but it's free and Java.
We have .NET version that is better and faster, but it's in-house thing, not OSS yet.

Genetic Algorithms: Genes values should sum up to one

I want to implement a genetic algorithm (I'm not sure about the language/framework yet, maybe Watchmaker) to optimize the mixing ratio of some fluids.
Each mix consists of up to 5 ingredients a, b, c, d, e, which I would model as genes with changing values. As the chromosome represents a mixing ratio, there are (at least) two additional conditions:
(1) a + b + c + d + e = 1
(2) a, b, c, d, e >= 0
I'm still in the stage of planning my project, therefore I can give no sample code, however I want to know if and how these conditions can be implemented in a genetic algorithm with a framework like Watchmaker.
[edit]
As this doesn't seem to be straight forward some clarification:
The problem is condition (1) - if each gene a, b, c, d, e is randomly and independently chosen, the probability of this to happen is approximately 0. I would therefore need to implement the mutation in a way where a, b, c, d, e are chosen depending on each other (see Random numbers that add to 100: Matlab as an example).
However, I don't know if this is possible and if it this would be in accordance with evolutionary algorithms in general.
The first condition (a+b+c+d+e=1) can be satisfied by having shorter chromosomes, with only a,b,c,d. The e value can then be represented (in the fitness function or for later use) by e:=1-a-b-c-d.
EDIT:
Another way to satisfy the first condition would be to normalize the values:
sum:= a+b+c+d+e
a:= a/sum;
b:= b/sum;
c:= c/sum;
d:= d/sum;
e:= e/sum;
The new sum will then be 1.
For the second condition (a,b,c,d,e>=0), you can add an approval phase for the new offspring chromosomes (generated by mutation and/or crossover) before throwing them into the gene pool (and allowing them to breed), and reject those who dont satisfy the condition.

A* search Java - TSP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Implementation of A Star (A*) Algorithm in Java
For an assignment I have to implement some search algorithms in order to solve the travelling salesman problem, I understand the problem and I understand how the algorithm works, I simply don't know how to implement it (my Java isn't great) but this should really be the easy part but unfortunately my knowledge of Java isn't good enough to apply what I know about the algorithm.
I was therefore wondering if anyone could provide some hints or tips on how to get started with this or maybe even some good links to read up on, I also have to implement some other algorithms too if there's an easier one to start with id be happy to go with that one! I've had a look on here already but can't seem to find anything which is relevant :/
Any help is much appreciated! Thank you
I'm not an expert, but I don't think the A* can be used to solve the Traveling Salesman Problem. I'm sure you know the TSP is NP-hard, so exact solutions are quite complex and don't exist for every case. The easiest way I know of (and have used) to approximate a solution is whats called 'Simulated Annealing', which is a stochastic method (i.e. random!).
The idea is very simple. You organise the points into a list, which represents a path between all the nodes (this is called a 'tour'). Calculate how long the tour is and save the length. You then pick a random sub-list and simply reverse it. Calculate the new length. If the new list is shorter then keep it, otherwise discard it. Simply repeat these steps 100/1,000/10,000,000 times (depending on the number of points) and you'll have a reasonable approximation.
Example, imagine there are 5 points (pseudocode)...
List tour = [e, b, a, d, c]
int len = tourLength(tour)
List newTour = [e, b] + [d, a] + [c] // a and d have been reversed
int newLen = tourLength(newTour)
if(newLen < len) {
tour = newTour
}
For 10 points, 100 iterations should yield a good result.

Efficient arrangement algorithm in java

I'm trying to write a method that will compute all permutations of a power set where order matters. I believe these are called "arrangements." What I mean by this is:
{a} -> {{a}, {}}
{a,b} -> {{a,b}, {b,a}, {a}, {b}, {}}
{a,b,c} -> {{a,b,c}, {a,c,b}, {b,a,c}, {b,c,a}, {c,a,b}, {c,b,a}, {a,b}, {a,c}, {b,a}, {b,c}, {c,a}, {c,b}, {a}, {b}, {c}, {}}
etc. My impression is that, given a set S, I should generate every permutation of every subset of the powerset of S. So first generate the powerset, then map a permutation function onto each set.
The problem is that this is immensely complex -- something like O(∑n!/k!) with k=0..n.
I'm wondering if there are any existing algorithms that do this sort of thing very efficiently (perhaps a parallel implementation). Or perhaps even if a parallel powerset algorithm exists and a parallel permutation algorithm exists, I can combine the two.
Thoughts?
The guava library provided by google contains different methods to permute collections.
See the javadoc of class com.google.common.collect.Collections2 here.
To do this you first generate the combinations for 1-n slots where n is the number of elements in the power set. For example, if you have 3 elements, then you will have:
C( 3, 3 ) = 1 combination (a b c)
C( 3, 2 ) = 3 combinations (a b) (a c) (b c)
C( 3, 1 ) = 3 combinations (a) (b) (c)
Now, you generate the permutations for each combination.
There are well known algorithms to calculate permutations and combinations. For example, Knuth's "The Art of Computer Programming", volume 4A, Sections 7.2.1.2 and 7.2.1.3, explain exactly how to construct the relevant algorithms.

Categories

Resources