recursive pathfinding -C

Discussion in 'Programmer's Corner' started by msshapira, Feb 15, 2009.

  1. msshapira

    Thread Starter New Member

    Feb 15, 2009
    Hi all,
    i have written a very simple function to find a shortest path through a matrix, where each Node has a value, and the final return value is the minimal cost to cross from starting point to end point while adding all values on the way.
    It works fine for matrices up to 6*6, but when I try bigger ones, it hangs.
    I'm not very fluent with algorithms, can someone help me find a more efficient algorithm?
    on the code side:
    All .status are started with "=CLOSED".
    "LARGE" is defined to be "MAX_LONG"
    Code ( (Unknown Language)):
    2. long int GetMin(Node** matrix,int currentR,int currentC,int endR,int endC,
    3.                 int rows,int columns)
    4. {
    5.     long int result;
    6.     //if out of bounds return max of field
    7.     if (currentR==rows || currentC==columns || currentR<0 || currentC<0)
    8.         return LARGE;
    10.     //if reached end
    11.     if (currentC==endC && currentR==endR)
    12.         return (matrix[currentR][currentC].value);
    14.     //if already checked here
    15.     if (matrix[currentR][currentC].status==OPEN)
    16.         return LARGE;
    17.     //if not reached end   
    18.     matrix[currentR][currentC].status=OPEN;
    19.     result=Smallest4(
    20.         GetMin(matrix,currentR,currentC+1,endR,endC,rows,columns),
    21.         GetMin(matrix,currentR,currentC-1,endR,endC,rows,columns),
    22.         GetMin(matrix,currentR+1,currentC,endR,endC,rows,columns),
    23.         GetMin(matrix,currentR-1,currentC,endR,endC,rows,columns));
    25.     //set current status to closed- can try here again 
    26.     matrix[currentR][currentC].status=CLOSED;
    28.     //if was blocked all directions
    29.     if (result==LARGE)
    30.         return LARGE;
    32.     return matrix[currentR][currentC].value+result;
    33. }
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    I'll bet your problem is right here:
    Code ( (Unknown Language)):
    2. result=Smallest4(
    3.         GetMin(matrix,currentR,currentC+1,endR,endC,rows,columns),
    4.         GetMin(matrix,currentR,currentC-1,endR,endC,rows,columns),
    5.         GetMin(matrix,currentR+1,currentC,endR,endC,rows,columns),
    6.         GetMin(matrix,currentR-1,currentC,endR,endC,rows,columns));
    I don't see a definition for Smallest4() -- I'm assuming it's another function-- but each time it is called, there are four recursive calls to GetMin(), one for each neighbor of the cell in the current row and current column. And each of those cells calls GetMin() four times, so you have exponential growth in the number of recursive function calls.

    You're most likely running out of stack space due to all of those calls to GetMin. Each time a function is called, the parameters and the return address are pushed onto the stack. When the function returns to its caller, that space is reclaimed, but not before then. This is why recursive functions, such as to compute the factorial of a number, are easy to code, but not very practical for large values.

    I don't know that there's an easy fix for this function. You'll probably have to rethink it completely so as not to use recursion. You know more about this problem than I do, but maybe an approach as follows this might work.

    For an n x n matrix, you should be able to find a path through it with somewhere between n and n^2 cells. Starting at the 1,1 entry of your matrix, check the cells adjacent to it for the cell with the minimum value. Keep track of which cell this was in an auxiliary matrix of some kind. Move to the cell with minimum value. Check the cells adjacent to it. Note which cell it is, and move to that cell, and so on, until you are across the matrix or run off its boundary.

    Again, you know this problem better than I do, so I might not have gotten exactly what you're trying to do, but maybe this will be of help.