Welcome to CS 1! Professor Tom Cormen, 204 Sudikoff Lab www.cs.dartmouth.edu/~cs1 Graduate TA: Shruti Agarwal Undergraduate course staff: • • • • Michael Aaberg Mazen Ammar Ross Bower Andre Chen Daniel Chen Kyle Dotterrer Ella Dzenitis Irene Feng Isabella Florissi Emily Greene Vivian Hu Mahina Kaholokula Jaki Kimball Gabrielle Kirlew Joshua Lang Justine Lee Courtney Ligh Annie Ma Andrew Meier Lauren Mitchell Tim Rizvanov Anya Rosen Brophy Tyree Kristen Vondrak Lily Xu Problem 1: Who has my Gumby? Algorithm: A precise sequence of simple steps to solve a problem Algorithm 1 Step 1: Ask someone new if he or she has my Gumby. Step 2: If the answer is yes, then stop: I have found my Gumby. Step 3: If the answer is no and everyone has been asked, then stop: nobody has my Gumby. Step 4: Otherwise (the answer is no but there is someone new to ask), go back to step 1. Evaluating an algorithm • • • • Is our algorithm correct? If it gives an answer, are we sure that the answer is right? Is our algorithm complete? Does it always find the answer, if the answer exists? Is our algorithm efficient? How much time will it take to run? In the best case? In the worst case? Could we implement the algorithm as a computer program? Problem 2: What is my friend’s phone number? Algorithm 2: Linear search Step 1: Read a new name in the phone book. Step 2: If it’s my friend’s name, then stop: we found my friend’s name and home phone number. Step 3: If not my friend’s name and all names have been read, then stop: my friend’s phone number is unlisted. Step 4: Otherwise (not my friend’s name but there is a new name to read), then go back to step 1. Problem 2: What is my friend’s phone number? Algorithm 3: Binary search Step 1: Rip the phone book in half. Step 2: Choose the half containing my friend’s name. Discard the other half. Step 3: If what remains is just one page, then search for my friend’s name on that page. Step 4: If what remains is more than just one page, then go back to step 1. Properties of binary search • Would binary search work if the phone book were not sorted by name? • How efficient is this algorithm? How many rips would we need if the phone book contained 2 pages? 4 pages? 8 pages? 16 pages? 32 pages? 256 pages? (Hint: 256 = 28) 1024 pages? (Hint: 1024= 210) 1,048,576 pages? (Hint: 1,048,576= 10242 = 220) Problem 3: How many students shall I tell my colleagues attended CS 1 today? Algorithm 4: Counting in parallel Step 1: Everyone stand up. Think of the number 1. Step 2: Everyone who is standing: find a partner. If an odd number of people are standing, then one person will remain without a partner. Step 3: If you have a partner, then one of you will sit down. Decide between you on who will sit. The one who sits should tell the one who remains standing the number he or she is thinking. Step 4: If you are standing and were told a number, add it to the number you were thinking, and think about this new number. Step 5: If you are the last person standing, report the number you are thinking. Step 6: Otherwise (more than one person is standing), then go back to step 2. Properties of counting in parallel • How many rounds would it take if only 2 people were in the class? • 4 people? • 8 people? • 16 people? • How many rounds did it take? Why?