Algorithm to Solve Genetic Crossover Problem
I have a list of objects like:
class PairingStrand
{
int startInt; // the start pos
int endInt; // the end pos
int Index; // the index of pairing
int Strand; // Strand is either "5" or "3"
}
Each position (integer) can only be affiliated with one object.
An objects with Strand = 5
is pairing with an object having the same "Index" and strand = 3
.
The list is ordered by the "startInt" of each object. So the different pairings regions could cross over with one another.
The "CROSS-OVER" means two regions overlap, but if one region is large enough to completely engulf the other one, it is not considered to be "CROSS-OVER".
For example...
{10~15 : 40~45} (startInt ~ endInt : startInd ~ endInx)
is cross-over with
{20~30 : 60~70}
However,
{10~15 : 40~45}
is not considered to be cross over with
{20~25: 30~35}
My question is how to identify the largest block which does not have any cross-over with any other blocks in the list. The cross开发者_如何学Go-over is allowed inside the block. (In other words, "Cross-over" is allowed but not required inside block, while it is not allowed between blocks)
Maybe I did not describe the question very clearly. Here is a simple example:
List as below:
obj1 (10~20, index 1, strand 5)
obj2 (25~30, index 2, strand 5)
obj3 (31~32, index 4, strand 5)
obj4 (33~34, index 4, strand 3)
obj5 (35~45, index 1, strand 3)
obj6 (50~55, index 2, strand3)
obj7 (60~80, index 3, strand 5)
obj8 (90~110, index 3, strand 3)
After process, the function should return a block composed of (obj1 ~ obj6)
.
Thanks in advance.
Pseudocode:
Dictionary<Index, Pairing> openPairings;
List<Index> possibilities;
foreach(pairing)
{
if(openPairings.ContainsKey(pairing.Index))
{
// This is the closing of the pair
// When there are no other open strands at the time of closing,
// AND the pairing was in the possibilities list, it could be the largest
if(possibilities.Contains(pairing.Index))
{
// The opening entry: openPairings[pairing.Index]
// The closing pairing: pairing
// Do your size check here to find the largest
}
openPairings.Remove(pairing.Index);
}
else
{
// This is the opening of the pair
// There must be no other open pairings or there will be an overlap OR
// the outer pairing is larger anyway, as it completely engulfs this pairing
if(openPairings.IsEmpty)
possibilities.Add(pairing.Index);
openPairings.Add(pairing.Index, pairing);
}
}
精彩评论