1 / 11

Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M

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.

Télécharger la présentation

Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M

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. 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. Tucker, Sec.3-5

  2. 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. Tucker, Sec.3-5

  3. m = 2, j = 6 m = 2, j = 3 m = 3, j = 6 m = 2, j = 2 m = 2, j = 5 m = 2, j = 4 m = 3, j = 4 m = 3, j = 5 3 4 6 1 5 2 3 4 6 1 2 5 1 3 4 6 2 5 1 3 4 2 6 5 1 3 4 6 2 5 3 4 1 6 2 5 3 4 6 1 2 5 3 1 4 6 2 5 Moves the smallest number to the top Moves the next smallest number to the top Bubble Sort For m from 2 to n, do For j from n (step –1) to m do If Aj < Aj-1, then interchange Ajand Aj-1. Tucker, Sec.3-5

  4. 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 . Tucker, Sec.3-5

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

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

  7. Complexity of MergingA Simplifying Assumption • Assume our set has n = 2r 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 2k vertices on level k • The sets on level k all have 2r-k elements in them n = 23 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Tucker, Sec.3-5

  8. Complexity of MergingCounting the Comparisons • At each vertex on level k of the merging tree we merge the two children sets each of size 2r-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 (2r-k-1) – 1 = 2r-k – 1 comparisons • There are 2k vertices on level k, for a total of 2k (2r-k – 1 ) = 2r - 2k comparisons on each level. • Since we must do this on each level, the total number of comparisons for a Merge Sort is • Since n = 2r, 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. Tucker, Sec.3-5

  9. QUIK Sort 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 9 6 7 8 0 2 4 1 3 5 6 7 8 9 0 2 4 1 3 5 7 8 9 6 1 2 4 3 5 8 9 3 4 7 5 2 1 8 4 9 3 Tucker, Sec.3-5

  10. start of list start of list start of list start of list 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. Preprocessing: Need to first construct the heap (homework…) Tucker, Sec.3-5

  11. Merge and where k + l = n. ri of the b’s Each ai must be compared to ri + 1 of the b’s for a total of Note: if , and if , then don’t have to do the last comparison so get rl instead of rl + 1 Class Exercises • 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. Tucker, Sec.3-5

More Related