Problem A
To solve this problem, I would start by defining an UnorderedSet class, containing element of type T (typical template C++ stuff), but since I don't want to get into the programming details, I will just say that we can deal with unordered sets that have elements that are unique. So let G be the set of all different combinations of alleles possible: OO, AO, AA, BO, BB, AB. Let F be the set of possible alleles for the father, M for the mother and C for the child, with F,M,C ⊆ G.
Our task for the first part would be to derive C from F and M. The second part consists on deriving M (F) from F (M) and C. In the last case the result can be the empty set.
Two basic functions are required to convert from and to our combinations in G to the "observed" genetic characteristics, so D(A) would give a set {AO, AA}, D(B)={BO,BB} D(AB)={AB}, D(O)={OO}. And similarly the reverse relation from elements of G (not subgroups as in the direct relation) towards blood types, we call this function R.
The first part (parents -> child) can be easily solved by taking the cartesian product of sF by sM where the s indicates the "simplify operation", that is taking all the alleles in F or M separately (and eliminating repeated ones), in that way, s{AB} -> {A, B} or s{AO, AA}->{A, O}. Since we are not interested in probabilities this will do. The result (after removing repeated results taking into account that alleles are commutative (AB=BA)) is C. Now just apply R to the elements of C. That is the result.
To solve the second part we need to make another formulation which serves us to solve the first part too, and takes into account the probabilities, so maybe it would be better to start with the second part and use the functions developed to easily solve the first part. It works like this:
- We define a function f: G³ ->{0,1} that takes 3 elements of G and returns 1 if it is a possible combination in the sense that f(x,y,z), where x ∈ F, y ∈ M, z ∈ C, is feasible ({AB,OO,AO} is a possible combination, {AO,AA,OO} is not), and 0 otherwise. This function is easily defined just by taking the cartesian product (as in the previous) of x and y and looking for z in the result. More programmatically, we evaluate the cartesian product for the 21 possible combinations (the cartesian product is abelian), or 36 if we don't want to bother with optimisations for such a small problem, of G x G and save the results in a 6 x 6 x 6 matrix (that we call by extension f).
- This second step is best explained in pseudocode:
for i in F
for k in C
for j in G
if (f(i,j,k)==1)
M.add(j)
end
of course, M is a set and the add method avoids having repeated entries.
We're done. The same algorithm could be applied to infer C from F and M (or F from M and C, for that matter). The only job remaining is to apply R to M to get the possible blood types for the mother.
The same algorithm can be applied to the +- alleles (this time the problem is smaller). The results should be the cartesian product of the ABO alleles and the +- alleles.
I haven't tested this solution. There's no guarantee that it will work, maybe it's dead wrong. Any comments are welcome. Problems B, C and I will surely follow when I get time to write them down. The rest... maybe, only if I can solve them :-)
3 comentarios:
Yup, I think that'll work. Good job. I think in this problem the key is knowing which data structures to use right away, and realizing that the possible cases are so few that you could test all of them directly.
I am one of the UPC guys that participated in that contest and more exactly the one who solved this problem. Your solution is correct, but the hard part is to code it in a short code (that doesn't take you a lot of time to do it). I did it in around 50 lines if i recall correctly.
Between, you may check your solutions at the UVA Live Archive!
I'll try to code this (and the rest of problems that I have solved in my head) when I get some spare time.
This is just a hobby. The thesis takes most of my time in front of a computer.
I hope to finish with all the problems before the next contest :-D
Cheers!
Publicar un comentario