Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 A Survey of Computer Science Timing Problems.

Similar presentations


Presentation on theme: "Computer Science 101 A Survey of Computer Science Timing Problems."— Presentation transcript:

1 Computer Science 101 A Survey of Computer Science Timing Problems

2 Efficiency Classes - Timing Problems We can use our knowledge of the efficiency class of an algorithm to make predictions about the execution time for a large data set based on the time observed for a smaller set using the same machine and program.

3 Timing Example - Sequential Search Suppose we have done several trials of Sequential Search on a set of 1000 elements and the average time is about.5 sec. Suppose we have done several trials of Sequential Search on a set of 1000 elements and the average time is about.5 sec. How long would we expect for 10,000 element searches using the same machine and same program? How long would we expect for 10,000 element searches using the same machine and same program?

4 Timing Example - Sequential Search Given: T(1000) .5 sec. Given: T(1000) .5 sec. Wanted: T(10,000) Wanted: T(10,000) Recall that for Sequential Search, T(n)  Kn. Recall that for Sequential Search, T(n)  Kn. If we knew the value of K, we could just “plug in” the value 10,000 for n to get the time. If we knew the value of K, we could just “plug in” the value 10,000 for n to get the time.

5 Timing Problem Structure These timing problems have the general form: These timing problems have the general form: –A specific algorithm is specified –We’re given approximate time for some specific size problem –We know the efficiency formula for the algorithm –We’re asked to approximate the time for this algorithm on some specified larger size problem

6 Timing Problem Strategy The timing problems we’ll work with have a single constant, say K The timing problems we’ll work with have a single constant, say K We’ll use the information given for the smaller size problem to solve for an approximate value of K We’ll use the information given for the smaller size problem to solve for an approximate value of K This gives us a formula for the approximate time of this algorithm on given machine, etc. without K This gives us a formula for the approximate time of this algorithm on given machine, etc. without K We can then “plug in” the larger size to compute approximate time for larger problem. We can then “plug in” the larger size to compute approximate time for larger problem.

7 Back to Sequential Search Example So we have T(n)  Kn, and we are given that T(1000) .5 sec So we have T(n)  Kn, and we are given that T(1000) .5 sec So, T(1000)  K. 1000 .5, gives K .5/1000 = 1/2000. This gives T(n)  (1/2000)n (without K) So, T(1000)  K. 1000 .5, gives K .5/1000 = 1/2000. This gives T(n)  (1/2000)n (without K) So T(10000)  (1/2000). 10000 = 5 sec. So T(10000)  (1/2000). 10000 = 5 sec.

8 Timing Example - Sequential Search How long would we expect for 1,000,000 element searches? How long would we expect for 1,000,000 element searches? We have T(n)  (1/2000)n So T(1,000,000)  (1/2000). 1000000 = 500 sec.  8.33 min. We have T(n)  (1/2000)n So T(1,000,000)  (1/2000). 1000000 = 500 sec.  8.33 min.

9 Timing Example - Bubble Sort Suppose Bubble Sort on 1000 elements takes.5 sec. Suppose Bubble Sort on 1000 elements takes.5 sec. How long to sort 10,000 elements with Bubble Sort? How long to sort 10,000 elements with Bubble Sort? For Bubble Sort, T(n)  Kn 2. Using T(1000)  K. 1000 2 .5, gives K .5/1000 2 and T(n)  (.5/1000 2 ) n 2 For Bubble Sort, T(n)  Kn 2. Using T(1000)  K. 1000 2 .5, gives K .5/1000 2 and T(n)  (.5/1000 2 ) n 2 So T(10000)  (.5/1000 2 ). 10000 2 =.5. 100 = 50 sec. So T(10000)  (.5/1000 2 ). 10000 2 =.5. 100 = 50 sec.

10 Timing Example - Bubble Sort How long to sort 1,000,000 elements with Bubble Sort? How long to sort 1,000,000 elements with Bubble Sort? We have T(n)  (.5/1000 2 ). n 2 So T(1,000,000)  (.5/1000 2 ). 1000000 2 =.5. 1000000 = 500000 sec. > 5.7 days. We have T(n)  (.5/1000 2 ). n 2 So T(1,000,000)  (.5/1000 2 ). 1000000 2 =.5. 1000000 = 500000 sec. > 5.7 days.

11 Timing Example - Quicksort Suppose for Quicksort, T(1000) .5 sec. Suppose for Quicksort, T(1000) .5 sec. Estimate T(10,000). Estimate T(10,000). For Quicksort, T(n)  Kn Lg(n) For Quicksort, T(n)  Kn Lg(n) Using T(1000)  K. 1000. 10 .5, gives K  1/20,000 and T(n)  (1/20000) n Lg(n) Using T(1000)  K. 1000. 10 .5, gives K  1/20,000 and T(n)  (1/20000) n Lg(n) So T(10000)  (1/20000). 10000. 13  6.5 sec. So T(10000)  (1/20000). 10000. 13  6.5 sec.

12 Timing Example - Quicksort Estimate T(1,000,000). Estimate T(1,000,000). We have T(n)  (1/20,000)n Lg(n) We have T(n)  (1/20,000)n Lg(n) So T(1,000,000)  (1/20000). 1000000. 20  1000 sec. < 17 min. So T(1,000,000)  (1/20000). 1000000. 20  1000 sec. < 17 min.

13 Timing Example - Binary Search Suppose for Binary Search, T(1000) .5 sec. Suppose for Binary Search, T(1000) .5 sec. Estimate T(10,000). Estimate T(10,000). For Binary Search, T(n)  K Lg(n) For Binary Search, T(n)  K Lg(n) Using T(1000)  K. 10 .5, gives K  1/20 and T(n)  (1/20) Lg(n) Using T(1000)  K. 10 .5, gives K  1/20 and T(n)  (1/20) Lg(n) So T(10000)  (1/20). 13 .65 sec. So T(10000)  (1/20). 13 .65 sec.

14 Timing Example - Binary Search Estimate T(1,000,000). Estimate T(1,000,000). We have T(n)  (1/20)Lg(n) We have T(n)  (1/20)Lg(n) So T(1000000)  (1/20). 20  1 sec. So T(1000000)  (1/20). 20  1 sec.

15 Timing Examples - Summary Suppose for algorithm A, T(1000) .5 sec. Algorithm T(10,000) T(1,000.000) Sequential Search5 sec. 8.33 min. Search for Largest5 sec. 8.33 min. Selection Sort 50 sec. 5.7 days Bubble Sort 50 sec. 5.7 days Quicksort 6.5 sec. 17 min. Binary Search.65 sec. 1 sec.

16


Download ppt "Computer Science 101 A Survey of Computer Science Timing Problems."

Similar presentations


Ads by Google