----------------------------------------------------------------------------- COMP SCI 731 - Data Struc & Algorithms - Lecture 4 - Wednesday, June 28, 2000 ----------------------------------------------------------------------------- Today: [ ] Asymptotic efficiency measures, cont. [ ] Heaps and the heapsort algorithm O, Theta, and Asymptotic Analysis --------------------------------- What is the running time of the above two programs? O(n+m). For a graph, this is linear time, as it is lineear in the size of the representation of the graph. sparse graph : m is small compared to n -- as small as 0 dense grraph : m is large compared to n -- as large as C(n,2) = n(n-1)/2 O(f(n)) is a set of functions (from N to N, say). A function g is in this set if there is a constant C such that g(n) <= C f(n) for almost all all n. Theta(f(n)) is a set of function (from N to N, say). A function g is in this set if there are constants c and C such that c f(n) <= g(n) <= Cf(n) for almost all n. Give examples and draw some pictures, eg.: o 7n^2 is O(n^2). Is Theta(n^2) o 7n^2 + 100 n is O(n^2). Is Theta(n^2) o 7n^2/log n is O(n^2). It is not Theta(n^2) T/F: o if f is Theta(g) then g is Theta(f) TRUE o O(n lg n) = O(n log n) TRUE o if f is O(g) and f' is O(g) then f+f' is O(g) TRUE o For every pair of functions f and g, f is O(g) or g is O(f) FALSE * Importance of the model of computation discussion. Heaps and heapsort ------------------ Conisder storing a key in every node of the tree. The key is drawn from a totally ordered universe. A binary heap is then A complete binary tree [a binary tree in which all the nodes have depth k-1 or k, the depth k-1 nodes are completely filled in, while the depth there are k nodes are filled in left-to right] having the heap property: key(parent(x)) >= key(x) This is a max-heap (a maximum element is at the root). A min-heap replaces the >=, above, by a <=. A typical heap: 23 15 20 13 11 6 19 5 12 Storing a heap: 1 2 3 4 5 6 7 8 9 ---------------------------- |23|15|20|13|11| 6|19| 5|12| ---------------------------- children of i: at 2i and 2i+1 parent of i: at \lfloor i/2 \rfloor Basic operations: insert: sift up O(lg n) delete_max: sift down O(lg n) make_heap: O(1) A method to sort: make an initially empty heap and then keep inserting. Then repeatedly delete the max, moving it to the end. An "in-place sort" (requires O(1) memeory beyond the memory used to store the data). A slicker method to build the initial heap. Illustrate with an example. The leaves already for heaps of size 1. Sift down the roots above them. And then above them. Etc. Analysis next time.