Ask Your Question

Revision history [back]

Concerning the generation of such paths, Sage has tools to generate lists of nonnegative integers with constraints, see IntegerListsLex?. Here you want a list of 0s and 1s of length 2*n whose sum is n. So you can do:

sage: n = 3
sage: IntegerListsLex(length=2*n,n=n,max_part=1)

You can iterate over this by doing:

for L in IntegerListsLex(length=2*n,n=n,max_part=1):
    blah

You can transform the generator into a list to check that nothing is missing:

sage: list(IntegerListsLex(length=2*n,n=n,max_part=1))
[[1, 1, 1, 0, 0, 0],
 [1, 1, 0, 1, 0, 0],
 [1, 1, 0, 0, 1, 0],
 [1, 1, 0, 0, 0, 1],
 [1, 0, 1, 1, 0, 0],
 [1, 0, 1, 0, 1, 0],
 [1, 0, 1, 0, 0, 1],
 [1, 0, 0, 1, 1, 0],
 [1, 0, 0, 1, 0, 1],
 [1, 0, 0, 0, 1, 1],
 [0, 1, 1, 1, 0, 0],
 [0, 1, 1, 0, 1, 0],
 [0, 1, 1, 0, 0, 1],
 [0, 1, 0, 1, 1, 0],
 [0, 1, 0, 1, 0, 1],
 [0, 1, 0, 0, 1, 1],
 [0, 0, 1, 1, 1, 0],
 [0, 0, 1, 1, 0, 1],
 [0, 0, 1, 0, 1, 1],
 [0, 0, 0, 1, 1, 1]]

Concerning the plotting, if you plot everything on the same plot, you will end up with a grid. I suggest to look towards animations, see http://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.html

Concerning the generation of such paths, Sage has tools to generate lists of nonnegative integers with constraints, see IntegerListsLex?. Here you want a list of 0s and 1s of length 2*n whose sum is n. So you can do:

sage: n = 3
sage: IntegerListsLex(length=2*n,n=n,max_part=1)

You can iterate over this by doing:

for L in IntegerListsLex(length=2*n,n=n,max_part=1):
    blah

You can transform the generator into a list to check that nothing is missing:

sage: list(IntegerListsLex(length=2*n,n=n,max_part=1))
[[1, 1, 1, 0, 0, 0],
 [1, 1, 0, 1, 0, 0],
 [1, 1, 0, 0, 1, 0],
 [1, 1, 0, 0, 0, 1],
 [1, 0, 1, 1, 0, 0],
 [1, 0, 1, 0, 1, 0],
 [1, 0, 1, 0, 0, 1],
 [1, 0, 0, 1, 1, 0],
 [1, 0, 0, 1, 0, 1],
 [1, 0, 0, 0, 1, 1],
 [0, 1, 1, 1, 0, 0],
 [0, 1, 1, 0, 1, 0],
 [0, 1, 1, 0, 0, 1],
 [0, 1, 0, 1, 1, 0],
 [0, 1, 0, 1, 0, 1],
 [0, 1, 0, 0, 1, 1],
 [0, 0, 1, 1, 1, 0],
 [0, 0, 1, 1, 0, 1],
 [0, 0, 1, 0, 1, 1],
 [0, 0, 0, 1, 1, 1]]
sage: len(list(IntegerListsLex(length=2*n,n=n,max_part=1)))
20
sage: binomial(2*n,n)
20

Concerning the plotting, if you plot everything on the same plot, you will end up with a grid. I suggest to look towards animations, see http://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.html