For any vertex of the tree
, denote the subtree rooted at
by
. Generically, if
has a unique child then we denote that
child by
, and if there are two children then we denote them
and
respectively. For
we consider a tadpole graph
defined as
appended by a single path extending upwards from
the root of
with
extra vertices.
Note that
.
For ,
We will consider the optimal placement of at most
pebbles in
, and denote the minimal cost by
.
More generally, for
and
,
we will consider the optimal placement of one pebble
at the tip of the sperm graph
which has distance
from the root
of
, and at most
pebbles within
.
We denote by
the minimal cost
of all feasible pebbling
of
with at most
pebbles in
, and where if
we
stipulate that one additional pebble is
placed at the tip of the external path from
.
If
and
then we have a feasible pebbling
if and only if all weights in
are zero, in which case
. Note that for any
and
, a feasible pebbling exists.
For
, and if some non-zero weights exist in
,
and thus no feasible pebbling exists,
we denote
.
We will compute for all
,
inductively for
according to the height of
the subtree
, starting with leaves
.
More formally, let be the number of leaves in
.
Let
be the depth of
in
, i.e., the distance
from the root of
to
(note that by our definition of
distance, the depth of the root is 0). Let
be the height of the tree
, which is the maximum depth of all
leaves in
, i.e.,
, where
ranges over all
leaves in
. Note that a tree with a singleton vertex has
height 0. Inductively for
, starting with
,
we compute
,
for all
such that the subtree
has
, and
for all
, and for all
.
Base Case :
In the base case and we are dealing with a singleton leaf,
together with an extension of a path of length
if
,
and no extensions if
.
Thus, for ,
Now for ,
Inductive Case :
For the inductive case , we have some
with
,
and we assume we have computed all
for children
of
.
There are two cases,
has either one or two children.
First we consider
has a unique child
.
For either
or
, we can consider either placing a pebble at
or not placing it there. But we claim that without loss of
generality we don't need to place it there. This is
because
has only one child, and if an optimal
pebbling places a pebble at
, we can obtain at least as good
a pebbling by moving the pebble from
to
, and if
is
already pebbled we can remove one pebble.
Thus, we have an optimal pebbling of
using
at most
pebbles in
without a pebble at
.
Hence,
Suppose now has two children
and
.
Basically we must decide how to distribute
pebbles
in the subtrees
and
with
and
pebbles each. There is a slight complication as to whether
to place a pebble at
, the root of
, which
affects how many
pebbles there are to be distributed, either
or
.
First . If we place a pebble at
, (which of course
presupposes
), then there are
pebbles to be distributed in
and
, but with respect to these two subtrees
the ``
'' values are both 1, i.e., we have
, minimized over all
pairs
. (To be precise, all pairs
, such that
,
and
; but we will not specify this range
explicitly in the following.)
If we don't place a pebble at , then there are
pebbles to be distributed in
and
, and since
for
,
with respect to these two subtrees we still have
the ``
'' values 0.
So we have
, minimized over all
pairs
.
The optimal cost
is the minimum of these two
minimizations, i.e.,
We consider the case next.
For
we have
This completes the description of the computations of
. The final answer is
,
where
is the root of
and
is the number of pebbles.
If
is given, (typically much smaller than
the number of leaves), in the above computations one never needs
to compute for
, the number of pebbles allowed, beyond
,
i.e., all
.
We estimate the complexity of the algorithm.
Let
be the height of the tree. Typically
.
For leaves, the algorithm spends
time per leaf.
For each vertex with one child the time is
. For each vertex with two children it
is
.
Hence the total running time is at most
,
which is only
with
.
It is also clear that the above algorithm can be easily modified to compute the actual optimal algorithm in addition to the optimal cost.