I'm trying to write an efficient pair matching algorithm for a color matching context
Let's say we have an object (representing a CIE-LAB colour):
public class labColour {
double l;
double a;
double b;
}
Now let's say we have two lists of LabColour:
List<LabColour> list1;
List<LabColour> list2;
Each list is of size n. Finally let's say we have a complex function which can calculate the similarity/distance between two colours:
public double cie94ColourDistance(LabColour labColour1, LabColour labColour2) {
final double L1 = labColour1.getL();
final double a1 = labColour1.getA();
final double b1 = labColour1.getB();
final double L2 = labColour2.getL();
final double a2 = labColour2.getL();
final double b2 = labColour2.getL();
double c1 = Math.sqrt(a1 * a1 + b1 * b1);
double deltaC = c1 - Math.sqrt(a2 * a2 + b2 * b2);
double deltaA = a1 - a2;
double deltaB = b1 - b2;
double deltaH = Math.sqrt(Math.max(0.0, deltaA * deltaA + deltaB * deltaB - deltaC * deltaC));
return Math.sqrt(Math.max(0.0, Math.pow((L1 - L2) / (kL * sl), 2) + Math.pow(deltaC / (kc * (1 + K1 * c1)), 2) + Math.pow(deltaH / (kh * (1 + K2 * c1)), 2.0)));
}
I want to find the best match between each of the n
elements in the two lists. The naive approach would be to use a nested for loop to iterate through list1
and list2
, and use cie94ColourDistance
to calculate the best match for each. This is computationally expensive at O(n^2) complexity.
Can I do this faster O(n^2)? If so, how?
Also, is there a way (e.g. multithreading, a certain library, caching etc.) to speed the whole thing up in Java?
Thanks in advance!