Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "L6: Searching Program in Java Breadth-first search vs depth-first search."— 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 [6]Downtown [5] SanDiego [7]Pasadena [3]Anaheim [4]GrandCanyon [9]LasVegas [8]DisneyLand

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: 1.Create a queue and add the first node to it. 2.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 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 } } } } …… } Outline of breadthFirst()

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: 1.Create a queue and add the first node to it. 2.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 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++; } } } } ……. } The outline of depthFirst()

7 Define a Node class  Construct a tree or a graph  Searching class Node { String name; Vector children; Node pointer; // 解表示のためのポイン タ Node(String theName){ name = theName; children = new Vector(); } …… } 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(); … } 321 1 2 3 3

8 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; } 1 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]); } 2

9 ソースコードの解説1 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]; L.A.Airport node[0] スタート地点 UCLA DisneyLand Node[1] Node[8] Las Vegas 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]UCLA[2]Hoolywood ※ 1 addchild 親ノード 子ノード ※ 2 addchild

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 [1]UCLA[2]Hoolywood ※ 3 addchild 親ノード 子ノード ※ 4 addchild [6]Downtown

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

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 Is the node, m, unchecked or not? Can you use other source code to replace this line? There, node is the parent node. m is the unchecked child node. 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]

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 1.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


Download ppt "L6: Searching Program in Java Breadth-first search vs depth-first search."

Similar presentations


Ads by Google