Search Trees Games Backtracking l Trees help with
Search, Trees, Games, Backtracking l Trees help with search Ø Ø l Set, map: binary search tree, balanced and otherwise Quadtree and more, scenes and objects in games 3 -4 Trees, KD-trees, search for files or points in “space” Gametrees, search for game-playing Intelligent search Ø Ø Ø How do we find the “best move” in a game? How do we find optimal needle in haystack? When is brute-force, exhaustive search (im)possible? CPS 100, Spring 2009 8. 1
Search, Backtracking, Heuristics l How do you find a needle in a haystack? Ø Ø How does a computer play chess? Why would you write that program? Ø l How does Mapquest/Googlemap find routes from one place to another? Ø Ø l Shortest path algorithms Longest path algorithms Optimal algorithms and heuristic algorithms Ø Ø When is close good enough? How do measure “closeness”? When is optimality important, how much does it cost? CPS 100, Spring 2009 8. 2
Exhaustive Search/Heuristics l We use binary search trees to organize data, in searching we don’t need to examine all the data to find what we’re looking for Ø Ø l What do we do when the search space is huge? Ø Ø l Where is the smallest item in a search tree? Largest? How are trees balanced? How many chess boards are there? How many routes are there between my house and yours? Exhaustive search: look at everything! CPS 100, Spring 2009 8. 3
Classic problem: N queens l Can queens be placed on a chess board so that no queens attack each other? Ø Ø l Make the board Nx. N, this is the N queens problem Ø Ø l Place one queen/column Horiz/Vert/Diag attacks Backtracking Ø Ø Ø l Easily place two queens What about 8 queens? Tentative placement Recurse, if ok done! If fail, undo tentative, retry wikipedia-n-queens CPS 100, Spring 2009 8. 4
Backtracking idea with N queens l Try to place a queen in each column in turn Ø Ø Try first row in column C, if ok, move onto next column If solved, great, otherwise try next row in column C, place queen, move onto the next column • Must unplace the placed queen to keep going l What happens when we start in a column, where to start? Ø Ø If we fail, move back to previous column (which remembers where it is/failed) When starting in a column anew, start at beginning • When backing up, try next location, not beginning l Backtracking in general, record an attempt go forward Ø If going forward fails, undo the record and backup CPS 100, Spring 2009 8. 5
N queens backtracking: Queens. java public boolean solve(int col){ if (col == my. Size) return true; // try each row until all are tried for(int r=0; r < my. Size; r++){ if (my. Board. safe. To. Place(r, col)){ my. Board. set. Queen(r, col, true); if (solve(col+1)){ return true; } my. Board. set. Queen(r, col, false); } } return false; } CPS 100, Spring 2009 8. 6
Basic ideas in backtracking search l We need to be able to enumerate all possible choices/moves Ø Ø We try these choices in order, committing to a choice If the choice doesn’t pan out we must undo the choice • This is the backtracking step, choices must be undoable l Process is inherently recursive, so we need to know when the search finishes Ø Ø Ø When all columns tried in N queens When we have found the exit in a maze When every possible moved tried in Tic-tac-toe or chess? • Is there a difference between these games? l Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything CPS 100, Spring 2009 8. 7
Pruning vs. Exhaustive Search l If we consider every possible placement of 4 queens on a 4 x 4 board, how many are there? (N queens) Ø Ø l What about if we avoid diagonal attacks? Ø l 4 x 4 x 4 x 4 if we don’t pay attention to any attacks 4 x 3 x 2 x 1 if we avoid attacks in same row Pruning search space makes more search possible, still could be lots of searching to do! Estimate how long to calculate # solutions to the N-queens problem with our Java code…. CPS 100, Spring 2009 8. 8
Queens Details l How do we know when it’s safe to place a queen? Ø Ø Ø l No queen in same row, or diagonal For each column, store the row that a queen is in See QBoard. java for details For GUI version, we use a decorator Ø Ø Ø The QBoard. GUI is an IQueen. State class and it has an IQueen. State object in it Appears as an IQueen. State to client, but uses an existing one to help do its work One of many object oriented design patterns, seen in Huff in the Bit. Input. Stream class CPS 100, Spring 2009 8. 9
Daphne Koller l l 2004, Macarthur 2008, first ACM/Infosys “The world is noisy and messy …You need to deal with the noise and uncertainty. ” “I find it distressing that the view of l the field is that you sit in your office by yourself surrounded by old pizza boxes and cans of Coke, hacking away at the bowels of the Windows operating system, ” she said. “I spend most of my time thinking about things like how does a cell work or how do we understand images in the world http: //tinyurl. com/3 tdlug around us? ” CPS 100, Spring 2009 8. 10
Computer v. Human in Games l Computers can explore a large search space of moves quickly Ø l Computers cannot explore every move (why) so must use heuristics Ø Ø l Rules of thumb about position, strategy, board evaluation Try a move, undo it and try another, track the best move What do humans do well in these games? What about computers? Ø CPS 100, Spring 2009 How many moves possible in chess, for example? What about at Duke? 8. 11
Games at Duke l Alan Biermann Ø Ø Ø l Tom Truscott Ø Ø l CPS 100, Spring 2009 Natural language processing Compsci 1: Great Ideas Duchess, checkers, chess Duke undergraduate working with/for Biermann Usenet: online community Second EFF Pioneer Award (with Vint Cerf!) 8. 12
Heuristics l A heuristic is a rule of thumb, doesn’t always work, isn’t guaranteed to work, but useful in many/most cases Ø l What heuristic is good for Sudoku? Ø Ø Ø l Search problems that are “big” often can be approximated or solved with the right heuristics 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
Boggle Program CPS 100, Spring 2009 8. 14
Boggle Search for Word l Starting at board location (row, col) to find a string s Ø Ø l How do we know when we’re done? Ø Ø l We want to keep track of where we are in the string We want to keep track of what board locations we’ve used Base case of recursive, backtracking call Where we are in the string? How do we keep track of used locations? Store in array list: tentatively use current one, recurse 8. 15 CPS 100, 2009 don’t succeed, take off the last one stored! ØSpring If we Ø
Backtracking, minimax, game search l We’ll use tic-tac-toe to illustrate the idea, but it’s a silly game to show the power of the method Ø l Minimax idea: two players, one maximizes score, the other minimizes score, search complete/partial game tree for best possible move Ø Ø l What games might be better? Problems? In tic-tac-toe we can search until the end-of-the game, but this isn’t possible in general, why not? Use static board evaluation functions instead of searching all the way until the game ends Minimax leads to alpha-beta search, then to other rules and heuristics CPS 100, Spring 2009 8. 16
Minimax, see Tic. Tac. java l Players alternate, one might be computer, one human (or two computer players) l Simple rules: win scores +10, loss scores – 10, tie is zero Ø l l What happens otherwise? As game tree is explored is there redundant search? Ø X O X X maximizes, O minimizes Assume opponent plays smart Ø X What can we do about this? CPS 100, Spring 2009 X X O X O O X X O O 8. 17
Interlude for trees l l Joyce Kilmer Balanced Trees Ø Ø Splay Red-Black AVL B-tree CPS 100, Spring 2009 8. 18
Tree functions (repeat, review) l Compute height of a tree, what is complexity? int height(Tree root){ if (root == null) return 0; else { return 1 + Math. max(height(root. left), height(root. right) ); } } l Modify function to compute number of nodes in a tree, does complexity change? Ø What about computing number of leaf nodes? CPS 100, Spring 2009 8. 19
Balanced Trees and Complexity l A tree is height-balanced if Ø Left and right subtrees are height-balanced Ø Left and right heights differ by at most one boolean is. Balanced(Tree root){ if (root == null) return true; return is. Balanced(root. left) && is. Balanced(root. right) && Math. abs(height(root. left) – height(root. right)) <= 1; } } CPS 100, Spring 2009 8. 20
Rotations and balanced trees l l Height-balanced trees Ø For every node, left and right subtree heights differ by at most 1 Ø After insertion/deletion need to rebalance Ø Every operation leaves tree in a balanced state: invariant property of tree Find deepest node that’s unbalanced then make sure: Ø On path from root to inserted/deleted node Ø Rebalance at this unbalanced point only CPS 100, Spring 2009 Are these trees heightbalanced? 8. 21
What is complexity? l Assume trees are “balanced” in analyzing complexity Ø Ø l How to develop recurrence relation? Ø Ø l Roughly half the nodes in each subtree Leads to easier analysis What is T(n)? What other work is done? How to solve recurrence relation Ø Ø Plug, expand, plug, expand, find pattern A real proof requires induction to verify correctness CPS 100, Spring 2009 8. 22
Balanced trees we won't study l B-trees are used when data is both in memory and on disk Ø Ø l File systems, really large data sets Rebalancing guarantees good performance both asymptotically and in practice. Differences between cache, memory, disk are important Splay trees rebalance during insertion and during search, nodes accessed often more closer to root Ø Other nodes can move further from root, consequences? • Performance for some nodes gets better, for others … Ø No guarantee running time for a single operation, but guaranteed good performance for a sequence of operations, this is good amortized cost (Array. List. add) CPS 100, Spring 2009 8. 23
Balanced trees we will study l l Both kinds have worst-case O(log n) time for tree operations AVL (Adel’son-Velskii and Landis), 1962 Ø Ø Ø l Nodes are “height-balanced”, subtree heights differ by 1 Rebalancing requires per-node bookkeeping of height http: //webpages. ull. es/users/jriera/Docencia/AVL tree applet. htm Red-black tree uses same rotations, but can rebalance in one pass, contrast to AVL tree Ø Ø Ø In AVL case, insert, calculate balance factors, rebalance In Red-black tree can rebalance on the way down, code is more complex, but doable Standard java. util. Tree. Map/Tree. Set use red-black CPS 100, Spring 2009 8. 24
Rotation do. Left (see AVLSet. java) l N N C A B C l Why is this called do. Left? Ø N will no longer be root, new value in left subtree Ø Left child becomes new root Unbalanced by two (not one!) Ø If left, left (or right, right) • do. Left (do. Right) Node do. Left(Node root) Ø Otherwise need two { • do. Left/do. Right Node new. Root = root. left; root. left = new. Root. right; Ø First to get to left, left • Or to right, right new. Root. right = root; return new. Root; } CPS 100, Spring 2009 8. 25
Rotation to rebalance ? ? ? N N C A Suppose we add a new node in right subtree of left child of root Ø Single rotation can’t fix Ø Need to rotate twice l C First stage is shown at bottom Ø Rotate blue node right l A B B • (its right child takes its place) This is left child of Nodeunbalanced do. Right(Node root) Ø C A B 1 B 2 { Node new. Root = root. right; root. right = new. Root. left; new. Root. left = root; return new. Root; A } CPS 100, Spring 2009 8. 26
Double rotation complete l Calculate where to rotate and what case, do the rotations Node do. Right(Node root) { Node new. Root = root. right; root. right = new. Root. left; new. Root. left = root; return new. Root; } C A B 1 B 2 C A Node do. Left(Node root) { Node new. Root = root. left; root. left = new. Root. right; new. Root. right = root; return new. Root; } B 1 A CPS 100, Spring 2009 B 2 C 8. 27
AVL tree practice Insert into AVL tree: Ø 18 10 16 12 6 3 8 13 14 Ø After adding 16: do. Left. Rightdo. Left l 18 18 10 do. Right 16 10 10 18 16 16 10 10 18 6 18 12 10 After 16 3, do. Left on 16 10 10 6 16 16 10 Ø 18 18 12 6 3 6 16 12 18 3 16 8 12 18 3 CPS 100, Spring 2009 8. 28
AVL practice: continued, and finished l l After adding 13, ok After adding 14, not ok Ø do. Right at 12 6 3 10 16 8 12 18 16 8 12 10 18 6 13 14 3 16 8 13 12 CPS 100, Spring 2009 18 14 8. 29
Dame Wendy Hall Multimedia, “web before the web”, President of ACM, cofounder of WSRI (web science research institute) l ". . We are living in very unpredictable times. . Every country, whether developed or the developing, need people with IT skills. . What we need are people who can innovate. I think there is a unique opportunity to say these are the types of skills we need young people to have going forward. . " CPS 100, Spring 2009 8. 30
- Slides: 30