I stumbled upon a problem that I'm looking for an smart solution to. It's basically a combinatorics question.
I have five objects of which I need to select two, I can select the same one twice and it does not matter in which order I select them. This gives me ( 5 + 2 - 1 choose 2) = 15 possible combinations. I now want to reduce each combination (ie (1,1) or (2, 5)) to a number between 1 and 15. Any suggestions?
Quasi lexicographic ordering. Assign each selection its position in the lexicographic ordering, with the second component not smaller than the first.
(i,j) -> (15 - (7-i)*(6-i)/2) + (j - i) + 1
Suppose you have a combination (i, j)
Without losing generality, say i <= j
((7-i)+5)*(i-1)/2 + (j-i+1)
You'll have
(1, 1)->1 (2, 2)->6 (3, 3)->10 (4, 4)->13 (5, 5)->15
(1, 2)->2 (2, 3)->7 (3, 4)->11 (4, 5)->14
(1, 3)->3 (2, 4)->8 (3, 5)->12
(1, 4)->4 (2, 5)->9
(1, 5)->5
Basically you'll first have the number of combinations before column i, then plus (j-i+1) as the row number
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.