1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 Decrease & conquer  previous examples  search spaces examples: travel, word ladder  depth.

Slides:



Advertisements
Similar presentations
Artificial Intelligence Uninformed Search by LISP
Advertisements

Review: Search problem formulation
CSC 421: Algorithm Design & Analysis
CS252: Systems Programming Ninghui Li Program Interview Questions.
Dynamic Programming.
ICS-171:Notes 4: 1 Notes 4: Optimal Search ICS 171 Summer 1999.
Problem Solving Agents A problem solving agent is one which decides what actions and states to consider in completing a goal Examples: Finding the shortest.
Solving Problems by Searching Currently at Chapter 3 in the book Will finish today/Monday, Chapter 4 next.
HST 952 Computing for Biomedical Scientists Lecture 9.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Review: Search problem formulation
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Artificial Intelligence Chapter 3: Solving Problems by Searching
BST Data Structure A BST node contains: A BST contains
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Search  Exhaustive/Blind Search Methods Depth First Search Breadth First Search  Heuristic Methods Hill Climbing Beam Search Best First Search…
Backtracking.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
1 CSC 550: Introduction to Artificial Intelligence Fall 2004 heuristics & informed search  heuristics  hill-climbing bold + informed search potential.
Heapsort Based off slides by: David Matuszek
Review: Search problem formulation Initial state Actions Transition model Goal state (or goal test) Path cost What is the optimal solution? What is the.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Computer Science CPSC 322 Lecture 9 (Ch , 3.7.6) Slide 1.
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
CSC 211 Data Structures Lecture 13
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
For Friday Finish reading chapter 4 Homework: –Lisp handout 4.
For Monday Read chapter 4, section 1 No homework..
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Lecture 3: Uninformed Search
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Problem-solving approaches  divide & conquer  greedy  backtracking examples: N-queens, Boggle,
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Decrease & conquer  previous examples  search spaces examples: travel, word ladder  depth.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Algorithm Design Methods (II) Fall 2003 CSE, POSTECH.
For Friday Read chapter 4, sections 1 and 2 Homework –Chapter 3, exercise 7 –May be done in groups.
Basic Problem Solving Search strategy  Problem can be solved by searching for a solution. An attempt is to transform initial state of a problem into some.
Goal-based Problem Solving Goal formation Based upon the current situation and performance measures. Result is moving into a desirable state (goal state).
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Fahiem Bacchus © 2005 University of Toronto 1 CSC384: Intro to Artificial Intelligence Search II ● Announcements.
CPSC 322, Lecture 5Slide 1 Uninformed Search Computer Science cpsc322, Lecture 5 (Textbook Chpt 3.5) Sept, 13, 2013.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Recursion Powerful Tool
Lecture 3: Uninformed Search
CSC 427: Data Structures and Algorithm Analysis
CSC 427: Data Structures and Algorithm Analysis
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CSC 222: Object-Oriented Programming
CSC 421: Algorithm Design & Analysis
Cse 373 April 14th – TreEs pt 2.
Binary Search Trees (I)
COMP 103 Binary Search Trees.
CMSC 341 Lecture 13 Leftist Heaps
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.
Graphs & Graph Algorithms 2
Searching for Solutions
CSE 373: Data Structures and Algorithms
CSC 421: Algorithm Design & Analysis
CSC 143 Binary Search Trees.
CSC 421: Algorithm Design & Analysis
Basic Search Methods How to solve the control problem in production-rule systems? Basic techniques to find paths through state- nets. For the moment: -
Presentation transcript:

1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 Decrease & conquer  previous examples  search spaces examples: travel, word ladder  depth first search w/o cycle checking, w/ cycle checking  breadth first search w/o cycle checking, w/ cycle checking

2 Divide & Conquer RECALL: the divide & conquer approach tackles a complex problem by breaking it into proportionally-sized pieces  e.g., merge sort divided the list into halves, conquered (sorted) each half, then merged the results  e.g., to count number of nodes in a binary tree, break into counting the nodes in each subtree (which are smaller), then adding the results + 1 divide & conquer is a natural approach to many problems and tends to be efficient when applicable sometimes, the pieces only reduce the problem size by a constant amount  such decrease-and-conquer approaches tend to be less efficient Cost(N) = Cost(N/2) + C  O(log N) Cost(N) = Cost(N-1) + C  O(N)

