330 likes | 447 Views
Analysis of Algorithms CS 477/677. Instructor: Monica Nicolescu Lecture 8. i < k search in this partition. i > k search in this partition. General Selection Problem. Select the i-th order statistic ( i-th smallest element) form a set of n distinct numbers Idea:
E N D
Analysis of AlgorithmsCS 477/677 Instructor: Monica Nicolescu Lecture 8
i < k search in this partition i > k search in this partition General Selection Problem • Select the i-th order statistic (i-th smallest element) form a set of n distinct numbers • Idea: • Partition the input array similarly with the approach used for Quicksort (use RANDOMIZED-PARTITION) • Recurse on one side of the partition to look for the i-th element depending on where i is with respect to the pivot p q r A k = q – p + 1 CS 477/677 - Lecture 8
A Better Selection Algorithm • Can perform Selection in O(n) Worst Case • Idea: guarantee a good split on partitioning • Running time is influenced by how “balanced” are the resulting partitions • Use a modified version of PARTITION • Takes as input the element around which to partition CS 477/677 - Lecture 8
x1 x3 x2 xn/5 x n - k elements k – 1 elements x Selection in O(n) Worst Case • Divide the nelements into groups of 5 n/5 groups • Find the median of each of the n/5 groups • Use insertion sort, then pick the median • Use SELECT recursively to find the median x of the n/5 medians • Partition the input array around x, using the modified version of PARTITION • There are k-1 elements on the low side of the partition and n-k on the high side • If i = k then return x. Otherwise, use SELECT recursively: • Find the i-th smallest element on the low side if i < k • Find the (i-k)-th smallest element on the high side if i > k A: CS 477/677 - Lecture 8
How Fast Can We Sort? • Insertion sort, Bubble Sort, Selection Sort • Merge sort • Quicksort • What is common to all these algorithms? • These algorithms sort by making comparisons between the input elements • To sort n elements, comparison sorts must make (nlgn) comparisons in the worst case (n2) (nlgn) (nlgn) CS 477/677 - Lecture 8
Lower-Bounds for Sorting • Comparison sorts use comparisons between elements to gain information about an input sequence a1, a2, …, an • We perform tests: ai < aj, ai≤ aj, ai = aj, ai≥ aj, or ai> aj to determine the relative order of aiand aj • We will show that there cannot be a comparison-based algorithm faster than nlgn • We assume that all the input elements are distinct CS 477/677 - Lecture 8
one execution trace node leaf: Decision Tree Model • Represents the comparisons made by a sorting algorithm on an input of a given size: models all possible execution traces • Control, data movement, other operations are ignored • Count only the comparisons • Decision tree for insertion sort on three elements: CS 477/677 - Lecture 8
Decision Tree Model • Allpermutations on n elements must appear as one of the leaves in the decision tree • Worst-case number of comparisons • the length of the longest path from the root to a leaf • the height of the decision tree n! permutations CS 477/677 - Lecture 8
Decision Tree Model • Goal: finding a lower bound on the running time on any comparison sort algorithm • find a lower bound on the heights of all decision trees for all algorithms CS 477/677 - Lecture 8
Lemma • Any binary tree of height h has at most Proof:induction on h Basis:h = 0 tree has one node, which is a leaf 2h = 1 Inductive step:assume true for h-1 • Extend the height of the tree with one more level • Each leaf becomes parent to two new leaves No. of leaves for tree of height h = = 2 (no. of leaves for tree of height h-1) ≤ 2 2h-1 = 2h 2h leaves CS 477/677 - Lecture 8
Lower Bound for Comparison Sorts Theorem: Any comparison sort algorithm requires (nlgn) comparisons in the worst case. Proof: How many leaves does the tree have? • At least n! (each of the n! permutations of the input appears as some leaf) n! ≤ l • At most 2h leaves n! ≤ l ≤ 2h h ≥ lg(n!) = (nlgn) h leaves l We can beat the (nlgn) running time if we use other operations than comparisons! CS 477/677 - Lecture 8
j COUNTING-SORT 1 n Alg.: COUNTING-SORT(A, B, n, k) • for i ← 0to k • do C[ i ] ← 0 • for j ← 1to n • do C[A[ j ]] ← C[A[ j ]] + 1 • C[i] contains the number of elements equal to i • for i ← 1to k • do C[ i ] ← C[ i ] + C[i -1] • C[i] contains the number of elements ≤i • for j ← ndownto 1 • do B[C[A[ j ]]] ← A[ j ] • C[A[ j ]] ← C[A[ j ]] - 1 A 0 k C 1 n B CS 477/677 - Lecture 8
A 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 C C C C C C 2 1 1 1 2 2 2 2 2 0 2 2 4 4 3 4 2 4 6 5 6 5 7 3 7 7 7 7 0 7 8 8 8 1 8 8 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 2 5 0 0 0 3 2 0 2 3 3 3 0 3 3 3 3 3 B B B B Example CS 477/677 - Lecture 8
A 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 C C C 0 0 0 2 2 2 3 3 3 4 4 5 7 7 7 8 7 8 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 0 0 0 2 0 0 5 0 0 0 2 3 2 2 2 2 0 3 2 3 3 3 3 3 3 3 3 0 3 3 3 3 5 5 B B B B Example (cont.) CS 477/677 - Lecture 8
Analysis of Counting Sort Alg.: COUNTING-SORT(A, B, n, k) • for i ← 0to k • do C[ i ] ← 0 • for j ← 1to n • do C[A[ j ]] ← C[A[ j ]] + 1 • C[i] contains the number of elements equal to i • for i ← 1to k • do C[ i ] ← C[ i ] + C[i -1] • C[i] contains the number of elements ≤i • for j ← ndownto 1 • do B[C[A[ j ]]] ← A[ j ] • C[A[ j ]] ← C[A[ j ]] - 1 (k) (n) (k) (n) Overall time: (n + k) CS 477/677 - Lecture 8
Analysis of Counting Sort • Overall time: (n + k) • In practice we use COUNTING sort when k = O(n) running time is (n) • Counting sort is stable • Numbers with the same value appear in the same order in the output array • Important when additional data is carried around with the sorted keys CS 477/677 - Lecture 8
Radix Sort • Considers keys as numbers in a base-k number • A d-digit number will occupy a field of d columns • Sorting looks at one column at a time • For a d digit number, sort the least significant digit first • Continue sorting on the next least significant digit, until all digits have been sorted • Requires only d passes through the list CS 477/677 - Lecture 8
RADIX-SORT Alg.: RADIX-SORT(A, d) for i ← 1to d do use a stable sort to sort array A on digit i • 1 is the lowest order digit, d is the highest-order digit CS 477/677 - Lecture 8
Analysis of Radix Sort • Given n numbers of d digits each, where each digit may take up to k possible values, RADIX-SORT correctly sorts the numbers in (d(n+k)) • One pass of sorting per digit takes (n+k) assuming that we use counting sort • There are d passes (for each digit) CS 477/677 - Lecture 8
Correctness of Radix sort • We use induction on the number d of passes through the digits • Basis: If d = 1, there’s only one digit, trivial • Inductive step: assume digits 1, 2, . . . , d-1 are sorted • Now sort on the d-th digit • If ad < bd, sort will put a before b: correct a < b regardless of the low-order digits • If ad > bd, sort will put a after b: correct a > b regardless of the low-order digits • If ad = bd, sort will leave a and b in the same order and a and b are already sorted on the low-order d-1 digits CS 477/677 - Lecture 8
Bucket Sort • Assumption: • the input is generated by a random process that distributes elements uniformly over [0, 1) • Idea: • Divide [0, 1) into n equal-sized buckets • Distribute the n input values into the buckets • Sort each bucket • Go through the buckets in order, listing elements in each one • Input: A[1 . . n], where 0 ≤ A[i] < 1 for all i • Output: elements in A sorted • Auxiliary array: B[0 . . n - 1] of linked lists, each list initially empty CS 477/677 - Lecture 8
BUCKET-SORT Alg.: BUCKET-SORT(A, n) for i ← 1to n do insert A[i] into list B[nA[i]] for i ← 0to n - 1 do sort list B[i] with insertion sort concatenate lists B[0], B[1], . . . , B[n -1] together in order return the concatenated lists CS 477/677 - Lecture 8
/ / .12 / .39 / .26 .68 / .17 .78 .23 / .21 .72 / .94 / / / Example - Bucket Sort 1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 CS 477/677 - Lecture 8
/ / .78 .23 .68 .78 / .17 / .72 .26 / .39 .94 / .72 .39 / .21 .12 .17 .12 .23 .94 / .26 .21 .68 / / / Example - Bucket Sort 0 1 2 3 4 5 6 7 Concatenate the lists from 0 to n – 1 together, in order 8 9 CS 477/677 - Lecture 8
Correctness of Bucket Sort • Consider two elements A[i], A[ j] • Assume without loss of generality that A[i] ≤ A[j] • Then nA[i] ≤ nA[j] • A[i] belongs to the same group as A[j] or to a group with a lower index than that of A[j] • If A[i], A[j] belong to the same bucket: • insertion sort puts them in the proper order • If A[i], A[j] are put in different buckets: • concatenation of the lists puts them in the proper order CS 477/677 - Lecture 8
Analysis of Bucket Sort Alg.: BUCKET-SORT(A, n) for i ← 1to n do insert A[i] into list B[nA[i]] for i ← 0to n - 1 do sort list B[i] with insertion sort concatenate lists B[0], B[1], . . . , B[n -1] together in order return the concatenated lists (n) (n) (n) (n) CS 477/677 - Lecture 8
Conclusion • Any comparison sort will take at least nlgn to sort an array of n numbers • We can achieve a better running time for sorting if we can make certain assumptions on the input data: • Counting sort: each of the n input elements is an integer in the range 0 to k • Radix sort: the elements in the input are integers represented with d digits • Bucket sort: the numbers in the input are uniformly distributed over the interval [0, 1) CS 477/677 - Lecture 8
A Job Scheduling Application • Job scheduling • The key is the priority of the jobs in the queue • The job with the highest priority needs to be executed next • Operations • Insert, remove maximum • Data structures • Priority queues • Ordered array/list, unordered array/list CS 477/677 - Lecture 8
PQ Implementations & Cost Worst-case asymptotic costs for a PQ with N items Insert Remove max ordered array N 1 ordered list N 1 unordered array 1 N unordered list 1 N Can we implement both operations efficiently? CS 477/677 - Lecture 8
Background on Trees • Def:Binary tree = structure composed of a finite set of nodes that either: • Contains no nodes, or • Is composed of three disjoint sets of nodes: a root node, a left subtree and a right subtree root 4 Right subtree Left subtree 1 3 2 16 9 10 14 8 CS 477/677 - Lecture 8
4 4 1 3 1 3 2 16 9 10 2 16 9 10 14 8 7 12 Complete binary tree Full binary tree Special Types of Trees • Def:Full binary tree = a binary tree in which each node is either a leaf or has degree (number of children) exactly 2. • Def:Complete binary tree = a binary tree in which all leaves have the same depth and all internal nodes have degree 2. CS 477/677 - Lecture 8
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) property: for any node x Parent(x) ≥ x 8 It doesn’t matter that 4 in level 1 is smaller than 5 in level 2 7 4 5 2 Heap CS 477/677 - Lecture 8
Readings • Chapters 8 and 9 CS 477/677 - Lecture 8