Question

In: Computer Science

Suppose we are given two skip lists, one storing a set A of m keys, and...

Suppose we are given two skip lists, one storing a set A of m keys, and the other storing a set B of n keys. Describe and analyze an algorithm to merge these into a single skip list storing the set A ∪ B in O(n + m) expected time. Do not assume that every key in A is smaller than every key in B; the two sets could be arbitrarily intermixed.

Solutions

Expert Solution

Merging two files is using a algorithm like this is, used for merging huge files in our hard disk when the total space exceed the RAM.

Now there are two cases, on is that both A and B are sorted, surely we don't know the relationship between elements together but within A and B the elements are sorted. (If not sort them first using any algorithm)

Now when you have these two, sorted List A and B then what we can do next is merge these in a sequential order

Let's say the files are sorted in descending order let's say the top of the list looks like below

A = 1,2,6,9.....

B = 2,5,8.......

Now while merging we take the first element of both the list, i.e here 1,2 and take comparison choose the smaller one to be placed in C ( final list).

Next since we chose form A next item of A is in so compare 2,2 since both are equal then we enter both, so C = (1,2,2...) and have two more elements i.e 6,5 and move ahead so on.

Once one of the list is empty, in that case the other list is already sorted so we just add it at the end of the list.

Formally we can write this Algorithm as :

Step 1: Take input from both the list, and compare

Try: If (a>b):

insert a in C, and take next value of A in a

Elif (a<b):

insert b in C, and take next value of B in B.

else:

insert a,b in C and take the next value of A,B in a,b

Except:. // When there is no more elements let in either list or both of them.

if ( A.empty()) : put all elements of B in C

. .if(B.empty()): put all elements of A in C

After all this is done the list C will have the merged result of A and B .

Comping to the result complexity, it's clearly visible that we are just going one element at the time to next one and traversing two list of size N,M so it's O(n+m)

It can be written as

T(N+M) = T(N+M-1) + 1

Description as merging two list of M+N Elem is merging lost of M+N-1 and a constant step ( compare and insert).

Hope it helps

The Lanagauge in the Ques has said it can be arbitrarily intermixed.

But if that was the intend you can do it in O(m) time by adding the elements of B at end of A or wise versa but the detailed explaination above make more sense to have a merging of sorted array.

But in case arbitrarily means that some elements of A is small then some elements of B then it's fine our described methods is the most effective way to do it in O(n+m).


Related Solutions

Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling...
Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling if L and M store the same sequence of elements (but perhaps with different starting points). Please provide a main() function with it in Java to test it.
With C++, 1. Assume we use two linked lists that represent Set A and Set B...
With C++, 1. Assume we use two linked lists that represent Set A and Set B respectively. Implement the following function to calculate A = A U B. Note that a SET should not contain duplicated elements (e.g., integers). void unionLL (Node * LA, Node * LB); 2. There are two linked lists, LA and LB. Their elements are both in the non-descending order. Implement the following function to merge LA and LB into a new linked list, LC. Make...
Write an algorithm for combining two skip lists in O(a + b) time, where a is...
Write an algorithm for combining two skip lists in O(a + b) time, where a is the number of keys in the first list, and b is the number of keys in the second list.
Suppose we are given a set ? containing 2? integers, and we wish to partition it...
Suppose we are given a set ? containing 2? integers, and we wish to partition it into two sets ?1 and ?2 so that |?1 | = |?2 | = ? and so that the sum of the numbers in ?1 is as close as possible to the sum of those in ?2. Let the neighborhood ? be determined by all possible interchanges of two integers between ?1 and ?2. Is ? exact?
Suppose that you are given a set of words; and two words from the set: word...
Suppose that you are given a set of words; and two words from the set: word 1 and word 2. Write a program which will transform word 1 into word 2 by changing a single letter in word 1 at a time. Every transition that word 1 takes will have to be in the set of words. You must output the smallest sequence of transitions possible to convert word 1 into word 2. You may assume that all the words...
Given two lists, write python code to print “True” if the two lists have at least...
Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.
A man has a set of n keys, one of which fits the door to his...
A man has a set of n keys, one of which fits the door to his apartment. He tries the keys randomly, throwing away each ill-fitting key that he tries until he finds the key that fits. That is, he chooses keys randomly from among those he has not yet tried. This way he is sure to find the right key within n tries. Let T be the number of times he tries keys until he finds the right key....
5.5. Why do we designate one of the candidate keys of a relation to be the...
5.5. Why do we designate one of the candidate keys of a relation to be the primary key? 5.6. Discuss the characteristics of relations that make them different from ordinary tables and files. 5.7. Discuss the various reasons that lead to the occurrence of NULL values in relations.
Need this in C++: Suppose that you are given a set of words; and two words...
Need this in C++: Suppose that you are given a set of words; and two words from the set: word 1 and word 2. Write a program which will transform word 1 into word 2 by changing a single letter in word 1 at a time. Every transition that word 1 takes will have to be in the set of words. You must output the smallest sequence of transitions possible to convert word 1 into word 2. You may assume...
Suppose we define a relation on the set of natural numbers as follows. Two numbers are...
Suppose we define a relation on the set of natural numbers as follows. Two numbers are related iff they leave the same remainder when divided by 5. Is it an equivalence relation? If yes, prove it and write the equivalence classes. If no, give formal justification.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT