Using recursion to find paths in a 2D array

Posted by rikkit on Stack Overflow See other posts from Stack Overflow or by rikkit
Published on 2010-04-13T17:06:00Z Indexed on 2010/04/13 17:23 UTC
Read the original article Hit count: 498

Filed under:
|
|
|

Hi, I'm working on a project for my A level. It involves finding the maximum flow of a network, and I'm using javascript.

I have a 2D array, with values in the array representing a distance between the two points. An example of the array:

0 2 2 0
0 0 1 2
0 0 0 2
0 0 0 0

I think I need to use a recursive technique to find a path; below is some pseudocode, assuming that the array is 4x4. a is (0,0), b is (3,3).

function search(a,b)
  from a to b
    if element(i,j) != 0 then
      store value of element
      search(j,3)

I was wondering if that was the right construction for a depth first search. Thanks for any help.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays