1 / 65

Priority Queues, Heaps, Lists Plus, Graphs

Priority Queues, Heaps, Lists Plus, Graphs. CISC2200, Fall 09. Priority Queue. Priority Queue An ADT in which only the item with the highest priority can be accessed. Application of Priority Queue. Telephone Answering System Priority: the length of waiting (FIFO queue)

salali
Télécharger la présentation

Priority Queues, Heaps, Lists Plus, Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Priority Queues, Heaps, Lists Plus, Graphs CISC2200, Fall 09

  2. Priority Queue Priority Queue An ADT in which only the item with the highest priority can be accessed

  3. Application of Priority Queue • Telephone Answering System • Priority: the length of waiting (FIFO queue) • Airline Check-In System • Priority: Travel Class • Hospital Emergency Room • Priority: Severeness of Injury

  4. Implement priority queue • There are many ways to implement a priority queue • an unsorted List • Enqueue: O(1) • Dequeue: searching through the entire list • An Array-Based Sorted List • Enqueue: O(N) • Dequeue: O(1) • A Linked Sorted List • Enqueuing again is O(N) • Dequeue: O(1) • A Binary Search Tree • On average, O(log2N) steps for both enqueue and dequeue • Any other choice?

  5. Heap A complete binary tree, where each node contains a value that is greater than or equal to the value of each of its children

  6. Heaps Another heap with letters 'A' .. 'J'

  7. treePtr 50 C 30 20 A T 10 18 Heaps treePtr Are these heaps? Shape & Order Properties

  8. 70 Heaps treePtr 12 60 40 30 8 10 Is this a heap? Shape & Order Properties

  9. 70 Heaps Where is the largest element? treePtr 12 60 40 30 8 Can we use this fact to implement an efficient Priority Queue?

  10. Heaps • Heap is good choice for implementing 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?

  11. Heaps Shape property is restored. Can order property be restored? How?

  12. ReheapDown( ) • Function: Restores the order property of heaps to the tree between root and bottom. • Precondition: The order property of heaps is violated only by the root node of the tree. • Postcondition: The order property applies to all elements of the heap.

  13. ReheapDown: illustration

  14. 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? • Precondition: • 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.

  15. Insert item into heap Add ‘K’: Where must the new node be put? Shape Property is kept, but Order Property?

  16. 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 violate the order property. • Postcondition: The order property applies to all elements of the heap from root through bottom.

  17. 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 “bottom” node 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.

  18. 70 0 Heaps: how to store a heap? root Number nodes left to right by level 12 2 60 1 40 3 30 4 8 5

  19. tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 70 0 12 2 60 1 40 3 30 4 8 5 Implementation of Heaps elements root bottom Use number as index

  20. Review • For any node tree.nodes[index] • its left child is in tree.nodes[index*2 + 1] • its right child is in tree.nodes[index*2 + 2] • its parent is intree.nodes[(index - 1)/2]

  21. Heap: a data structure struct HeapType { void ReheapDown(int root, int bottom); void ReheapUp(int root, int bottom); ItemType* elements; // Dynamic array int numElements; };

  22. 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 has a left child, i.e., it is not a leaf node { //find the largest child if (leftChild == bottom) // only one child maxChild = leftChild; else { // find the largerchild 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); } } }

  23. Recursive ReheapUp void HeapType::ReheapUp(int root, int bottom) { int parent; if (bottom > root) //not reach the root { parent = ( bottom - 1 ) / 2; if (elements[parent].ComparedTo(elements[bottom])== LESS) { //violate the order property Swap(elements[parent], elements[bottom]); ReheapUp(root, parent); } } }

  24. Heaps/Priority Queues • How can heaps be used to implement Priority Queues? • Enqueue() • 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 leaf node to the root position • Apply the order property by calling ReheapDown()

  25. 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: log2N +1 • A heap guarantees O(log2N) steps, even in the worst case • A complete tree is of minimum height. • At most log2N levels exist above the leaf node. • At most log2N levels exist below the root.

  26. Enqueue Dequeue Heap O(log2N) O(log2N) Linked List O(N) O(N) Binary Search Tree Balanced O(log2N) O(log2N) Skewed O(N) O(N) Comparison of Priority Queue Implementations

  27. Linear Linked List • Given a pointer to a node anywhere in a linear linked list, we can access all the nodes that follow but none of the nodes that precede it. • Extension to linear linked list: • Circular Linked List • Doubly Linked List

  28. Circular Linked Lists • A list in which each node has a successor; the “last” element is succeeded by the “first” element • Good for applications that require access to both ends of the list

  29. Circular Linked List Q: Why is it better to have the external pointer points to the last element? A: To have direct access to both the first and the last elements in the list.

  30. Sorted Circular Linked List Initialization for search

  31. FindItem in sorted circular linked list FindItem(item) Set location to Next(listData) Set predLoc to listData Set found to false Set moreToSearch to true While moreToSearch AND NOT found Do if item.ComparedTo(info(location)) == LESS Set moreToSearch to false else if item.ComparedTo(info(location)) == EQUAL Set found to true else Set predLoc to location Set location to Next(location) Set moreToSearch to (location!= Next(listData))

  32. Circular Linked List FindItem cases

  33. Circular Linked List Insert an item Don’t forget to reassign listData to point to the new Node.

  34. Circular Linked List Delete an Item

  35. Doubly Linked List A linked list in which each node is linked to both its successor and its predecessor • You could traverse the list in reverse order! • No need to have two temporary pointers working with us!

  36. Doubly Linked List: insert an item Does it matter in which order we change the pointers?

  37. Doubly Linked List: delete an item location->prev->next = location->next; location->next->prev = location->prev; delete location;

  38. Circular Doubly Linked List What are the advantages of this structure?

  39. 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. Adjacent vertices Two vertices in a graph that are connected by an edge

  40. Graphs Undirected graph 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

  41. Graphs Formally a graph G is defined as follows G = (V,E) where V(G) is a finite, nonempty set of vertices E(G) is a set of edges (written as pairs of vertices)

  42. Graphs Vertex Undirected Graphs have no arrows on the edges Edge or arc

  43. Graphs Directed edge

  44. Graphs What other structure is this ? A tree is a acyclic graph.

  45. Complete Graphs A graph in which every vertex is directly connected to every other vertex How many edges in a directed graph with N vertices? How many edges in an undirected graph with N vertices?

  46. Weighted Graphs A graph in which each edge carries a value Weight

  47. 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

  48. Adjacency Matrix for Flight Connections Where could marking variable be kept?

  49. 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

More Related