Saturday, January 7, 2012

Count number of paths in a Matrix with or without Constraints

Question 1:Count All Paths
Count All Paths from Top left to bottom right in Two Dimensional Array including Diagonal Paths. Modify the solution to include constraints that from each cell you can either move only to right or downAlso extend both algo. to print all paths.

Alternatively [ Robot on MxN Grid],
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 unique paths are there?
Or
Given two positions in a 2-D matrix, say (x1, y1) and (x2, y2) where x2>=x1 and y2>=y1. Find the total number of distinct paths between (x1, y1) and (x2, y2). You can only move in right direction i.e. positive x direction (+1, 0) or in up direction i.e. positive y direction (0, +1) from any given position.
Example: If the given coordinates are  (3,3)  and (5,5), the number of distinct paths are 6 :  one going through 3,5 ; one going through 5,3 and four going through 4,4.

Count:
Diagonal Paths Allowed
http://algorithms.tutorialhorizon.com/count-all-paths-from-top-left-to-bottom-right-in-two-dimensional-array-including-diagonal-paths/
http://www.geeksforgeeks.org/count-possible-paths-top-left-bottom-right-nxm-matrix/
Diagonal Paths not allowed
http://algorithms.tutorialhorizon.com/dynamic-programming-count-all-paths-from-top-left-to-bottom-right-of-a-mxn-matrix/
http://www.geeksforgeeks.org/count-possible-paths-top-left-bottom-right-nxm-matrix/

Question 2:Unique Paths
http://articles.leetcode.com/unique-paths
http://www.programcreek.com/2014/05/leetcode-unique-paths-java/
http://www.programcreek.com/2014/05/leetcode-unique-paths-ii-java/

Variation 1Robot Travel with obstructions
Count all paths in 2D Matrix with Obstructions in it
http://algorithms.tutorialhorizon.com/dynamic-programming-count-all-paths-in-2d-matrix-with-obstructions-in-it/

Longest Possible Route in a Matrix with Hurdles
http://www.geeksforgeeks.org/longest-possible-route-in-a-matrix-with-hurdles/

Print:
1. http://www.geeksforgeeks.org/print-all-possible-paths-from-top-left-to-bottom-right-of-a-mxn-matrix/
2. http://algorithms.tutorialhorizon.com/print-all-paths-from-top-left-to-bottom-right-in-two-dimensional-array/
3. Given m x n matrix print all the possible paths top to down.

Example
1 2 3
4 5 6
7 8 9

path for root(0,0) 1
1-4-7
1-4-8
1-5-7
1-5-8
1-5-9

similarly path for 2(0,1)
2-4-7
2-4-8
2-5-7
2-5-8
2-5-9
2-6-8
2-6-9

note- root 1 can go to middle down or right down since there is no left index available. if root element has left middle and right it can go to all those paths like 2 or 5.

follow up : Provide the path which has maximum path sum.

Minimum Cost path matrix
Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell.
http://www.geeksforgeeks.org/dynamic-programming-set-6-min-cost-path/
http://algorithms.tutorialhorizon.com/dynamic-programming-minimum-cost-path-problem/

Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only traverse down, right and diagonally lower cells from a given cell, i.e., from a given cell (i, j), cells (i+1, j), (i, j+1) and (i+1, j+1) can be traversed. You may assume that all costs are positive integers.

Robot Combinatoric Solution:
long factorial(int n)
{
   int c;
   long result = 1;
   for( c = 1 ; c <= n ; c++ )
      result = result*c;
   return ( result );
}
long C(int n, int r)
{
   long result;
   result = factorial(n)/(factorial(r)*
factorial(n-r));
   return result;
}
int countPaths(int x1, int y1, int x2, int y2) {
   int n = abs(x2-x1);
   int m = abs(y2-y1);
   return C(m+n,n);

}

1 comment:

  1. http://geeksforgeeks.org/forum/topic/largest-palindrome-in-a-string
    http://johanjeuring.blogspot.in/2007/08/finding-palindromes.html

    ReplyDelete