computer science 101 a survey of computer science timing problems
TRANSCRIPT
Computer Science Computer Science 101101
A Survey of Computer A Survey of Computer ScienceScience
Timing ProblemsTiming Problems
Efficiency Classes - Timing Efficiency Classes - Timing ProblemsProblems
We can use our knowledge of the We can use our knowledge of the efficiency class of an algorithm to efficiency class of an algorithm to make predictions about the execution make predictions about the execution time for a large data set based on the time for a large data set based on the time observed for a smaller set using time observed for a smaller set using the same machine and program.the same machine and program.
Timing Example - Sequential Timing Example - Sequential SearchSearch
Suppose we have done several trials of Suppose we have done several trials of Sequential Search on a set of 1000 elements Sequential Search on a set of 1000 elements and the average time is about .5 sec. and the average time is about .5 sec.
How long would we expect for 10,000 How long would we expect for 10,000 element searches using the same machine element searches using the same machine and same program?and same program?
Timing Example - Sequential Timing Example - Sequential SearchSearch
Given: T(1000) Given: T(1000) .5 sec. .5 sec.
Wanted: T(10,000)Wanted: T(10,000)
Recall that for Sequential Search, T(n) Recall that for Sequential Search, T(n) Kn. Kn.
If we knew the value of K, we could just “plug If we knew the value of K, we could just “plug in” the value 10,000 for n to get the time.in” the value 10,000 for n to get the time.
Timing Problem StructureTiming Problem Structure These timing problems have the general These timing problems have the general
form:form:– A specific algorithm is specifiedA specific algorithm is specified
– We’re given approximate time for some We’re given approximate time for some specific size problemspecific size problem
– We know the efficiency formula for the We know the efficiency formula for the algorithmalgorithm
– We’re asked to approximate the time for We’re asked to approximate the time for this algorithm on some specified larger size this algorithm on some specified larger size problemproblem
Timing Problem StrategyTiming Problem Strategy The timing problems we’ll work with have a The timing problems we’ll work with have a
single constant, say Ksingle constant, say K
We’ll use the information given for the smaller We’ll use the information given for the smaller size problem to solve for an approximate value size problem to solve for an approximate value of Kof K
This gives us a formula for the approximate This gives us a formula for the approximate time of this algorithm on given machine, etc. time of this algorithm on given machine, etc. without Kwithout K
We can then “plug in” the larger size to We can then “plug in” the larger size to compute approximate time for larger problem.compute approximate time for larger problem.
Back to Sequential Search Back to Sequential Search ExampleExample
So we have T(n) So we have T(n) Kn, and we are given that Kn, and we are given that T(1000) T(1000) .5 sec .5 sec
So,So, T(1000) T(1000) K K..1000 1000 .5, .5,
gives K gives K .5/1000 = 1/2000. This gives .5/1000 = 1/2000. This gives T(n) T(n) (1/2000)n (1/2000)n
(without K)(without K)
So T(10000) So T(10000) (1/2000) (1/2000) ..10000 = 5 sec.10000 = 5 sec.
Timing Example - Sequential Timing Example - Sequential SearchSearch
How long would we expect for How long would we expect for 1,000,000 element searches?1,000,000 element searches?
We have T(n) We have T(n) (1/2000)n (1/2000)nSo So
T(1,000,000) T(1,000,000) (1/2000) (1/2000) ..1000000 1000000
= 500 sec. = 500 sec. 8.33 min. 8.33 min.
Timing Example - Bubble SortTiming Example - Bubble Sort Suppose Bubble Sort on 1000 elements Suppose Bubble Sort on 1000 elements
takes .5 sec. takes .5 sec. How long to sort 10,000 elements with How long to sort 10,000 elements with
Bubble Sort?Bubble Sort? For Bubble Sort, T(n) For Bubble Sort, T(n) Kn Kn22..
Using T(1000) Using T(1000) K K..1000100022 .5, .5,gives K gives K .5/1000 .5/100022 and and T(n) T(n) (.5/1000(.5/10002 2 ) n) n22
So So T(10000) T(10000) (.5/1000 (.5/100022) ) ..100001000022 = .5 = .5 .. 100 = 50 sec. 100 = 50 sec.
Timing Example - Bubble SortTiming Example - Bubble Sort
How long to sort 1,000,000 elements with How long to sort 1,000,000 elements with Bubble Sort?Bubble Sort?
We have T(n) We have T(n) (.5/1000 (.5/100022) ) ..nn22
So T(1,000,000) So T(1,000,000) (.5/1000 (.5/100022) ) ..1000000100000022 = .5 = .5 .. 1000000 = 500000 1000000 = 500000
sec.sec. > 5.7 days. > 5.7 days.
Timing Example - QuicksortTiming Example - Quicksort
Suppose for Quicksort, T(1000) Suppose for Quicksort, T(1000) .5 sec. .5 sec.
Estimate T(10,000).Estimate T(10,000).
For Quicksort, T(n) For Quicksort, T(n) Kn Lg(n) Kn Lg(n)
Using T(1000) Using T(1000) K K..10001000..10 10 .5, .5,gives K gives K 1/20,000 and 1/20,000 and T(n) T(n) (1/20000) n Lg(n)(1/20000) n Lg(n)
So T(10000) So T(10000) (1/20000) (1/20000) ..1000010000..13 13 6.5 sec. 6.5 sec.
Timing Example - QuicksortTiming Example - Quicksort
Estimate T(1,000,000).Estimate T(1,000,000).
We have T(n) We have T(n) (1/20,000)n Lg(n) (1/20,000)n Lg(n)
So T(1,000,000) So T(1,000,000) (1/20000) (1/20000) ..10000001000000..20 20
1000 sec. 1000 sec. < 17 min. < 17 min.
Timing Example - Binary Timing Example - Binary SearchSearch
Suppose for Binary Search, T(1000) Suppose for Binary Search, T(1000) .5 .5 sec. sec.
Estimate T(10,000).Estimate T(10,000).
For Binary Search, T(n) For Binary Search, T(n) K Lg(n) K Lg(n)
Using T(1000) Using T(1000) K K..10 10 .5, .5,gives K gives K 1/20 and 1/20 and T(n) T(n) (1/20) Lg(n) (1/20) Lg(n)
So T(10000) So T(10000) (1/20) (1/20) ..13 13 .65 sec. .65 sec.
Timing Example - Binary Timing Example - Binary SearchSearch
Estimate T(1,000,000).Estimate T(1,000,000).
We have T(n) We have T(n) (1/20)Lg(n) (1/20)Lg(n)
So T(1000000) So T(1000000) (1/20) (1/20) ..20 20 1 sec. 1 sec.
Timing Examples - SummaryTiming Examples - Summary
Suppose for algorithm A, T(1000) Suppose for algorithm A, T(1000) .5 sec. .5 sec. Algorithm T(10,000) Algorithm T(10,000) T(1,000.000) T(1,000.000) Sequential SearchSequential Search 5 sec. 8.33 min.5 sec. 8.33 min.Search for LargestSearch for Largest 5 sec. 8.33 min.5 sec. 8.33 min.Selection SortSelection Sort 50 sec. 5.7 days 50 sec. 5.7 daysBubble SortBubble Sort 50 sec. 5.7 days 50 sec. 5.7 daysQuicksortQuicksort 6.5 sec. 17 min. 6.5 sec. 17 min.Binary SearchBinary Search .65 sec. 1 sec..65 sec. 1 sec.