Search, Trees, Games, Backtracking Search, Backtracking,Heuristics

 How do you find a needle in a haystack?  Trees help with search  How does a computer play chess?  Set, map: binary search , balanced and otherwise  Why would you write that program?  Quadtree and more, scenes and objects in games   3-4 Trees, KD-trees, search for files or points in “space”  How does Mapquest/Googlemap find routes from  Gametrees, search for game-playing one place to another?  Shortest path  Intelligent search  Longest path algorithms  How do we find the “best move” in a game?   How do we find optimal needle in haystack? Optimal algorithms and heuristic algorithms  When is close good enough? How do measure “closeness”?  When is brute-force, exhaustive search (im)possible?  When is optimality important, how much does it cost?

CPS 100, Spring 2009 8.1 CPS 100, Spring 2009 8.2

Exhaustive Search/Heuristics Classic problem: N queens

 We use binary search trees to organize data, in  Can queens be placed on a chess board so that no queens searching we don’t need to examine all the data to attack each other? find what we’re looking for  Easily place two queens  Where is the smallest item in a search tree? Largest?  What about 8 queens?  How are trees balanced?  Make the board NxN, this is the N queens problem  Place one queen/column  What do we do when the search space is huge?  Horiz/Vert/Diag attacks  How many chess boards are there?  Backtracking  Tentative placement  How many routes are there between my house and yours?  Recurse, if ok done!  If fail, undo tentative, retry  Exhaustive search: look at everything!  wikipedia-n-queens

CPS 100, Spring 2009 8.3 CPS 100, Spring 2009 8.4 Backtracking idea with N queens N queens backtracking: Queens.java

 Try to place a queen in each column in turn public boolean solve(int col){  Try first row in column C, if ok, move onto next column if (col == mySize) return true;  If solved, great, otherwise try next row in column C, place queen, move onto the next column // try each row until all are tried • Must unplace the placed queen to keep going for(int r=0; r < mySize; r++){  What happens when we start in a column, where to start? if (myBoard.safeToPlace(r,col)){  If we fail, move back to previous column (which remembers myBoard.setQueen(r,col,true); where it is/failed) if (solve(col+1)){  When starting in a column anew, start at beginning return true; • When backing up, try next location, not beginning } myBoard.setQueen(r,col,false); }  Backtracking in general, record an attempt go forward }  If going forward fails, undo the record and backup return false;

CPS 100, Spring 2009 8.5 CPS} 100, Spring 2009 8.6

Basic ideas in backtracking search Pruning vs. Exhaustive Search

 We need to be able to enumerate all possible choices/moves  If we consider every possible placement of 4 queens  We try these choices in order, committing to a choice on a 4x4 board, how many are there? (N queens)  If the choice doesn’t pan out we must undo the choice • This is the backtracking step, choices must be undoable  4x4x4x4 if we don’t pay attention to any attacks  4x3x2x1 if we avoid attacks in same row  Process is inherently recursive, so we need to know when the search finishes  When all columns tried in N queens  What about if we avoid diagonal attacks?  When we have found the exit in a maze  Pruning search space makes more search possible, still  When every possible moved tried in Tic-tac-toe or chess? could be lots of searching to do! • Is there a difference between these games?

 Summary: enumerate choices, try a choice, undo a choice, this  Estimate how long to calculate # solutions to the N- is brute force search: try everything queens problem with our Java code….

CPS 100, Spring 2009 8.7 CPS 100, Spring 2009 8.8 Queens Details Daphne Koller

 How do we know when it’s safe to place a queen?  2004, Macarthur  No queen in same row, or diagonal  2008, first ACM/Infosys  For each column, store the row that a queen is in “The world is noisy and messy …You  See QBoard.java for details need to deal with the noise and uncertainty.” “I find it distressing that the view of  For GUI version, we use a decorator the field is that you sit in your office  The QBoardGUI is an IQueenState class and it has an by yourself surrounded by old pizza IQueenState object in it boxes and cans of Coke, hacking away at the bowels of the Windows  Appears as an IQueenState to client, but uses an existing one to help do its work operating system,” she said. “I spend most of my time thinking about  One of many object oriented design patterns, seen in Huff things like how does a cell work or in the BitInputStream class how do we understand images in the  http://tinyurl.com/3tdlug world around us?” CPS 100, Spring 2009 8.9 CPS 100, Spring 2009 8.10

