public void nearest(int i, int d, int [] reach).
The invocation g.nearest(theI, theD, theReach)
labels all
vertices j such that the shortest path from
theI to j has at most
theD edges.
You may assume that, initially,
theReach[j] > theD for all vertices
j. When done,
theReach[j] equals the length of the shortest path
from theI for those vertices j
that have been labeled
by your code. For the remaining vertices, theReach[j]
is unchanged.
nearest.
If you need to use a stack or a queue, use
either ArrayStack (the relevant methods are
empty,
push,
and pop),
or ArrayQueue
(the relevant methods are
isEmpty,
put,
and remove).
Use iterators to move around the graph.
The statement
Iterator iw = iterator(w);
w of the graph, and
the statement
u = ((EdgeNode) iw.next()).vertex;
gets you a vertex that is adjacent to vertex w.
x[] is a sorted array iff
x[0] < x[1] < x[2] < ... < x[x.length - 1].
A sorted aray has an equilibrium point iff there
is an i, 0 <= i < x.length
for which x[i] = i.
public int equilibriumPoint(int [] x).
You may assume that x is a sorted array.
If the sorted array has no equilibrium point, your method should return
-1. If the sorted array has an equilibrium
point,
your method should return an integer i
such that x[i] = i.
Use divide and conquer.
d(v,k) is the
length of a shortest path from the source
vertex to vertex v,
under the constraint that the path has at most k edges;
p(v,k) is the
vertex (if any) that comes just before vertex
v on this constrained shortest path.
The p matrix computed for a certain
7 vertex graph is given below.
v ------>
[1] [2] [3] [4] [5] [6] [7]
[0] - - - - - - -
[1] - 1 1 - - 1 1
k [2] - 1 6 2 6 1 6
| [3] - 1 6 2 3 1 4
| [4] - 1 6 5 3 1 5
V [5] - 4 6 5 3 1 5
[6] - 4 6 5 3 1 2
The source vertex is vertex 1.
Determine the shortest path from the source to vertex 7.
Show how you obtained your answer.
c(i,j,k) = min{c(i,j,k-1), c(i,k,k-1) + c(k,j,k-1}, k > 0
c(i,j,k) is the length of the shortest path
from i to j that goes through
no vertex larger than k. Explain why the following
pseudocode computes c(i,j) = c(i,j,n) when started
with c(i,j) = c(i,j,0) (n is the
number of vertices in the graph).
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
c(i,j) = min{c(i,j), c(i,k) + c(k,j)}
n items are to be packed into the fewest number of
bins. The height of each bin is h, and the
height of item i is h(i).
Each bin must contain a consecutive set of items.
For example, if items 2 and 5 are packed into the same bin, then
items 3 and 4 must also be packed into this bin.
The items in a bin are packed in ascending order of item index from
bottom to top. For example, if items 2 through 5 are packed in a bin,
then item 2 is at the bottom and item 5 at the top.
Let p(i) be the height of padding needed at the bin
bottom (or top) when item i is the
bottom (top) item of the bin. So, items 2 through 5 can be packed
into the same bin iff h(2) + h(3) + h(4) +
h(5) + p(2) + p(5) <= h. You may assume that
h(i) + 2p(i) <= h for all i.
Let bins(j) be the minimum number of
bins needed to pack items j through n.
So bins(n+1) = 0.
Obtain a dynamic programming recurrence for bins(j),
1 <= j <= n.
q(i,j) = 0, i = n+1 or j = m+1
q(i,j) = 1 + q(i+1, j+1), if ai = bj
q(i,j) = max{q(i+1, j), q(i, j+1)}, otherwise
O(mn) time algorithm to
compute q(i,j), 1 <= i <= n+1,
1 <= j <= m+1.
Pseudocode
will suffice.
a1...an,
b1...bm, m and n are inputs
to your algorithm.
Show that the complexity of your algorithm is
O(mn).