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

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

Related

How to compare two paths

My data structure is a path represented by a list of cities. If, for example, the cities are
A, B, C, D
A possible configuration could be: A, B, D, C or D, C, A, B.
I need two compare two paths in order to find the differences between these two, in such a way that the output of this procedure returns the set of swapping operations necessary to transform the second path into the first one.
For example, given the following paths:
X = {A, B, D, C}
Y = {D, C, A, B}
indexes = {0, 1, 2, 3}
A possible way to transform the path Y into X would be the set of the following swaps: {0-2, 1-3}.
{D, C, A, B} --> [0-2] --> {A, C, D, B} --> [1-3] --> {A, B, D, C}
Is there any known (and fast) algorithm that allows to compute this set?
Your problem looks like a problem of counting the minimal number of swaps to transform one permutation to another.
In fact it's a well known problem. The key idea is to create new permutation P such that P[i] is the index of X[i] city in the Y. Then you just calculate the total number of cycles C in the P. The answer is the len(X) - C, where len(X) is the size of X.
In your case P looks like: 3, 4, 1, 2. It has two cycles: 3, 1 and 4, 2. So the answer is 4 - 2 = 2.
Total complexity is linear.
For more details see this answer. It explains this algorithm in more details.
EDIT
Okay, but how we can get swaps, and not only their number? Note, that in this solution we reorder each cycle independently doing N - 1 swaps if the length of cycle is N. So, if you have cycle v(0), v(1), ..., v(N - 1), v(N) you just need to swap v(N), v(N - 1), v(N - 1), v(N - 2), ..., v(1), v(0). So you swap cycle elements in reverse order.
Also, if you have C cycles with lengths L(1), L(2), ..., L(C) the number of swaps is L(1) - 1 + L(2) - 1 + ... + L(C) - 1 = L(1) + L(2) + ... + L(C) - C = LEN - C where LEN is the length of permutation.

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).

Find if permutation is possible

Given a permutation of natural integers from 1 to N, inclusive. Initially, the permutation is 1, 2, 3, ..., N. We are also given M pairs of integers, where the i-th is (Li,Ri). In a single turn we can choose any of these pairs (let's say with the index j) and arbitrarily shuffle the elements of our permutation on the positions from Lj to Rj, inclusive (the positions are 1-based). We are not limited in the number of turns and you can pick any pair more than once.
The goal is to obtain the permutation P, that is given. If it's possible, output "Possible", otherwise output "Impossible".
Example : Let N=7 and M=4 and array be [3 1 2 4 5 7 6] and queries are :
1 2
4 4
6 7
2 3
Here answer is Possible.
Treat each pair as an interval, compute the union of intervals as a list of non-overlapping intervals, and then test, for each i, whether the value at position i of the permutation either is i or is in the same non-overlapping interval as i.
This works because, if we have a <= b <= c <= d with pairs (a, c) and (b, d), then by repeatedly invoking (a, c) and (b, d), we can get any permutation that we could get with (a, d). Conversely, (a, d) enables any permutation that we could get with (a, c) and (b, d). Once the list of pairs is non-overlapping, it's clear that we can move element i to position j != i if and only if i and j are in the same interval.

Find cardinality of set

I have faced the following problem recently:
We have a sequence A of M consecutive integers, beginning at A[1] = 1:
1,2,...M (example: M = 8 , A = 1,2,3,4,5,6,7,8 )
We have the set T consisting of all possible subsequences made from L_T consecutive terms of A.
(example L_T = 3 , subsequences are {1,2,3},{2,3,4},{3,4,5},...). Let's call the elements of T "tiles".
We have the set S consisting of all possible subsequences of A that have length L_S. ( example L_S = 4, subsequences like {1,2,3,4} , {1,3,7,8} ,...{4,5,7,8} ).
We say that an element s of S can be "covered" by K "tiles" of T if there exist K tiles in T such that the union of their sets of terms contains the terms of s as a subset. For example, subsequence {1,2,3} is possible to cover with 2 tiles of length 2 ({1,2} and {3,4}), while subsequnce {1,3,5} is not possible to "cover" with 2 "tiles" of length 2, but is possible to cover with 2 "tiles" of length 3 ({1,2,3} and {4,5,6}).
Let C be the subset of elements of S that can be covered by K tiles of T.
Find the cardinality of C given M, L_T, L_S, K.
Any ideas would be appreciated how to tackle this problem.
Assume M is divisible by T, so that we have an integer number of tiles covering all elements of the initial set (otherwise the statement is currently unclear).
First, let us count F (P): it will be almost the number of subsequences of length L_S which can be covered by no more than P tiles, but not exactly that.
Formally, F (P) = choose (M/T, P) * choose (P*T, L_S).
We start by choosing exactly P covering tiles: the number of ways is choose (M/T, P).
When the tiles are fixed, we have exactly P * T distinct elements available, and there are choose (P*T, L_S) ways to choose a subsequence.
Well, this approach has a flaw.
Note that, when we chose a tile but did not use its elements at all, we in fact counted some subsequences more than once.
For example, if we fixed three tiles numbered 2, 6 and 7, but used only 2 and 7, we counted the same subsequences again and again when we fixed three tiles numbered 2, 7 and whatever.
The problem described above can be countered by a variation of the inclusion-exclusion principle.
Indeed, for a subsequence which uses only Q tiles out of P selected tiles, it is counted choose (M-Q, P-Q) times instead of only once: Q of P choices are fixed, but the other ones are arbitrary.
Define G (P) as the number of subsequences of length L_S which can be covered by exactly P tiles.
Then, F (P) is sum for Q from 0 to P of the products G (Q) * choose (M-Q, P-Q).
Working from P = 0 upwards, we can calculate all the values of G by calculating the values of F.
For example, we get G (2) from knowing F (2), G (0) and G (1), and also the equation connecting F (2) with G (0), G (1) and G (2).
After that, the answer is simply sum for P from 0 to K of the values G (P).

Resources