A-Star Algorithm & Heaps/Priority Queues Mark Redekopp 2 A* - - PowerPoint PPT Presentation

a star algorithm
SMART_READER_LITE
LIVE PREVIEW

A-Star Algorithm & Heaps/Priority Queues Mark Redekopp 2 A* - - PowerPoint PPT Presentation

1 EE 355 Unit 14 A-Star Algorithm & Heaps/Priority Queues Mark Redekopp 2 A* Search Algorithm ALGORITHM HIGHLIGHT 3 Search Methods Many systems require searching for goal states Path Planning Roomba Vacuum


slide-1
SLIDE 1

1

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

Mark Redekopp

slide-2
SLIDE 2

2

ALGORITHM HIGHLIGHT

A* Search Algorithm

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

  • rder

– Goal state:

1 2 3 4 5 6 7 8

HW6 Goal State

1 2 3 4 5 6 7 8

Goal State for these slides

slide-5
SLIDE 5

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

  • pponent. 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

  • pponent”)
slide-6
SLIDE 6

6

Brute Force Search

  • Brute Force Search

Tree

– Generate all possible moves – Explore each move despite its proximity to the goal node

1 2 4 8 3 7 6 5 1 2 4 8 3 7 6 5 1 2 4 8 3 7 6 5 1 8 2 4 3 7 6 5 1 2 4 8 3 7 6 5 1 2 4 8 3 7 6 5 1 2 3 4 8 5 7 6 1 2 3 4 8 7 6 5 1 2 3 4 8 7 6 5 1 2 3 4 8 5 7 6 1 2 3 4 5 7 8 6

W S W S E

slide-7
SLIDE 7

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

1 8 3 4 5 6 2 7 1 8 3 4 5 6 2 7

# of Tiles out of Place = 3 Total x-/y- distance = 6

slide-8
SLIDE 8

8

Heuristic Search

  • Heuristic Search Tree

– Use total x-/y- distance (Manhattan distance) heuristic – Explore the lowest scored states

1 2 4 8 3 7 6 5 1 2 4 8 3 7 6 5 1 2 4 8 3 7 6 5 1 2 3 4 8 5 7 6 1 2 3 4 5 6 7 8 1 2 3 4 8 7 6 5 1 2 3 4 8 7 6 5 1 2 3 4 8 5 7 6 1 2 3 4 5 7 8 6 1 2 3 4 5 7 8 6 H=6 H=7 H=5 H=6 H=6 H=4 H=3 H=2 H=1 Goal 1 2 3 4 8 7 6 5 H=5

slide-9
SLIDE 9

9

Caution About Heuristics

  • Heuristics are just estimates and

thus could be wrong

  • Sometimes pursuing lowest

heuristic score leads to a less-than

  • ptimal solution or even no

solution

  • Solution

– Take # of moves from start (depth) into account

H=2 Start H=1 H=1 H=1 H=1 H=1 H=1

Goal H=1

slide-10
SLIDE 10

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

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

slide-11
SLIDE 11

11

A-Star Algorithm

  • Maintain 2 lists

– Open list = Nodes to be explored (chosen from) – Closed list = Nodes already explored (already chosen)

  • Pseudocode
  • pen_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

slide-12
SLIDE 12

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 = 21 46 78 35 67

  • riginal heap

46 21 78 35 67 1 2 3 4 remove_min = 35 46 78 67

slide-13
SLIDE 13

13

Path-Planning w/ A* Algorithm

  • Find optimal path from S to G using A*

– Use heuristic of Manhattan (x-/y-) distance

S G

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-14
SLIDE 14

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

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-15
SLIDE 15

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=5, f=6 g=1, h=7, f=8

Closed List Open List

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-16
SLIDE 16

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=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

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-17
SLIDE 17

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=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

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-18
SLIDE 18

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=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

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-19
SLIDE 19

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=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

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-20
SLIDE 20

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=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=2, h=8, f=10 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

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-21
SLIDE 21

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=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=2, h=8, f=10 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=5, f=10 g=5, h=3, f=8 g=1, h=7, f=8 g=1, h=7, f=8 g=2, h=6, f=8 g=1, h=7, f=8 g=1, h=7, f=8 g=2, h=6, f=8

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-22
SLIDE 22

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=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=2, h=8, f=10 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=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=1, h=7, f=8 g=2, h=6, f=8

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-23
SLIDE 23

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=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=2, h=8, f=10 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=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=1, h=7, f=8 g=2, h=6, f=8

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-24
SLIDE 24

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=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=2, h=8, f=10 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=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=1, h=7, f=8 g=2, h=6, f=8

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-25
SLIDE 25

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=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=2, h=8, f=10 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=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=1, h=7, f=8 g=2, h=6, f=8

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-26
SLIDE 26

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=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=2, h=8, f=10 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=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=1, h=7, f=8 g=2, h=6, f=8

  • pen_list.push(Start State)

