Algorithms and Data Structures: Java Programming Assignment - Shuffling Integer Lists, Assignments of Computer Science

Instructions for a java programming assignment focused on implementing an integer list abstract data type (adt) and shuffling lists using permutations. Students are required to write a java program with two files: shuffle.java and list.java. The client module, shuffle.java, uses the list adt to apply permutations and shuffle lists. The list adt is a double-ended queue with a current-position marker. Students must read permutations from an input file, shuffle the list using the given permutation, and print the arrangement representation of the permutation along with its order in the output file.

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-9xf
koofers-user-9xf 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMPS 101
Algorithms and Abstract Data Types
Summer 2006
Programming Assignment 1
Due Wednesday July 5, 10:00 pm
Introduction
The purpose of this assignment is threefold: to make sure everyone is up to speed with Java, to practice
modularity and ADTs, and to implement an Integer List ADT which will be used (perhaps with minor
modifications) in future programming assignments. You should therefore test your ADT carefully, even
though all of its features may not be used here.
In this project you will write a Java program which performs shuffles (i.e. permutations) on lists of
integers. Observe that there are many ways to shuffle a given list of integers. For example the list (1 2 3)
can be shuffled in 6 distinct ways: (1 2 3), (1 3 2), (2 1 3), (2 3 1), (3 1 2), (3 2 1). In general a list of
length n has !n arrangements or permutations. Formally, a permutation of a set S is a bijection (i.e. a one-
to-one onto mapping) from S to S. From now on we take S to be the set } , ,2 ,1{ nS
=
. One way to
denote a permutation of S is to simply list its elements twice, side by side, showing the image of each
element under the permutation. For instance, let 5
=
n and write
=25413
54321
σ
to stand for the mapping SS
:
σ
which takes 31
, 12
, 43
, 54
, and 25
. Such a
mapping can be composed with itself to obtain another bijection 2
σσσ
= from S to S. One may verify
in this case that
=12534
54321
2
σ
.
Composing again we see
=31245
54321
3
σ
,
=43152
54321
4
σ
, and
=54321
54321
5
σ
.
Observe that 5
σ
is the identity mapping, which takes
i
i
for all Si
. The order of a permutation
σ
is the smallest positive integer k such that identity=
k
σ
. Thus in the above example, the order of
σ
is 5.
In this project you will compute the order of a permutation by applying it repeatedly to a list (i.e. shuffling
the list) until the list is brought back into its original order.
Our notation for permutations is still somewhat cumbersome. If we fix an ordering of S (such as
increasing numerical order), then we can leave out the top row entirely. The permutation
σ
in the
preceding example is then written as )25413(=
σ
. In this way any arrangement of the elements
of S is understood to denote the permutation which takes the standard arrangement (i.e. the one in
increasing order) into the given arrangement. We will call this method for specifying permutations the
arrangement representation.
There is yet another way to specify a permutation of } , ,1{ nS
=
as a list of n integers. In this notation a
list is interpreted to be a set of instructions for transforming the standard arrangement of S into a given
pf3
pf4
pf5

Partial preview of the text

Download Algorithms and Data Structures: Java Programming Assignment - Shuffling Integer Lists and more Assignments Computer Science in PDF only on Docsity!

CMPS 101

Algorithms and Abstract Data Types Summer 2006

Programming Assignment 1

Due Wednesday July 5, 10:00 pm

Introduction The purpose of this assignment is threefold: to make sure everyone is up to speed with Java, to practice modularity and ADTs, and to implement an Integer List ADT which will be used (perhaps with minor modifications) in future programming assignments. You should therefore test your ADT carefully, even though all of its features may not be used here.

In this project you will write a Java program which performs shuffles (i.e. permutations) on lists of integers. Observe that there are many ways to shuffle a given list of integers. For example the list (1 2 3) can be shuffled in 6 distinct ways: (1 2 3), (1 3 2), (2 1 3), (2 3 1), (3 1 2), (3 2 1). In general a list of length n has n! arrangements or permutations. Formally, a permutation of a set S is a bijection (i.e. a one- to-one onto mapping) from S to S. From now on we take S to be the set S = { 1 , 2 ,, n }. One way to

denote a permutation of S is to simply list its elements twice, side by side, showing the image of each element under the permutation. For instance, let n = 5 and write

to stand for the mapping σ : S → S which takes 1 → 3 , 2 → 1 , 3 → 4 , 4 → 5 , and 5 → 2. Such a

mapping can be composed with itself to obtain another bijection σ  σ= σ^2 from S to S. One may verify

in this case that

 

Composing again we see (^)  

σ , and 

Observe that σ 5 is the identity mapping, which takes i → i for all i ∈ S. The order of a permutation σ

is the smallest positive integer k such that σ k =identity. Thus in the above example, the order of σ is 5.

In this project you will compute the order of a permutation by applying it repeatedly to a list (i.e. shuffling the list) until the list is brought back into its original order.

Our notation for permutations is still somewhat cumbersome. If we fix an ordering of S (such as

increasing numerical order), then we can leave out the top row entirely. The permutation σ in the

preceding example is then written as σ =( 3 1 4 5 2 ). In this way any arrangement of the elements

of S is understood to denote the permutation which takes the standard arrangement (i.e. the one in increasing order) into the given arrangement. We will call this method for specifying permutations the arrangement representation.

There is yet another way to specify a permutation of S = { 1 ,, n }as a list of n integers. In this notation a

list is interpreted to be a set of instructions for transforming the standard arrangement of S into a given

arrangement. To distinguish this representation from the arrangement representation, we will surround the list with square brackets [ ] rather than round brackets ( ). We refer to this scheme as the operator representation of a permutation, and illustrate with the following example. The list [1 2 1] instructs us to place elements from the standard arrangement (1 2 3) into an initially empty list ( ) in three steps:

  1. Place element 1 in position 1: (1)
  2. Place element 2 in position 2: (1 2)
  3. Place element 3 in position 1: (3 1 2)

The result is the arrangement (3 1 2). Similarly [1 1 3] applied to (1 2 3) gives (2 1 3). Below are the 6 permutations of S = {1, 2, 3} written in both operator and arrangement representation.

[1 2 3] (1 2 3) [1 2 2] (1 3 2) [1 1 3] (2 1 3) [1 1 2] (2 3 1) [1 2 1] (3 1 2) [1 1 1] (3 2 1)

Observe that in the operator representation of a permutation, the integer at position i must be in the range 1 to i , since it is literally an instruction to insert something into a list of length i − 1. Thus [1 3 2] is not a valid operator representation of any permutation. (What would it say? Starting with an empty list, insert 1 into position 1 to get the list (1), then insert 2 into position 3 to get …? But inserting anything into the list (1) results in a list of length 2, which therefore has no position 3.) One nice thing about the operator representation of a permutation is that it can be easily applied to any list of length n , not just the standard arrangement of S. For instance [1 2 2] applied to (a b c) gives (a c b), and [1 1 1] applied to (x y z) gives (z y x). The reader is urged at this point to write out all 24 permutations of S = {1, 2, 3, 4} in both

representations. Also check that the permutation σ from the preceding example, which was given in

arrangement representation as σ =( 3 1 4 5 2 ), has the operator representation σ =[ 1 2 1 3 4 ].

Program Operation Your program will be structured in two files: a client module called Shuffle.java, and a List ADT module called List.java. Each file will contain one top level class, Shuffle and List, respectively. The client Shuffle will use List variables in two ways. On the one hand, a List will represent a permutation in operator representation. Such a List will itself be made to operate on a second List by splicing and dicing according to the ‘instructions’ in the first List. These shuffling operations will be performed in the client by calling the methods in the List module. Shuffle will be invoked at the command line by doing: Shuffle input_file output_file. Notice that one does not type java Shuffle at the command line. A Makefile is included at the end of this handout which places all .class files for this project in an executable jar file called Shuffle, making it possible to leave out java when invoking the program.

The input file will contain a number of permutations (of various sizes) in operator representation. For each such permutation your program will do the following.

  1. Read the permutation from the input file and store it in a List P.
  2. Initialize a List L consisting of the integers (1 2 … n ), where n is the length P.
  3. Shuffle L once by applying the permutation P. The list L now gives the arrangement representation of the permutation. Print L to the output file.

int getLast() // Returns last element. Pre: !isEmpty(). int getCurrent() // Returns current element. Pre: !isEmpty(), !offEnd(). int getLength() // Returns length of this List. boolean equals(List L) // Returns true if this List has same elements as L in the // same order. Ignores the current marker in both Lists.

// Manipulation Procedures void makeEmpty() // Sets this List to the empty state. Post: isEmpty(). void moveFirst() // Sets current marker to first element. // Pre: !isEmpty(); Post: !offEnd(). void moveLast() // Sets current marker to last element // Pre !isEmpty(); Post: !offEnd(). void movePrev() // Moves current marker one step toward first element. // Pre: !isEmpty(), !offEnd(). void moveNext() // Moves current marker one step toward last element. // Pre: !isEmpty(), !offEnd(). void insertBeforeFirst(int data) // Inserts new element before first element. // Post: !isEmpty(). void insertAfterLast(int data) // Inserts new element after last element. // Post: !isEmpty(). void insertBeforeCurrent(int data) // Inserts new element before current element. // Pre: !isEmpty(), !offEnd(). void insertAfterCurrent(int data) // Inserts new element after current element. // Pre: !isEmpty(), !offEnd(). void deleteFirst() // Deletes first element. Pre: !isEmpty(). void deleteLast() // Deletes last element. Pre: !isEmpty(). void deleteCurrent() // Deletes current element. // Pre: !isEmpty(), !offEnd(); Post: offEnd()

// Other methods List copy() // Returns a new list which contains the same elements as this List, and // in the same order. The current marker in the new list is undefined, // regardless of the state of the current marker in this list. The state // of this list is unchanged. public String toString() // Overrides Object's toString method and returns a string // representation of this List consisting of a space // separated list of integers. public static void main(String[] args) // Used as a test driver for the List class.

The above operations are required for full credit, although it is not expected that all will be used by the client module in this project. The following operation is optional, and may come in handy in some future assignment:

List cat(List L) // Returns a new List which is the concatenation of this list // followed by L. The current marker in the new list is undefined, // regardless of the states of the current markers in the two lists. // The states of the two Lists are unchanged.

Your List class should contain a private Node class which encapsulates one List element. This private class should contain fields for an int (the value stored at that node), a Node (the previous element in the list), and another Node (the next element in the list). It should also define an appropriate constructor, as well as a toString() method. The List class should contain private fields of type Node which refer to the first, last, and current Nodes in the List.

Makefile The following Makefile creates an executable jar file called Shuffle. Place it in a directory containing List.java and Shuffle.java, then type gmake to compile your program.

Makefile for CMPS 101 pa1 Summer 2006.

MAINCLASS = Shuffle JAVAC = javac JAVASRC = $(wildcard .java) SOURCES = $(JAVASRC) makefile README CLASSES = $(patsubst %.java, %.class, $(JAVASRC)) JARCLASSES = $(patsubst %.class, %.class, $(CLASSES)) JARFILE = $(MAINCLASS)

all: $(JARFILE)

$(JARFILE): $(CLASSES) echo Main-class: $(MAINCLASS) > Manifest jar cvfm $(JARFILE) Manifest $(JARCLASSES) chmod +x $(JARFILE) rm Manifest

%.class: %.java $(JAVAC) $<

clean: rm *.class $(JARFILE)

Note that this Makefile will compile all .java files in your current working directory. Also be aware that if you are using the bash shell and you type make (instead of gmake), this makefile may not work properly. To be safe always use gmake. You may of course alter this Makefile as you see fit to perform other tasks (such as submit), but the Makefile your turn in must make an executable jar called Shuffle, and must include a clean utility.

You must also submit a README file for this (and every) assignment describing the files created for the assignment, their purposes and relationships, along with any special notes to myself and the grader. README is essentially a table of contents for the project. Each file you turn in must begin with your name, user id, and assignment name. Thus, for this project you are to submit four files in all: List.java, Shuffle.java, Makefile, and README.

Advice Start early and ask questions if anything is unclear. It is helpful to write simple test programs to make sure you understand each part of the problem. The main method in your List class is required because it is much easier to debug your List ADT in isolation before you use it in the Shuffle class. You should first design and build your List ADT, test it thoroughly, and only then start coding your Shuffle class. Information on how to turn in your program is posted on the webpage.