Clustering number pairs - sorting

I have a set of number pairs in random order in an array as follows:
(5,6) (2,7) (3,7) (1,2) (3,4) (4,5)
I need to link all these numbers based on the common number when comparing pairs. So my expected output would be: 1,2,7,3,4,5,6. I can easily get it if pairs can be ordered in the sequence of (1,2) (2,7) (3,7) (3,4) (4,5) (5,6). Any algorithm that support this sorting?

Related

Dividing rocks into minimum number of groups

Given a list of rocks' weights and an integer maxVariance, divide the rocks into groups such that within a group the difference in weight between any two rocks is less than or equal to maxVariance. Find the minimum number of groups to divide all the rocks.
Example:
weights = [5,3,2,4,6]
maxVariance = 2
Output: 2
Explanation: divide into two groups [3,2,4] and [5,6]
My intuition is to sort the weights in ascending order and divide greedily, but I can't prove that this will definitely work. What do you guys think?

Random generation of tuple (A, B) so that A + B <= C?

After the discussion in the comments I must note that terms used here are out of math context, not programming context.
How can I uniformly generate random tuples of natural numbers A and B, so that A + B <= C, where C is constant?
Each possible tuple that meets the criteria should have an equal chance of being generated. For the purposes of this question, a natural number means a positive integer greater than or equal to 1.
Wrong solution (just to explain the question): take random A from 1 to C, take random B from A to C. This way you're as likely to get a tuple where A = 1 as the tuple where A = C, but you have C tuples of first kind and only 1 tuple of the second kind, so individual tuples of these types don't appear with the same probability.
For a given natural number C, there are (C-1) * C / 2 possible natural number* tuples where A + B <= C
e.g. C = 5, the 10 possible natural number tuples are:
(1,1), (1,2), (1,3), (1,4)
(2,1), (2,2), (2,3)
(3,1), (3,2)
(4,1)
So you could choose a random value between [1, (C-1) * C / 2] and find the tuples based on that.
To make it easier to find the tuple, imagine the list doubled with the triangle flipped around and fitted to itself:
(1,1), (1,2), (1,3), (1,4), (4,1)
(2,1), (2,2), (2,3), (3,2), (3,1)
(3,1), (3,2), (2,3), (2,2), (2,1)
(4,1), (1,4), (1,3), (1,2), (1,1)
Now you just need one random number for the row in the range [1, C-1] and one for the column in the range [1, C]
If the row + column <= C then A = row, B = column
Otherwise A = C - row, B = C + 1 - column
(*) Going by the definition of a natural number as a "positive integral starting with 1" given by the OP, which is not the only possible definition of a natural number
Edited for updated question
Generate two numbers:
X ~ U(1, C)
Y ~ U(1, C - X)
Now toss a coin:
with probability 1/2, A <- X, B <- Y
with probability 1/2, A <- Y, B <- X

finding the maximum sum in Matrix

Input: A 2-dimensional NxN -symmetric Matrix - with NxN positive elements.
Output: A 2-dimensional matrix of NxN size with N selected elements such that its summation is the maximum among all possible selection. Other elements that are not selected are zero. In other words, we should select N elements from matrix to return maximum sum.
Requirement: If the dimension of matrix is 4*4, we should select 4 integer. Every row and column in matrix should not be used more than 2 times.
For example if we have 4*4 matrix, the following element could be selected:
(1,2)
(2,3)
(3,4)
(4,1)
but if we select (1,2)and (4,1), we cannot select (1,3), because we used 1 two times.
IS there an efficient algorithm for this problem?
This problem is in that weird place where it's not obvious that there's a solution polytope with integral vertices and yet its similarity to general matching discourages me from looking for an NP-hardness reduction. Algorithms for general matching are complicated enough that modifying one would be a daunting prospect even if there were a way to do it, so my advice would be to use an integer program solver. There's a formulation like
maximize sum_{ij} w_{ij} x_{ij}
subject to
sum_{ij} x_{ij} = n
forall k, sum_i x_{ik} + sum_j x_{kj} ≤ 2
forall ij, x_{ij} in {0, 1},
where w_{ij} is the ijth element of the weight matrix.

Maximum number of distinct inversions in an array

Given an array A of n integers, we say that a pair of indices i<j∈[n] is an inversion in A if A[i]>A[j]. What is the maximum number of distinct inversions that A can have?
Is it
a) n - 1
b) n
c) n(n−1)/2
d) n^2
e) n(n−1)(2n−1)/6
Well, it's obviously possible for all pairs of distinct indices to be inversions (if the entire array is in reverse order, e.g.: [5,4,3,2,1]). And it's obviously not possible for more than all pairs of distinct indices to be inversions.
So the question is: how many pairs of distinct indices are there?
If you arrange them geometrically, the pattern is pretty clear:
(1,2) (1,3) (1,4) (1,5)
(2,3) (2,4) (2,5)
(3,4) (3,5)
(4,5)
(Note that I didn't include e.g. (2,1), since that's the same two indices as (1,2).)
Such numbers are called triangular numbers, for obvious reasons. Wikipedia gives a formula, but be sure not to confuse the n in its formula with the n in your problem statement. (They are slightly different. You'll need to do a small amount of algebra.)

Algorithm for matching point sets

I have two sets of points A and B, whereas the points can be 2D or 3D. Both sets have the same size n, which is rather low (5 - 20).
I would like to know how well these sets agree. That is, ideally I would find pairings between the points such that the sum of all Euclidean pair distances d(A,B) is minimal. So
d(A,B) = \sum_{i=1}^n ||A_i - B_i||_2
The final outcome is used to compare with other point sets. So, for example:
A = (1,1), (1,2), (1,3)
B = (1,1), (2,2), (1,3)
would give me d(A,B) = 1.
C = (1,1), (2,1), (3,1)
D = (2,1), (2,2), (3,1)
would give me d(C,D) = 1.414.
Any good ideas?
You can for example model your problem as an assignment problem (Wikipedia link), where you define the cost C_ij of assigning point A_i (from set A) to point B_j (from set B) to be equal to the distance between them. This assignment problem can then be solved using the Hungarian algorithm (Wikipedia link).

Resources