while(open_list is not empty)

  • 1. s ← remove min. f-value state from
  • pen_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

  • pen_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

slide-27
SLIDE 27

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=5, f=6 g=1, h=7, f=8

Closed List Open List

slide-28
SLIDE 28

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=5, f=6 g=1, h=7, f=8

Closed List Open List

g=2, h=8, f=10 g=2, h=8, f=10 g=2, h=8, f=10 g=2, h=6, f=8 g=2, h=6, f=8 g=2, h=4, f=6

slide-29
SLIDE 29

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=5, f=6 g=1, h=7, f=8

Closed List Open List

g=2, h=8, f=10 g=2, h=8, f=10 g=2, h=8, f=10 g=2, h=6, f=8 g=2, h=6, f=8 g=2, h=4, f=6

slide-30
SLIDE 30

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

  • pen_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

  • pen_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

1 2 4 8 3 7 6 5 g=0,H=6 1 2 4 8 3 7 6 5

g=k,H=6

The first occurrence of a board has to be on the shortest path to the solution

slide-31
SLIDE 31

31

DATA STRUCTURE HIGHLIGHT

Heaps

slide-32
SLIDE 32

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

7 9 18 19 35 14 10 28 39 36 43 16 25

slide-33
SLIDE 33

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*log2n) time while push/pop takes O(log2n)

slide-34
SLIDE 34

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

7 9 18 19 35 14 10 28 39 36 43 16 25 7 8 18 19 35 14 9 28 39 36 43 16 25

8

8

10

Push_Heap(8)

1 2 3 4 5 6 7 8 9 10 11 12 13 14

slide-35
SLIDE 35

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

7 9 18 19 35 14 10 28 39 36 43 16 25 9 10 18 19 35 14 7 28 39 36 43 16

Original

9

25 9 18 19 35 14 10 28 39 36 43 16 25

1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12

slide-36
SLIDE 36

36

Practice

7 21 35 26 24 50 29 43 36 18 19 39 28

1 2 3 4 5 6 7 8 9 10 11 12 13

7 9 35 14 10 36 18 19 39 28

1 2 3 4 5 6 7 8 9 10

Push(11) Pop()

4 8 35 26 24 36 17 19 39 28

1 2 3 4 5 6 9 10

Pop()

7

7 21 35 26 24 50 29 43 36 18 19 39 28

1 2 3 4 5 6 7 9 10 11 12 13

Push(23)

slide-37
SLIDE 37

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 + 1

7 9 18 19 35 14 10 28 39 36 43 16 17

em 7 18 9 19 1 2 3 4 35 14 10 28 39 5 6 7 8 9 36 43 16 17 10 11 12 13 parent(5) = 5/2 = 2 Left_child(5) = 2*5 = 10 Right_child(5) = 2*5+1 = 11

slide-38
SLIDE 38

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

  • http://www.cplusplus.com/refere

nce/stl/priority_queue/push/

// priority_queue::push/pop #include <iostream> #include <queue> using namespace std; int main () { priority_queue<int> 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 100 40 30 25

slide-39
SLIDE 39

39

STL Priority Queue Template

  • Template that allows type of element, container class, and comparison
  • peration for ordering to be provided
  • First template parameter should be type of element stored
  • Second template parameter should be vector<type_of_elem>
  • 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 <iostream> #include <queue> using namespace std; intmain () { priority_queue<int, vector<int>, greater<int>> 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

slide-40
SLIDE 40

40

STL Priority Queue Template

  • For user defined classes,

must implement

  • perator<() for max-heap
  • r operator>() for min-

heap

  • Code here will pop in
  • rder:

– Jane – Charlie – Bill

// priority_queue::push/pop #include <iostream> #include <queue> #include <string> 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<Item, vector<Item>, greater<Item> > 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(); } }