Question

Let A = {a1, . . . , ak} and B = {b1,. . . ,...

Let A = {a1, . . . , ak} and B = {b1,. . . , bm} be two sets of numbers. Consider the problem of finding the difference set of A and B(A–B), i.e., the set of all elements of A that are not elements of B.

a)Design a n O(nlogn) algorithm for solving this problem.

b)Show that the worst-case complexity of your algorithm is O(n logn)

Homework Answers

Answer #1

Solution

Part 1

A = {a1, . . . , ak} and B = {b1,. . . , bm}

The required algorithm is given below:

  • Elements of set A are stored in array A. If set has k elements the index of first element in array A is 0 and the index of the last element is k-1
  • Elements of array B are stored in array B. If set has m elements the index of first element in array B is 0 and the index of the last element is m-1
  • Array B is sorted using merge sort.
  • Scan array A one element at a time starting from index 0 and corresponding to each element in A, a binary search is performed in array B.
  • If the binary search is succesful ,the element will not be present in A-B.
  • If the binary search is not successful the element is present in A but is not present in B. So it is present in A-B . Print the element

Part 2

Consider array A has k elements and array B has m elements.

Sorting the elements of array B using merge sort results in mlog2m time complexity.

Corresponding to each element of array A a binary search is performed in array B.

Binary search of a single element in an array of size m takes log2m complexity.

So for all the elements of array A, total time complexity due to binary search is k*log2m

Hence total complexity = mlog2m + k*log2m

For the problem lets consider both array A and array B has n elements each.In this case sorting of array B results in nlog2n complexity. And the binary search of n elements of array A in array B one by one results in n * log2n complexity.

So total complexity = nlog2n + nlog2n = 2nlog2n = nlog2n = O(nlogn)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete this function (the instructions are below the code): private static int numCommonElements(int A[], int B[],...
Complete this function (the instructions are below the code): private static int numCommonElements(int A[], int B[], int lenA, int lenB) { // Complete this function. } We want to solve the following problem: given two sorted arrays A[n] and B[m] of length n and m respectively, we want to nd the number of elements that are common to both the arrays (without repeated counting of the same element). For example, the arrays A[ ] = {7; 13; 19; 20; 22;...
1. Suppose you are given 3 algorithms A1, A2 and A3 solving the same problem. You...
1. Suppose you are given 3 algorithms A1, A2 and A3 solving the same problem. You know that in the worst case the running times are T1(n) = (10^4)n, T2(n) = 10n , T3(n) = (10^3)(n^3) log10 n (a) Which algorithm is the fastest for very large inputs? Which algorithm is the slowest for very large inputs? (Justify your answer.) (b) For which problem sizes is A1 the best algorithm to use (out of the three)? Answer the same question...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
We denote |S| the number of elements of a set S. (1) Let A and B...
We denote |S| the number of elements of a set S. (1) Let A and B be two finite sets. Show that if A ∩ B = ∅ then A ∪ B is finite and |A ∪ B| = |A| + |B| . Hint: Given two bijections f : A → N|A| and g : B → N|B| , you may consider for instance the function h : A ∪ B → N|A|+|B| defined as h (a) = f (a)...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Suppose you are given a set S = {a1,a2,···,an} of tasks, where task ai requires pi...
Suppose you are given a set S = {a1,a2,···,an} of tasks, where task ai requires pi units of processing time to complete, once it has started. You have one computer on which to run these tasks, and the computer can run only one task at a time. Let ci be the completion time of task ai, that is, the time at which task ai completes processing. Your goal is to minimize the average completion time, that is, to minimize n1...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1.Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2,...
Let A[1, . . . , n] be an array of n distinct numbers. If i...
Let A[1, . . . , n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. 1. Which arrays with distinct elements from the set {1, 2, . . . , n} have the smallest and the largest number of inversions and why? State the expressions exactly in terms of n. 2. For any 0 < a < 1/2, construct an array for...
Complete the proof Let V be a nontrivial vector space which has a spanning set {xi}...
Complete the proof Let V be a nontrivial vector space which has a spanning set {xi} ki=1. Then there is a subset of {xi} ki=1 which is a basis for V. Proof. We will divide the set {xi} ki=1 into two sets, which we will call good and bad. If x1 ≠ 0, then we label x1 as good and if it is zero, we label it as bad. For each i ≥ 2, if xi ∉ span{x1, . ....
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT