-7

I wanted to know the Java code to find all the possible paths of a multidimensional array. We should start from the [0][0] node and move forward. The thing to keep in mind that the paths must be in an increasing order irrespective of the elements order. We should not traverse if the next element is equal to (or less then) the current one.

Example Array:

3   5   1
6   7   4
8   2   9

Result:

3,5,6,7,8
3,5,6,7,9
3,5,7,8
3,5,7,9
3,6,7,8
3,6,7,9
3,7,8
3,7,9
user000001
  • 32,226
  • 12
  • 81
  • 108
user2044187
  • 107
  • 1
  • 1
  • 10
  • like a binary tree? http://stackoverflow.com/questions/5020738/php-binary-tree-recursion-algorithm - whoops thought this was PHP, still link might help. hmm, same thing but for Java: http://stackoverflow.com/questions/9648756/java-binary-trees – ficuscr Feb 05 '13 at 18:34
  • 6
    Have you tried anything yet? If so, please include your thoughts. This kinda smells like a "do my homework for me" question. – cmonkey Feb 05 '13 at 18:35
  • So what have you tried? – Tony Hopkinson Feb 05 '13 at 18:37
  • 1
    I am on it since last 8 hours, well this is my first question on any online forum. I've tried a lot, was coding since then but no success. The code I coded is very cultured and a lot so I refrained myself from posting it. – user2044187 Feb 05 '13 at 18:39
  • 1
    Welcome to StackOverflow! We expect questions to be supported by code and original research. As such, we expect to see an attempt to solve the problem or at least have an approach that you're considering. StackOverflow is not a "give me teh codez" site where we do your work for you. Please try to implement a solution or come up with an algorithm, then come back and ask questions when you run into an actual problem. We'll be happy to help you then. – Brian Feb 05 '13 at 18:42
  • So just scribble out your approach then, and where it seems to not work. Not findng all the paths, not doing diagonals etc.. – Tony Hopkinson Feb 05 '13 at 18:43
  • first I tried it doing with arrays like, taking zeroth element and checking it with 1,3&4 (as in 3x3 array), if the zeroth element is lesser than the element against which it is checked, I stored it in another array and did same for the 9 elements. I was thinking after all the arrays will be formed i'll check the last index of first array with the first index of second array and if they were same i'll concat them and so on but on executing the arrays had 0s that failed the algo, since i wasn't able to remove the 0s (couldn't do search and remove because the actual 3x3 array may also have 0s). – user2044187 Feb 05 '13 at 18:52
  • I would suggest using something like a modified BFS algorithm – Sednus Feb 05 '13 at 18:54
  • @AnasAzeem 1) Telling us that you spent 8 hours working on this doesn't mean that you actually have. We have no way of verifying that. 2) Even if it's true, we still shouldn't be doing it for you from scratch. It's important for us to help *you* solve *your* problem, not just do it for you. You won't learn much that way, it's not academically honest, and it's simply an rude request to just ask people to do all of your work for you. 3) If you include in your question what you're doing, and the *specific problems* you're having implementation your solution we can help you solve them. – Servy Feb 05 '13 at 20:23
  • @Servy: Next time when I post a question I'll definitely won't start from the scratch, I didn't post my code because it was too cluttered, and I was worried that people would mock me. But you have to believe that I tried. – user2044187 Feb 05 '13 at 20:47
  • @AnasAzeem No, I don't *have* to believe you. Next, dumping your entire code base here and saying "fix it" is also a very poor question that is likely to be closed. You should be asking for help solving a *specific* problem in your program, not the whole thing, and as such you shouldn't be posting your entire solution, just a few small relevant sections of it with a question that indicates what, specifically, isn't working and describes the steps you've currently taken to try to resolve the problem. – Servy Feb 05 '13 at 20:49
  • @Servy i'll keep that in mind.....plz don't scold me. – user2044187 Feb 05 '13 at 20:53
  • @AnasAzeem I am not scolding you, I am simply telling you what you need to fix so that you can be helped by the site. If you're not interested in hearing what you need to change in order to ask a question that this site considers appropriate then that's fine. – Servy Feb 05 '13 at 20:55
  • dear @Servy I'm saying that next time i'll keep that in mind. plz don't take me otherwise. – user2044187 Feb 05 '13 at 20:59

2 Answers2

1

You should build a tree, beginning with your start point. In your example array, starting at the top left, the tree might look like this:

     3
   / \ \ 
  5  6  7 
 / \  \  \
7   6  ..  ..

Once you have that tree, you just have to step down each path in the tree.

Although it may look intimidating, you can break this up into two parts:

  • Build the tree.
  • Follow the paths.

  • First, try to come up with a way to get your starting point to print. That's fairly easy.

  • Once you've made it that far, print the neighbors of your starting point that are > its value.

  • Then, do the same thing for each neighbor!

sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • I think it should be a graph. Not a tree. – Sednus Feb 05 '13 at 19:21
  • @Sednus I don't think a graph is possible if the nodes can only be visited once. – sdasdadas Feb 05 '13 at 19:23
  • @Sednus Technically a tree is a type of graph - see [here](http://en.wikipedia.org/wiki/Tree_%28graph_theory%29) or [here](http://stackoverflow.com/questions/8367485). But like sdasdadas was saying, it looks like OP is restricting this to down, right, and down+right so a tree works (though that restriction could be a result of how the sample array is filled...) – Windle Feb 05 '13 at 20:35
  • 3,5,6,7,8 goes to the left+down twice. So I assume that it should be able to go in any direction. Also, something like BFS would work alike, just using a queue instead of a tree – Sednus Feb 05 '13 at 20:47
  • The major point being that you cannot visit a node more than once. If you can visit nodes more than once, then a graph with cycles will be generated. And then you have a bit of a harder problem to solve without infinitely traversing the graph. – sdasdadas Feb 05 '13 at 20:49
  • To be clear , I mean each position being a node , not the array being a graph. – Sednus Feb 05 '13 at 21:02
  • Right, that's what I mean too. – sdasdadas Feb 05 '13 at 21:07
  • @Sednus Oh geez, that's embarrassing =P Ignoring my goof there, I'm still with sdasdadas that if you don't visit nodes more than once you can build this as a tree with the path between the root, 3, and each leaf being your answers. +1 – Windle Feb 05 '13 at 21:07
  • 1
    Yes, I agree it should work fine. My point is that you need to traverse the matrix initially to build the tree. That traversal should look something similar to a BFS traversal. So, my thinking was to generate the output in-place instead of going through the tree again. – Sednus Feb 05 '13 at 21:47
0

I think the following code does what you describe. It start checking with the first node (0,0). For every node that is checked, a vector of neighbors is created. The neighbors are the nodes that are eligible to be a continuation to the path (i.e. neighboring nodes with higher value in the table). Then for each neighbor, the path is cloned, and the new neighbor is checked. This continues until the checked node has no eligible neighbors, at which point the path is printed and the algorithm terminates.

Try this:

import java.util.Arrays;
import java.util.Vector;

class Main {
  class Coords {
    int x;
    int y;
    Coords(int x, int y) {
      this.x = x;
      this.y = y;
    }
  }
  int [][] array = { {3,5,1},{6,7,4},{8,2,9}};
  Vector<Coords> getNeighbors(Coords coords) {
     int x = coords.x;
     int y = coords.y;
     Vector<Coords> result = new Vector<Coords>();
     if (x < array.length - 1) {
        if (array[x + 1][y] >= array[x][y])
          result.add(new Coords(x + 1, y));
     }
     if (x > 0) {
        if (array[x - 1][y] >= array[x][y])
          result.add(new Coords(x - 1, y));
     }
     if (y < array[x].length - 1) {
        if (array[x][y + 1] >= array[x][y])
          result.add(new Coords(x, y + 1));
     }
     if (y > 0) {
        if (array[x][y - 1] >= array[x][y])
          result.add(new Coords(x, y - 1));
     }
     if (x < (array.length - 1 ) &&  (y < array[x].length - 1)) {
        if (array[x + 1][y + 1] >= array[x][y])
          result.add(new Coords(x + 1, y + 1));
     }
     if (x < (array.length - 1 ) &&  (y > 0)) {
        if (array[x + 1][y - 1] >= array[x][y])
          result.add(new Coords(x + 1, y - 1));
     }
     if (x > 0 &&  (y < array[x].length - 1)) {
        if (array[x - 1][y + 1] >= array[x][y])
          result.add(new Coords(x - 1, y + 1));
     }
     if (x > 0 &&  y > 0) {
        if (array[x -1][y - 1] >= array[x][y])
          result.add(new Coords(x - 1, y - 1));
     }
     return result;
  }
  void checkNode(Vector<Integer> path, Coords coords) {
    path.add(array[coords.x][coords.y]);
    Vector<Coords> neighbors = getNeighbors(coords);
    if (neighbors.size() == 0) {
       for (Integer i : path) {
         System.out.print(i+"\t");
       }
       System.out.println();
    }
    for (Coords c : neighbors) {
      Vector<Integer> newpath = (Vector<Integer>) path.clone();
      checkNode(newpath, c);
    }
  }
  Main() {
    System.out.println ("Array: " + Arrays.deepToString(array));
    checkNode(new Vector<Integer>(),new Coords(0,0));
  }

  public static void main(String args[]) {
    new Main();
  }
}

Output:

Array: [[3, 5, 1], [6, 7, 4], [8, 2, 9]]
3   6   8   
3   6   7   9   
3   6   7   8   
3   5   7   9   
3   5   7   8   
3   5   6   8   
3   5   6   7   9   
3   5   6   7   8   
3   7   9   
3   7   8   

It also gives me the path 3,6,8, which is not in your sample output

user000001
  • 32,226
  • 12
  • 81
  • 108
  • I may not be the best programmer, but I was fed up trying since 4:00 PM and now it is 1:30 AM. Its been 8 1/2 hours. If you think I haven't tried thats wrong.Thank You – user2044187 Feb 05 '13 at 20:03
  • @AnasAzeem Yea this kind of problems is a bit tricky until you get the hang of it... – user000001 Feb 05 '13 at 20:16