Chapter 9. Priority Queue

Similar documents
Trees 1: introduction to Binary Trees & Heaps. trees 1

Programming II (CS300)

Graphs & Digraphs Tuesday, November 06, 2007

Priority Queues and Heaps. Heaps of fun, for everyone!

9. Heap : Priority Queue

CMSC 341 Lecture 14: Priority Queues, Heaps

Data Structures and Algorithms

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

8. Binary Search Tree

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Data Structure. IBPS SO (IT- Officer) Exam 2017

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

ME/CS 132: Advanced Robotics: Navigation and Vision

Programming II (CS300)

CS61BL. Lecture 5: Graphs Sorting

Binary Trees, Binary Search Trees

Sorting and Searching

Priority queues. Priority queues. Priority queue operations

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Algorithm Design (8) Graph Algorithms 1/2

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

CSE 100: GRAPH ALGORITHMS

Name CPTR246 Spring '17 (100 total points) Exam 3

Sorting and Searching

CS350: Data Structures Heaps and Priority Queues

Trees. Eric McCreath

Priority Queues, Binary Heaps, and Heapsort

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

12 Abstract Data Types

CS 206 Introduction to Computer Science II

CE 221 Data Structures and Algorithms

CSE 214 Computer Science II Heaps and Priority Queues

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Algorithm Design and Analysis

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Heaps Goodrich, Tamassia. Heaps 1

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS24 Week 8 Lecture 1

Do you remember what is the most powerful tool for managing complexity?

CSE 100: GRAPH ALGORITHMS

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Brute Force: Selection Sort

Graphs Data Structures

SELF-BALANCING SEARCH TREES. Chapter 11

UNIT III TREES 3.1 Basic Terminologies Root Child Parent Siblings Descendant Ancestor Leaf Inte rnal node External node Degree Edge Path Level

Data Structures Question Bank Multiple Choice

18: GRAPH DATA STRUCTURES. Introduction

CS350: Data Structures B-Trees

Priority Queues and Heaps

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Foundations of Discrete Mathematics

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Priority Queues. 04/10/03 Lecture 22 1

Algorithms and Data Structures

QUIZ QuickSort (Ch.7)

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Data Structures and Algorithms for Engineers

Chapter 6 Heapsort 1

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Heaps. 2/13/2006 Heaps 1

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Programming Abstractions

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Binary Heaps. CSE 373 Data Structures Lecture 11

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Chapter 10 Sorting and Searching Algorithms

FINAL EXAM REVIEW CS 200 RECITATIOIN 14

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

CS 350 : Data Structures B-Trees

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Binary Trees. Height 1

March 20/2003 Jayakanth Srinivasan,

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Topic: Heaps and priority queues

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Design and Analysis of Algorithms

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Chapter 28 Graphs and Applications. Objectives

Data Structures Brett Bernstein

Trees. (Trees) Data Structures and Programming Spring / 28

Heap Model. specialized queue required heap (priority queue) provides at least

CS301 - Data Structures Glossary By

Transcription:

Chapter 9 Priority Queues, Heaps, Graphs Spring 2015 1 Priority Queue Priority Queue An ADT in which only the item with the highest priority can be accessed 2Spring 2015

Priority Depends on the Application Telephone Answering System The length of waiting (FIFO queue) Airline Check-In System Travel Class Hospital Emergency Room Severeness of Injury Spring 2015 3 Implementation Level How items are enqueued? How items are dequeued? How to sort the items? Based on their priorities ComparedTo( ) function Spring 2015 4

Implementation Level There are many ways to implement a priority queue An unsorted List- dequeuing would require searching through the entire list An Array-Based Sorted List- Enqueuing is expensive A Linked Sorted List- Enqueuing again is 0(N) A Binary Search Tree- On average, 0(log 2 N) steps for both enqueue and dequeue Any other choice? Spring 2015 5 Heaps Heap A complete binary tree, each of whose elements contains a value that is greater than or equal to the value of each of its children Complete tree Spring 2015 Remember? 6

Heaps Spring 2015 Heap with letters 'A'.. 'J' 7 Heaps Another heap with letters 'A'.. 'J' Spring 2015 8

Heaps treeptr treeptr C 50 A T 20 30 18 10 Are these heaps? Shape & Order Properties 9Spring 2015 Heaps treeptr 70 60 40 30 12 8 10 Is this a heap? Shape & Order Properties 10 Spring 2015

Where is the largest element? treeptr Heaps 70 60 12 40 30 8 11 Spring 2015 Can we use this fact to implement an efficient Priority Queue? Heaps Heap is good for Priority Queue. We have immediate access to highest priority item BUT if we remove it, the structure isn't a heap Can we "fix" the problem efficiently? 12 Spring 2015

