COS 109 Monday October 12 • Housekeeping – Problem Set 4 and Lab 3 are available – Questions about previous assignments – Problem Set 2 returned today (with a few Problem Set 1’s) A few math issues If you have 4 people and each loses 1 pound per day, how long does it take for each to lose 4 pounds? • An overview of software – What will we cover in this section? • Algorithms – A few simple tasks – Some more complicated tasks – Measuring running time • Problems for which there is no algorithm Problem Set 2 Problem Set 2 grades 40 38 36 34 32 30 28 26 24 22 20 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 Another view 45 Yet another view 40 40 38 35 30 36 25 34 20 32 15 10 30 5 28 0 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 26 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 Course high level outline • hardware (3-4 weeks) • software (3-4 weeks) • networks and the impact of internetworking (3-4 weeks) • along the way – current events, history, QR, ... Course high level outline • hardware (3-4 weeks) – how computers represent and process information – what's inside a computer, how it works, how it's built – the universality of a computer • software (3-4 weeks) • networks and the impact of internetworking (3-4 weeks) • along the way Survey (part 2) • Have you heard of • Have you held – – – – – – – – – – Random access memory Moore’s Law NP completeness Javascript Net neutrality Spambots The Cloud The internet TCP/IP Malware/ransomware – – – – – A transistor An integrated circuit A disk drive Memory A CPU Survey (part 2) • Have you heard of • Have you held – – – – – – – – – – Random access memory Moore’s Law NP completeness Javascript Net neutrality Spambots The Cloud The internet TCP/IP Malware/ransomware – – – – – A transistor An integrated circuit A disk drive Memory A CPU CPU block diagram Registers Control unit ALU PC Cache memory ALU = arithmetic/ logic unit PC = program counter = location of next instr Course high level outline • hardware (3-4 weeks) • software (3-4 weeks) – – – – how we tell computers how to do things algorithms as recipes for computations a very gentle introduction to programming in Javascript programs that make us able to write programs, run apps, … • networks and the impact of internetworking (3-4 weeks) • along the way Software: how we tell the machine what to do • hardware is a general purpose machine – capable of doing instructions repetitively and very fast – doesn't do anything itself unless we tell it what to do • software: the instructions we want it to do – different set of instructions -> different program -> machine behaves differently – program and data are stored in the same memory and manipulated by the same instructions • to tell a machine what to do, – we have to spell out the steps in excruciating detail – programming languages help handle a lot of the details Software roadmap • algorithms – precise but abstract descriptions of how to do task • programs – complete concrete descriptions of how to do task on a real computer • programming languages – precise notations for describing how to do tasks on a computer e.g., Toy, Javascript • real programs (big software) – operating systems file systems, databases – applications • social / political / economic / legal issues – intellectual property: patents, copyrights, interfaces – standards – open source Algorithms • Algorithms are recipes Input My algorithm Output • Sample algorithms • Cheese sandwich • Input is 2 slices of bread and a slice of cheese • Output is a cheese sandwich • Maximum • Input is the number N followed N integers • Output is the largest of the integers • Sorting • Input is the number N followed N integers • Output is the input integers in sorted order Specification of cheese sandwich algorithm • Cheese sandwich • Input is 2 slices of bread and a slice of cheese • Output is a cheese sandwich • The algorithm • Place a slice of bread on a plate • Place the cheese on the bread • Place the other slice of bread over the cheese • Concerns • Where did the plate come from? • What if the slices of bread are rectangular is not a good sandwich xkcd.com/627 A real-world algorithm A real-world algorithm and its implementation If (L50 < L49) L51 = L50 Else L51 = L49 L52 = .08*L51 L53 = L49 – L51 L54 = .10 * L53 L55 = L46 – L47 L56 = L44 – L45 If (L55 < L56) L57 = L55 Else L57 = L56 L58 = .15 * L57 L59 – L56 – L57 L60 = .20 * L59 If (L38 == 0) go to Line63 L61 = L40 – L44 L62 = .25 * L61 Line63: L63 = L42 + L48 + L52 + L54 + L58 + L60 + L62 If (L36 < 175000) L64 = .26*L36 Else L64 = .28 *L36 – 3500 If (L63 < L64) L65 = L63 Else L65 = L64 Algorithms • an algorithm is the computer science version of a really careful, precise, unambiguous recipe or procedure • a sequence of steps that performs some computation • each step is expressed in terms of basic operations whose meaning is completely specified – basic operations or "primitive operations" are given e.g., arithmetic operations • all possible situations are covered – the algorithm never gets to a situation where it doesn't know what to do next • designed to stop and not run forever – does not run forever (or maybe it does in some situations) What is wrong with this picture? [ Some sample algorithms ] • compute average of two numbers average = (first number + second number) / 2 • compute average of N numbers sum = 0 for each number (from 1 to N) add ith number to sum average = sum / N • convert decimal to binary divide number by 2, write down remainder use quotient as new number repeat until number becomes zero show remainders in reverse order • many algorithms have this form: – set up initial conditions (get started, get data to work on, ...) – repeat until some criterion is satisfied – finish the job [ Algorithm for getting rid of jokers in card deck] Here is a deck of cards • How do we remove the jokers? – How long does it take? • Does the answer change if we were removing the ace of spades? Linear time algorithms • lots of algorithms have this same basic form: look at each item in turn do the same simple computation on each item: does it match something (looking up a name in a list of names) count it (how many items are in the list) count it if it meets some criterion (how many of some kind in the list) remember some property of items found (largest, smallest, …) transform it in some way (limit size, convert case of letters, …) • amount of work (running time) is proportional to amount of data – twice as many items will take twice as long to process – computation time is linearly proportional to length of input A linear time algorithm acted out • Finding the maximum of N numbers Acting out an algorithm • Need 8 volunteers – Each volunteer bring a piece of paper • Each person is given a number (between 1 and 100) • Line up in a row – – – – First person compares their number to second Larger number stays, smaller number steps back Now the row is one person shorter Repeat until there is only 1 person in the row • Alternative approach – Person 1 compares to person 2, 3 to 4, 5 to 6, 7 to 8 in sequence larger number steps forward – Repeat in front row -- Person 1 compares to person 2, 3 to 4 larger number steps forward – Repeat in front row -- Person 1 compares to person 2 larger number steps forward Comparing the 2 algorithms • Both gave us the largest number • The second approach gave us an easier approach to the second largest number • Lewis Carroll (Rev. C. L. Dodgson) “LAWN TENNIS TOURNAMENTS: THE TRUE METHOD OF ASSIGNING PRIZES with a Proof of the Fallacy of the Present Method.”, 1883, London: Macmillan. Printed in Oxford. 8vo. Websites of the day Kitten version Puppy version Student government version Guess my number • I’m think of a number between 1 and 1000 – You can guess and I’ll tell you if you are high or low • How many queries does it take you to find my number? • What if I was thinking of a number between 1 and 1,000,000? • What if I was thinking of a number between 1 and N (N known = 2n)? • What if I was thinking of a number between 1 and N (N unknown)? Log n algorithms • how do we find a word in a dictionary? – linear search requires looking at all the words • if the words are sorted into alphabetical order, we can use binary search, which is much faster than linear – an example of a "divide and conquer" algorithm • data has to be sorted – have to be able to access any data item equally quickly – "random access" • why is binary search faster than linear searching? – each test / comparison cuts the number of things to search in half • how much faster is it? – the number of comparison is approximately log n for n items 2 Can you search even faster in a dictionary? • If the word starts with the letter A, does it make sense to start the search half way through the dictionary? – Could improve by starting 1/26th of the way through For a search word starting with the ith letter, start i/26 of the way through – Could improve further by having word counts Record the position of the first word starting with each letter and begin your search at that position (or halfway between that position and the position of the next letter) This is why dictionaries historically have had tabs – Repeat this method for the second letter First letter gives you a range of positions, start proportionally in the range based on the second letter A F FE FO G Z Logarithms for COS 109 • all logs in 109 are base 2 • all logs in 109 are integers • if N is a power of 2 like 2m, log2 of N is m • if N is not a power of 2, log2 of N is the number of bits needed to represent N the power of 2 that's bigger than N the number of times you can divide N by 2 before it becomes 0 • you don't need a calculator for these! – just figure out how many bits or what's the right power of 2 • logs are related to exponentials: log2 2N is N ; 2 • it's the same as decimal, but with 2 instead of 10 log N 2 is N Algorithms for sorting • binary search needs sorted data • how do we sort names into alphabetical order? • how do we sort numbers into increasing or decreasing order? • how do we sort a deck of cards? • how many comparison operations does sorting take? • "selection sort": – find the smallest using a variant of "find the largest" algorithm – repeat on the remaining names – this is what bridge players typically do when organizing a hand • what other algorithms might work?