To get a better understanding of the technical definitions of asymptotic complexity (big O and friends).
Unlike other assignments, this is mostly a problem set. There is one part of it that involves programming. Work on this assignment individually.
As always, you will submit your work on Moodle. This means that you will need to create a PDF of your solutions to the written exercises. You have a few options for this:
Writeups that are illegible, because of either handwriting or non-conforming file formats, will not be accepted for credit.
Answer the following questions:
for(int pass = 1; pass <= n; pass++) { for(int index = 0; index < n; index++) { for(int count = 1; count < 10; count++) { .... } } }
The algorithm involves an array of $n$ items. The previous code shows the only repetition in the algorithm, but it does not show the computations that occur within the loops. These computations, however, are independent of $n$ (in other words, they are “constant-time” operations). What is the asymptotic order of the algorithm?
(Book project 4.7, slightly altered) In mythology, the Hydra is a monster with many heads. Every time a hero chops off a head, two smaller heads grow in its place. Fortunately for the hero if the head is small enough, it does not grow back when chopped off. To kill the Hydra, all that the hero needs to do is to chop off all the heads.
Write a program that simulates the Hydra, in a class Hydra
. Instead of heads, we will use strings. A stack of strings, then, represents the Hydra. Every time you pop a string from the stack, delete the first letter of the string and push two copies of the remaining string back onto the stack. For example, if you pop the string HYDRA
, you then push two copies of the string YDRA
to the bag. If you pop a one-letter word, don't push anything onto the stack; one-letter heads get completely destroyed. To begin the program, push a single word (passed in as a command-line argument) onto the empty stack. The Hydra dies when the stack is empty.
If you printed each “head” that you chopped off, then a sample run of the program might look like this:
> java Hydra ABC Starting Hydra with "ABC" Chopping head ABC Chopping head BC Chopping head C Chopping head C Chopping head BC Chopping head C Chopping head C The Hydra is slain!
You'll need the following files for the Stack ADT:
In your writeup, along with your answers to the exercises above, include your answers to the following questions:
Create a zip file called hw9.zip containing your writeup (which must be a PDF file, no exceptions!) and your Hydra.java file. Submit the zip file on Moodle.
Start early, ask lots of questions, and have fun!