3 Decrease & Conquer previous examples of decrease-and-conquer  sequential search : check the first item; if not it, search the remainder  linked list length : count first node, then add to length of remainder  selection sort : find the smallest item and swap it into the first location; then sort the remainder of the list  insertion sort : traverse the items, inserting each into the correct position in a sorted list some problems can be naturally viewed as a series of choices  e.g., a driving trip, the N-queens problem can treat these as decrease-and-conquer problems  take the first step, then solve the problem from that point

4 Example: airline connections suppose you are planning a trip from Omaha to Los Angeles initial situation: located in Omaha goal: located in Los Angeles possible flights: Omaha  ChicagoDenver  Los Angeles Omaha  DenverDenver  Omaha Chicago  DenverLos Angeles  Chicago Chicago  Los AngelesLos Angeles  Denver Chicago  Omaha Omaha Chicago Denver L.A.

5 State space search can view the steps in problem-solving as a tree node : a situation or state edge : the action moving between two situations/states goal: find a path from the start (root) to a node with desired properties Omaha ChicagoDenver L.A.OmahaL.A.Omaha L.A.Omaha

DFS vs. BFS two common strategies for conducting a search are:  depth first search: boldly work your way down one path  breadth first search: try each possible move at a level before moving to next 6 Omaha ChicagoDenver L.A.OmahaL.A.Omaha L.A.Omaha tradeoffs?

