“It ain’t no good if it ain’t snappy enough.” (Efficient Computations) DiscussionBureaucratic stuffQuestion: How do we measure the “speed” of an algorithm?“Running time” of an algorithmExample: Find MinSelection SortGauss’s trick : Sum of (n – i) for i = 1 to n – 1 “20 Questions”: Guess the number I have in mind; you can only ask yes/no questions Brief detour: Logarithms (CS view)Binary search and binary representation of numbersBinary representations (cont’d)Binary representation of n (more standard definition)Efficiency of Effort: A lens on the world“It ain’t no good if it ain’tsnappy enough.”(Efficient Computations) COS 1162/21/2006Instructor: Sanjeev AroraDiscussion1. What different ways does Brooks describe for a robotto “orient” itself? Did your experiments with Scribbler give you insight into any of them?2. In what ways (according to Brian Hayes) is the universe like a cellular automaton?Bureaucratic stuff New blogging assignment for this week. (see handout). Reminder for this week’s lab: Come with robots,cables. Make sure you understand pseudocode. Precept Wed 7-8pm in Computer Science 102 Scribbler Lab again in 2 weeks: make robot do some Kind of fine art ( music, dance, sketching, etc.) Super cool guest lecturer on Thurs. (computer music)Question: How do we measure the “speed” of an algorithm? Ideally, should be machine and technology independent“Running time” of an algorithm Definition: the number of “elementary operations” performed by the algorithm Elementary operations: +, -, *, /, assignment, evaluation of conditionals “Speed” of computer: number of elementary steps it can perform per second (NB. Simplified Definition.)Example: Find Min n items, stored in array A Variables are i, best best ← 1 for i = 2 to n do{if (A[ i ] < A[best]) then{ best ← i }}Uses at most 2(n – 1) + 1 operationsInitializationNumber of iterations2 operations per iteration: 1 comparison, 1 assignmentSelection SortDo for i = 1 to n –1{Find cheapest bottle among those numbered i to nSwap that bottle and the i’th bottle.} For the i’th round, takes at most 2(n – i) + 3 To figure out running time, need to figure out how to sum up (n – i) for i = 1 to n –1About 2(n – i) steps3 stepsGauss’s trick : Sum of (n – i) for i = 1 to n –1S = 1 + 2 + … + (n –2) + (n –1) + S = (n –1) + (n – 2) + … + 2 + 12S = n + n + … + n + nS = n(n –1) / 2 So total time for selectionsort is ≤ n(n –1) + 3nn – 1 times“20 Questions”: Guess the number I have in mind; you can only ask yes/no questions My number is an integer from 1 to n Binary Search Algorithm: First Question: “Isnumber ≥ n / 2?” Answer halves the range!RepeatRepeat2n1, 2, … , – 1, , + 1, … , n2n2nExercise: Express as pseudocode.Brief detour: Logarithms (CS view) log2n = K means 2K-1≤ n < 2K In words: K is the number of times you need to divide n by 2 in order to get a number ≤ 1n= 8 n= 1024 n= 1048576 n=8388608log2 n 310 20 23n 8 1024 1048576 8388608n264 1048576 1099511627776 70368744177664John NapierBinary search and binary representation of numbers Say we know 0 ≤ number < 2KIs 2K/ 2 ≤ number < 2K?No YesIs 2K/ 4 ≤ number < 2K/ 2?NoYesIs 2K×3/8 ≤ number < 2K/ 2?No Yes……02KBinary representations (cont’d) In general, each number uniquely represented by a sequence of yes/no answers to these questions. Correspond to paths down this tree:Is 2K/ 2 ≤ number < 2K?NoYesIs 2K/ 4 ≤ number < 2K/ 2?NoYesIs 2K/ 8 ≤ number < 2K/ 4?No Yes……Is 2K×3/8 ≤ number < 2K/ 2?No Yes………Binary representation of n(more standard definition)Ifn = 2kbk+ 2k-1bk-1+ … + 2 b2+ b1where the b’s are either 0 or 1)Then binary representation of n ⎣n⎦2= bkbk –1… b2b1Efficiency of Effort: A lens on the world QWERTY keyboard “UPS Truck Driver’s Problem” (a.k.a. Traveling Salesman Problem or TSP) Handwriting Recognition CAPTCHA’s Quantum ComputingCan n particles do 2n“operations” in a single step?Or is Quantum Mechanics not quite correct.?SIAM J. Computing26(5)
View Full Document