package FindPathsPls; import java.util.ArrayList; import java.util.Arrays; public class PathFinder { public static void main(String[] args) { ArrayList Graph = new ArrayList<>(); Graph.add(new String[]{"", "e1", "e3"}); Graph.add( new String[]{"e1", "", "e2"}); Graph.add( new String[]{"e3", "e2", ""}); GetPaths(Graph,3,3,2); ArrayList ExtremeGraph = new ArrayList<>(); ArrayList Graph2 = new ArrayList<>(); Graph2.add(new String[]{"", "e1,e2,e3", "e3,e4"}); Graph2.add( new String[]{"e1,e2,e3", "", "e6"}); Graph2.add( new String[]{"e3,e4", "e6", ""}); GetPaths(Graph2,3,3,2); } private static void GetPaths(ArrayList Graph , int row_len, int col_len, int path_len) { int power =2; int count = 0; if(path_len == 2) { ArrayList output = SquareMatrix(Graph, row_len, col_len); for(int i = 0; i < output.size();i++) { count++; System.out.print(Arrays.toString(output.get(i))+" , "); if(count % 3 == 0) System.out.println(); } } else { ArrayList output = Graph; while(power <= path_len) { output = MultiplyMatrix(output, Graph, row_len, col_len); power++; } for(int i = 0; i <= output.size();i++) { count++; System.out.print(Arrays.toString(output.get(i))); if((count % 3) == 0) System.out.println(); } } } private static ArrayList MultiplyMatrix(ArrayList M1, ArrayList M2, int row_len, int col_len) { return MatrixMultiplication(M1, M2, row_len, col_len); } private static ArrayList MatrixMultiplication(ArrayList M1, ArrayList M2, int row_len, int col_len) { String[] R; String[] C; ArrayList output = new ArrayList<>(); for(int rows = 0; rows < row_len; rows++) { R = getRow(M1,rows); for (int cols = 0; cols < col_len; cols++) { C = getColumn(M2, cols); output.add(findCart(R, C)); } } return output; } private static ArrayList SquareMatrix(ArrayList input, int row_len, int col_len) { return MatrixMultiplication(input, input, row_len, col_len); } public static String[] findCart(String[] arr1, String[] arr2) { ArrayList list = new ArrayList<>(); for (String s : arr1) for (String value : arr2) { if (!(s.equals("") || value.equals(""))) { list.add(s + "->" + value); } } String[] arr = new String[list.size()]; return list.toArray(arr); } public static String[] getColumn(ArrayList input , int index) { String[] output = new String[input.size()]; for(int i = 0; i < input.size(); i++) { if(input.get(i) != null) output[i] = input.get(i)[index]; } return output; } public static String[] getRow(ArrayList input , int index) { return input.get(index); } }