Analysis of AlgorithmsCS 477-677.ppt
《Analysis of AlgorithmsCS 477-677.ppt》由会员分享,可在线阅读,更多相关《Analysis of AlgorithmsCS 477-677.ppt(40页珍藏版)》请在麦多课文档分享上搜索。
1、Analysis of Algorithms CS 477/677,Instructor: Monica Nicolescu,CS 477/677,2,The Heap Data Structure,Def: A heap is a nearly complete binary tree with the following two properties: Structural property: all levels are full, except possibly the last one, which is filled from left to right Order (heap)
2、property: for any node xParent(x) x,Heap,5,7,8,4,It doesnt matter that 4 in level 1 is smaller than 5 in level 2,2,CS 477/677,3,Array Representation of Heaps,A heap can be stored as an array A. Root of tree is A1 Left child of Ai = A2i Right child of Ai = A2i + 1 Parent of Ai = A i/2 HeapsizeA lengt
3、hA The elements in the subarray A(n/2+1) n are leaves The root is the maximum element of the heap,A heap is a binary tree that is filled in order,CS 477/677,4,Maintaining the Heap Property,Suppose a node is smaller than a child Left and Right subtrees of i are max-heaps Invariant: the heap condition
4、 is violated only at that node To eliminate the violation: Exchange with larger child Move down the tree Continue until node is not smaller than children,CS 477/677,5,Building a Heap,Convert an array A1 n into a max-heap (n = lengthA) The elements in the subarray A(n/2+1) n are leaves Apply MAX-HEAP
5、IFY on elements between 1 and n/2,A:,CS 477/677,6,Heapsort,Goal: Sort an array using heap representations Idea: Build a max-heap from the array Swap the root (the maximum element) with the last element in the array “Discard” this last node by decreasing the heap size Call MAX-HEAPIFY on the new root
6、 Repeat this process until only one node remains,CS 477/677,7,Alg: HEAPSORT(A),BUILD-MAX-HEAP(A)for i lengthA downto 2do exchange A1 AiMAX-HEAPIFY(A, 1, i - 1)Running time: O(nlgn),CS 477/677,8,Example: A=7, 4, 3, 1, 2,CS 477/677,9,HEAP-MAXIMUM,Goal: Return the largest element of the heapAlg: HEAP-M
7、AXIMUM(A)return A1,Running time: O(1),Heap A:,Heap-Maximum(A) returns 7,CS 477/677,10,HEAP-EXTRACT-MAX,Goal: Extract the largest element of the heap (i.e., return the max value and also remove that element from the heap Idea: Exchange the root element with the last Decrease the size of the heap by 1
8、 element Call MAX-HEAPIFY on the new root, on a heap of size n-1,Heap A:,CS 477/677,11,HEAP-EXTRACT-MAX,Alg: HEAP-EXTRACT-MAX(A, n)if n 1then error “heap underflow”max A1A1 AnMAX-HEAPIFY(A, 1, n-1) remakes heapreturn max,Running time: O(lgn),CS 477/677,12,Example: HEAP-EXTRACT-MAX,max = 16,Heap size
9、 decreased with 1,Call MAX-HEAPIFY(A, 1, n-1),CS 477/677,13,HEAP-INCREASE-KEY,Goal: Increases the key of an element i in the heap Idea: Increment the key of Ai to its new value If the max-heap property does not hold anymore: traverse a path toward the root to find the proper place for the newly incr
10、eased key,Key i 15,CS 477/677,14,HEAP-INCREASE-KEY,Alg: HEAP-INCREASE-KEY(A, i, key)if key 1 and APARENT(i) Aido exchange Ai APARENT(i)i PARENT(i)Running time: O(lgn),Key i 15,CS 477/677,15,Example: HEAP-INCREASE-KEY,CS 477/677,16,MAX-HEAP-INSERT,Goal: Inserts a new element into a max-heap Idea: Exp
11、and the max-heap with a new element whose key is - Calls HEAP-INCREASE-KEY to set the key of the new node to its correct value and maintain the max-heap property,8,2,4,14,7,1,16,10,9,3,CS 477/677,17,MAX-HEAP-INSERT,Alg: MAX-HEAP-INSERT(A, key, n)heap-sizeA n + 1An + 1 - HEAP-INCREASE-KEY(A, n + 1, k
12、ey),Running time: O(lgn),8,2,4,14,7,1,16,10,9,3,CS 477/677,18,Example: MAX-HEAP-INSERT,CS 477/677,19,Summary,We can perform the following operations on heaps: MAX-HEAPIFY O(lgn) BUILD-MAX-HEAP O(n) HEAP-SORT O(nlgn) MAX-HEAP-INSERT O(lgn) HEAP-EXTRACT-MAX O(lgn) HEAP-INCREASE-KEY O(lgn) HEAP-MAXIMUM
13、 O(1),CS 477/677,20,The Search Problem,Find items with keys matching a given search key Applications: Keeping track of customer account information at a bank Search through records to check balances and perform transactions Keep track of reservations on flights Search to find empty seats, cancel/mod
14、ify reservations Search engine Looks for all documents containing a given word,CS 477/677,21,Symbol Tables (Dictionaries),Dictionary = data structure that supports two basic operations: insert a new item and return an item with a given key Queries: return information about the set Search (S, k) Mini
15、mum (S), Maximum (S) Successor (S, x), Predecessor (S, x) Modifying operations: change the set Insert (S, k) Delete (S, k),CS 477/677,22,Implementations of Symbol Tables,Key-indexed-array Key values are distinct, small numbers Store the items in an array, indexed by keys Ordered/unordered arrays Ord
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ANALYSISOFALGORITHMSCS477677PPT
