16
Computer Science Computer Science 101 101 A Survey of A Survey of Computer Science Computer Science Timing Problems Timing Problems

Computer Science 101 A Survey of Computer Science Timing Problems

Embed Size (px)

Citation preview

Page 1: Computer Science 101 A Survey of Computer Science Timing Problems

Computer Science Computer Science 101101

A Survey of Computer A Survey of Computer ScienceScience

Timing ProblemsTiming Problems

Page 2: Computer Science 101 A Survey of Computer Science Timing 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.

Page 3: Computer Science 101 A Survey of Computer Science Timing Problems

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?

Page 4: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 5: Computer Science 101 A Survey of Computer Science Timing Problems

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

Page 6: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 7: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 8: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 9: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 10: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 11: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 12: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 13: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 14: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 15: Computer Science 101 A Survey of Computer Science Timing Problems

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.

Page 16: Computer Science 101 A Survey of Computer Science Timing Problems