1 EE 355 Unit 14 A-Star Algorithm & Heaps/Priority Queues Mark Redekopp.

Slides:



Advertisements
Similar presentations
Review: Search problem formulation
Advertisements

Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
Search in AI.
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2004 Heaps and heap sort  complete tree, heap property  min-heaps & max-heaps  heap operations:
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Review: Search problem formulation
Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Informed Search (no corresponding text chapter). Recall: Wanted " An algorithm and associated data structure(s) that can: 1) Solve an arbitrary 8-puzzle.
UnInformed Search What to do when you don’t know anything.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 261 – Data Structures Priority Queues & Heaps.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Week 10: Heap and Priority queue. Any feature here?
Priority Queues, Heaps & Leftist Trees
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Game Trees: MiniMax strategy, Tree Evaluation, Pruning, Utility evaluation Adapted from slides of Yoonsuck Choe.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning CPSC 315 – Programming Studio Spring 2008 Project 2, Lecture 2 Adapted from slides of Yoonsuck.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
1 Finding Shortest Paths with A* Search A* search operates similarly to Dijkstra’s algorithm, but extends the cost function to include an estimated distance.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Foundations of Data Structures Practical Session #8 Heaps.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Heaps & Priority Queues
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.
CS 367 Introduction to Data Structures Lecture 8.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
A* Reference: “Artificial Intelligence for Games”, Ian Millington.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
CSCI 104 Priority Queues / Heaps
Priority Queues (Heaps)
Hashing Exercises.
Heaps © 2010 Goodrich, Tamassia Heaps 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.
Chapter 8 – Binary Search Tree
Priority Queue & Heap CSCI 3110 Nan Chen.
Lecture 13 – Heaps Container Adapters priority_queue – impl. with heap
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Priority Queues & Heaps
Priority Queues & Heaps
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Heaps & Multi-way Search Trees
Presentation transcript:

1 EE 355 Unit 14 A-Star Algorithm & Heaps/Priority Queues Mark Redekopp

2 ALGORITHM HIGHLIGHT A* Search Algorithm

3 Search Methods Many systems require searching for goal states – Path Planning Roomba Vacuum Mapquest/Google Maps Games!! – Optimization Problems Find the optimal solution to a problem with many constraints

4 Search Applied to 8-Tile Game 8-Tile Puzzle – 3x3 grid with one blank space – With a series of moves, get the tiles in sequential order – Goal state: PA4 Goal State Goal State for these slides

5 Search Methods Brute-Force Search: When you don’t know where the answer is, just search all possibilities until you find it. Heuristic Search: A heuristic is a “rule of thumb”. An example is in a chess game, to decide which move to make, count the values of the pieces left for your opponent. Use that value to “score” the possible moves you can make. – Heuristics are not perfect measures, they are quick computations to give an approximation (e.g. may not take into account “delayed gratification” or “setting up an opponent”)

6 Brute Force Search Brute Force Search Tree – Generate all possible moves – Explore each move despite its proximity to the goal node WS W S E

7 Heuristics Heuristics are “scores” of how close a state is to the goal (usually, lower = better) These scores must be easy to compute (i.e. simpler than solving the problem) Heuristics can usually be developed by simplifying the constraints on a problem Heuristics for 8-tile puzzle – # of tiles out of place Simplified problem: If we could just pick a tile up and put it in its correct place – Total x-, y- distance of each tile from its correct location (Manhattan distance) Simplified problem if tiles could stack on top of each other / hop over each other # of Tiles out of Place = 3 Total x-/y- distance = 6

8 Heuristic Search Heuristic Search Tree – Use total x-/y- distance (Manhattan distance) heuristic – Explore the lowest scored states H=6 H=7 H=5 H=6 H=4 H=3 H=2 H=1 Goal H=5

9 Caution About Heuristics Heuristics are just estimates and thus could be wrong Sometimes pursuing lowest heuristic score leads to a less-than optimal solution or even no solution Solution – Take # of moves from start (depth) into account H=2 Start H=1 … Goal H=1

10 A-star Algorithm Use a new metric to decide which state to explore/expand Define – h = heuristic score (same as always) – g = number of moves from start it took to get to current state – f = g + h As we explore states and their successors, assign each state its f-score and always explore the state with lowest f-score Heuristics should always underestimate the distance to the goal – If they do, A* guarantees optimal solutions g=1,h=2 f=3 Start g=1,h=1 f=2 g=2,h=1 f=3 g=3,h=1 f=4 Goal g=2,h=1 f=3

