Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/10/03Tucker, Sec.3-51 Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M “Big O” Notation We say that a function is if for some constant c, when n is large.

Similar presentations


Presentation on theme: "3/10/03Tucker, Sec.3-51 Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M “Big O” Notation We say that a function is if for some constant c, when n is large."— Presentation transcript:

1 3/10/03Tucker, Sec.3-51 Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M “Big O” Notation We say that a function is if for some constant c, when n is large. For example, is since for n > 3. This is described by saying that is ‘on the order of’. Big O notation calls attention to the part of a function that grows the fastest, so gives a simple estimate of how many steps are required for an algorithm to run.

2 3/10/03Tucker, Sec.3-52 Binary Testing Tree Compare things (objects, subsets, lists, whatever) in some way with two outcomes. Keep comparing… Eventually arrive at one of the n! possible orders Since there are n! leaves on this tree, by Theorem 3 in section 3.1, the tree has height. Thus, whatever binary comparison technique is used, the worst case will require at least comparisons.

3 3/10/03Tucker, Sec.3-53 Bubble Sort For m from 2 to n, do For j from n (step –1) to m do If A j < A j-1, then interchange A j and A j-1. 346152346152 m = 2, j = 6 346125346125 m = 2, j = 5 346125346125 m = 2, j = 4 341625341625 m = 2, j = 3 314625314625 m = 2, j = 2 134625134625 m = 3, j = 6 134625134625 m = 3, j = 5 134265134265 m = 3, j = 4 Moves the smallest number to the top Moves the next smallest number to the top

4 3/10/03Tucker, Sec.3-54 Complexity of the Bubble Sort When m = 2, j goes from n to 2, so you have to do n –1 comparisons. When m = 3, j goes from n to 3, so you have to do n –2 comparisons, and so on. Thus, the total number of comparisons is: Thus, the Bubble sort takes comparisons, which is a lot more than the theoretical bound of.

5 3/10/03Tucker, Sec.3-55 Merge Sort—Subdivision Tree First divide the set repeatedly, roughly in half, until only single elements are left at the leaves…. 5 0 9 2 6 47 1 3 8 5 0 9 2 6 4 7 1 3 8 2 6 5 0 9 4 7 1 3 8 5 0 9 6 2 1 4 7 8 3 0 5 4 7

6 3/10/03Tucker, Sec.3-56 Merge Sort—Merging Tree 1 2 3 4 5 6 7 8 9 0 2 5 6 9 1 3 4 7 8 2 6 0 5 9 1 4 7 3 8 0 5 9 6 2 1 4 7 8 3 0 5 4 7 …then merge the sequences in order. Note: This tree is drawn upside down, so that the root is at the bottom. Thus, the set {0, 5} is at level 3

7 3/10/03Tucker, Sec.3-57 Complexity of Merging A Simplifying Assumption Assume our set has n = 2 r elements. This means that: We always divide exactly in half in the subdivision tree. There are r levels All the leaves in both the subdivision and the merging tree are on level r. There are 2 k vertices on level k The sets on level k all have 2 r-k elements in them n = 2 3 * * * * * * * * * * *

8 3/10/03Tucker, Sec.3-58 Complexity of Merging Counting the Comparisons At each vertex on level k of the merging tree we merge the two children sets each of size 2 r-k-1. Merging two lists of length L into a single list of length 2L requires 2L-1 comparisons (class exercise). For each vertex on level k, this merging takes 2 (2 r-k-1 ) – 1 = 2 r-k – 1 comparisons There are 2 k vertices on level k, for a total of 2 k (2 r-k – 1 ) = 2 r - 2 k comparisons on each level. Since we must do this on each level, the total number of comparisons for a Merge Sort is Since n = 2 r, this becomes. (Still need to account for the subdivision preprocessing, but this isn’t bad) Thus Merge Sort is and achieves the theoretical bound of a binary search.

9 3/10/03Tucker, Sec.3-59 Use the first element in the list to partition the list, then put it at the end of the left hand child list. Worst case,, on average 5 0 9 2 6 47 1 3 8 0 2 4 1 3 5 9 6 7 8 0 6 7 8 9 3 4 9 6 2 1 8 3 5 4 7 2 4 1 3 5 1 2 4 3 5 7 8 9 8 9 QUIK Sort

10 3/10/03Tucker, Sec.3-510 Heap Sort A heap is a (nearly) binary tree so that a parent is always bigger than its children (root is biggest of all). Put the root at the beginning of a list, then move up the biggest grandchildren. start of list Preprocessing: Need to first construct the heap (homework…)

11 3/10/03Tucker, Sec.3-511 Prove that it takes at most n – 1 comparisons to merge two sorted lists into a single sorted list of length n. Sort {4, 5, 2, 3, 0, 1} using each of a Bubble, Merge, QUIK, and Heap Sort. Class Exercises Note: if, and if, then don’t have to do the last comparison so get r l instead of r l + 1 Merge and where k + l = n. r i of the b’s Each a i must be compared to r i + 1 of the b’s for a total of


Download ppt "3/10/03Tucker, Sec.3-51 Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M “Big O” Notation We say that a function is if for some constant c, when n is large."

Similar presentations


Ads by Google