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
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...
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.    In a multiple regression model, the following coefficients were obtained: b0 = -10      b1 =...
1.    In a multiple regression model, the following coefficients were obtained: b0 = -10      b1 = 4.5     b2 = -6.0 a.    Write the equation of the estimated multiple regression model. (3 pts) b     Suppose a sample of 25 observations produces this result, SSE = 480. What is the estimated standard error of the estimate? (5 pts) 2.    Consider the following estimated sample regression equation: Y = 12 + 6X1 -- 3 X2 Determine which of the following statements are true,...
1) Describe an example of each of the following that may be found of your kitchen:...
1) Describe an example of each of the following that may be found of your kitchen: Explain how your choice falls into this category, and if there is a chemical name or symbol for it, provide that as well. Provide a photo of your example with your ID card in it. a) a compound b) a heterogeneous mixture c) an element (symbol) Moving to the Caves… Lechuguilla Caves specifically. Check out this picture of crystals of gypsum left behind in...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
The Business Case for Agility “The battle is not always to the strongest, nor the race...
The Business Case for Agility “The battle is not always to the strongest, nor the race to the swiftest, but that’s the way to bet ’em!”  —C. Morgan Cofer In This Chapter This chapter discusses the business case for Agility, presenting six benefits for teams and the enterprise. It also describes a financial model that shows why incremental development works. Takeaways Agility is not just about the team. There are product-management, project-management, and technical issues beyond the team’s control. Lean-Agile provides...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...