11 A-Star Algorithm Maintain 2 lists – Open list = Nodes to be explored (chosen from) – Closed list = Nodes already explored (already chosen) Pseudocode open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. Generate successors/neighbors of s, compute their f values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value g=1,h=2 f=3 Start g=1,h=1 f=2 g=2,h=1 f=3 g=3,h=1 f=4 Goal g=2,h=1 f=3

12 Data Structures & Ordering We’ve seen some abstract data structures – Queue (first-in, first-out access order) Deque works nicely There are others… – Stack (last-in, first-out access order) Vector push_back / pop_back – Trees A new one…priority queues (heaps) – Order of access depends on value – Items arrive in some arbitrary order – When removing an item, we always want the minimum or maximum valued item – To implement efficiently use heap data structure remove_min = original heap remove_min =

13 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance S G open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value Closed List Open List **If implementing this for a programming assignment, please see the slide at the end about alternate closed-list implementation

14 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance S G Closed List Open List g=0, h=6, f=6 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value **If implementing this for a programming assignment, please see the slide at the end about alternate closed-list implementation

15 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

16 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

17 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

18 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=1, h=5, f=6 g=2, h=6, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

19 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

20 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 g=2, h=8, f=10 g=3, h=7, f=10 g=3, h=5, f=8 g=4, h=6, f=10 g=4, h=4, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

21 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 g=2, h=8, f=10 g=3, h=7, f=10 g=3, h=5, f=8 g=4, h=6, f=10 g=4, h=4, f=8 g=5, h=5, f=10 g=5, h=3, f=8 g=1, h=7, f=8 g=2, h=6, f=8 g=1, h=7, f=8 g=2, h=6, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

22 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 g=2, h=8, f=10 g=3, h=7, f=10 g=3, h=5, f=8 g=4, h=6, f=10 g=4, h=4, f=8 g=5, h=5, f=10 g=5, h=3, f=8 g=6, h=4, f=10 g=6, h=2, f=8 g=1, h=7, f=8 g=2, h=6, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

23 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 g=2, h=8, f=10 g=3, h=7, f=10 g=3, h=5, f=8 g=4, h=6, f=10 g=4, h=4, f=8 g=5, h=5, f=10 g=5, h=3, f=8 g=6, h=4, f=10 g=6, h=2, f=8 g=7, h=3, f=10 g=7, h=1, f=8 g=1, h=7, f=8 g=2, h=6, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

24 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 g=2, h=8, f=10 g=3, h=7, f=10 g=3, h=5, f=8 g=4, h=6, f=10 g=4, h=4, f=8 g=5, h=5, f=10 g=5, h=3, f=8 g=6, h=4, f=10 g=6, h=2, f=8 g=7, h=3, f=10 g=7, h=1, f=8 g=8, h=2, f=10 g=8, h=0, f=8 g=1, h=7, f=8 g=2, h=6, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

25 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 g=2, h=8, f=10 g=3, h=7, f=10 g=3, h=5, f=8 g=4, h=6, f=10 g=4, h=4, f=8 g=5, h=5, f=10 g=5, h=3, f=8 g=6, h=4, f=10 g=6, h=2, f=8 g=7, h=3, f=10 g=7, h=1, f=8 g=8, h=2, f=10 g=8, h=0, f=8 g=1, h=7, f=8 g=2, h=6, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

26 Path-Planning w/ A* Algorithm Find optimal path from S to G using A* – Use heuristic of Manhattan (x-/y-) distance g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=6, f=8 g=2, h=4, f=6 g=2, h=6, f=8 g=3, h=7, f=10 g=3, h=5, f=8 g=2, h=8, f=10 g=3, h=7, f=10 g=3, h=5, f=8 g=4, h=6, f=10 g=4, h=4, f=8 g=5, h=5, f=10 g=5, h=3, f=8 g=6, h=4, f=10 g=6, h=2, f=8 g=7, h=3, f=10 g=7, h=1, f=8 g=8, h=2, f=10 g=8, h=0, f=8 g=1, h=7, f=8 g=2, h=6, f=8 open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. else Generate successors/neighbors of s, compute their f-values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value

27 A* and BFS BFS explores all nodes at a shorter distance from the start (i.e. g value) g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List

