1 / 44

Algorithms: Design and Analysis

Learn about heaps, heap sort, and priority queues in the context of designing and analyzing algorithms. Understand tree terminology, binary trees, array-based complete binary trees, and the operations involved in heaps. Explore the uses of heaps in sorting, selection algorithms, and graph algorithms. Gain knowledge on inserting and deleting elements from a max heap, and the complexity of heap operations.

kathleena
Download Presentation

Algorithms: Design and Analysis

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. Algorithms: Design and Analysis 240-310, Semester2, 2018-2019 • Objectives • implement heaps (an array-based complete binary tree), heap sort, priority queues 10. Heaps

  2. 1. Tree Terminology continued

  3. A path between a parent node X0 anda subtree node N is a sequence of nodes P = X0, X1, . . ., (Xk is N) • k is the length of the path • each node Xi is the parent of Xi+1 for 0  i k-1 • The level of a node is the length of the path from root to that node. • The height of a tree is the maximum level in the tree. continued

  4. 2. Binary Trees • In a binary tree, each node has at most two children. continued

  5. Each node of a binary tree defines a left and a right subtree. Each subtree is a tree. Right child of T Left child of T

  6. Height of a Binary Tree Node degenerate binary tree (a list)

  7. A Complete Binary Tree • A complete binary tree of height h has all its nodes filled through level h-1, and the nodes at depth h run left to right with no gaps. continued

  8. An Array-based Complete Binary Tree • An array arr[] can be viewed as a complete binary tree if: • the root is stored in arr[0] • the level 1 children are in arr[1], arr[2] • the level 2 children are in arr[3], arr[4], arr[5], arr[6] • etc. continued

  9. Integer[] arr = {5, 1, 3, 9, 6, 2, 4, 7, 0, 8}; continued

  10. For arr[i] in an n-element array‑based complete binary tree: Left child of arr[i] is arr[2*i + 1]; or undefined if (2*i + 1)  n Right child of arr[i] is arr[2*i + 2]; or undefined if (2*i + 2)  n Parent of arr[i] is arr[(i-1)/2]; or undefined if i = 0

  11. 3. Heaps • A maximum heap is an array‑based complete binary tree in which the value of a parent is ≥ the value of both its children. lvl 0 2 3 1 5 55 50 52 25 10 11 20 22 0 1 2 3 4 5 6 7 8 there’s no ordering within a level(between siblings) continued

  12. lvl 0 2 1 40 15 30 10 0 1 2 3

  13. A minimum heap uses the relation ≤. lvl 0 2 3 1 55 5 10 50 11 20 52 25 22 0 1 2 3 4 5 6 7 8 continued

  14. lvl 0 2 1 10 15 30 40 0 1 2 3

  15. Heap Uses • Heapsort • a fast sorting method: O(n log2n) • in-place; no quadratic worst-case • Selection algorithms • finding the min, max, median, k-th element in sublinear time • Graph algorithms • Prim's minimal spanning tree; • Dijkstra's shortest path

  16. Max Heap Operations • Inserting an element: pushHeap() • Deleting an element: popHeap() • most of the work is done by calling adjustHeap() • Array --> heap conversion: makeHeap() • Heap sorting: heapSort() • utilizes makeHeap() then popHeap()

  17. 4. Inserting into a Max Heap • Assume that the array is a maximum heap. • a new item will enter the array at index last with the heap expanding by one element pushHeap() continued

  18. Insert an item by moving the nodes on the path of parents down one level until the item is correctly placed as a parent in the heap. insert 50 path of parents continued

  19. continued

  20. At each step, compare item with parent • if item is larger, move parent down one level • arr[currPos] = parent; • currPos = the parent index; • Stop when parent is larger than item • assign item to the currPos position • arr[currPos] = item;

  21. 5. Deleting from a Heap popHeap() • Deletion is normally restricted to the root only • remove the maximum element (in a max heap) • To erase the root of an n‑element heap: • exchange the root with the last element (the one at index n‑1); delete the moved root • filter (sift) the new root down to its correct position in the heap

  22. Deletion Example for a Max Heap • Delete 63 • exchange with 18; remove 63 • filter down 18 to correct position continued

  23. (63) removed continued

  24. Move 18 down: • smaller than 30 and 40; swap with 40 18 18 38 continued

  25. Move 18 down: • smaller than 38; swap with 38 38 18 18 • Stop since 18 is now a leaf node.

  26. Filter (Sift) Down a Max Heap • Move root value down the tree: • compare value with its two children • if value < a child then heap order is wrong • select largest child and swap with value • repeat algorithm but with new child • continue until value ≥ both children or at a leaf

  27. 6. Complexity of Heap Operations • A heap stores elements in an array-based complete tree. • pushHeap() reorders elements in the tree by moving up the path of parents. • popHeap() reorders elements in the tree by moving down the path of the children. • their cost depends on path length continued

  28. Assuming the heap has n elements, the maximum length for a path between a leaf node and the root is log2n • since the tree is balanced • The runtime efficiency of the algorithms is O(log2 n)

  29. 7. Heapifying O(n) makeHeap() • Transforming an array into a heap is called "heapifying the array". • Turn an n‑element array into a heap by filtering down each parent in the tree • begin with the last parent at index (n-2)/2 • end with the root node at index 0 continued

  30. Max Heap Creation Integer[] arr = {9, 12, 17, 30, 50, 20, 60, 65, 4, 19}; The grey nodes are the parents. Adjust in order: 50, 30, 17, 12, 9 continued

  31. continued

  32. continued

  33. 8. Sorting with a Max Heap • If the array is a maximum heap, it has an efficient sorting algorithm: • For each iteration i, the largest element is arr[0]. • Exchange arr[0] with arr[i] and then reorder the array so that elements in the index range [0, i) are a heap. • This is done by popHeap(), which is O(log2n) heapSort()

  34. Max Heap Sort continued

  35. the max heap sort is into ascending order

  36. Heapsort • Heap sort is a modified version of selection sort for an array that is a heap. • for each i = n, n-1, ..., 2, call popHeap() which pops arr[0] from the heap and assign it at index i-1. • A maximum heap is sorted into ascending order • A minimum heap is sorted into descending order.

  37. The worst case running time of makeHeap() is closer to O(n), not O(n log2 n). • During the second phase of the heap sort, popHeap() executes n-1 times. Each operation has efficiency O(log2 n). • The worst-case complexity of the heap sort is O(n) + O(n log2 n) = O(n log2 n).

  38. 9. Priority Queue • In a priority queue, all the elements have priority values. • A deletion always removes the element with the highest priority.

  39. Two types of priority queues: • maximum priority queue • remove the largest value first • what I’ll be using • minimum priority queue • remove the smallest value first

  40. PQueue Example // create a max priority queue of Strings PQueue<String> pq = new PQueue<String>(); pq.push("green"); pq.push("red"); pq.push("blue"); // output the size, and element with highest priority System.out.println(pq.size() + ", " + pq.peek()); // use pop() to empty the pqueue and list elements // in decreasing priority order while ( !pq.isEmpty() ) System.out.print( pq.pop() + " "); The max priority for Strings is z --> a order 3, red red green blue

  41. Implementing PQueue • We can use a heap to implement the PQueue. • The user can specify either a max heap or min heap • this dictates whether deletion removes the maximum or the minimum element from the collection • max heapmaximum priority queue • min heapminimum priority queue

More Related