/* * Finding all paths in a graph */ import java.util.ArrayList; public class AllPaths { static private int[][] tree = { {0,1,0,0,0,0}, // 0->1 {0,0,1,0,1,0}, // 1->2, 1->4 {0,0,0,1,1,0}, // 2->3, 2->4 {0,0,0,0,1,0}, // 3->4 {0,0,0,0,0,1}, // 4->5 {0,0,0,0,0,0} }; static ArrayList path = new ArrayList(); public static void main (String[] args) { System.out.println(findPath(0,5,path)); } public static ArrayList findPath(int x, int y, ArrayList path) { System.out.println("CALL FIND: " + path); ArrayList path2 = null; if (tree[x][y]==1) { ArrayList path1 = (ArrayList)path.clone(); path1.add(y); System.out.println("EXIT BASE: " + path1); path2 = path1; } else { int z; for (z=0; z