4
The most appealing solution so far seems to be a spin on Python's own permutations function (source) that can be slightly simplified for this use case; thanks @Peilonrayz:
def combinations(r: int, n: int=10) -> Iterable[Sequence[int]]:
pool = range(n)
indices = list(pool)
cycles = list(range(n, n-r, -1))
r_range = range(r-1, -1, -1)
...
4
Here is a simple iterative solution. We maintain two arrays, an output array $L$, and a Boolean array $A$, which keeps track of the elements currently in $L$. We update $A$ as we add and remove elements from $L$.
We initialize $L$ with $0,\ldots,k-1$, which is also our first output. Now we repeatedly try to "increment" $L$. If successful, we output ...
answered Dec 26 '20 at 15:38
Yuval Filmus
241k2020 gold badges243243 silver badges426426 bronze badges
3
The way Raymond Hettinger's itertools permutations works is by following the cyclical nature of permutations. And converting from a recursive to iterative function.
Establishing a recursive pattern
We can build a recursive function to show us the underlying pattern. We can build off of Yuval Filmus's recursive solution. However we should change the code to ...
2
The problem of iterating all partial permutations is closely related to the problem of generating a uniform random sequence $U$ of $k$ (u)nique integers from $0 \le U[j] < n$. The usual solution to that problem is first to generate a uniform (r)andom sequence of numbers $R$ satisfying
$|R| = k$
$0 \le R[j] < n - j$
then covert $R$ to $U$. So for ...
2
You can indeed formulate an Integer Linear Program. Let $x_i \in \{0,1\}$ denote whether you select item $i$. For each item $i$ define $f_a(i)$, $f_b(i)$, and $f_c(i)$ as the integer values for the properties $A$, $B$, and $C$.
Then we define the objective function as maximizing $\sum_{i = 1}^{m} f_a(i) \cdot x_i$, given $m$ items in total.
Your constraints ...
2
It's a bad practice to use the same variable for two purposes, so I'll say that you have $C$ conflicts $(c_1, c_2, \dots, c_C)$ and similarly $M$ conflict resolution methods. To simplify, let $r_{ji} = 0$ if $c_i$ and $m_j$ are disconnected.
Suppose $t_j$ is a variable indicating how many times method $m_j$ was used we can phrase your problem as
\begin{align}...
1
What's wrong with (i) + j + k ?
I live in a world where floating-point arithmetic has rounding errors, and where integer arithmetic has overflow checks, so (i + j) + k and i + (j + k) are most definitely not the same, for example if i is a large positive integer, and j, k are two large negative integers then the second one can produce an overflow (crash) ...
Only top voted, non community-wiki answers of a minimum length are eligible
Related Tags
combinatorics × 593algorithms × 204
graphs × 99
optimization × 75
discrete-mathematics × 40
complexity-theory × 31
dynamic-programming × 31
permutations × 30
sets × 26
data-structures × 22
trees × 21
probability-theory × 19
algorithm-analysis × 16
word-combinatorics × 15
reference-request × 14
binary-trees × 14
recursion × 13
counting × 13
recurrence-relation × 12
matrices × 12
knapsack-problems × 12
time-complexity × 11
randomized-algorithms × 10
greedy-algorithms × 10
formal-languages × 9