ICS 16OE/ECE 144 - Algorithms Homework 3, 25 Points
Due: Monday, April 21, 2003
-
5 points.
Problem R-2.13 from Goodrich-Tamassia.
-
5 points.
Problem R-2.18 from Goodrich-Tamassia.
-
5 points.
Problem C-2.15 from Goodrich-Tamassia.
-
5 points.
Problem C-2.31 from Goodrich-Tamassia.
-
5 points.
In the children's game hot potato,
a group of n children sit in a circle passing an object, called the
"potato," around the circle (say in a clockwise direction).
The potato begins with a starting child in the circle, and
the children continue passing the potato until a leader rings a bell,
at which point the child holding the potato must leave the game after
handing the potato to the next child in the circle.
After the selected child leaves,
the other children close up the circle.
This process is then continued until there is only one child
remaining, who is declared the winner.
Using a java.util.LinkedList, write a small Java function,
Josephus(L,k), which takes a LinkedList L (of instances
of the Object class) and an integer k, and
returns the object in L that would win in the hot potato game where
L is viewed as a circular sequence (that would wrap around the end
to go back to the beginning) and the leader
always rings the bell immediately after the potato has been
passed k times.
(Determining the last child remaining in this variation of hot potato
is known as the Josephus problem; hence the name for the function.)
Assume the game begins with the the hot potato being at the first
object in L.
So, for example,
Josephus((1,2,3,4,5),3) is 1, and
Josephus((1,2,3),2) is 2.
Turn in a listing of your Josephus program and
show the output of
Josephus(L,k) on each of the following:
- L=(Alice,Bob,Charles,Darwin,Ed,Fred,George,Holly,Irene), k=5
- L=(USA,Iraq,Syria,Iran,France,Germany,England), k=8
- L=(Mike), k=2
- L=(23,34,104,1025,2,15,27), k=4
- L=(2.3,3.4,10.4,10.25,2.2,1.5,2.7), k=6
Note that the first three of the above are lists are strings, whereas the
fourth is a list of integers, and the last one is a list of floats.
Rule: This problem must be done independently. You cannot work with
someone else on problem 5!
Hint: study the class
java.util.LinkedList.
You may also wish to look
at the classes
java.lang.String,
java.lang.Integer,
and
java.lang.Float.