Heaps Order Shape Shape property is restored. Can order property be restored? How? Spring 2015 13 ReheapDown( ) Function: Restores the order property of heaps to the tree between root and bottom. Precondition: The order property of heaps may be violated only by the root node of the tree. Postcondition: The order property applies to all elements of the heap. Spring 2015 14

Heaps 15 Spring 2015 ReheapDown() If the root is not a leaf node If the value of the root is smaller than its largest child Swap the value of the root with the value of this child. ReheapDown the subtree with the new root value. Why just compare the root with its children? How about the rest of the tree? Base Cases The root of the current subtree is a leaf node. The value of the root is greater or equal to the values in both its children. Spring 2015 16

Heaps Add K : Where must the new node be put? Shape Property is kept, but Order Property? 17 Spring 2015 ReheapUp Function: Restores the order property to the heap between root and bottom. Precondition: The order property is satisfied from the root of the heap through the next-to-last leaf node; the last (bottom) leaf node may violate the order property. Postcondition: The order property applies to all elements of the heap from root through bottom. Spring 2015 18

Spring 2015 19 ReheapUp() If the root is not reached If the value of the bottom node is larger than the value of its parent Swap the value of the bottom node with the value of its parent ReheapUp the subtree with the new bottom node. Why just compare the root with its parent? How about the rest of the tree? Base Cases Reach the root of the tree The value of the bottom node is smaller or equal to the values of its parent. Spring 2015 20

Heaps root 60 1 70 0 12 2 Number nodes left to right by level 40 30 8 Spring 2015 3 4 5 21 Implementation of Heaps elements root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] bottom [ 5 ] [ 6 ] Spring 2015 70 60 12 40 30 8 tree 70 0 60 1 12 2 40 3 30 4 8 5 Use number as index Why use array, not linked list? 22

