0

I am trying to find an efficient way to compare a given array that represents the Quality of Service of a Client with the QOS of a Two Web Services and pick up the most similar to the given QOS.

 int[] arrayCompare(int[] clientQOS){ int[] service1QOS=new int[]{3,1,4}; int[] service2QOS=new int[]{1,3,3}; //I want to compare clientQOS with S1 QOS and S2 QOS return mostSimilarArray } 
5
  • 1
    What have you tried? Commented Oct 16, 2012 at 22:04
  • Client QOS values can vary from 0-5 so it can be something like (3,5,2) it has no weighted values. Commented Oct 16, 2012 at 22:07
  • 1
    Have a look at near duplicate detection based on hashes - it is highly efficient. Commented Oct 16, 2012 at 22:08
  • What is the expected output? I mean what should the method return. Commented Oct 16, 2012 at 22:12
  • @Grrrrr as you can see in the statement of the method its int[].The most similar Array Commented Oct 16, 2012 at 22:14

3 Answers 3

1

Loop through the source array for each target, incrementing a counter by the delta (positive difference between the values). The smallest delta sum is the closest match.

Caveats

This should be true if you consider numerically closer to be more similar, and the scale between different values are equal.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice,i think that should work fine :) i dont need something more complex as i could use a small database to give me the most similar WebService given a clientQOS
The method you described is called Manhattan distance :) Thank you that you reminded me
0

I'd make a custom object instead of array and implement interface Comparable. This way you can encapsulate algorithm of comparison and reuse it later in other places.

3 Comments

When u say algorithm of comparison ? Isn't it what i am looking for in the first place ?
Right you are. First you need to encapsulate it. Than you can follow TDD practice and create an "obvious implementation". Example of obvious implementation: number of numbers that are similar in both arrays. As long as your question can have several correct answers, encapsulation can allow to use several heuristics together in bounds of one JVM.
I think this method is great for objects more complicated than an Array with ints but i think i catch the point about the "several correct answers" Thank you very much
0

There are a lot of method for calculate array "similarity".

Here the Golang implementation:

// SimilarityPreCheck is delegated to verify that the given array have the correct size func SimilarityPreCheck(a, b []float64) bool { if len(a) == 0 || len(b) == 0 { log.Println("CosineSimilarity | Nil input data") return false } if len(a) != len(b) { log.Printf("CosineSimilarity | Input vectors have different size") return false } return true } // CosineSimilarity is delegated to calculate the Cosine Similarity for the given array func CosineSimilarity(a, b []float64) float64 { if !SimilarityPreCheck(a, b) { return -1 } // Calculate numerator var numerator float64 for i := range a { numerator += a[i] * b[i] } // Caluclate first term of denominator var den1 float64 for i := range a { den1 += math.Pow(a[i], 2) } den1 = math.Sqrt(den1) // Caluclate second term of denominator var den2 float64 for i := range b { den2 += math.Pow(b[i], 2) } den2 = math.Sqrt(den2) result := numerator / (den1 * den2) return result } // EuclideanDistance is delegated to calculate the euclidean distance for the given array func EuclideanDistance(v1, v2 []float64) float64 { if !SimilarityPreCheck(v1, v2) { return -1 } var euclidean float64 for i := range v1 { euclidean += math.Pow(v1[i]-v2[i], 2) } return math.Sqrt(euclidean) } // ManhattanDistance is delegated to calculate the Manhattan norm for the given array func ManhattanDistance(v1, v2 []float64) float64 { if !SimilarityPreCheck(v1, v2) { return -1 } var taxicab float64 for i := range v1 { taxicab += math.Abs(v2[i] - v1[i]) } return taxicab } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.