/** * Main method to solve the maze. pre: args[0] contains the name of the input file. * * @param args Command line argument */ public static void main(String[] args) { int numV = 0; // The number of vertices. Graph theMaze = null; // Load the graph data from a file. try { Scanner scan = new Scanner(new FileReader(args[0])); theMaze = AbstractGraph.createGraph(scan, false, "List"); numV = theMaze.getNumV(); } catch (IOException ex) { System.err.println("IO Error while reading graph"); System.err.println(ex.toString()); System.exit(1); } // Perform breadth-first search. int parent[] = BreadthFirstSearch.breadthFirstSearch(theMaze, 0); // Construct the path. Deque<Integer> thePath = new ArrayDeque<Integer>(); int v = numV - 1; while (parent[v] != -1) { thePath.push(new Integer(v)); v = parent[v]; } // Output the path. System.out.println("The Shortest path is:"); while (!thePath.isEmpty()) { System.out.println(thePath.pop()); } }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // int[] // postOrder1={79,101,133,92,116,57,58,140,124,129,162,45,170,73,95,90,47,88,32,8,19,161,56,108,166,4,125,172,163,104,141,70,43,120,115,110,84,86,26,41,113,12,17,37,160,109,5,16,135,121,169,24,174,29,102,3,94,48,155,112,130,15,165,150,61,138,71,11,171,157,66,52,119,18,168,69,13,1,28,164,148,152,144,173,85,62,126,167,117,159,82,7,139,145,50,27,14,81,131,36,60,91,114,21,80,96,74,68,143,132,23,25,31,55,103,9,118,146,151,123,106,122,76,44,65,111,6,127,54,142,105,137,128,134,77,30,49,153,89,42,39,99,75,10,156,83,33,35,136,158,72,46,53,93,51,67,59,40,22,38,98,20,100,64,147,78,34,2,107,149,63,97,154,87,0}; // int[] // inOrder1={154,97,63,149,2,34,147,64,100,20,40,67,51,93,72,158,136,35,33,156,10,75,99,42,49,30,77,128,54,44,76,151,146,23,68,80,21,36,50,139,82,159,117,167,85,173,144,152,28,1,168,18,157,171,71,15,112,155,94,3,102,174,121,135,16,109,113,41,26,84,115,120,104,163,166,56,161,8,32,88,170,129,140,116,133,79,101,92,57,58,124,162,45,73,95,90,47,19,108,4,125,172,141,70,43,110,86,12,17,37,160,5,169,24,29,48,130,165,150,61,138,11,66,52,119,69,13,164,148,62,126,7,145,27,14,81,131,60,91,114,96,74,143,132,25,31,55,103,9,118,123,106,122,65,111,6,127,142,105,137,134,153,89,39,83,46,53,59,22,38,98,78,107,87,0}; int[] postOrder = { 110, 121, 135, 71, 101, 116, 40, 22, 32, 100, 82, 142, 6, 143, 25, 49, 123, 87, 77, 31, 120, 12, 48, 41, 66, 17, 109, 63, 28, 35, 140, 113, 70, 79, 122, 92, 53, 96, 90, 75, 8, 114, 21, 36, 91, 9, 115, 43, 128, 37, 93, 107, 86, 1, 104, 138, 16, 65, 14, 2, 102, 111, 98, 83, 10, 119, 52, 68, 19, 85, 84, 80, 118, 13, 130, 94, 18, 50, 112, 11, 44, 124, 51, 134, 55, 99, 62, 56, 33, 57, 30, 137, 81, 39, 105, 108, 89, 42, 5, 126, 64, 23, 60, 141, 136, 127, 103, 3, 78, 139, 4, 74, 26, 129, 88, 20, 7, 59, 69, 133, 73, 97, 61, 131, 45, 38, 125, 54, 76, 46, 95, 106, 47, 67, 24, 72, 15, 34, 27, 29, 58, 117, 132, 0 }; int[] inOrder = { 117, 58, 67, 95, 46, 54, 125, 73, 69, 59, 7, 88, 26, 139, 78, 103, 136, 60, 23, 126, 42, 89, 105, 81, 137, 30, 33, 56, 99, 51, 44, 112, 50, 18, 13, 118, 80, 84, 85, 68, 52, 10, 83, 98, 111, 102, 2, 16, 86, 36, 21, 114, 8, 75, 90, 53, 92, 122, 70, 66, 41, 120, 31, 77, 87, 123, 25, 143, 6, 82, 100, 22, 116, 101, 135, 110, 121, 71, 40, 32, 142, 49, 12, 48, 17, 109, 63, 28, 35, 140, 113, 79, 96, 91, 9, 115, 43, 128, 37, 93, 107, 1, 104, 138, 65, 14, 119, 19, 130, 94, 11, 124, 134, 55, 62, 57, 39, 108, 5, 64, 141, 127, 3, 4, 74, 129, 20, 133, 97, 61, 131, 45, 38, 76, 106, 47, 24, 72, 15, 34, 27, 29, 132, 0 }; TreeNode root = null; try { root = new CreateTreeGivenPostAndInOrder() .createBinaryTree(postOrder, 0, postOrder.length - 1, inOrder, 0, inOrder.length - 1); } catch (Exception e) { System.out.println("Invalid Input"); } // root.inOrderTraversal(root); if (root != null) { BreadthFirstSearch bfs = new BreadthFirstSearch(); StringBuffer sBuffer = bfs.doBFS(root); System.out.println(sBuffer.substring(0, sBuffer.length() - 1)); } }
public static boolean isTree(Digraph digraph) { Object root = null; for (Iterator i = digraph.vertexIterator(); i.hasNext(); ) { Object vertex = i.next(); int inSize = digraph.incomingSize(vertex); if (inSize == 0) { root = vertex; break; } } // not a tree - no vertex with 0 in-degree if (root == null) return false; // try to reach all vertices from the root candidate BreadthFirstSearch traversal = new BreadthFirstSearch(digraph, root); while (traversal.isValidTree() && traversal.hasNext()) traversal.next(); // not a tree - one of vertices has been seen more than once by the BFS if (!traversal.isValidTree()) return false; // has every vertex been reached? Set seenVertices = traversal.getSeenVertices(); for (Iterator i = digraph.vertexIterator(); i.hasNext(); ) if (!seenVertices.contains(i.next())) return false; // all tests are passed - good! return true; }