1 / 17

L6: Searching Program in Java

L6: Searching Program in Java. Breadth-first search vs depth-first search. ソースコードの解説4. [0]L.A.Airport. [1]UCLA. [2]Hoolywood. [5] SanDiego. [3]Anaheim. [6]Downtown. [7]Pasadena. [4]GrandCanyon. [8]DisneyLand. [9]LasVegas. Breath-first search.

mhead
Download Presentation

L6: Searching Program in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. L6: Searching Program in Java Breadth-first search vs depth-first search

  2. ソースコードの解説4 [0]L.A.Airport [1]UCLA [2]Hoolywood [5] SanDiego [3]Anaheim [6]Downtown [7]Pasadena [4]GrandCanyon [8]DisneyLand [9]LasVegas

  3. Breath-first search The breadth-first search algorithm searches a state space by constructing a hierarchical tree structure consisting of a set of nodes and links. The algorithm defines a way to move through the tree structure, examining the value at nodes. From the start, it looks at each node one edge away. Then it moves out from those nodes to all two edges away from the start. This continues until either the goal node is found or the entire tree is searched. • The algorithm follows: • Create a queue and add the first node to it. • Loop: • If the queue is empty, quit. • Remove the first node from the queue. • If the node contains the goal state, then exit with the node as the solution. • For each child of the current node: add the new state to the back of the queue.

  4. Outline of breadthFirst() public void breadthFirst(){ Vector open = new Vector(); Vector closed = new Vector(); …… for(;;){ …… if(open.size() == 0){// openは空か? …… } else { Node node = (Node)open.elementAt(0); open.removeElementAt(0); if(node == goal){ success = true; break; } else { Vector children = node.getChildren(); closed.addElement(node); for(int i = 0 ; i < children.size() ; i++){ …… open.addElement(m); //add at the back } } } } …… }

  5. Depth-first search The depth-first algorithm follows a single branch of the tree down as many levels as possible until we either reach a solution or a dead end. It searches from the start or root node all the way down to a leaf node. If it does not find the goal node, it backtracks up the tree and searches down the next untested path until it reaches the next leaf. • The algorithm follows: • Create a queue and add the first node to it. • Loop: • If the queue is empty, quit. • Remove the first node from the queue. • If the node contains the goal state, then exit with the node as the solution. • For each child of the current node: add the new state to the front of the queue.

  6. The outline of depthFirst() public void depthFirst(){ Vector open = new Vector(); Vector closed = new Vector(); …… for(;;){ …… if(open.size() == 0){ …… } else {   Node node = (Node)open.elementAt(0); open.removeElementAt(0); if(node == goal){ success = true; break; } else { Vector children = node.getChildren(); closed.addElement(node); int j = 0; for(int i = 0 ; i < children.size() ; i++){ …… open.insertElementAt(m, j ); …… j++; } }} } ……. }

  7. Define a Node class  Construct a tree or a graph  Searching 1 2 3 import java.util.*; public class Search{ Node node[]; Node goal; Node start; Search(){ makeStateSpace(); } private void makeStateSpace(){ … } public void breadthFirst(){ … } public void depthFirst(){ … } public void printSolution(Node theNode){ … } public static void main(String args[]){ … (new Search()).breadthFirst(); (new Search()).depthFirst(); … } 1 class Node { String name; Vector children; Node pointer; // 解表示のためのポインタ Node(String theName){ name = theName; children = new Vector(); } …… } 2 3 3

  8. 2 private void makeStateSpace(){ node = new Node[10]; node[0] = new Node("L.A.Airport"); start = node[0]; node[1] = new Node("UCLA"); node[2] = new Node("Hoolywood"); node[3] = new Node("Anaheim"); node[4] = new Node("GrandCanyon"); node[5] = new Node("SanDiego"); node[6] = new Node("Downtown"); node[7] = new Node("Pasadena"); node[8] = new Node("DisneyLand"); node[9] = new Node("Las Vegas"); goal = node[9]; node[0].addChild(node[1]); node[0].addChild(node[2]); node[1].addChild(node[2]); node[1].addChild(node[6]); node[2].addChild(node[3]); node[2].addChild(node[6]); node[2].addChild(node[7]); node[3].addChild(node[4]); node[3].addChild(node[7]); node[3].addChild(node[8]); node[4].addChild(node[8]); node[4].addChild(node[9]); node[5].addChild(node[1]); node[6].addChild(node[5]); node[6].addChild(node[7]); node[7].addChild(node[8]); node[7].addChild(node[9]); node[8].addChild(node[9]); } 1 class Node { String name; Vector children; Node pointer; // 解表示のためのポインタ Node(String theName){ name = theName; children = new Vector(); } public String getName(){ return name; } public void setPointer(Node theNode){ this.pointer = theNode; } public Node getPointer(){ return this.pointer; } public void addChild(Node theChild){ children.addElement(theChild); } public Vector getChildren(){ return children; } public String toString(){ String result = name; return result; } }

  9. ソースコードの解説1 L.A.Airport node[0] スタート地点 node[0] = new Node("L.A.Airport"); start = node[0]; node[1] = new Node("UCLA"); node[2] = new Node("Hoolywood"); node[3] = new Node("Anaheim"); node[4] = new Node("GrandCanyon"); node[5] = new Node("SanDiego"); node[6] = new Node("Downtown"); node[7] = new Node("Pasadena"); node[8] = new Node("DisneyLand"); node[9] = new Node("Las Vegas"); goal = node[9]; UCLA Node[1] ~ ~ DisneyLand Node[8] LasVegas node[9] ゴール地点

  10. ソースコードの解説2 親ノード node[0].addChild(node[1]); //※1 node[0].addChild(node[2]); //※2 node[1].addChild(node[2]); // node[1].addChild(node[6]); // node[2].addChild(node[3]); // node[2].addChild(node[6]); // node[2].addChild(node[7]); // node[3].addChild(node[4]); // node[3].addChild(node[7]); node[3].addChild(node[8]); node[4].addChild(node[8]); node[4].addChild(node[9]); node[5].addChild(node[1]); node[6].addChild(node[5]); node[6].addChild(node[7]); node[7].addChild(node[8]); node[7].addChild(node[9]); node[8].addChild(node[9]); [0]L.A.Airport ※1 addchild ※2 addchild [1]UCLA [2]Hoolywood 子ノード 子ノード

  11. ソースコードの解説3 node[0].addChild(node[1]); // node[0].addChild(node[2]); // node[1].addChild(node[2]); //※3 node[1].addChild(node[6]); //※4 node[2].addChild(node[3]); // node[2].addChild(node[6]); // node[2].addChild(node[7]); // node[3].addChild(node[4]); // node[3].addChild(node[7]); node[3].addChild(node[8]); node[4].addChild(node[8]); node[4].addChild(node[9]); node[5].addChild(node[1]); node[6].addChild(node[5]); node[6].addChild(node[7]); node[7].addChild(node[8]); node[7].addChild(node[9]); node[8].addChild(node[9]); [0]L.A.Airport 親ノード ※3 addchild [1]UCLA [2]Hoolywood 子ノード ※4 addchild [6]Downtown 子ノード

  12. ソースコードの解説4 [0]L.A.Airport [1]UCLA [2]Hoolywood [5] SanDiego [3]Anaheim [6]Downtown [7]Pasadena [4]GrandCanyon [8]DisneyLand [9]LasVegas

  13. public void breadthFirst(){ Vector open = new Vector(); //unchecked children nodes open.addElement(node[0]); Vector closed = new Vector(); //checked parent nodes boolean success = false; int step = 0; for(;;){ System.out.println("STEP:"+(step++)); System.out.println("OPEN:"+open.toString()); System.out.println("closed:"+closed.toString()); if(open.size() == 0){// openは空か? success = false; break; } else { Node node = (Node)open.elementAt(0); open.removeElementAt(0); if(node == goal){ success = true; break; } else { Vector children = node.getChildren(); closed.addElement(node); for(int i = 0 ; i < children.size() ; i++){ Node m = (Node)children.elementAt(i); if(!open.contains(m) && !closed.contains(m)){ m.setPointer(node); if(m == goal){ open.insertElementAt(m,0); } else { open.addElement(m); //add at the back } } } } } } if(success){ System.out.println("*** Solution ***"); printSolution(goal); } } public void depthFirst() { // Add your code here } 3 Breadth First Search STEP:0 OPEN:[L.A.Airport] closed:[] STEP:1 OPEN:[UCLA, Hoolywood] closed:[L.A.Airport] STEP:2 OPEN:[Hoolywood, Downtown] closed:[L.A.Airport, UCLA] Is the node, m, unchecked or not? There,node is the parent node. m is the unchecked child node. Can you use other source code to replace this line?

  14. public void printSolution(Node theNode){ if (theNode == start) { System.out.println(theNode.toString()); } else { System.out.print(theNode.toString()+" <- "); printSolution(theNode.getPointer()); } public static void main(String args[]){ if(args.length != 1){ System.out.println("USAGE:"); System.out.println("java Search [Number]"); System.out.println("[Number] = 1 : Bredth First Search"); System.out.println("[Number] = 2 : Depth First Search"); } else { int which = Integer.parseInt(args[0]); switch(which){ case 1: // 幅優先探索 System.out.println("\nBreadth First Search"); (new Search()).breadthFirst(); break; case 2: // 深さ優先探索 System.out.println("\nDepth First Search"); (new Search()).depthFirst(); break; default: System.out.println("Please input numbers 1 to 2"); } } } }

  15. Exercise • Input the program from keyboard, understand and run it. 2. Complete the depthFirst() searching method.

  16. Output: Breadth First Search STEP:0 OPEN:[L.A.Airport] closed:[] STEP:1 OPEN:[UCLA, Hoolywood] closed:[L.A.Airport] STEP:2 OPEN:[Hoolywood, Downtown] closed:[L.A.Airport, UCLA] STEP:3 OPEN:[Downtown, Anaheim, Pasadena] closed:[L.A.Airport, UCLA, Hoolywood] STEP:4 OPEN:[Anaheim, Pasadena, SanDiego] closed:[L.A.Airport, UCLA, Hoolywood, Downtown] STEP:5 OPEN:[Pasadena, SanDiego, GrandCanyon, DisneyLand] closed:[L.A.Airport, UCLA, Hoolywood, Downtown, Anaheim] STEP:6 OPEN:[Las Vegas, SanDiego, GrandCanyon, DisneyLand] closed:[L.A.Airport, UCLA, Hoolywood, Downtown, Anaheim, Pasadena] *** Solution *** Las Vegas <- Pasadena <- Hoolywood <- L.A.Airport

  17. Output: Depth First Search STEP:0 OPEN:[L.A.Airport] closed:[] STEP:1 OPEN:[UCLA, Hoolywood] closed:[L.A.Airport] STEP:2 OPEN:[Downtown, Hoolywood] closed:[L.A.Airport, UCLA] STEP:3 OPEN:[SanDiego, Pasadena, Hoolywood] closed:[L.A.Airport, UCLA, Downtown] STEP:4 OPEN:[Pasadena, Hoolywood] closed:[L.A.Airport, UCLA, Downtown, SanDiego] STEP:5 OPEN:[Las Vegas, DisneyLand, Hoolywood] closed:[L.A.Airport, UCLA, Downtown, SanDiego, Pasadena] *** Solution *** Las Vegas <- Pasadena <- Downtown <- UCLA <- L.A.Airport

More Related