25
Computer Science 101 Fast Algorithms

Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Embed Size (px)

Citation preview

Page 1: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Computer Science 101

Fast Algorithms

Page 2: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

What Is Really Fast?

n O(log2n) O(n) O(n2) O(2n)

2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5 32 1024 4294967296 64 6 64 4096 19 digits 128 7 128 16384 whoa! 256 8 256 65536 512 9 512 2621441024 10 1024 1048576

Page 3: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Logarithms

• Definition: The logarithm to base 2 of n is a number k such that 2k=n, so log2(n) = k

• When we say log(n), we mean log2(n)• Example: 25 = 32 so log(32) = 5• Another way to think of this is that log(n) is the

number of times we must divide n by 2 until we get 1

• Note: log(n) is usually a fraction, but the closest integer will work for us

Page 4: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Improving Efficiency

• Tweaking does not improve the big-O, so the change is not noticeable when N becomes very large

• If we can go from O(n) to O(logn), we have really accomplished something

Page 5: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Example: Sequential Search

If the data items are in random order, then each one must be examined in the worst case

Complexity of sequential search = O(n)

set Current to 1Set Found to false while Current <= N and not Found do if A(Current) = Target then set Found to true else increment Currentif Found then output Currentelse output 0

Page 6: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Searching a Sorted List

• When we search a phone book, we don’t begin with the first name and look at each successor

• We skip over large numbers of names until we find the target or give up

Page 7: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Binary Search

• Strategy– Have pointers marking left and right ends of the list still to be

processed– Compute the position of the midpoint between the two pointers– If the target equals the value at midpoint, quit with the position

found– Otherwise, if the target is less than that value, search just the

positions to the left of midpoint– Otherwise, search the just the positions to the right of midpoint– Give up when the pointers cross

14 16 22 32 34 66 80 9066target

left rightmid

Page 8: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Binary Search

• Strategy– Have pointers marking left and right ends of the list still to be

processed– Compute the position of the midpoint between the two pointers– If the target equals the value at midpoint, quit with the position

found– Otherwise, if the target is less than that value, search just the

positions to the left of midpoint– Otherwise, search the just the positions to the right of midpoint– Give up when the pointers cross

14 16 22 32 34 66 80 9066target

left rightmid

Page 9: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

The Binary Search Algorithmset Left to 1Set Right to NSet Found to false while Left <= Right and not Found do compute the midpoint if Target = A(Mid) then set Found to true else if Target < A(Mid) then search to the left of the midpoint else search to the right of the midpointif Found then output Midelse output 0

Page 10: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

The Binary Search Algorithmset Left to 1Set Right to NSet Found to false while Left <= Right and not Found do set Mid to (Left + Right) / 2 if Target = A(Mid) then set Found to true else if Target < A(Mid) then search to the left of the midpoint else search to the right of the midpointif Found then output Midelse output 0

Page 11: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

The Binary Search Algorithmset Left to 1Set Right to NSet Found to false while Left <= Right and not Found do set Mid to (Left + Right) / 2 if Target = A(Mid) then set Found to true else if Target < A(Mid) then set Right to Mid – 1 else search to the right of the midpointif Found then output Midelse output 0

Page 12: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

The Binary Search Algorithmset Left to 1Set Right to NSet Found to false while Left <= Right and not Found do set Mid to (Left + Right) / 2 if Target = A(Mid) then set Found to true else if Target < A(Mid) then set Right to Mid – 1 else set Left to Mid + 1if Found then output Midelse output 0

Page 13: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Analysis of Binary Search

• On each pass through the loop, ½ of the positions in the list are discarded

• In the worst case, the number of comparisons equals the number of times the size of the list can be divided by 2

• O(logn) algorithm! However, must have a sorted list

Page 14: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Improving on N2 Sorting

• Several algorithms have been developed to break the N2 barrier for sorting

• Most of them use a divide-and-conquer strategy

• Break the list into smaller pieces and apply another algorithm to them

Page 15: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Quicksort

• Strategy - Divide and Conquer:– Partition list into two parts, with small elements in the

first part and large elements in the second part

– Sort the first part

– Sort the second part

• Question - How do we sort the sections?Answer - Apply Quicksort to them

• Recursive algorithm - one which makes use of itself to solve smaller problems of the same type

Page 16: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Quicksort

• Question - Will this recursive process ever stop?

• Answer - Yes, when the problem is small enough, we no longer use recursion. Such cases are called base cases

Page 17: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Partitioning a List

• To partition a list, we choose a pivot element

• The elements that are less than or equal to the pivot go into the first section

• The elements larger than the pivot go into the second section

Page 18: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

19 8 15 5 30 20 10 1 28 25 12

19 8 15 5 302010 1 28 2512

Partition

Partitioning a List

Pivot is the element at the midpoint

Sublist to sort Sublist to sort

Data are where they should be relative to the pivot

Page 19: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

The Quicksort Algorithm

if the list to sort has more than 1 element then if the list has exactly two elements then

if the elements are out of order then exchange them else perform the Partition Algorithm on the list apply QuickSort to the first section apply QuickSort to the second section

Page 20: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Partitioning: Choosing the Pivot

• Ideal would be to choose the median element as the pivot, but this would take too long

• Some versions just choose the first element

• Our choice - the median of the first three elements

Page 21: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

19 8 15 5 30 20 10 1 28 25 12

12 5 1 15 283010 8 25 1920

Partition

Partitioning a List

Pivot is median of first three items

The median of the first three items is a better approximation to the actual median than the item at the midpoint and results in more even splits

Page 22: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

The Partition Algorithmexchange the median of the first 3 items with the first set P to first position of listset L to second position of listset U to last position of listwhile L <= U while A(L) A(P) do set L to L + 1 while A(U) > A(P) do set U to U - 1 if L < U then exchange A(L) and A(U)exchange A(P) and A(U)

A The listP The position of the pivot elementL Probes for items > pivotU Probes for items <= pivot

Page 23: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Quicksort: Rough Analysis

• For simplification, assume that we always get even splits when we partition

• When we partition the entire list, each element is compared with the pivot - approximately n comparisons

• Each of the halves is partitioned, each taking about n/2 comparisons, thus about n more comparisons

• Each of the fourths is partitioned,each taking about n/4 comparisons - n more

Page 24: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Quicksort: Rough Analysis

• How many levels of about n comparisons do we get?• Roughly, we keep splitting until the pieces are about

size 1• How many times must we divide n by 2 before we

get 1?• log(n) times, of course• Thus comparisons n Log(n)

• Quicksort is O(n Log(n)) – in the ideal or best case

Page 25: Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n ) 2 1 2 4 4 4 2 4 16 64 8 3 8 64 256 16 4 16 256 65536 32 5

Quicksort: Best Case/Worst Case

• List is already sorted and we choose the pivot from the midpoint – leads to even splits all the way down, log(n) total splits

• List is already sorted and we choose the pivot from the first position – leads to the worst splits at each level, n total splits

• O(nlogn) in best case, O(n2) in worst case