Computer v. Human in Games Games at Duke

 Computers can explore a large  Alan Biermann search space of moves quickly  Natural language processing  How many moves possible in  Compsci 1: Great Ideas chess, for example?  Duchess, checkers, chess

 Computers cannot explore every move (why) so must use heuristics  Tom Truscott  Rules of thumb about position,  Duke undergraduate working strategy, board evaluation with/for Biermann  Try a move, undo it and try  Usenet: online community another, track the best move  Second EFF Pioneer Award (with Vint Cerf!)  What do humans do well in these games? What about computers?  What about at Duke?

CPS 100, Spring 2009 8.11 CPS 100, Spring 2009 8.12 Heuristics Boggle Program

 A heuristic is a rule of thumb, doesn’t always work, isn’t guaranteed to work, but useful in many/most cases  Search problems that are “big” often can be approximated or solved with the right heuristics

 What heuristic is good for Sudoku?  Is there always a no-reasoning move, e.g., 5 goes here?  What about “if I put a 5 here, then…”  Do something else? http://en.wikipedia.org/wiki/Algorithmics_of_sudoku

 What other optimizations/improvements can we make?  For chess, checkers: good heuristics, good data structures

CPS 100, Spring 2009 8.13 CPS 100, Spring 2009 8.14

Boggle Search for Word Backtracking, , game search

 Starting at board location (row,col) to find a string s  We’ll use tic-tac-toe to illustrate the idea, but it’s a silly game to show the power of the method  We want to keep track of where we are in the string  What games might be better? Problems?  We want to keep track of what board locations we’ve used  Minimax idea: two players, one maximizes score, the other minimizes score, search complete/partial game tree for best  How do we know when we’re done? possible move  Base case of recursive, backtracking call  In tic-tac-toe we can search until the end-of-the game, but this isn’t possible in general, why not?  Where we are in the string?  Use static board evaluation functions instead of searching all the way until the game ends  How do we keep track of used locations?  Minimax leads to alpha-beta search, then to other rules and  Store in array list: tentatively use current one, recurse heuristics  If we don’t succeed, take off the last one stored!

CPS 100, Spring 2009 8.15 CPS 100, Spring 2009 8.16 Minimax, see TicTac.java Interlude for trees

 Players alternate, one might be  Joyce Kilmer computer, one human (or two X computer players)  Balanced Trees X X  Splay  Simple rules: win scores +10, X X loss scores –10, tie is zero O O  Red-Black O O  X maximizes, O minimizes  AVL  B-tree  Assume opponent plays smart X X X X X X  What happens otherwise? O O X X O O  As game tree is explored is there redundant search? X X O X X  What can we do about this? O O O

CPS 100, Spring 2009 8.17 CPS 100, Spring 2009 8.18

Tree functions (repeat, review) Balanced Trees and Complexity

 Compute height of a tree, what is complexity?  A tree is height-balanced if  Left and right subtrees are height-balanced int height(Tree root){  Left and right heights differ by at most one if (root == null) return 0; else { return 1 + Math.max(height(root.left), height(root.right) ); } } boolean isBalanced(Tree root){  Modify function to compute number of nodes in a if (root == null) return true; return tree, does complexity change? isBalanced(root.left) && isBalanced(root.right) &&  What about computing number of leaf nodes? Math.abs(height(root.left) – height(root.right)) <= 1; } }

CPS 100, Spring 2009 8.19 CPS 100, Spring 2009 8.20 Rotations and balanced trees What is complexity?

 Height-balanced trees  Assume trees are “balanced” in analyzing  For every node, left and right subtree heights differ complexity by at most 1  Roughly half the nodes in each subtree  After insertion/deletion  Leads to easier analysis need to rebalance Are these trees height-  Every operation leaves tree balanced? in a balanced state: invariant  property of tree How to develop recurrence relation?  Find deepest node that’s  What is T(n)? unbalanced then make sure:  What other work is done?  On path from root to inserted/deleted node  Rebalance at this  How to solve recurrence relation unbalanced point only  Plug, expand, plug, expand, find pattern  A real proof requires induction to verify correctness

