Algorithm of two array intersection

Source: Internet
Author: User
Tags ranges

In Lucene, if the user's query vector term t = {Xx,xx,......},booleanquery is and, each t----> corresponds to the resulting inverted table, the inverted table is composed of many inverted index entries, then the duplicate document number is then sorted. The core idea of the device is similar to the following:

Existing two arrays: int []data1 = {12,45,65,2,5} int []data2 = {12,5,-8,9}, take the intersection of the two.

The implementation of a number of scenarios, now take a relatively good time and space algorithm: set compression algorithm, namely: 1. Calculate the Min and max (range of values) of two arrays separately, add to Rangelist, and then calculate the repeated values in the Rangelis. Added to the result (list);

2. Calculate the intersection of the value range of the rangelist, such as [1,20,3,15], two array of values range intersection is [3,15], placed in the array, and then according to the intersection of the two arrays are removed from the values in the range, empty rangelist, Qing Zero Group;

3. Repeat the above steps until the termination condition is met.

The above is the logical implementation, the most important is the data results, because in this process, the values in the array will be continuously removed, so the underlying use of chain-stored linear table, performance will be higher.

This afternoon try to write this algorithm, after debugging, accurate, now upload code, for sharing:

Package com.txq.dataStructure;

Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.Collections;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Queue;
Import Java.util.Set;
Import Java.util.concurrent.ConcurrentLinkedDeque;

/**
* Intersection of two arrays
* @author Tongxueqiang
* @date 2016/05/12
*/

public class Dataintersection {
A linked list that stores raw data
Private queue<integer> data1 = new concurrentlinkeddeque<integer> ();
Private queue<integer> data2 = new concurrentlinkeddeque<integer> ();
A collection of two data set value ranges
Private list<integer> Minandmax = new arraylist<integer> ();
Holds the intersection of two range of values
Private int[] intersection = new Int[2];
A result set that stores the intersection of two datasets
Private list<integer> result = new arraylist<integer> ();

Public list<integer> intersection (int[] originaldata, int[] originalData1) {
//1. Data backup
for (int i:originaldata) {
Data1.add (i);
}
for (int j:originaldata1) {
Data2.add (j);
}
While (true) {
///2. Calculate the range of values for the two sets to add to Minandmax
Minandmax.add (Collections.min (data1));
Minandmax.add (Collections.max (data1));
Minandmax.add (Collections.min (data2));
Minandmax.add (Collections.max (data2));
//3. Calculate the same value in the Minandmax, add to result, and then remove the same value from the two data sets respectively
int intersect;
int sum1 = SUM (Minandmax);
set<integer> Set = new hashset<integer> (Minandmax);
int sum2 = SUM (set);
if (set.size () = = 1) {//If set size==1, direct output result
for (int i:set)
Result.add (i);
return result;
} else if (Minandmax.size ()-set.size () = = 1) {
intersect = sum1-sum2;
//Add to result
Result.add (intersect);
//Data1 and Data2 respectively removed intersection
Data1.remove (intersect);
Data2.remove (intersect);
} else if (set.size () = = 2) {//set itself is intersection
if (minandmax.get (0) = = Minandmax.get (1) && minandmax.get (2) = = Minandmax.get (3)) {
return null;
} else if (minandmax.get (0)! = Minandmax.get (1) && Minandmax.get (2)! = Minandmax.get (3)) {
//Add to result
Result.addall (set);
//Data1 and Data2 respectively removed intersection
for (int num:set) {
data1.remove (num);
data2.remove (num);
}
} else if (minandmax.get (0)! = Minandmax.get (1) | | minandmax.get (2)! = Minandmax.get (3)) {
if (minandmax.get (0)! = Minandmax.get (1)) {
intersect = Minandmax.get (2);
//Add to result
Result.add (intersect);
//Data1 and Data2 respectively removed intersection
Data1.remove (intersect);
Data2.remove (intersect);
}
if (Minandmax.get (2)! = Minandmax.get (3)) {
intersect = minandmax.get (0);
//Add to result
Result.add (intersect);
//Data1 and Data2 respectively removed intersection
Data1.remove (intersect);
Data2.remove (intersect);
}
}

}

//4. Calculates the intersection of two ranges of values into the intersection array

intersection[1] = Math.min (Minandmax.get (1), Minandmax.get (3));

5. Remove values that are not in the intersection range, respectively
Removenotinintersection (DATA1);
Removenotinintersection (DATA2);
Termination conditions
if (Intersection[0] > intersection[1] | | data1.size () = = 0 | | data2.size () = = 0) {
Break
}
6. Clear the Minandmax, clear 0 intersection
Minandmax.clear ();
intersection = Setintersecttozero (intersection);
}
return result;

}

Private int[] Setintersecttozero (int[] intersection) {
for (int i = 0; i < intersection.length; i++) {
Intersection[i] = 0;
}
return intersection;
}

/**
* Remove values that are not in the intersection range, respectively
*
* @param data
*/
private void Removenotinintersection (queue<integer> data) {
if (data.size () = = 0) {
return;
}
int min = collections.min (data);
int max = Collections.max (data);
While (Max > intersection[1] | | min < intersection[0]) {
if (Max > intersection[1])
Data.remove (max);
if (min < intersection[0])
data.remove (min);
if (data.size () = = 0) {
Break ;
}
min = collections.min (data);
max = Collections.max (data);
}
}

/**
* Summation
*
* @param Collection
* @return
*/
Private Integer sum (collection<integer> Collection) {
int sum = 0;
for (int i:collection) {
sum + = i;
}
return sum;
}
}

Algorithm of two array intersection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.