Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms: Design and Analysis

Similar presentations


Presentation on theme: "Algorithms: Design and Analysis"— Presentation transcript:

1 Algorithms: Design and Analysis
, Semester 2, 10. Heaps Objectives implement heaps (an array-based complete binary tree), heap sort, priority queues

2 1. Tree Terminology continued

3 The level of a node is the length of the path from root to that node.
A path between a parent node X0 and a 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

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

6 Each node of a binary tree defines a left and a right subtree
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

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

8 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

9

10 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

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

12 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

13 within a level (between siblings)
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 1 2 3 55 50 52 25 10 11 5 20 22 1 2 3 4 5 6 7 8 there’s no ordering within a level (between siblings) continued

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

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

16 lvl 0 1 2 10 15 30 40 1 2 3

17 Heap Uses Heapsort Selection algorithms Graph algorithms
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

18 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()

19 4. Inserting into a Max Heap
pushHeap() 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 continued

20 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

21 continued

22 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;

23 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

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

25 (63) removed continued

26 Move 18 down: smaller than 30 and 40; swap with 40 18 18 38 continued

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

28 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

29 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

30 The runtime efficiency of the algorithms is O(log2 n)
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)

31 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

32 Max Heap Creation The grey nodes are the parents. Adjust in order:
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

33 continued

34 continued

35

36 8. Sorting with a Max Heap heapSort()
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)

37 Max Heap Sort continued

38 the max heap sort is into ascending order

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

40 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).

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

42 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

43 The max priority for Strings
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

44 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


Download ppt "Algorithms: Design and Analysis"

Similar presentations


Ads by Google