Efficient algorithm to produce the n-way intersection of sorted arrays in C -
i need produce intersection between sorted arrays of integers in c. know how find intersection between 2 sorted arrays, need more 2 arrays, efficiently , without prior knowledge of number of arrays. can impose sensible limit on maximum number - let's ten now. these arrays anywhere few items couple of hundred thousand items long, , no means same length.
pseudo-code producing intersection of 2 sorted arrays:
while < m , j < n do: if array1[i] < array2[j]: increment else if array1[i] > array2[j]: increment j else add array1[i] intersection(array1, array2) increment increment j i working c, , after clear explanation rather code.
i assume arrays sorted. lets assume have arrays a_1 a_n. have counter each array (thus, have n counters i_1 i_n, did 2 arrays).
now introduce minimum-heap, contains whole arrays in manner such minimum array array lowest number pointed corresponding pointer. means, can @ each moment, retrieve array lowest number pointed to.
now, extract minimum array heap , remember it. go on extracting minimum array long number pointed stays same. if extract all arrays (i.e. if arrays have same lowest pointed number), know number in intersection. if not (i.e. if not arrays contain same lowest pointed number), know number examining can not in intersection. thus, increment counters arrays extracted , put them heap.
we until find 1 array's pointer reaching array's end. i'm sorry undetailed description, not have enough time work out in more detail.
edit.
if have 1 array few elements, might useful binary-search other arrays these numbers or checking these numbers using hash table.
Comments
Post a Comment