0

Suppose we have an m×n matrix with starting index (1, 1). We have to reach the position (m, n) either by moving right or down to the adjacent element. How can we explore all possible paths?

kaya3
  • 31,244
  • 3
  • 32
  • 61
RISHAB
  • 37
  • 7
  • Finding the maximum or minimum path through a graph is a well-defined problem, solved with `Dijkstra's algorithm`. – Prune Feb 28 '20 at 19:31

3 Answers3

2

Turn Pascal's triangle 1/8 turn:

1  1  1  1  1 ...
1  2  3  4  5 ...
1  3  6 10 15 ...
1  4 10 20 35 ...

Element [M, N] of this matrix is your desired answer.

The construction of this array is isomorphic to that of Pascal's triangle:

Start at (1, 1); there is only one way to reach that spot. The next move is to either (2, 1) or (1, 2) -- only one way to reach that spot. On the next move, there are now two way to reach (2, 2): it's reachable from either of the previous spots, so the number of ways is the sum of those two elements. That's the critical isomorphism with Pascal's triangle.


More directly, simply use the formula for any element of the triangle:

(a+b)! / a!b!

Where a = M-1, b = N-1

This is the quantity of right and down moves you need, using the combinatoric formula for all permutations thereof.

Prune
  • 72,213
  • 14
  • 48
  • 72
  • There are something wrong in your approach, I am getting 35 for (4, 5). And I've all the paths as well. – User_67128 Feb 28 '20 at 18:53
  • There's something wrong with my editing; thanks. Fixed. – Prune Feb 28 '20 at 18:57
  • This will give me the total possible ways only I guess, if I am not wrong. I am interested in traversal operation mainly, not only the count. – RISHAB Feb 28 '20 at 19:28
  • Then do as `@Manojj` suggested. We've solved your posted problem. Finding the maximum or minimum path through a graph is a well-defined problem, solved with `Dijkstra's algorithm`. – Prune Feb 28 '20 at 19:30
0

It can be done by recursively,

Given a position (x, y), we have two choices - right or down.

We can take any choice and will check our target (m, n). If at anytime x exceeds m (or y exceeds n) STOP.

During traversing, we can save the path to some list/array (depending on your implementation).

Pseudo code:

   waysToReach(startX, startY, endX, endY, path)
   {
      if (startX > endX || startY > endY)
         return;

      if (startX == endX && startY == endY)
      {
         count++;
         println(count+ ". " + path);
         return;
      }

      // Right traverse
      add 1 to path; // 1 for right
      waysToReach(startX + 1, startY, endX, endY, path);

      remove last-step from path;

      // Down traverse
      add 0 to path; // 0 for down
      waysToReach(startX + 1, startY, endX, endY, path);

      remove last-step from path;
   }

I implemented my idea in Java, if you need the code, I can provide you,

For an example, when m=3 and n=4, there are a total of 10 ways, these are as follows: here 1 for RIGHT, 0 for DOWN.

1. [1, 1, 0, 0, 0]
2. [1, 0, 1, 0, 0]
3. [1, 0, 0, 1, 0]
4. [1, 0, 0, 0, 1]
5. [0, 1, 1, 0, 0]
6. [0, 1, 0, 1, 0]
7. [0, 1, 0, 0, 1]
8. [0, 0, 1, 1, 0]
9. [0, 0, 1, 0, 1]
10. [0, 0, 0, 1, 1]

when m=6 and n=3, there are a total of 10 ways, these are as follows: here 1 for RIGHT, 0 for DOWN.

1. [1, 1, 1, 1, 1, 0, 0]
2. [1, 1, 1, 1, 0, 1, 0]
3. [1, 1, 1, 1, 0, 0, 1]
4. [1, 1, 1, 0, 1, 1, 0]
5. [1, 1, 1, 0, 1, 0, 1]
6. [1, 1, 1, 0, 0, 1, 1]
7. [1, 1, 0, 1, 1, 1, 0]
8. [1, 1, 0, 1, 1, 0, 1]
9. [1, 1, 0, 1, 0, 1, 1]
10. [1, 1, 0, 0, 1, 1, 1]
11. [1, 0, 1, 1, 1, 1, 0]
12. [1, 0, 1, 1, 1, 0, 1]
13. [1, 0, 1, 1, 0, 1, 1]
14. [1, 0, 1, 0, 1, 1, 1]
15. [1, 0, 0, 1, 1, 1, 1]
16. [0, 1, 1, 1, 1, 1, 0]
17. [0, 1, 1, 1, 1, 0, 1]
18. [0, 1, 1, 1, 0, 1, 1]
19. [0, 1, 1, 0, 1, 1, 1]
20. [0, 1, 0, 1, 1, 1, 1]
21. [0, 0, 1, 1, 1, 1, 1]
User_67128
  • 1,182
  • 4
  • 18
  • I like this approach but the thing with recursion is, its complexity increases when we try to tweak little bit of conditions. For instance, I was trying to solve a question where you are given a matrix NxM with some integer value at all positions, say weights. Negative weight means we can't traverse down/right and we can traverse down/right through non-negative values. I was asked to find the maximum value possible by traversing down/right. – RISHAB Feb 28 '20 at 19:19
  • @RISHAB, you can try to convert it from recursive to iterative. Until that, we can use this approach. It can traverse from any point (x, y) to any target position (X, Y) as long as x <= X and y <= Y. – User_67128 Feb 28 '20 at 19:45
0

To reach (N, M) from (1, 1), you must go (N-1) steps to the right, and (M-1) steps downwards.

You can the denote a path by listing the "right" (r) and "down" (d) moves, e.g. rddrd for right, down, down, right down.

Now, since you have to go (N-1) r-steps and (M-1) d-steps, you have (N-1)+(M-1) steps in total. In mathematics, this means that you want to generate all "d-r-sequences with (N-1)+(M-1) characters, where exactly (N-1) are r". This is covered in detail here.

phimuemue
  • 30,699
  • 8
  • 74
  • 109