Algorithm for optimal "intersection" of two results? - algorithm

I have two results and like to get the best "order" of both these.
Example:
We have a race with 5 people in one race, and 7 in another. The outcome is:
Race 1.
1. Karl
2. Fred
3. John
4. Peter
5. Sid
Race 2.
1. Steven
2. John
3. Karl
4. Peter
5. Aron
6. Fred
7. Kevin
The questin is: whats the top 7 of both these races?
Its quite obvious that that nr 1 is Karl in this case, since he hold one 1st place and one 3rd, which is better than Johns second and third place. However, Steven could be equally good but he did only participate in one race and should suffer some kind of penalty for that.
What are the known algorithms for this problem? Are there any trivial solutions? I just can't figure it out

You can relate the positions to weights (think of it as points)
For instance 1st position has a weight of 20. 2nd has 18. 3rd 16, etc.
Participation miss could relate to a weight of -5.
You can adjust the numbers as required.
To find the final result you add everyone's weights and compare the numbers.
I think it should work..

Another approach would be to create an ordering of the top nodes which are consistent with the ordering of the previous races. This could be done by using a max-flow algorithm.

A naive solution would be to have some kind of ID for all the players. Go through both lists and add any new IDs you come across to a separate list, called Positions for example. Set all values for these IDs to some large number that can not be a race position, call it BIG_VAL (eg. 100). Go through the first list and mark all the positions as the new values in the Positions list. This was just the first race, so there is nothing special here. Then go through the second list and add the second position results for those IDs into the Positions list. For the IDs that don't occur, add another BIG_VAL to their result. The list will now have the race positions in order, all that remains is to sort them. Karl will be 4, John will be 5, Fred will be 8, and so on in this list:
http://bit.ly/fvYtal

Related

Donald Knuth Algorithm Mastermind

I'm working on a mastermind game that implements the Donald Knuth algorithm. The first five steps are clear. I have to create a set of permutations for each possible answer, use 1122 as my first guess, compare each possible answer from the set to 1122 and then remove any of the possible answers that does not return the same feedback as the current guess. The problem now lies in determining the next guess and how I'm supposed to implement step 6. The algorithm is shown below.
Mastermind-Five-Guess-Algorithm Donal Knuth's five guess algorithm for solving the game Mastermind.
In 1977, Donald Knuth demonstrated that the codebreaker can solve the
pattern in five moves or fewer, using an algorithm that progressively
reduced the number of possible patterns.
The algorithm works as follows:
Create the set S of 1296 possible codes (1111, 1112 ... 6665, 6666).
Start with initial guess 1122 (Knuth gives examples showing that other first guesses such as 1123, 1234 do not win in five tries on
every code).
Play the guess to get a response of colored and white pegs.
If the response is four colored pegs, the game is won, the algorithm terminates.
Otherwise, remove from S any code that would not give the same response if the current guess were the code. For example, if
your current guess is 1122 and you get a response of BW; If the
code were 1111 you would get two black pegs (BB) with a guess of 1122,
which is not the same as one black peg and one white peg (BW). So,
remove 1111 from the list of potential solutions. F(1122,1112)
= BBB≠BW →Remove 1112 from S F(1122,1113) = BB≠BW →Remove 1113 from S F(1122,1114) = BB≠BW →Remove 1114 from S
F(1122,1314) = BW=BW →Keep 1314 in S
Apply minimax technique to find a next guess as follows: For each possible guess, that is, any unused code of the 1296 not just
those in S, calculate how many possibilities in S would be eliminated
for each possible colored/white peg score. The score of a guess is the
minimum number of possibilities it might eliminate from S. A
single loop through S for each unused code of the 1296 will provide a
'hit count' for each of the possible colored/white peg scores; Create
a set of guesses with the smallest max score (hence minmax). From
the set of guesses with the minimum (max) score, select one as the
next guess, choosing a member of S whenever possible. Knuth
follows the convention of choosing the guess with the least numeric
value e.g. 2345 is lower than 3456. Knuth also gives an example
showing that in some cases no member of S will be among the highest
scoring guesses and thus the guess cannot win on the next turn, yet
will be necessary to assure a win in five.
Repeat from step 3
Link to Wikipedia page
Take the set of untried codes, and call it T.
Iterate over T, considering each code as a guess, g.
For each g, iterate over T again considering each code as a possible true hidden code, c.
Calculate the black-white peg score produced by guessing g if the real code is c. Call it s.
Keep a little table of possible scores, and as you iterate over the possible c, keep track of how many codes produce each score. That is, how many choices of c produce two-blacks-one-white, how many produce two-blacks-two-whites, and so on.
When you've considered all possible codes (for that g) consider the score that came up the most often. You might call that the least informative possible result of guessing g. That is g's score; the lower it is, the better.
As you iterate over g, keep track of the guess with the lowest score. That's the guess to make.

