Project 2: Algorithms in Pseudocode Assigned: Wednesday February 6th Due: Friday February 15th 11:00PM Project type: individual This project contains 5 problems. Each problem is weighted equally. Each problem has multiple questions, and each question is weighted equally. Instructions 1. You have to write your answers in THIS file. You can write your answers to each problem question right after the corresponding question, or you can write them altogether starting at a new page in the end of this document. In this case, remember to use Qxx ANSWER at the beginning of your answers to question xx. Turnin instructions You will turn-in your project using the turn-in command you used in the labs. Hence, on your UNIX account on the lore machine, create a directory project2 using the following commands: $ cd $ cd CS177 $ mkdir project2 To submit your project2 from the LWSN lab, refer to the instructions you got in Lab2. For project2, enter the following commands on your lore account: $ cd $ cd CS177 $ turnin –v -c cs177=COMMON -p project2 project2 1|Page of 7 CS177 Spring 13 –Project2 IMPORTANT – WORKING FROM HOME: You can also work at home and then transfer remotely your project1 (i.e. THIS file with your answers) to your UNIX account, and then turn-in it using the instructions above. Please read the document http://courses.cs.purdue.edu/_media/cs17700:spring13:remoteturnin-s13.pdf. Please note that this document describes how to turn-in a python file. However you can transfer and turn-in a MS Word file too. 2|Page of 7 CS177 Spring 13 –Project2 1. Reversing a 1-D array. Given an array A of length n, write an algorithm that outputs an array B that has the elements of A in reverse order. For example, if A = {2, 3, 0, 1, 5}, (n is 5), then B should be {5, 1, 0, 3, 2}. Hint: the algorithm traverses B and sets each element to the corresponding element in A. a. Give a brief step by step description of the algorithm. b. Give a pseudo-code description of the algorithm. c. Trace the algorithm for A = {1, 3, 2, 4, 0}, n = 5. d. What is the algorithm running time? Explain. 3|Page of 7 CS177 Spring 13 –Project2 2. Searching for a given string in an array. Given an array A of length n that stores characters and an array B of length m that also stores characters, write an algorithm that prints out “yes” if B is found in A and “no” if B is not found in A. For example if A is “Today was a cold day.” and B is “old”, the algorithm should print out “yes”, as B is a substring of A. Hint: traverse A from the beginning and for each element (i.e. letter) in A check if B is in A starting at that element. a. Give a brief step by step description of the algorithm. b. Give a pseudo-code description of the algorithm. c. Trace the algorithm for A = “black cat”, n = 9, and B = “at”, m = 2. d. What is the algorithm running time? Explain. 4|Page of 7 CS177 Spring 13 –Project2 3. Mirror along a vertical axis. Given the following image of size wxh (e.g. 7 rows by x 6 columns), give an algorithm that mirror the image along a vertical axis. 10 10 20 15 15 30 30 20 30 40 25 25 40 40 30 35 40 18 18 50 50 40 20 10 18 18 11 11 50 30 20 15 15 13 13 60 35 10 15 15 12 12 Input image a. b. c. d. What is the output of the algorithm on the following image? Give a brief step by step description of the algorithm. Give a pseudo-code description of the algorithm. What is the algorithm running time? Explain. Hint: The visual effect of mirroring an image along a vertical axis is depicted below: Input image 5|Page of 7 Output image CS177 Spring 13 –Project2 4. Summing up values stored at the leafs of a binary tree. CountBTR is a recursive algorithm for counting the nodes in a binary tree, see lecture notes. Modify the algorithm to compute the sum of all the numbers stored at the leafs of a binary tree T. T stores a number at each node. Hint: a node doesn’t count as 1 anymore, but rather as 0 if the node is an internal node, or as V if the node is a leaf, where V is the number stores at the leaf. a. Give a brief step by step description of the algorithm. b. Give a pseudo-code description of the algorithm. c. What is the algorithm running time, assuming that the binary tree stores n nodes? Explain. 6|Page of 7 CS177 Spring 13 –Project2 5. Fast searching in sorted array. Given an array of integers A sorted in descending order and an integer B, describe a fast algorithm that finds if B is in A, where n is the length of A. Hint: check B against the middle element of A, A[n/2]; if B is greater than A[n/2], continue your search in the left half of A, else continue your search in the right half of A. a. Give a high-level step-by-step description of algorithm. b. Give a pseudo-code description of the algorithm. c. Trace the algorithm for A = {1, 3, 4, 6, 7, 7, 9, 12, 19, 22, 30, 100}, B = 1, then for B = 100, then for B = 9. d. Explain why the running time is log2n. Hint: how many times can a number be divided by 2 before you get 1? If log2n = a, then n = 2a. 7|Page of 7 CS177 Spring 13 –Project2