Queues, Stacks and Heaps

17
Queues, Stacks and Heaps

description

Queues, Stacks and Heaps. Queue. List structure using the FIFO process Nodes are removed form the front and added to the back. Front. A. B. C. D. Back. Queue. Removing a node (popping) Then adding a node (pushing) Uses include Breadth First Search and other graph-related algorithms. - PowerPoint PPT Presentation

Transcript of Queues, Stacks and Heaps

Page 1: Queues, Stacks and Heaps

Queues, Stacks and Heaps

Page 2: Queues, Stacks and Heaps

Queue

List structure using the FIFO processNodes are removed form the front and

added to the back

A B DCFront Back

Page 3: Queues, Stacks and Heaps

Queue

Removing a node (popping)

Then adding a node (pushing)

Uses include Breadth First Search and other graph-related algorithms

B C DFront Back

B C DFront BackA

Page 4: Queues, Stacks and Heaps

Stack

List structure using the FILO processNodes added to and removed from the top

D

Bottom

C

B

A

Top

Page 5: Queues, Stacks and Heaps

Stack

Removing a node Then adding a node

popping pushing

Bottom

Top

E

Bottom

C

B

A

Top

C

B

A

Page 6: Queues, Stacks and Heaps

Stack

Used in Depth First Search and other recursive algorithms

Page 7: Queues, Stacks and Heaps

Tree Basics

A tree is a connected graph with no cyclesNodes can have multiple children and at

most one parentNodes with no children

are called leavesTopmost node called

the root

4

2 6

8

7

5

RootParent of node

3

Child of node

A leaf

Page 8: Queues, Stacks and Heaps

Heap

A heap is a binary tree - no more than 2 children per parent

The binary heap is complete – all levels are full with the possible exception of the last

The value of each node is greater than or equal to the values of each of its children

Page 9: Queues, Stacks and Heaps

Heap

Properties of a heap of size n:Height of the heap is trunc(log2n)

Root of the heap containsthe largest value

10

8 3

1 25

Page 10: Queues, Stacks and Heaps

HeapA heap can be conveniently stored in an array as such:• The root is stored at index 1• The children of node i are stored at indices 2i and 2i+1• The parent of node i is stored at index trunc(i/2)

Page 11: Queues, Stacks and Heaps

Heap

A simple heap with array representation

9

8 3

6 25 1

4

0 9 8 3 6 5 2 1 4 50 1 2 3 4 5 6 7 8 9Index

Value

5

Page 12: Queues, Stacks and Heaps

HeapHeap construction:• Read values into array• For each node from the last parent down to the root:

If the node value is less than either of the children, switch the node with the greater childContinue until the node value is greater than or

equal to both children (automatically true if it is a leaf)• Construction is in O(n)

Page 13: Queues, Stacks and Heaps

HeapInserting a value:• Increment the size and add the value as the last node• Sift the node up the heap if it is larger than its parent until

its parent is greater than it or it has become the root• Insertion is in O(log2n)

8

6 5

4 9

8

9 5

4 6

9

8 5

4 6

Page 14: Queues, Stacks and Heaps

HeapDeleting the root (when popping):• Change the value of the root to the value of the last node

in the heap and decrement the size of the heap• If the node is less than either child, swap it with the larger

child, repeat until it is greater than both children• Deletion is in O(log2n)

9

6 5

2 3

3

6 5

2

6

3 5

2

Page 15: Queues, Stacks and Heaps

HeapA heap can be used to sort a list of values (heapsort):• Heapify the list of values• Pop the root off and reheap• Repeat until the heap is empty• Deletion of a node is O(log2n) and this is repeated n times,

so heapsort is in O(nlog2n) (this is also the worst case)

• Heapsort can be done in-place, but it is not a stable sort

Page 16: Queues, Stacks and Heaps

Priority QueuePriority queues are queues which pop the minimum or

maximum value in the queue. As the root of a heap is always the largest or smallest value in the heap, priority queues can use a heap structure.

Priority queues have important uses in:• Dijkstra’s Algorithm (shortest path)• Prim’s Algorithm (a faster alternative to Kruskal’s for a

minimum spanning tree)• Simply finding the minimum/maximum value of a dynamic

list efficiently

Page 17: Queues, Stacks and Heaps

Problem ExamplesShortest path is a fairly common problem, with The Cheese

Universe from the first training camp being a straight-forward example. A heap priority queue converts Dijkstra’s to O((E+V)log2n) from O(n2).

An example of a minimum spanning tree problem for which Prim’s Algorithm might be used is the Caves of Caerbannog problem from last years SACO.