CS 202 Fall 2011: Assignment 8
Homework Assignment #8 : Due Friday 11/9 before 5pm
Part A: Computational Complexity (3 points)
It is useful to know the complexity complexity of an algorithm in order to know how long that algorithm will take to run on problems of different sizes (i.e., as a function of the parameter N). For each of the following scripts, you will analyze their behavior as a function of the input parameter N (e.g., assume N represents the number of items in an input list). Assume each of the scripts is run independently.
A) To understand the running time of an algorithm, we usually examine the total number of times the inner-most loop iterates, since the inner-most loop is the one that iterates the most. To make the number of times the inner-most loop iterates more clear, in these examples we increment a variable "counter" each time in the inner-most loop; this variable is initialized to 0 only when the program begins.
For each of the scripts, how many times will a statement in the (largest) innermost loop execute? In other words, what will be the value of the counter variable when the script terminates? For those scripts that use the variable N, express counter in terms of N. It is alright if your equation is off by one (i.e., missing a "+ 1") or does not contain floor or ceiling functions.
B) What is the order of magnitude for the number of operations in each algorithm as a function of the variable N? Express your answer in big-O notation. Remember that order of magnitude corresponds to the rate of growth of the function. The fastest growing part of a function determines its order of magnitude. For example, for the function f = 200N, at large values of N the magnitude of the value of f is determined by N, not by the factor of 200; similarly, for f = 3N – 5, at large values of N the magnitude of the value of f depends on N, but the -5 in the equation doesn't change the magnitude of the value significantly. In other words, when calculating the order of magnitude, one disregards all constant factors. Also remember that if an algorithm runs in an amount of time completely independent of the size of N, then that algorithm is O(1).
C) Imagine that you must choose between two different algorithms, A and B, that perform the same task (i.e., the solve the same problem). Algorithm A executes 2 * N * N instructions; algorithm B executes 30000*N instructions. For what values of N is Algorithm A faster? For what values of N is Algorithm B faster? As N gets larger, which Algorithm is the better choice?
Part B: Sorting (2 points)
Sorting is an important algorithm in Computer Science. Selection sort and insertion sort are two intuitive, but slow, sorting algorithms. They are fine algorithms for small data sets (that is, when the value of N is small). These algorithms were covered in detail in Lecture. These two algorithms are straight-forward enough that you should be able to step through their operations by hand.
A) Imagine that you are to use a selection sort (like the one shown above). For each of the following two inputs sets, show the contents of Unsorted List at the end of each iteration of the outermost loop (where the variable i is increased). Note that each input set is a list of 10 elements; therefore, for each input set, you should create a table containing 10 rows (i.e., one row for each value of i).
i) 28, 72, 56, 85, 9, 19, 56, 99, 100, 32
ii) 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100
B) Now imagine that you are to use an insertion sort (such as the one shown above) on the following lists. For each of the following two inputs sets of 10 keys, show the contents of Unsorted List at the end of each iteration of the outermost loop (where the variable i is increased).
i) 28, 72, 56, 85, 9, 19, 56, 99, 100, 32
ii) 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100
Part C: Programming in Python (5 points)
Python is a great programming language for you to have some experience with for a variety of reasons: it is popular, it runs on many different platforms, it lets you do a large range of computational tasks easily, and its structure has many similarlities with other high-level languages (including Scratch).This week, your goal is to translate a number of short, straight-forward scripts in Scratch to Python. We will begin with only a small subset of the Python language; in particular, you will need to know how to
- use variables
- get input from the user (use "raw_input")
- convert strings to integers (use "int")
- print to the terminal (use "print")
- concatenate strings (use "+" instead of "join" in Scratch)
- import math library
- use an if statement (and else and elif)
- use a while loop (similar to a repeat until loop in Scratch)
- define and invoke functions (similar to broadcasting and receiving messages in Scratch)
- pass parameters to functions (similar to setting a variable before broadcasting a message in Scratch)
- Sit at a machine that has Python 2.x installed. The Macs in the CS lab 1370 have Python; you are encouraged to use those computers. Remember how to activate your CS account! If you prefer to use your own laptop, you may need to install Python which is available from here.
- Open up a text editor. To write code, you need to use a text editor (and not a word processor like MS Word) that will save the words you type as plain ASCII text and not with special formatting commands (e.g., bolds and titles). Text editors often have built-in understanding of the syntax of different languages to ease programming; for example, if you save your python files with the extension ".py", the text editor will know to highlight key words like "if" and "def" and will correctly tab or space lines that should be indented. You can use a text editor like "emacs" or "gedit".
- Read/skim through and type in some of the examples in this wonderful tutorial: Think Python: How to Think Like a Computer Scientist. You can probably skip Chapters 4 (Case Study: Interface Design) and 6 (Fruitful Functions) and stop after Chapter 7 for this homework. You definitely should type in (some of) their examples to really make sure you are understanding. For experimenting, you can run python in interactive mode (by running "python" with no arguments) and then just type commands directly to the python interpreter. For longer examples, you'll want to use your text editor to compose your program and then have python execute your program (by typing "python filename.py"). The one command the tutorial doesn't seem to introduce is "raw_input('some question')" which you will use instead of the "ask" block in Scratch.
To show you've learned some basic Python, you should turn in FOUR simple Python programs that match the behavior of the four Scratch scripts shown above. You can find the Scratch code here. You should make each Scratch script into its own python program. In other words, each program should be placed in its own file with a file name extension of ".py" (e.g., "name.py", "age.py", "song.py").
You should follow the format of the Scratch code as closely as you can in Python. For example, you should use a variable in Python every time the Scratch script uses a variable; you should concatenate strings in the same places; you should use while loops where the Scratch scripts used repeat loops; you should call functions in the places where the Scratch scripts broadcasted messages.
Please communicate with the instructor and TAs if you have difficulties with Python. You need to learn how to accomplish each of these tasks so that you can proceed through the steps in the next homeworks! Remember office hours!
Turning in your Homework
You should turn in this assignment through your Learn@UW account.Menu
Fall 2012Time: TuTh 9:30-10:45
Room: 1325 CS
Lab: 1370 CS (1st floor)
Instructor:
Prof Andrea Arpaci-Dusseau
Office Hours
TuTh 10:45-12:00
Office:
7375 Computer Sciences
Email: dusseau "at" cs.wisc.edu
Teaching Assistant:
Benjamin Bramble
Lab Hours (CS 1370)
Wed 2:00-4:00
Teaching Assistant:
Sharad Punuganti
Lab Hours (CS 1370)
Thu 1:30-3:30