Introduction to Algorithms in Java
Total Page:16
File Type:pdf, Size:1020Kb
Algorithms in Java: Shuffling and Random Numbers Algorithms Introduction Itʹs impossible to study programming – or even talk in depth about it – without talking about algorithms. Theyʹre an essential part of programming. Fortunately, all of us – programmers and non‐programmers alike – have experience with algorithms. Every time you give someone driving directions; every time you use a recipe to cook; every time you follow instructions for using a piece of software – all of these activities (and countless others) involve algorithms. So what is an algorithm? Simply put, an algorithm is a set of instructions, intended to solve a specific problem or accomplish a specific, finite goal, in a direct fashion. What Makes an Effective Algorithm? In general, ambiguity doesnʹt make for a good algorithm; this is particularly true in programming, because computers are mostly incapable of knowing what we want them to do, unless we spell it out precisely. So the algorithms we use should also be precise – even before we translate them into a programming language. An effective algorithm usually has the following characteristics: • Clear conditions of application: What task is the algorithm intended to accomplish, and what are the requirements for us to be able to use it? • Initialization steps & starting conditions: What do we do to prepare our data for the algorithm? How can we tell if weʹre ready to proceed? • Explicit stopping conditions and states: How do we know when weʹre done? How do we know whether we met the desired goal? • Unambiguous steps that lead from the starting conditions to the stopping conditions. Note that “unambiguous” is a subjective term. No matter how careful we are, there will always be details left out that the writer assumes are understood. This is a problem in some cases, but not others. For example, a complicated recipe will certainly omit definitions for terms assumed to be understood by an experienced chef; if the recipe isnʹt intended for beginners, including such definitions would be a waste of space. Java Tutorials Nick Bennett, Roger Critchlow, Janet Penevolpe Shuffling: The Flip Side of Sorting The shuffling problem is easily stated: How can we rearrange a list of items so that the order is random and fair? Fortunately, this isnʹt a very difficult problem – though itʹs easy to do it incorrectly. There are two commonly used approaches to shuffling in computer programs: Sorting on a random number 1. Assign a randomly generated value to each item to be shuffled. 2. Sort the items, in order of the assigned random numbers. 3. Stop. The list is now shuffled. Though the need for sorting makes this method less efficient than the one that follows, itʹs very easy to implement in some languages and environments – for example, this can be a good approach for retrieving and presenting database records in a random order. Durstenfeldʹs version of the Fisher‐Yates shuffle (aka Knuth shuffle) 1. Begin with the list of items X , containing the N items x1, x2, , x N . 2. For each integer value j , starting at 1 and ending with N −1 , do the following: a. Generate a random value k , which can be any one of { j , j1, , N } ; each of the values must be equally likely to be selected. b. Exchange the positions of xk and x j in the list. Note that itʹs possible that k = j ; obviously, no exchange is needed when thatʹs the case. 3. Stop. The list is now shuffled. As we repeat steps 2a‐b with values of j from 1 to N −1 (such repetition is called iteration), each randomly selected item is shuffled by moving it to the start of the unshuffled items, trading places with the item already there. In effect, the list is divided into two parts, one shuffled and one unshuffled, with the latter growing as the former shrinks, until no unshuffled items remain. Exercise 1: Shuffling a List of Six Items by Rolls of a Die Using a six‐sided die to generate random numbers, letʹs shuffle the words “apple”, “banana”, “chile”, “donut”, “egg”, and “flan” into a random order. To help us keep track of the items as we shuffle them, weʹll use tables 1‐3. Algorithms in Java: Shuffling and Random Numbers Page 2 Items (N = Number of Items = 6) Position (i) Iteration Roll (j) (k)123456 Initial apple banana chile donut egg flan 1 2 3 4 5 Table 1: Example of Fisher‐Yates shuffle, initial state We start at row 1 – i.e. the row where 1 appears in the Iteration (j) column – and roll the die to get a value of k . For example, assume we roll a 4. We write this value in the Roll (k) column. Next, we copy the six words in our list from the previous row to the current row. As we do so, we exchange the item in column 4 (since that was our roll) with the first unshuffled item, which is in column 1; thus we exchange “donut” with “apple”. The result appears in table 2. Items (N = Number of Items = 6) Position (i) Iteration Roll (j) (k)123456 Initial apple banana chile donut egg flan 1 4 donut banana chile apple egg flan 2 3 4 5 Table 2: Example of Fisher‐Yates shuffle, after 1 iteration (The line that zig‐zags through the table divides the list into shuffled and unshuffled items. In our example, “donut” is now shuffled, and will stay in column 1.) In each successive iteration (a repeated set of steps in an algorithm), we move down one row (increasing the value of j by 1) and roll the die. If the roll is less than the value in Algorithms in Java: Shuffling and Random Numbers Page 3 the Iteration (j) column, we keep rolling until we get a value greater than or equal to j ; then, we write our roll in the Roll (k) column. Finally, we copy the items from the previous row to the current row, exchanging the items in columns j and k . Assume that in iteration 2, we roll a 1. However, because 1 is less than the current value of j , we roll again; this time, we get a 3, so we write that value in the Roll (k) column. Then, we copy our six items from the previous row to the current row, exchanging the item in column k (or 3), “chile”, with the item in column j (or 2), “banana”. Our randomly selected item, “chile”, is now shuffled. Items (N = Number of Items = 6) Position (i) Iteration Roll (j) (k)123456 Initial apple banana chile donut egg flan 1 4 donut banana chile apple egg flan 2 3 donut chile banana apple egg flan 3 4 5 Table 3: Example of Fisher‐Yates shuffle, after 2 iterations Complete this shuffle on your own, by performing iterations 3‐5. Exercise 2: Additional Questions and Tasks I. What is a fair shuffle? Does random always imply fair? II. If each iteration of steps 2a‐b of the Fisher‐Yates algorithm shuffles one item in the list, how is it that weʹre able to shuffle 6 items in 5 iterations? More generally, how are we able to shuffle all N items in N −1 iterations? III. In any given iteration of steps 2a‐b of the Fisher‐Yates shuffle, itʹs possible that item j will trade places with itself. Even if we donʹt need to do anything to exchange an item with itself, it might seem inefficient to allow this to happen. Would eliminating this possibility, by limiting our random selection to the range from j1 to N , still give us a complete, fair shuffle? IV. Is there any value in performing a Fisher‐Yates shuffle multiple times in a row, similar to the way we would manually shuffle a deck of cards several times? Algorithms in Java: Shuffling and Random Numbers Page 4 Shuffling with Java: Lotteries Introduction Lotteries have existed – legally and illegally, and with many variations – for over 2000 years. In the U.S.A., lotteries were illegal for most of the 20th century, but by the 1960s, some states were modifying their constitutions to allow state lotteries. 43 states now run (or participate in) lotteries, using them as a source of state revenues. All of these lotteries set the payoffs so that the total amount paid to the winners is always much less than the total amount paid in, giving the state an unbeatable advantage. In most of the state lotteries, the drawing of numbers is done with actual balls in a container. However, some lotteries are conducted electronically; also, state lotteries usually offer some form of “quick pick” option, letting players buy lottery tickets with the numbers selected at random by computer. So weʹll write a Java program that generates our own lottery draws or quick picks. (The next three exercises assume the DrJava development environment. Other IDEs can be used to write and compile the code in exercises 3 and 5, but exercise 4 depends on the interactive features of DrJava.) Exercise 3: Writing a Lottery Program in Java One of the first things we need to do, when writing any Java program, is decide what classes we need. In Java, classes are units of Java code, which generally perform one or more of three primary roles: • A set of related methods (named procedures that perform specific tasks) that operate on variables of the basic Java data types, or object types defined by other classes. • A set of methods to manage the execution (and termination) of Java programs, applets, etc. • An encapsulation of the attributes and behaviors of a type of object, often corresponding to a physical or logical object related to the problem weʹre working on.