7 Depth first search basic idea:  keep track of the path you are currently searching:  if the current state/node is the goal, then SUCCEED  if there are no moves from the current state/node, then FAIL  otherwise, (recursively) try the first action/edge if not successful, try successive actions/edges public WordLadder findDepth1(String startWord, String endWord) { WordLadder startLadder = new WordLadder(); startLadder.addWord(startWord); if (startWord.equals(endWord)) { return startLadder; } else { for (String word : dictionary) { if (this.differByOne(word, startWord)) { WordLadder restLadder = findDepth1(word, endWord); if (restLadder != null) { startLadder.append(restLadder); return startLadder; } return null; } somewhat wasteful: each subproblem generates its own ladder, which must then be merged

8 Depth first search v. 2 alternatively, could pass the partial ladder along the recursion  1 st parameter is the ladder so far (initially contains just the starting word)  2 nd parameter is the ending word public WordLadder findDepth2(String startWord, String endWord) { WordLadder ladder = new WordLadder(); ladder.addWord(startWord); if (this.findDepth2(ladder, endWord)) { return ladder; } else { return null; } private boolean findDepth2(WordLadder sofar, String endWord) { if (sofar.endWord().equals(endWord)) { return true; } else { for (String word : dictionary) { if (this.differByOne(word, sofar.endWord())) { sofar.addWord(word); if (findDepth2(sofar, endWord)) { return true; } else { sofar.removeWord(); } return false; }

9 DFS and cycles since DFS moves blindly down one path, cycles are a SERIOUS problem  what if Omaha was listed before Denver in the Chicago flights? Omaha ChicagoDenver OmahaL.A.DenverL.A.Omaha it often pays to test for cycles:  already have the current path stored, so relatively easy  before adding next node/state to path, make sure not already a member  if it is a member, abandon the path and try a different action/edge

10 Depth first search with cycle checking public WordLadder findDepth2(String startWord, String endWord) { WordLadder ladder = new WordLadder(); ladder.addWord(startWord); if (this.findDepth2(ladder, endWord)) { return ladder; } else { return null; } private boolean findDepth2(WordLadder sofar, String endWord) { if (sofar.endWord().equals(endWord)) { return true; } else { for (String word : dictionary) { if (this.differByOne(word, sofar.endWord()) && !sofar.contains(word)) { sofar.addWord(word); if (findDepth2(sofar, endWord)) { return true; } else { sofar.removeWord(); } return false; } since the ladder so far is being passed along, checking for cycles is easy  make sure word is not already in the ladder before adding

11 Breadth vs. depth even with cycle checking, DFS may not find the shortest solution  if state space is infinite, might not find solution at all breadth first search (BFS)  extend the search one level at a time i.e., from the start state (root), try every possible action/edge (& remember them all) if don't reach goal, then try every possible action/edge from those nodes/states...  requires keeping a list of partially expanded search paths  ensure breadth by treating the list as a queue when want to expand shortest path: take off front, extend & add to back [ [Omaha] ] [ [Omaha, Chicago], [Omaha, Denver] ] [ [Omaha, Denver], [Omaha, Chicago, Omaha], [Omaha, Chicago, LA], [Omaha, Chicago, Denver] ] [ [Omaha, Chicago, Omaha], [Omaha, Chicago, LA], [Omaha, Chicago, Denver], [Omaha, Denver, LA], [Omaha, Denver, Omaha] ] [ [Omaha, Chicago, LA], [Omaha, Chicago, Denver], [Omaha, Denver, LA], [Omaha, Denver, Omaha], [Omaha, Chicago, Omaha, Chicago], [Omaha, Chicago, Omaha, LA] ]

12 Breadth first search BFS is trickier to code since you must maintain an ordered queue of all paths currently being considered public WordLadder findBreadth(String startWord, String endWord) { Queue ladders = new LinkedList (); WordLadder startLadder = new WordLadder(); startLadder.addWord(startWord); ladders.add(startLadder); while (!ladders.isEmpty() && !ladders.peek().endWord().equals(endWord)) { WordLadder shortestLadder = ladders.remove(); for (String word : dictionary) { if (this.differByOne(word, shortestLadder.endWord())) { WordLadder copy = new WordLadder(shortestLadder); copy.addWord(word); ladders.add(copy); } if (ladders.isEmpty()) { return null; } else { return ladders.remove(); }

13 Breadth first search w/ cycle checking as before, can add cycle checking to avoid wasted search  don't extend path if new state already occurs on the path WILL CYCLE CHECKING AFFECT THE ANSWER FOR BFS? IF NOT, WHAT PURPOSE DOES IT SERVE? [ [Omaha] ] [ [Omaha, Chicago], [Omaha, Denver] ] [ [Omaha, Denver], [Omaha, Chicago, LA], [Omaha, Chicago, Denver] ] [ [Omaha, Chicago, LA], [Omaha, Chicago, Denver], [Omaha, Denver, LA] ] [ [Omaha, Chicago, LA], [Omaha, Chicago, Denver], [Omaha, Denver, LA], [Omaha, Chicago, Omaha, LA] ]

14 Breadth first search w/ cycle checking again, since you have the partial ladder stored, it is easy to check for cycles  can greatly reduce the number of paths stored, but does not change the answer public WordLadder findBreadth(String startWord, String endWord) { Queue ladders = new LinkedList (); WordLadder startLadder = new WordLadder(); startLadder.addWord(startWord); ladders.add(startLadder); while (!ladders.isEmpty() && !ladders.peek().endWord().equals(endWord)) { WordLadder shortestLadder = ladders.remove(); for (String word : dictionary) { if (this.differByOne(word, shortestLadder.endWord()) && !shortestLadder.contains(word)) { WordLadder copy = new WordLadder(shortestLadder); copy.addWord(word); ladders.add(copy); } if (ladders.isEmpty()) { return null; } else { return ladders.remove(); }

Transform & conquer twist can further optimize if all you care about is the shortest ladder CLAIM: can remove a word from the dictionary when it is added to a ladder JUSTIFICATION? how much difference would removing words make on efficiency? 15

16 DFS vs. BFS Advantages of DFS  requires less memory than BFS since only need to remember the current path  if lucky, can find a solution without examining much of the state space  with cycle-checking, looping can be avoided Advantages of BFS  guaranteed to find a solution if one exists – in addition, finds optimal (shortest) solution first  will not get lost in a blind alley (i.e., does not require backtracking or cycle checking)  can add cycle checking to reduce wasted search note: just because BFS finds the optimal solution, it does not necessarily mean that it is the optimal control strategy !