Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting 2 numbers n times and placing back the addition in O(n) instead of O(n*log(n))

Tags:

I'm presenting a problem my professor showed in class, with my O(n*log(n)) solution:

Given a list of n numbers we'd like to perform the following n-1 times:

  • Extract the two minimal elements x,y from the list and present them
  • Create a new number z , where z = x+y
  • Put z back into the list

Suggest a data structure and algorithm for O(n*log(n)) , and O(n)

Solution:

We'll use a minimal heap:

Creating the heap one time only would take O(n). After that, extracting the two minimal elements would take O(log(n)). Placing z into the heap would take O(log(n)).

Performing the above n-1 times would take O(n*log(n)), since:

O(n)+O(n∙(logn+logn ))=O(n)+O(n∙logn )=O(n∙logn ) 

But how can I do it in O(n)?

EDIT:

By saying: "extract the two minimal elements x,y from the list and present them ", I mean printf("%d,%d" , x,y), where x and y are the smallest elements in the current list.

like image 549
JAN Avatar asked Jun 19 '12 00:06

JAN


1 Answers

This is not a full answer. But if the list was sorted, then your problem is easiy doable in O(n). To do it, arrange all of the numbers in a linked list. Maintain a pointer to a head, and somewhere in the middle. At each step, take the top two elements off of the head, print them, advance the middle pointer until it is where the sum should go, and insert the sum.

The starting pointer will move close to 2n times and the middle pointer will move about n times, with n inserts. All of those operations are O(1) so the sum total is O(n).

In general you cannot sort in time O(n), but there are a number of special cases in which you can. So in some cases it is doable.

The general case is, of course, not solvable in time O(n). Why not? Because given your output, in time O(n) you can run through the output of the program, build up the list of pairwise sums in order as you go, and filter them out of the output. What is left is the elements of the original list in sorted order. This would give a O(n) general sorting algorithm.

Update: I was asked to show how could you go from the output (10, 11), (12, 13), (14, 15), (21, 25), (29, 46) to the input list? The trick is that you always keep everything in order then you know how to look. With positive integers, the next upcoming sum to use will always be at the start of that list.

Step 0: Start   input_list: (empty)   upcoming sums: (empty)  Step 1: Grab output (10, 11)   input_list: 10, 11   upcoming_sums: 21  Step 2: Grab output (12, 13)   input_list: 10, 11, 12, 13   upcoming_sums: 21, 25  Step 3: Grab output (14, 15)   input_list: 10, 11, 12, 13, 14, 15   upcoming_sums: 21, 25, 29  Step 4: Grab output (21, 25)   input_list: 10, 11, 12, 13, 14, 15   upcoming_sum: 29, 46  Step 5: Grab output (29, 46)   input_list: 10, 11, 12, 13, 14, 15   upcoming_sum: 75 
like image 160
btilly Avatar answered Oct 13 '22 03:10

btilly