Phoenix
Phoenix

Reputation: 8923

Robot moving in a grid algorithm possible paths and time complexity ?

I'm not able to understand how for the problem below the number of paths are (x+y)!/x!y! .. I understand it comes from choose X items out of a path of X+Y items, but why is it not choosing x items over x+y + choosing y items over x+y ? Why does it have to be only x ?

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below). How many possible paths are there?

  1. Are all of these paths unique ?
  2. How do I determine that ?
  3. And what would be the time complexity for the backtracking algorithm ?

Upvotes: 2

Views: 4711

Answers (4)

mbeckish
mbeckish

Reputation: 10579

You don't add "How many ways can I distribute my X moves?" to "How many ways can I distribute my Y moves?" for two reasons:

  1. The distribution of X moves and Y moves are not independent. For each configuration of X moves, there is only 1 possible configuration of Y moves.
  2. If they were independent, you wouldn't add them, you would multiply them. For example, if I have X different color shirts and Y different color pants, there are X * Y different combinations of shirts and pants.

Note that for #1 there is nothing special about X - I could just have easily chosen Y and said: "The distribution of Y moves and X moves are not independent. For each configuration of Y moves, there is only 1 possible configuration of X moves." Which is why, as others have pointed out, counting the number of ways to distribute your Y moves gives the same result as counting the number of ways to distribute your X moves.

Upvotes: 1

Bernhard Barker
Bernhard Barker

Reputation: 55589

This is somewhat based on Mukul Joshi's answer, but hopefully a little clearer.

To go from 0,0 to x,y, you need to move right exactly x times and down exactly y times.

Let each right movement be represented by a 0 and a down movement by a 1.

Let a string of 0s and 1s then indicate a path from 0,0 to x,y. This path will contain x 0s and y 1s.

Now we want to count all such strings. This is equivalent to counting the number of permutations of any string containing x 0s and y 1s. This string is a multiset (each element can appear more than once), thus we want a multiset permutation, which can be calculated as n!/(m1!m2!...mk!) where n is the total number of characters, k is the number of unique characters and mi is the number of times the ith unique character is repeated. Since there are x+y characters in total, and 0 is repeated x times and 1 is repeated y times, we get to (x+y)!/x!y!.

Time Complexity:

The time complexity of backtracking / brute force would involve having to explore all of these paths. Think of it as a tree, with there being (x+y)!/x!y! leaves. I might be wrong, but I think the number of nodes in trees with a branching factor > 1 can be represented as the big-O of the number of leaves, thus we end up with O((x+y)!/x!y!) nodes, and thus the same time complexity.

Upvotes: 3

Mukul Joshi
Mukul Joshi

Reputation: 324

Well here is the explanation.

To reach till the destination no matter how you go, the path has to have m rows and n columns.

Consider that you represent row by 1 and column by 0. Your path is a string of m+n characters. But it can have only m 1s and n 0s.

if you have m+n different characters the number of permutations will be (m+n)! but when you have repeating characters then it will be (m+n)!/m!n! Refer to this

Of course this will be unique. Test it for 4*3 grid and you can see it.

Upvotes: 1

Chechulin
Chechulin

Reputation: 2496

Ok, I give you a solution to that problem so that you have better time catching it.
First of all, let us decide a solution algorithm. We will count all possible paths for every cell to reach end from it. The algorithm will check cells and write there sum of right and bottom cells. We do it because robot can move down and follow any of bottom paths or move right and follow any of rightside paths, thus, adding the total number of different paths. It is quite obvious for me to prove the divercity of these paths. If you want I can do it in comments.
Initial values for cells will be 1 for rightmost bottom cell (finish) because there only 1 way to get there from this cell (not to move at all). And if cell doesn't exist (e.g. taking bottom cell for bottommost cell) it will have value of 0.
Building cell values one by one will result in a Pascal's Triangle which values are (x + y)! / x! / y! in a (x, y) cell where x is the Ox distance from finish and y is Oy one.

Talking about complexity we will have x * y iterations over grid cells, each iteration is a constant time. If you don't want to use backtracking algorith you can use the formula that is mentioned above and have O(x + y) instead of O(x * y)

Upvotes: 2

Related Questions