Comp1211: Algorithms Worksheet Purpose (a) To understand the concept (“idea”) of an algorithm (b) To investigate some algorithms, such as search and password cracking, (c) To help you understand what programming (“coding”) is all about, (d) To introduce you to the concept and activity of “Computational Thinking”. Important Tasks to Complete. You should all complete tasks 1,2,3 then 6,7,8,9. You are strongly advised to complete all other tasks during your independent study time, since they will help you both with your assignment and other modules on your degree course. That’s why I wrote them Tasks 1 The Sequential Search Algorithm. Let’s take the following list of names and apply the sequential search algorithm using pencil and paper. (Note you can refer to each name using only its first letter). Alan, Betty, Carol, David, Edward, Fred, George (a) How many comparisons do you need to find Edward? (b) What is the smallest number of comparisons you may need to find a given name? (c) What is the largest number of comparisons you may need to find a given name? (d) How many comparisons do you need to locate Zara ? 2 Binary Tree Search Algorithm. Construct a binary tree using the names in Task1. You may use the first letter of the name. The binary tree should have the following structure; you must decide which name goes where. Now answer the following questions (a) How many comparisons do you need to find Edward? (b) What is the smallest number of comparisons you may need to find a given name? (c) What is the largest number of comparisons you may need to find a given name? (d) How many comparisons do you need to locate Zara ? 3 Computational Thinking. You have three colored balls in three containers as shown below on the left. You must move the balls to arrive at the arrangement on the right. There are four rules Rule 1. You can only move one ball at a time Rule 2. The balls must always be inside a container Rule 3. You can stack balls on top of each other Rule 4. You can only move the top ball in a stack Show each stage in your solution. Now repeat for the case shown below You may wish to Google the “Tower of Hanoi” problem. 4 Password Cracking. Assume a system accepts passwords using only lower-case characters a…z. Let’s consider the number of possible passwords for strings of several character lengths. Each character can be drawn from the set a..z. (a) How may passwords can be written with only 1 character? (b) How many passwords can be written with 2 characters? (c) What is the rule for calculating the number of passwords which can be written with N characters? Consider a password of length 15 characters. (d) How many possible passwords are there? (e) If a comparison of the real password and the cracking password takes 0.0000001 seconds on a 3GHz Core processor, how many seconds does it take to check all possible passwords? (f) Express your answer to (e) as days and as years. What do you find? 5 Password Cracking (continued). Grab the program PasswordCracker.exe from the module webpages. This program will do a brute force attack, trying out all possible passwords. (a) Run the program for passwords of lengths from 1 to 8. Note down the time needed to crack each password. (b) Draw a plot of time needed versus password length. You could use Excel. What shape does this plot have. What does this tell you? 6 Pseudo-code (Sequence). Here we shall see that the sequential order of statements is important. (a) You have the following words: “fairy”,” like”, “cakes”,”I”. Write these words in a sequence (a vertical column) so that they make sense. (b) Now consider the following sentences. “Pete is mortal” ,“All men are mortal”, “Pete is a man” in a sequence (a vertical column) to make a logical argument. (c) You have the following statements intended to add the values of a and b and print out the result. They are not written in the correct order write the result a=5 add a to b b=4 Write out the sequence in the correct order. 7 Pseudo-code (Sequence + Selection). You are designing a simple game where a car (C) moves to the right along a sequence of cells and when it reaches a cell with a tree (T) in it, the car stops. C T (a) Write out a sequence of pseudo-code statements to make this happen. You may wish to use expressions like these move car …, cell contains …. But to test when the car hits the tree, you must use pseudo-code which looks like this IF …. THEN …. Note you are not allowed to jump back to any previous statement or to loop. Please assign line numbers to your statements. (b) You are now allowed to use the pseudo-code expression goto X where X is a line number. Now re-write your pseudo-code using this expression. 8 Pseudo-code (Sequence + Selection + Iteration). Let’s explore one iteration construct written in pseudo-code like this, on the left. One example is shown on the right repeat the following statements statement1 statement2 … until condition is true. repeat increase count by 1 until count is 5. (a) Let’s say that in the above example, the value of count was 2 before the repeat – until loop. How many times does the loop run before it halts? (b) Write some pseudo-code to do the following. First set the value of a to 5 and the value of b to zero. Then put the value of a into b. However you are only allowed to use the following inside the loop: “add 1 to x”, “subtract 1 from x” (where x is a or b.). You may not say “set the value of b equal to the value of a” (c) Rewrite your pseudo-code for Task 7. 9 Pseudo-code Problems. Look at the following pseudo-code. Work out what it does. Look carefully at the value of b when the pseudo-code halts. set the value of a to 5 set the value of b to 0 repeat the following statements add 2 to the value of b subtract 1 from the value of a until the value of a is 0 What would happen if you swapped the order of the statements inside the loop? 10. Life is Algorithmic. Select one of the scenarios below and write an algorithm for it in pseudo-code using sequence, selection and iteration. (a) What to do when your car engine does not start. (b) What to do if your computer does not start (c) The behaviour of a football player during a game (d) Identify something you do every day without thinking about it. Whitehead calls these “algorithms”. 11. Algorithm Execution Time. You are given two executable files, “Quicksort.exe” and “Bubblesort.exe”. These programs run both sorting algorithms for a variable number of random numbers and record the time each run takes. The time is given via two ways (i) using the clock() function of C, and also by counting the number of machine cycles. Run both algorithms for various numbers of numbers and record the times taken and the number of machine cycles. For bubble sort you may want to try n from, 1000 to 150,000, for quicksort, I suggest 50,000 to 20,000,000. There’s already a hint! Plot the results from each algorithm using Excel on individual graphs. Now plot the data available for the same n from each algorithm on the same graph. What do you notice?