28 A* and BFS BFS explores all nodes at a shorter distance from the start (i.e. g value) g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=8, f=10 g=2, h=6, f=8 g=2, h=4, f=6

29 A* and BFS BFS is A* using just the g value to choose which item to select and expand g=1, h=7, f=8 S G g=1, h=5, f=6 g=1, h=7, f=8 Closed List Open List g=2, h=8, f=10 g=2, h=6, f=8 g=2, h=4, f=6

30 Implementation Note When the distance to a node/state/successor (i.e. g value) is uniform, we can greedily add a state to the closed-list at the same time as we add it to the open-list open_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 2. Add s to closed list 3a. if s = goal node then trace path back to start; STOP! 3b. Generate successors/neighbors of s, compute their f values, and add them to open_list if they are not in the closed_list (so we don’t re-explore), or if they are already in the open list, update them if they have a smaller f value open_list.push(Start State) Closed_list.push(Start State) while(open_list is not empty) 1. s ← remove min. f-value state from open_list (if tie in f-values, select one w/ larger g-value) 3a. if s = goal node then trace path back to start; STOP! 3b. Generate successors/neighbors of s, compute their f values, and add them to open_list and closed_list if they are not in the closed_list Non-uniform g-values Uniform g-values g=0,H= … g=k,H=6 The first occurrence of a board has to be on the shortest path to the solution

31 DATA STRUCTURE HIGHLIGHT Heaps

32 Heap Data Structure Can think of heap as a full binary tree with the property that every parent is less-than (if min-heap) or greater-than (if max- heap) both children – But no ordering property between children Minimum/Maximum value is always the top element Min-Heap

33 Heap Operations Push: Add a new item to the heap and modify heap as necessary Pop: Remove min/max item and modify heap as necessary Top: Returns min/max To create a heap from an unordered array/vector takes O(n*log 2 n) time while push/pop takes O(log 2 n)

34 Push Heap Add item to first free location at bottom of tree Recursively promote it up until a parent is less than the current item Push_Heap(8)

35 Pop Heap Takes last (greatest) node puts it in the top location and then recursively swaps it for the smallest child until it is in its right place Original

36 Practice Push(11) Pop() Pop() Push(23)

37 Array/Vector Storage for Heap Binary tree that is full (i.e. only the lowest-level contains empty locations and items added left to right) can be modeled as an array (let’s say it starts at index 1) where: – Parent(i) = i/2 – Left_child(p) = 2*p – Right_child(p) = 2*p em parent(5) = 5/2 = 2 Left_child(5) = 2*5 = 10 Right_child(5) = 2*5+1 = 11

38 STL Priority Queue Implements a max-heap by default Operations: – push(new_item) – pop(): removes but does not return top item – top() return top item (item at back/end of the container) – size() – empty() nce/stl/priority_queue/push/ // priority_queue::push/pop #include using namespace std; int main () { priority_queue mypq; mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout<< " " << mypq.top(); mypq.pop(); } cout<< endl; return 0; } Code here will print

39 STL Priority Queue Template Template that allows type of element, container class, and comparison operation for ordering to be provided First template parameter should be type of element stored Second template parameter should be vector Third template parameters should be comparison function object/class that will define the order from first to last in the container // priority_queue::push/pop #include using namespace std; intmain () { priority_queue, greater > mypq; mypq.push(30); mypq.push(100); mypq.push(25); cout<< "Popping out elements..."; while (!mypq.empty()) { cout<< " " << mypq.top(); mypq.pop(); } } Code here will print 25, 30, 100 'greater' will yield a min-heap 'less' will yield a max-heap

40 STL Priority Queue Template For user defined classes, must implement operator () for min- heap Code here will pop in order: – Jane – Charlie – Bill // priority_queue::push/pop #include using namespace std; class Item { public: int score; string name; Item(int s, string n) { score = s; name = n;} bool operator>(const Item &lhs, const Item &rhs) const { if(lhs.score > rhs.score) return true; else return false; } }; int main () { priority_queue, greater > mypq; Item i1(25,”Bill”); mypq.push(i1); Item i2(5,”Jane”); mypq.push(i2); Item i3(10,”Charlie”); mypq.push(i3); cout<< "Popping out elements..."; while (!mypq.empty()) { cout<< " " << mypq.top().name; mypq.pop(); }