Lambert's O(n^2) algorithm on pairwise sum sort - algorithm

Sorting Pairwise Sums of X and Y with O(n^2) comparisons are quite hard.
http://cs.smith.edu/~orourke/TOPP/P41.html
http://en.wikipedia.org/wiki/X_%2B_Y_sorting
It seems Lambert has concluded that if some non-comparison operations are allowed, then it is achievable.
I can find his original paper: http://www.sciencedirect.com/science/article/pii/030439759290089X
Also I found a functional approach for his algorithm.
However, I really don't understand even after several times of attempts.
So what I roughly know is his algorithm tries:
tag element with its original index
negate it
using the equation of X - Y < X' - Y' == X - X' < Y - Y'.
Other than that I am totally lost.
So anyone here really understands that algorithm? Can anyone clearly explain it in a simple way?

Related

Fast solution of linear equations starting from approximate solution

In a problem I'm working on, there is a need to solve Ax=b where A is a n x n square matrix (typically n = a few thousand), and b and x are vectors of size n. The trick is, it is necessary to do this many (billions) of times, where A and b change only very slightly in between successive calculations.
Is there a way to reuse an existing approximate solution for x (or perhaps inverse of A) from the previous calculation instead of solving the equations from scratch?
I'd also be interested in a way to get x to within some (defined) accuracy (eg error in any element of x < 0.001), rather than an exact solution (again, reusing the previous calculations).
You could use the Sherman–Morrison formula to incrementally update the inverse of matrix A.
To speed up the matrix multiplications, you could use a suitable matrix multiplication algorithm or a library tuned for high-performance computing. The classic matrix multiplication has complexity O(n³). Strassen-type algorithms have O(n^2.8) and better.
A similiar question without real answer was asked here.

Why does this algorithm have a O(n^2) time instead of O(n^3)?

I've been studying Big-O for an upcoming interview and I wanted something explained.
So one way I've been determining the run-time of algorithms is by counting the number of for-loops. For example, if an algorithm uses 1 for-loop I'd count that as O(n)
and if it used two for-loops I'd say it was O(n^2). I find that this is generally an easy way to figure out time for simple algorithms. Although in a textbook I am reading they said this algorithm has an O(n^2) run-time.
a=5
b=6
c=10
for i in range(n):
for j in range(n):
x = i * i
y = j * j
z = i * j
for k in range(n):
w = a*k + 45
v = b*b
d = 33
How would this have an O(n^2) run-time? If 𝑇(𝑛)=3+3𝑛2+2𝑛+1=3𝑛2+2𝑛+4 then are we just discarding the last loop (2n) because it does not add to 3n^2?
Thanks for reading!
Let's look at what the curves look like:
You can see here what a curve for just N looks like, along with the curve for N^2 and their sum. Notice anything? The N^2+N curve looks a lot more like an N^2 curve than an N curve, adding the N didn't really change it a whole lot. In fact, if we scale the sum curve by a constant (.93 in this case), we can make them look almost identical:
If we're analyzing algorithms, at least theoretically, we're generally worried about the factors that will dominate our processing time. If adding a factor of N to the runtime doesn't change much, why bother thinking about it? We should worry about the quadratic behavior. This isn't necessarily true in real life, where we might really care if something is a constant 2x factor, but at least for theoretical analysis it is.
This is why big-O notation is an asymptotic measure of performance. We only consider what happens as N gets very large. If we think about the ratio N^2/N as N -> infinity, it's obvious that this will trend to zero. So, the larger n gets, the less important the linear factor is, and thus we discard it.

Complexity of solving a Diophantine equation with potential solutions

Say I have an equation, given as
13x + 5y = M, where M is given each time.
Evidently this is a diophantine equation and could take a long time to solve depending on the case. However, a reading told me that if we have a set of unique integer "possible solutions" of size k for X and Y stored in a Binary search tree (meaning the correct values for X and Y are contained in there somewhere), we can compute the solution pair (x, y) to the equation in O(k) time.
Now, I'm stuck on this logic because I do not see how storing elements in this data structure helps us or prevents us from having to plug in each of the k elements for X or Y, solve for the other variable, and check if the data structure contains that variable. The only thing I can think of would be somehow keeping two pointers to move along the tree, but that doesn't seem feasible.
Could someone explain the reasoning behind this logic?
Solving linear Diophantine equations (which is what you seem to be thinking of) is trivial and requires nothing more than the extended Euclidian algorithm. On the other hand, the successful resolution of Hilbert's tenth problem implies that there is no algorithm which is able to solve arbitrary Diophantine equations.
There is a vast gap between linear and arbitrary. Perhaps you can focus your question on the type of equation you are interested in.

