Assignment 9
Assignment 9
Instructions: Your assignment should represent your own effort. However, you
are not expected to work alone. It is fine to discuss the exercises and try to find
solutions together, but each student shall write down and submit his/her solutions
separately. It is good academic standard to acknowledge collaborators, so if you
worked together with other students, please list their names.
Be prepared to present your solution at the lab. If you are not able to explain your
solution, this will be considered as if you had not done your work at all.
You can write up your answers by hand (provided your handwriting is legible) or
use a word processing system like Latex or Word. Experience shows that Word is
in general difficult to use for this kind of task.
For a programming task, your solution must contain (i) an explanation of your
solution to the problem, (ii) the Java code, in a form that we can run it, (iii) in-
structions how to run it. Also put the source code into your solution document. For
all programming tasks, it is not allowed to use any external libraries (“import”)
or advanced built-in API functions (for example, String.indexof("a") or
String.substring(1,5)), if not stated otherwise.
Please, include name, matriculation number and email address in your submis-
sion.
For instance, on that 6×5 climbing grid, R(1, 3) = 3 and the climber can move
to (2, 2), (2, 3), (2, 4), each having a cost of 7, 1, 9, respectively. For a climbing
path from the bottom to the top, the danger rating is the maximum of all values
on the blocks that make up the path.
1. Write a recursive algorithm in Java that finds a path from bottom to top
with minimal danger factor and prints it. Test the algorithm with the grid in
Figure 1.
2. Write a memoized version of the recursive “best path” algorithm from Task 1
in Java. Compare the complexity of both algorithms.
Hint 1: First, solve the optimization problem. Then create an additional data
structure that keeps track of the choices made when finding the optimum.
Hint 2: For the optimization problem, consider the more specific problem to find
for a given cell in the top row the best path leading to that cell.
(10 Points)
Hint 2: Consider the following variant of the problem. Let Gaux (i, j, k) be the
maximal gain that can be achieved by walking from i to j along a path that
uses only the nodes 1, ..., k as intermediate points. Develop an algorithm
for Gaux .
1. Explain how Gaux (i, j, k) can be used to compute G(i, j). Develop a recur-
rence for Gaux (i, j, k).
2. Write pseudo-code for algorithms that compute arrays with all values of
Gaux and G and explain why your algorithms are correct.
4. Implement your algorithms in Java and run them on the test matrix B above.
Hint 3: To find the recurrence for Gaux , observe that the best path from i to j,
visiting only nodes among 1, . . . , k,
Hint 4: To print an optimal path from i to j, it is good to know for all i, j an in-
termediate point k such that walking from i to j across k brings the greatest
gain.
(15 Points)
If you want to submit a hand-written solution, hand it over to your lab tutor or in
the lecture. However, all code has to be submitted in electronic form.