Review For any node tree.nodes[index] its left child is in tree.nodes[index*2 + 1] right child is in tree.nodes[index*2 + 2] its parent is in tree.nodes[(index - 1)/2] Spring 2015 23 Heaps struct HeapType { void ReheapDown(int root, int bottom); void ReheapUp(int root, int bottom); ItemType* elements; // Dynamic array int numelements; }; Spring 2015 24

Recursive ReheapDown void HeapType::ReheapDown(int root, int bottom) { int maxchild, rightchild, leftchild ; } leftchild = root * 2 + 1 ; rightchild = root * 2 + 2 ; if ( leftchild < = bottom) //root is not a leaf node { //find the largest child if (leftchild == bottom) // only one child maxchild = leftchild; else { //find the largest child if (elements[leftchild].comparedto(elements[rightchild])!= GREATER) maxchild = rightchild; else maxchild = leftchild; } if (elements[root].comparedto(elements[maxchild])== LESS) //violate the order property { Swap(elements[root], elements[maxchild]); ReheapDown(maxChild, bottom); } } Spring 2015 25 Recursive ReheapUp void HeapType::ReheapUp(int root, int bottom) { int parent; if (bottom > root) //not reach the root { parent = ( bottom - 1 ) / 2; //violate the order property if (elements[parent].comparedto(elements[bottom])== LESS) { Swap(elements[parent], elements[bottom]); ReheapUp(root, parent); } } } Spring 2015 26

Heaps/Priority Queues How can heaps be used to implement Priority Queues? Enqueue() Spring 2015 Insert a node as the right-most leaf node Apply the order property by calling ReheapUp() Dequeue() Remove the root node Move the right-most node to the root position Apply the order property by calling ReheapDown() 27 Why Heap is Good Heap s Order Property: Root has the largest value / highest priority Heap s Shape Property: Min. number of levels with N nodes of a binary tree: log 2 N +1 A heap guarantees 0(log 2 N) steps, even in the worst case A complete tree is of minimum height. At most log 2 N levels exist above the leaf node. At most log 2 N levels exist below the root. Spring 2015 28

Comparison of Priority Queue Implementations Enqueue Dequeue Heap O(log 2 N) O(log 2 N) Linked List O(N) O(N) Binary Search Tree Balanced O(log 2 N) O(log 2 N) Skewed O(N) O(N) 29 Spring 2015 Graphs Graph A data structure that consists of a set of nodes and a set of edges that relate the nodes to each other Vertex A node in a graph Edge (arc) A connection between two nodes in a graph, represented by a pair of vertices. Spring 2015 30

Undirected graph Graphs A graph in which the edges have no direction Directed graph (digraph) A graph in which each edge is directed from one vertex to another (or the same) vertex Spring 2015 31 Graphs Formally a graph G is defined as follows where G = (V,E) V(G) is a finite, nonempty set of vertices E(G) is a set of edges (written as pairs of vertices) Spring 2015 32

Graphs Vertex Edge or arc Undirected Graphs have no arrows on the edges Spring 2015 33 Graphs Spring 2015 34

Graphs What other structure is this? A tree is a acyclic graph. Spring 2015 35 Graphs Adjacent vertices Two vertices in a graph that are connected by an edge Path A sequence of vertices that connects two nodes in a graph Complete graph A graph in which every vertex is directly connected to every other vertex; For a graph with N nodes, N*(N-1) edges in a complete directed graph N*(N-1)/2 edges in a complete undirected graph Weighted graph A graph in which each edge carries a value Spring 2015 36

Graphs How many edges in a directed graph with N vertices? How many edges in an undirected graph with N vertices? Spring 2015 37 Graphs Weight Spring 2015 38

Array-Based Implementation Adjacency Matrix For a graph with N nodes, an N by N table that shows the existence (and weights) of all edges in the graph Mapping Array An array that maps vertex names into array indexes Marking Associate a Boolean variable with each vertex: true if visited, false if not yet visited Spring 2015 39 Adjacency Matrix for Flight Connections Where could marking variable be kept? Spring 2015 40

Linked Implementation Adjacency List A linked list that identifies all the vertices to which a particular vertex is connected; each vertex has its own adjacency list Spring 2015 41 Adjacency List Representation of Graphs Where could a marking variable go? Spring 2015 42

Graph Algorithms Breadth-first search algorithm Visit all the nodes on one level before going to the next level Depth-first search algorithm Visit all the nodes in a branch to its deepest point before moving up Single-source shortest-path algorithm An algorithm that displays the shortest path from a designated starting node to every other node in the graph Spring 2015 43 Graph Traversal Visits all the vertices, beginning with a specified start vertex. No vertex is visited more than once, and vertices are only visited if they can be reached that is, if there is a path from the start vertex. Spring 2015 44

Breadth-First Traversal with Queue Neighbors-First. A queue data structure is needed. It holds a list of vertices which have not been visited yet but which should be visited soon. While visit a vertex involves, adding its neighbors to the queue. Neighbors are not added to the queue if they are already in the queue, or have already been visited. Queue : FIFO. Spring 2015 45 Graphs 2 1 4 7 6 5 3 BFT result Spring 2015 46

Depth-First Traversal with Stack Neighbors-First. A stack data structure is needed. It holds a list of vertices which have not been visited yet but which should be visited soon. While visit a vertex involves, adding its neighbors to the stack. Neighbors are not added to the stack if they are already in the stack, or have already been visited. Stack: LIFO. Spring 2015 47 Graphs 2 1 4 6 5 3 7 DFT result Spring 2015 48

Breadth-First vs. Depth-First Breadth-first Traversal: all one-flight solutions, then all two-flight solutions, etc. Austin Dallas Houston Denver Chicago Atlanta Washington Spring 2015 49 Breadth-First vs. Depth-First Depth-first Traversal: keep going forward in one direction; backtrack only when reach a dead end. Austin Dallas Chicago Denver Atlanta Washington Houston Spring 2015 50

Single-Source Shortest-Path Shortest-Path A path through the graph is a sequence (v1,..., vn) such that the graph contains an edge e1 going from v1 to v2, an edge e2 going from v2 to v3, and so on. The path whose weights, when added together, have the smallest sum. If the weight is the distance between two cities The shortest path between two cities is the route which has the minimum travelling distance. If the weight is the travelling time between two cities The shortest path between two cities is the route which has the shortest travelling time. Spring 2015 51 Applications of Shortest Path Traversal Google Map finds the route with the shortest travelling time. Networking Routing Protocols Game Spring 2015 52

Dijkstra's Algorithm Similar to Breath-First Traversal A FIFO queue for unvisited neighbors of a visited vertex. A priority queue for the possible path Each item of the priority queue has three data members fromvertex: last visited vertex on the path from the starting vertex tovertex: next visiting vertex Distance : the min. distance from the starting vertex to next visiting vertex. Spring 2015 53 Dijkstra's Algorithm The priority-queue implemented with a minimum heap Starts from the Starting vertex, the first edge is itself (distance is 0). For each visited vertex, put its unvisited neighbors into FIFO queue. Dequeue the FIFO queue and put the possible path from the starting vertex to the neighbor into the priority queue. Dequeue the priority queue one by one. Spring 2015 54

Graphs Start Here Washington -> Atlanta 600 Washington ->Dallas 1300 Washington -> Atlanta -> Houston 1400 Washington -> Dallas -> Austin 1500 Washington -> Dallas -> Denver 2080 Washington -> Dallas -> Chicago 2200 Spring 2015 55 Reference Reproduced from C++ Plus Data Structures, 4 th edition by Nell Dale. Reproduced by permission of Jones and Bartlett Publishers International. Spring 2015 56