If Y is reducible to X in polynomial time, then how is it true that X is at least as hard as Y?

I am having difficulty understanding the relationship between the complexity of two classes of problems, say NP-hard and NP-complete problems.
The answer at https://stackoverflow.com/a/1857342/ states:
NP Hard
Intuitively, these are the problems that are at least as hard as the NP-complete problems. Note that NP-hard problems do not have to be in NP, and they do not have to be decision problems.
The precise definition here is that a problem X is NP-hard, if there is an NP-complete problem Y, such that Y is reducible to X in polynomial time.
If a problem Y can be reduced to X in polynomial time, should we not say that Y is at least as hard as X? If a problem Y is reducible to X in polynomial time, then the time required to solve Y is polynomial time + the time required to solve X. So it appears to me that problem Y is at least as hard as X.
But the quoted text above says just the opposite. It says, if an NP-complete problem Y is reducible to an NP-hard problem X, then the NP-hard problem is at least as hard as the NP-complete problem.
How does this make sense? Where am I making an error in thinking?
Your error is in supposing that you have to solve X in order to solve Y. Y might be actually much easier, but one way to solve it is to change it to an instance of X problem. And since we are in big O notation and in NP class we are way past linear algorithms, you can always safely discard any linear parts of an algorithm. Heck you can almost safely discard any polynomial parts until P=NP problem is solved. That means O(f(n) + n) = O(f(n)) where n=O(f(n)).
Example (which is obviously with neither NP-hard or NP-complete problems but just a mere illustration): You are to find the lowest number in an unsorted array of n numbers. There is obvious solution to iterate over the whole list and remember the lowest number you found, pretty straight-forward and solid O(n).
Someone else comes and says, ok, let's change it to sorting the array, then we can just take the first number and it will be the lowest. Note here, that this conversion of the problem was O(1), but we can for example pretend there had to be some preprocessing done with the array that would make it O(n). The overall solution is O(n + n*log(n)) = O(n * log(n)).
Here you too changed easy problem to a hard problem, thus proving that the hard problem is indeed the same or harder as the easy one.
Basically what the NP-hard problem difinition means is, that X is at least as hard as an NP-complete Y problem. If you find an NP-complete Y problem that you can solve by solving X problem, it means either that X is as hard or harder than Y and then it is indeed NP-hard, or if it is simpler, it means you found an algorithm to solve Y faster than any algorithm before, potentially even moving it out of NP-complete class.
Another example: let's pretend convolution is in my set of "complete", and normally takes O(n²). Then you come up with Fast Fourier Transformation with O(n * log(n)) and you find out you can solve convolution by transforming it to FFT problem. Now you came up with a solution for convolution, which is o(n²), more specifically O(n * log(n)).
Let I_X be the indicator function of X (i.e., 1 if the input is in X and 0 otherwise) and I_Y be the indicator function of Y. If Y reduces to X via a function f that can be computed in polynomial-time, then I_Y = I_X . f, where . denotes function composition. X is at least as hard as Y because, given an algorithm for I_X, the formula above gives an algorithm for I_Y that, for any class of running times closed under polynomial substitution (e.g., polynomial, exponential, finite), if the algorithm for I_X belongs to the class, then so does the algorithm for I_Y. The contrapositive of this statement is, if Y has no fast decision procedure, then X has no fast decision procedure.

Finding the best BigO notation with two strong terms

I am asked to find the simplest exact answer and the best big-O expression for the expression:
sum n, n = j to k.
I have computed what I think the simplest exact answer is as:
-1/2(j-k-1)(j+k)
Now when I go to take the best possible big-O expression I am stuck.
From my understanding, big-O is just finding the operation time of the worst case for an algorithm by taking the term that over powers the rest. So like I know:
n^2+n+1 = O(n^2)
Because in the long run, n^2 is the only term that matters for big n.
My confusion with the original formula in question:
-1/2(j-k-1)(j+k)
is as to what the strongest term is? To try and solve again I try factoring to get:
-1/2(j^2-jk-j+jk-k^2-k)
Which still does not make itself clear to me since we now have j^2-k^2. Is the answer I am looking for O(k^2) since k is the end point of my summation?
Any help thanks.
EDIT: It is unspecified as to which variable (j or k) is larger.
If you know k > j, then you have O(k^2). Intuitively, that's because as numbers get bigger, squares get farther apart.
It's a little unclear from your question which variable is the larger of the two, but I've assumed that it's k.

Resources