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

Slides:



Advertisements
Similar presentations
GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.
Advertisements

Tree Searching. Tree searches A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions,
State Space Search 2 Chapter 3 Three Algorithms. Backtracking Suppose We are searching depth-first No further progress is possible (i.e., we can only.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Using Search in Problem Solving
Using Search in Problem Solving
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
1 Structures and Strategies for State Space Search 3 3.0Introduction 3.1Graph Theory 3.2Strategies for State Space Search 3.3Using the State Space to Represent.
Search  Exhaustive/Blind Search Methods Depth First Search Breadth First Search  Heuristic Methods Hill Climbing Beam Search Best First Search…
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Search.
Dr. Jouhaina Chaouachi Siala
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Tree Searching Breadth First Search Dept First Search.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
L5: Searching Program in Java Breadth-first search vs depth-first search.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
1 Review of search strategies Decisions in games Minimax algorithm  -  algorithm Tic-Tac-Toe game Review of search strategies Decisions in games Minimax.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
1 PAGE of designing an agent Problem types Search Strategies PAGE of designing an agent Problem types Search Strategies.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Basic Search Procedure 1. Start with the start node (root of the search tree) and place in on the queue 2. Remove the front node in the queue and If the.
1 Solving problems by searching Chapter 3. Depth First Search Expand deepest unexpanded node The root is examined first; then the left child of the root;
Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Building Java Programs Binary Trees reading: 17.1 – 17.3.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Source: David Lee Matuszek
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
L12: A Wumpus World Project To be checked on 16 th January 11:10~12:40 In Lab1.
Backtracking. 2 Suppose you have to make a series of decisions, among various choices, where You don’t have enough information to know what to choose.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
Lecture 19: Binary Trees reading: 17.1 – 17.3
Data Structures and Design in Java © Rick Mercer
Building Java Programs
Composite Design Pattern
Trees.
Cse 373 April 14th – TreEs pt 2.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Algorithm Design and Analysis (ADA)
CS212: Data Structures and Algorithms
Depth-First Searches Introduction to AI.
Lecture 12 CS203 1.
Building Java Programs
L2. Problem Solving Using Search
Non-Linear Structures
Backtracking.
Building Java Programs
Algorithms: Design and Analysis
1 Lecture 10 CS2013.
Tree Searching.
Tree Searching.
Tree.
Trees.
Objects with ArrayLists as Attributes
Depth-First Searches.
Presentation transcript:

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

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

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.

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()

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.

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()

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(); … }

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

ソースコードの解説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] ゴール地点

ソースコードの解説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

ソースコードの解説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

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

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]

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 "); }

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

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

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