Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 51

Questions on the use of Mathematica in combinatorics, including the Combinatorica add-on package.

13 votes
Accepted

Combination and Permutation

Take all subsets of length 10, then for each one find all splits into two sets of five such that the first of the ten is in the first part of the split. In[29]:= Timing[ msets = Subsets[Range[12], { …
Daniel Lichtblau's user avatar
12 votes
Accepted

What is the fastest way to count square-free words?

This has [quadratic, he said] actually perhaps cubic complexity in a worst case (okay, now I'm just confused. More below).. Not the fastest of the lot, but it seems reasonable, or at least not entirel …
Daniel Lichtblau's user avatar
10 votes
Accepted

How to enumerate multisets?

This will generate all of them, just like Tuples. Not too hard to redo so as to get one at a time. Just use the correspondence between k-digit numbers base n (n=length of input set) and subsets length …
Daniel Lichtblau's user avatar
9 votes

find the number of integral solutions a+b+c+d+e+f = 18

Depending on whether you care about permutations or not, here are some ways to go about it. One is to solve a system of equations via Reduce and count the solutions. vars = Array[a, 6]; eqn = Total[ …
Daniel Lichtblau's user avatar
9 votes

Integer partitions without repetitions

Could set this up as a 1-0 integer linear programming problem. Module[{vars = Array[a, 10]}, vars*Range[10] /. Solve[Flatten@{vars.Range[10] == 28, Total[vars] == 4, Map[0 <= # <= 1 &, v …
Daniel Lichtblau's user avatar
7 votes

How to improve the performance of solutions to Project Euler (#39)?

Not fastest, but gives an idea of how Solve might be used with tolerable efficiency. In[181]:= Timing[ soln = Solve[{x^2 + y^2 - z^2 == 0, z >= y - 1 >= x - 1 >= 0, 0 <= x + y + z <= 1000}, {x …
Daniel Lichtblau's user avatar
7 votes

Generating Tuples with restrictions

This is not really very efficient but here goes. We can create a rational function that is effectively a generating function in three variables, one to force 8 factors, one to force a sum e1ual to 24, …
Daniel Lichtblau's user avatar
7 votes

Combinatorica: Girth[] and FindCycle[] disagreement

Edited for correctness: I use the variant for directed graphs from here. I take your graph as above, extract edges, rename so vertices are integers from 1 to #vertices. After finishing we revert to t …
Daniel Lichtblau's user avatar
7 votes

(Efficiently) Generating graphs with vertex degree 3 for all vertices

This might get you started. It generates (I think) all graphs of given number of vertices and degree, with the following caveats: They are undirected. They do not have self edges. Edges only have we …
Daniel Lichtblau's user avatar
7 votes
Accepted

Generating Linear Extensions of a Partial Order

Could use integer linear programming. In Mathematica this can be done with Reduce[]. One way (probably not the best) to set this up is shown below. It uses an array of 0-1 variables, where a 1 in posi …
Daniel Lichtblau's user avatar
6 votes

How do I generate the upper triangular indices from a list?

Could do this procedurally by constructing iterator lists. indices[n_, max_] := Module[ {jj = Array[j, n], starts, ends}, starts = Prepend[Most[jj] + 1, 1]; ends = max - Range[n - 1, 0, -1]; …
Daniel Lichtblau's user avatar
6 votes

Solving variant of the knapsack/money-changing problem

Not really what you want, but you can find the closest hit using FindMinimum, as below. n = 6; target = 10; SeedRandom[1111]; vals = RandomReal[1, n]; coeffs = Array[c, 6]; c1 = Map[# >= 0 &, coeff …
Daniel Lichtblau's user avatar
6 votes
Accepted

How to generate 3-subsets that satisfy certain conditions without post-filtering?

One way is to place the first element in each of the subsets formed from s-1 of the remaining elements, find complements, and recursively subdivvy those complements. subsetSubsets[set_, s_ /; s <= 0] …
Daniel Lichtblau's user avatar
6 votes

Exact cover solution

I will use David Carraher's example to illustrate a way using integer linear programming. x = Range[7]; a[1] = {1, 4, 7}; a[2] = {1, 4}; a[3] = {4, 5, 7}; a[4] = {3, 5, 6}; a[5] = {2, 3, 6, 7}; a[6] …
Daniel Lichtblau's user avatar
6 votes
Accepted

Efficient generation of n-bit base-m Gray code with adjacent bit changes

A simple recursion handles this. fullGrayCode[1, n_Integer] /; n >= 1 := Transpose[{Range[0, n - 1]}] fullGrayCode[m_Integer, n_Integer] /; m >= 2 && n >= 1 := Module[ {gcm1 = fullGrayCode[m - 1, n …
Daniel Lichtblau's user avatar

15 30 50 per page