w4231 analysis of algorithms
play

W4231: Analysis of Algorithms A trivial example 9/23/1999 An array - PDF document

W4231: Analysis of Algorithms A trivial example 9/23/1999 An array of integers a 1 a n is given such that 1 a i n and all the elements are distinct. Sorting in linear time (sometimes). Solution: output 1 , . . . , n .


  1. W4231: Analysis of Algorithms A trivial example 9/23/1999 An array of integers a 1 · · · a n is given such that 1 ≤ a i ≤ n and all the elements are distinct. • Sorting in linear time (sometimes). Solution: output 1 , . . . , n . – COMSW4231, Analysis of Algorithms – 1 – COMSW4231, Analysis of Algorithms – 2 Repetitions are allowed Implementation An array of integers a 1 · · · a n is given such that 1 ≤ a i ≤ n and sort(int a[], int n){ elements may be repeated. int c[n],i,j,k; // initialize c[] Create a vector c 1 , . . . , c n , where for (j=0; j<n; j++) c[j]=0; // fill in the entries of c[] c i = |{ j : a j = 1 }| for (i=0; i<n; i++) c[a[i]]++; // sort a[] If A = [2 , 4 , 1 , 2 , 5 , 8 , 3 , 1] then i=0; for (j=0; j<n; j++) C = [2 , 2 , 1 , 1 , 1 , 0 , 0 , 1] . for (k=0; k<c[j]; k++){ a[i]=j; i++;} Scan C , for every i , write i for c i times. } – COMSW4231, Analysis of Algorithms – 3 – COMSW4231, Analysis of Algorithms – 4 Stability An example of non-stability A sorting algorithm is stable if The difference between stable and non-stable algorithms is important only if each item has a key used for sorting and some on input a 1 · · · a n it outputs the sorted sequence a π (1) · · · a π ( n ) other information; and the keys can be repeated. with the property that if i < j and a π ( i ) ≤ a π ( j ) E.g. sort the pairs then π ( i ) < π ( j ) . (1997 , LA Confidential ) , (1998 , Life is Beautiful ) , (1993 , Schindler’s List ) , (1997 , Titanic ) , (1993 , The Piano ) using the first number as a key. – COMSW4231, Analysis of Algorithms – 5 – COMSW4231, Analysis of Algorithms – 6

  2. If the algorithm reports A Stable Version of Counting Sort (1993 , Schindler’s List ) , (1993 , The Piano ) , Each c j is a queue. For every i , we copy a i in the queue c j , where j is the key of (1997 , Titanic ) , (1997 , LA Confidential ) , (1998 , Life is Beautiful ) a i . Then it is not stable At the end we patch the queues together. Impossible to have an inversion. Alternative method in CLR. – COMSW4231, Analysis of Algorithms – 7 – COMSW4231, Analysis of Algorithms – 8 Analysis Radix Sort Let c j be the number of items of key j . Then � m j =1 c j = n . Suppose we have in input n integers that are b -digits binary numbers. Running time; O ( m ) to initialize c ; O ( n ) to fill c ; � m j =1 O ( c j )+ O (1) = O ( � j c j )+ O ( m ) = O ( m + n ) total time is O ( n + m ) . Put the numbers whose last digit is 0 before those whole last digit is 1 . Better than mergesort when m = o ( n log n ) . Proceed like that for every digit using a stable sorting. Dealing with each digit takes O ( n ) time. Total time: O ( nb ) . – COMSW4231, Analysis of Algorithms – 9 – COMSW4231, Analysis of Algorithms – 10 More on Radix Sort Summary of Sorting Algs for Integers Input: n integers in the range 1 , . . . , m . Generalization: each number has b digits in base k . • Mergesort O ( n log n ) -time independent of m (assuming unit- Do b passes of a stable sort. cost RAM model). For integers in the range 1 , . . . , m , we can view these integers as having log n m digits in base n . • Radix Sort O ( n log m/ log n ) . Do log n m passes of stable counting sort. Each one takes time • Counting Sort O ( n + m ) . O ( n ) . Counting sort is preferable only if m = O ( n ) . Radix sort works Sort in time O ( n log m/ log n ) . well for bigger m , provided m = O ( n log n ) . For bigger values of m , Mergesort is better. – COMSW4231, Analysis of Algorithms – 11 – COMSW4231, Analysis of Algorithms – 12

  3. Lexicographic order E.g platform < plausible ( j = 4 in prev. definition — t < u ). p l a t f o r m p l a u s i b l e Consider strings over a certain alphabet set S on which an order < is defined. E.g. S is the set of Roman characters and also platform < platforms . a, b, . . . , z and the order < is the alphabetic order. For two strings a = a 1 · · · a n and b = b 1 · · · b m , we write a < lex b if there is a j such that • a i = b i for i = 1 , . . . j − 1 and • a j < b j . or if a i = b i for i = 1 , . . . , n and m > n . – COMSW4231, Analysis of Algorithms – 13 – COMSW4231, Analysis of Algorithms – 14 Sorting strings 1 2 3 4 t r u e We first sort the 4th component d i s h d i s k b l o w disk dish blow true 1 2 3 4 1 2 3 4 d i s k b l o w d i s h Then the 3rd d i s h b l o w d i s k t r u e t r u e – COMSW4231, Analysis of Algorithms – 15 – COMSW4231, Analysis of Algorithms – 16 Running Time 1 2 3 4 d i s h Then the 2nd d i s k If we have n strings of length l this takes linear and optimal b l o w time O ( nl ) , provided we can do each pass in O ( n ) time. t r u e This is possible if we sort the array of pointers to the strings. Then the 1st 1 2 3 4 b l o w d i s h d i s k t r u e – COMSW4231, Analysis of Algorithms – 17 – COMSW4231, Analysis of Algorithms – 18

  4. Strings of different lengths Analysis If the strings have different length l 1 , . . . , l n , and l max is the For every 1 ≤ l ≤ l max , call c l the number of strings of length max length, the algorithm can be adapted to work in O ( nl max ) ≥ c l . time. This is not linear (neither optimal) if there are only a few Then � l max l =1 c l = l tot . long strings. Can you see why? A better algorithm takes time O ( l tot ) where l tot = � i l i . Then if we sort in time O ( c l ) the l -th entry of the strings who Idea of the better algorithm: sort the l max -th entry of strings have an l -th entry, the algorithm takes time O ( l tot ) . of length l max , then the ( l max − 1) -th entry of strings of length ≥ l max . – COMSW4231, Analysis of Algorithms – 19 – COMSW4231, Analysis of Algorithms – 20 Example m i t c o l u m b i a r u t g e r s Entry 9 h a r v a r d p r i n c e t o n mit, columbia, rutgers, harvard, princeton, yale y a l e m i t m i t c o l u m b i a c o l u m b i a r u t g e r s r u t g e r s Entry 8 h a r v a r d h a r v a r d p r i n c e t o n p r i n c e t n o y a l e y a l e – COMSW4231, Analysis of Algorithms – 21 – COMSW4231, Analysis of Algorithms – 22 m i t m i t h a r v a r d h a r v a r d c o l u m b a p r i n e t o n i c Entry 7 Entry 5 p r i n c e t o n r u t g e r s r u t g e r s c o l u m b i a y a l e y a l e m i t m i t c o l u m b i a y a l e p r i n c e t o n r u t g e r s Entry 6 Entry 4 h a r v a r d p r i n c e t o n r u t g e s c o l m b i a r u y a l e h a r v a r d – COMSW4231, Analysis of Algorithms – 23 – COMSW4231, Analysis of Algorithms – 24

  5. p r i n c e t o n c o l u m b i a y a l e h a r v a r d c o u m b i a i t l m Entry 3 Entry 1 m i t p r i n c e t o n r u t g e r s r u t g e r s h a v a r d a l e r y y l e a h a r v a r d m i t Entry 2 c o l u m b i a p i n c e t o n r r u t g e r s – COMSW4231, Analysis of Algorithms – 25 – COMSW4231, Analysis of Algorithms – 26

Recommend


More recommend