Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.

Similar presentations


Presentation on theme: "Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack."— Presentation transcript:

1 Queues, Stacks and Heaps

2 Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack

3 Queue Removing a node (popping) Then adding a node (pushing) Uses include Breadth First Search and other graph-related algorithms BCD FrontBack BCD FrontBack A

4 Stack List structure using the FILO process Nodes added to and removed from the top D Bottom C B A Top

5 Stack Removing a node Then adding a node popping pushing Bottom Top E Bottom C B A Top C B A

6 Stack Used in Depth First Search and other recursive algorithms

7 Tree Basics A tree is a connected graph with no cycles Nodes can have multiple children and at most one parent Nodes with no children are called leaves Topmost node called the root 4 26 8 7 5 Root Parent of node 3 Child of node A leaf

8 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

9 Heap Properties of a heap of size n: Height of the heap is trunc(log 2 n) Root of the heap contains the largest value 10 83 125

10 Heap A 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)

11 Heap A simple heap with array representation 9 83 6251 4 0983652145 0123456789 Index Value 5

12 Heap Heap 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 child Continue until the node value is greater than or equal to both children (automatically true if it is a leaf) Construction is in O(n)

13 Heap Inserting 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(log 2 n) 8 65 49 8 95 46 9 85 46

14 Heap Deleting 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(log 2 n) 9 65 23 3 65 2 6 35 2

15 Heap A 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(log 2 n) and this is repeated n times, so heapsort is in O(nlog 2 n) (this is also the worst case) Heapsort can be done in-place, but it is not a stable sort

16 Priority Queue Priority 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

17 Problem Examples Shortest 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)log 2 n) from O(n 2 ). 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.


Download ppt "Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack."

Similar presentations


Ads by Google