How to draw all paths from (1,1) to (n,n) by move (+1, 0) or (0, +1)?
Stealing half of evanb's answer we could do:
With[{n = 3}, Graphics[{
LightGray, Disk[#, 0.5] & /@ Flatten[Table[{i, j}, {i, 0, n}, {j, 0, n}], 1],
Thick, Module[{m, paths = Sort@Permutations[Join @@ ({{0, 1}, {1, 0}} & /@ Range[n])]},
m = Length@paths;
Table[{Hue[(i - 1)/(m - 1)], Line@FoldList[Plus,
1/(2 Sqrt[2]) {-1 + (2 (-1 + i))/(-1 + m),
1 - (2 (-1 + i))/(-1 + m)}, paths[[i]]]}, {i, m}]
]}]]
You can use the Graph
capabilities to solve this problem. First, note that every path from $(1,1)$ to $(n,n)$ has $2n-2$ moves, so it is just a matter of using FindPath
with GridGraph
n = 3;
g = GridGraph[{n, n}, VertexLabels -> "Name"]
FindPath[g, 1, n n, {2 n - 2}, All] /.
Thread[Range[n n] -> GraphEmbedding[g]]
You can visualize all the paths with PathGraph
,
With[{n = 4},
g = GridGraph[{n, n}];
HighlightGraph[g, PathGraph@#] & /@
FindPath[g, 1, n n, {2 n - 2}, All]]
ListAnimate@%
Edit
Here is a method, less simple than above, but still distinctly different from kirma's answer, that answer's OP's request to draw all of the paths together in a manner in which they are all visible.
gridpaths[n_] := Module[{g, coords, paths},
g = GridGraph[{n, n}, EdgeStyle -> Opacity[0]];
coords[offs_] := Thread[Range@(n n) -> (offs + GraphEmbedding@g)];
paths = FindPath[g, 1, n n, 2 n - 2, All];
Show[
PathGraph[#1,
VertexCoordinates -> #1 /. coords[#3],
EdgeStyle -> Directive[Thick, #2],
VertexSize -> 0] & @@@
Transpose[{paths,
ColorData[97] /@ Ordering@paths,
Subdivide[-.3, .3, Length@paths - 1]}]}],
GridLines -> {#, #} &@Range[0.5, n + .5, 1],
PlotRange -> {{.5, n + .5}, {.5, n + .5}}
]
]
It can be tested via
gridpaths /@ Range[2, 7]
You could try something like this:
Show[Graphics[{RandomColor[], Line[#]}] & /@ ( (* Make graphics of *)
FoldList[Plus, RandomReal[{0, 0.1}, 2] + {0, 0}, #] & /@ (* Trajectories built from *)
Permutations[ (* All possible orderings of *)
Join @@ ({{0, 1}, {1, 0}} & /@ Range[5]) (* Five steps N and five steps E*)
]
)]
The RandomReal[{0,0.1},2]
is a random near-the-origin starting location, just to offset the different lines from one another visually.