What's a good algorithm for searching arrays N and M, in order to find elements in N that also exist in M?
Date : March 29 2020, 07:55 AM
I hope this helps . 1000 is a very small number. Also, keep in mind that parallelizing a search will only give you speedup as the number of cores you have increases. If you have more threads than cores, your application will start to slow down again due to context switching and aggregating information. A simple solution for your problem is to use a hash join. Build a hash table from M, then look up the elements of N in it (or vice versa; since both your arrays are small it doesn't matter much).
|
MongoDB - Is searching in arrays as fast as searching in plain keys?
Date : March 29 2020, 07:55 AM
this will help 1st query: Using an Array is probably slower than using a plain key.
|
Algorithm help! Fast algorithm in searching for a string with its partner
Tag : chash , By : pacorro2000
Date : March 29 2020, 07:55 AM
To fix this issue I thought this was an interesting problem, so I put together a program based on considering 'foldings', which scans outward for possible symmetrical matches from different 'fold points'. If N is the number of nucleotides and M is 'maxInterval-minInterval', you should have running time O(N*M). I may have missed some boundary cases, so use the code with care, but it does work for the example provided. Note that I've used a padded intermediate buffer to store the genome, as this reduces the number of comparisons for boundary cases required in the inner loops; this trades off additional memory allocation for better speed. Feel free to edit the post if you make any corrections or improvements. class Program
{
public sealed class Pairing
{
public int Index { get; private set; }
public int Length { get; private set; }
public int Offset { get; private set; }
public Pairing(int index, int length, int offset)
{
Index = index;
Length = length;
Offset = offset;
}
}
public static IEnumerable<Pairing> FindPairings(string genome, int minLen, int maxLen, int intervalMinLen, int intervalMaxLen)
{
int n = genome.Length;
var padding = new string((char)0, maxLen);
var padded = string.Concat(padding, genome, padding);
int start = (intervalMinLen + minLen)/2 + maxLen;
int end = n - (intervalMinLen + minLen)/2 + maxLen;
//Consider 'fold locations' along the genome
for (int i=start; i<end; i++)
{
//Consider 'odd' folding (centered on index) about index i
int k = (intervalMinLen+2)/2;
int maxK = (intervalMaxLen + 2)/2;
while (k<=maxK)
{
int matchLength = 0;
while (IsPaired(padded[i - k], padded[i + k]) && (k <= (maxK+maxLen)))
{
matchLength++;
if (matchLength >= minLen && matchLength <= maxLen)
{
yield return new Pairing(i-k - maxLen, matchLength, 2*k - (matchLength-1));
}
k++;
}
k++;
}
//Consider 'even' folding (centered before index) about index i
k = (intervalMinLen+1)/2;
while (k <= maxK)
{
int matchLength = 0;
while (IsPaired(padded[i - (k+1)], padded[i + k]) && (k<=maxK+maxLen))
{
matchLength++;
if (matchLength >= minLen && matchLength <= maxLen)
{
yield return new Pairing(i - (k+1) - maxLen, matchLength, 2*k + 1 - (matchLength-1));
}
k++;
}
k++;
}
}
}
private const int SumAT = 'A' + 'T';
private const int SumGC = 'G' + 'C';
private static bool IsPaired(char a, char b)
{
return (a + b) == SumAT || (a + b) == SumGC;
}
static void Main(string[] args)
{
string genome = "ATCAGGACCATACGCCTGAT";
foreach (var pairing in FindPairings(genome, 4, 5, 9, 10))
{
Console.WriteLine("'{0}' pair with '{1}'",
genome.Substring(pairing.Index, pairing.Length),
genome.Substring(pairing.Index + pairing.Offset, pairing.Length));
}
Console.ReadKey();
}
}
|
Searching for specific sub-arrays in a multidimensional array, then loop through the found sub-arrays to get all the val
Date : March 29 2020, 07:55 AM
it should still fix some issue Loop through the main array and then just use the keys of the inner array: foreach ($cartArray as $prod => $option) {
if (strpos($prod, "prod_") === 0) {
echo $option["qty"];
//etc...
}
}
|
Why is searching arrays using cts different than searching arrays using node api?
Date : March 29 2020, 07:55 AM
To fix the issue you can do By default, the SJS searches run filtered, which will remove any false positive results. You can toggle that behavior by adding explicit options to the SJS search: cts.search(queryText, "unfiltered");
const query = qb.where(queryText)
.withOptions({search:['filtered']});
|