CPS 100, Spring 2009 8.21 CPS 100, Spring 2009 8.22

Balanced trees we won't study Balanced trees we will study

 B-trees are used when data is both in memory and  Both kinds have worst-case O(log n) time for tree on disk operations  File systems, really large data sets  AVL (Adel’son-Velskii and Landis), 1962  Rebalancing guarantees good performance both  Nodes are “height-balanced”, subtree heights differ by 1 asymptotically and in practice. Differences between cache,  Rebalancing requires per-node bookkeeping of height memory, disk are important  http://webpages.ull.es/users/jriera/Docencia/AVL/AVL tree applet.htm  Splay trees rebalance during insertion and during search, nodes accessed often more closer to root  Red-black tree uses same rotations, but can  Other nodes can move further from root, consequences? rebalance in one pass, contrast to AVL tree • Performance for some nodes gets better, for others …  In AVL case, insert, calculate balance factors, rebalance  No guarantee running time for a single operation, but guaranteed good performance for a sequence of  In Red-black tree can rebalance on the way down, code is operations, this is good amortized cost (ArrayList.add) more complex, but doable  Standard java.util.TreeMap/TreeSet use red-black CPS 100, Spring 2009 8.23 CPS 100, Spring 2009 8.24 Rotation doLeft (see AVLSet.java) Rotation to rebalance ?????  Why is this called doLeft? N  Suppose we add a new node in N N  N will no longer be root, right subtree of left child of root N new value in left.left  Single rotation can’t fix subtree C A  Need to rotate twice  Left child becomes new C A A B B C First stage is shown at bottom root A B B C  Rotate blue node right  Unbalanced by two (not one!) • (its right child takes its place)  If left, left (or right, right)  This is left child of unbalanced • doLeft (doRight) Node doRight(Node root) Node doLeft(Node root)  Otherwise need two { { • doLeft/doRight C B Node newRoot = root.left; B 2 Node newRoot = root.right;  First to get to left, left 1 root.left = newRoot.right; A B1 B2 root.right = newRoot.left; • Or to right, right A newRoot.right = root; newRoot.left = root; return newRoot; return newRoot; } }

CPS 100, Spring 2009 8.25 CPS 100, Spring 2009 8.26

Double rotation complete AVL tree practice

 Calculate where to rotate and what  Insert into AVL tree: 18 case, do the rotations 16  18 10 16 12 6 3 8 13 14 Node doRight(Node root) {  After adding 16: doLeftRight 10 10 18 Node newRoot = root.right; C root.right = newRoot.left; B C doLeft newRoot.left = root; A B B B 2 16 1 2 1 18 18 return newRoot; 16 } A 10 doRight 16 10 18 10 18 Node doLeft(Node root) 16 { 10 6 12 Node newRoot = root.left; root.left = newRoot.right; newRoot.right = root;  After 3, doLeft on 16 return newRoot; 10 16 } 10 B1 B2 10 18 6 16 A C 6 16 6 12 3 12 18 3 8 12 18 3

CPS 100, Spring 2009 8.27 CPS 100, Spring 2009 8.28 AVL practice: continued, and finished Dame Wendy Hall

 After adding 13, ok  Multimedia, “web before the  After adding14, not ok 10 web”, President of ACM,  doRight at 12 cofounder of WSRI (web science 6 16 research institute) 10 3 8 12 18 "....We are living in very 6 16 unpredictable times....Every country, whether developed or the 3 8 12 18 10 developing, need people with IT 13 6 16 skills....What we need are people who can innovate. I think there is a 14 3 8 13 18 unique opportunity to say these are the types of skills we need young 12 14 people to have going forward...."

CPS 100, Spring 2009 8.29 CPS 100, Spring 2009 8.30