How to caculate the probability of this cell is mine

the unknown one blue marked
it's 3/5? 1/3? 2/5? or max value of above, or maybe another(I think this...)?
how to caculate? it confuses me very much...
It's actually 1/2.
Note that there must be at least two mines in the three cells below 3 (because the other two cells are adjacent to a 1 and as such can't have more than 1 mine).
This means that there must be at least one mine between the cell below the 3 and cell to the bottom right of 3. Since both of these are also adjacent to a 1, only one of them can have mine at most. Thus, exactly one of these cells is a mine. The 2 on the right becomes irrelevant at this point.
With that out of the way, is there a general algorithm which can generate results like these?
I can't thing of any polynomial time solutions but it might be possible to simple try all the alternatives while backtracking when a constraint fails.

n-place mastermind variation algorithm

A few days ago I came across such a problem at the contest my uni was holding:
Given the history of guesses in a mastermind game using digits instead
of colors in a form of pairs (x, y) where x is the guess and y is how
many digits were placed correctly, guess the correct number. Each
input is guaranteed to have a solution.
Example for a 5-place game:
(90342, 2)
(70794, 0)
(39458, 2)
(34109, 1)
(51545, 2)
(12531, 1)
Should yield:
39542
Create an algorithm to correctly guess the result in an n-place
mastermind given the history.
So the only idea I had was to keep the probability of each digit being correct based on the correct shots in a given guess and then try to generate the most possible number, then the next one and so on - so for example we'd have 9 being 40% possible for the first place (cause the first guess has 2/5=40% correct), 7 being impossible and so on. Then we do the same for other places in the number and finally generate a number with the highest probability to test it against all the guesses.
The problem with this approach, though, is that generating the next possible number, and the next, and so on (as we probably won't score a home run in the first try) is really non-trivial (or at least I don't see an easy way of implementing this) and since this contest had something like a 90 minute timeframe and this wasn't the only problem, I don't think something so elaborate was the anticipated approach.
So how could one do it easier?
An approach that comes to mind is to write a routine that can generally filter an enumeration of combinations based on a particular try and its score.
So for your example, you would initially pick one of the most constrained tries (one of the ones with a score of 2) as a filter and then enumerate all combinations that satisfy it.
The output from that enumeration is then used as input to a filter run for the next unprocessed try, and so on, until the list of tries is exhausted.
The candidate try that comes out of the final enumeration is the solution.
Probability does not apply here. In this case a number is either right or wrong. There is no "partially right".
For 5 digits you can just test all 100,000 possible numbers against the given history and throw out the ones where the matches are incorrect. This approach becomes impractical for larger numbers at some point. You will be left with a list of numbers that meet the criteria. If there is exactly one in the list, then you have solved it.
python code, where matches counts the matching digits of its 2 parameters:
for k in range(0,100000):
if matches(k,90342)==2 and matches(k,70794)==0 and matches(k,39458)==2 and matches(k,34109)==1 and matches(k,51545)==2 and matches(k,12531):
print k
prints:
39542

Sort array based on binary comparisons while minimizing intransitive trials

I have a list of 15 cities. I randomly draw 70 pairs out of the possible 15*14/2=105 pairs of cities. For each of the 70 pairs, I ask my participants to decide whether city A is bigger than city B.
The important thing is, sometimes participants make 'mistakes' and give an answer that's incompatible with their previous answers. (i.e., it violates transitivity).
I need a way to sort my cities based on each participant's response, in a way that minimizes the number of trials that violate transitivity.
I don't need the actual order of cities, as there might not be a unique solution. I just need to calculate the (minimum) number of intransitive answers given by each participant.
How could I do this other than using exhaustive search?
EDIT: To give an example, take cities A,B,C,D and E. Participant Jon Doe thinks that the correct order of the cities (from smallest to biggest) is ABCDE. I don't care whether he's actually right or not, I just care about how well his responses -listed below- match his belief.
In three independent trials, Jon replied the following:
trial 1: A < B
trial 2: B < C (+)
trial 3: C < D
trial 4: D < E (+)
trial 5: E > B (*)
So, the answer in trial 5 (*) is incompatible with those in trials 2 and 4 together. Either one trial (nr. 5) did not correspond to Jon's belief, or 2 trials (2 and 4) didn't. I don't care to figure out which was Jon's belief (ABCDE), I just need to know that the "minimum number of intransitive answers" for Jon Doe is 1.
So... the problem might be interesting, but it's not clear what you want. You need to sort your cities but you don't need their order?
Minimize the number of trials that violate transitivity... how do you do that? The intransitivity is in the answers you get, not in whatever you do with them.
Calculate the number of intransitive answers given by each participant... if you have all the answers of each of the subjects, then an inconsistency is a cycle in the direct graph where nodes are cities and a node points to another iff the participant said its city is bigger than the other's. There are algorithms for that, see this question.
Of course an edge might be part of more than one cycle, and in this case we could try to find the minimum number of edges we have to remove to make it acyclical. Unfortunately, the problem is NP complete; so you won't find a fast answer. However, since your numbers are fairly low, you might manage to find a passably fast solution.
Hope this helps.

Algorithm to select random pairs, schedule matchups

I'm working in Ruby, but I think this question is best asked agnostic of language. It may be assumed that we have access to basic list/array functions, as well as a "random" number generator. Here's what I'd like to be able to do:
Given a collection of n teams, with n even,
Randomly pair each team with an opponent, such that every team is part of exactly one pair. Call this ROUND 1.
Randomly generate n-2 subsequent rounds (ROUND 2 through ROUND n-1) such that:
Each round has the same property as the first (every team is a
member of one pair), and
After all the rounds, every team has faced every other team exactly once.
I imagine that algorithms for doing exactly this must be well known, but as a self-taught coder I'm having trouble figuring out how to find them.
I belive You are describing a round robin tournament. The wikipedia page gives an algorithm.
If You need a way to randomize the schedule, randomize team order, round order, etc.
Well not sure if this is the most efficient algorithm but:
Randomly assign N teams into two lists of same length n/2 (List1, List2)
Starting with i = 0:
Create pairs: List1[i],List2[i] = a team pair
Repeat for i = 1-> (n/2-1)
For rounds 2-> n/2-1:
Rotate List2, so that the first team in List2 is now at the end.
Repeat steps 2 through 5, until List2 has been cycled once.
This link was very helpful to me the last time I wrote a round robin scheduling algorithm. It includes a C implementation of a first fit algorithm for round robin pairings.
http://www.devenezia.com/downloads/round-robin/
In addition to the algorithm, he has some helpful links to other aspects of tournament scheduling (balancing home and away games, as well as rotating teams across fields/courts).
Note that you don't necessarily want a "random" order to the pairings in all cases. If, for example, you were scheduling a round robin soccer league for 8 games that only had 6 teams, then each team is going to have to play two other teams twice. If you want to make a more enjoyable season for everyone, you have to start worrying about seeding so that you don't have your top 2 teams clobbering the two weakest teams in their last two games. You'd be better off arranging for the extra games to be paired against teams of similar strength/seeding.
Based on info I found through Maniek's link, I went with the following:
A simple round robin algorithm that
a. Starts with pairings achieved by zipping [0,...,(n-1)/2] and [(n-1)/2 + 1,..., n-1]. (So, if n==10, we have 0 paired with 5, 1 with 6, etc.)
b. Rotates all but one team n-2 times clockwise until all teams have played each other. (So in round 2 we pair 1 with 6, 5 with 7, etc.)
Randomly assigns one of [0,..., n-1] to each of the teams.

Resources