
CSE 202 Homework 2
Combinatorial Search and Optimization:
Graph Algorithms Sample Solutions
1. Broadcast Times for a Tree
Problem Let
T
be a rooted binary tree whose edges are given positive real
weights, representing message delay times. For a node
x
2
T
,definethe
broadcast time for
x
to be the maximum over
y
2
T
of the weighted dis-
tance in
T
between
x
and
y
. (In other words, the broadcast time is the time
before a message originatin at
x
is received by every other processor
y
.)
Give an efficient algorithmto compute all the broadcast times for nodes in
T
simultaneously.
Solution
This solution basically performs two level traversals of the tree. One upward
from the leaves keeping track of the largest broadcast up times and one down-
ward adding on the broadcast down part of a path.
Associate with each edge,
(
u; v
)
,avariable
d
[(
u; v
)]
that will hold the broadcast
time first from the farthest node in the direction of
v
(the child), then from the
farthest in the direction of
u
. Starting at the leaves and working in levels toward
the root, set
d
[(
u; v
)] =
max
j
2
C
(
v
)
(
d
[(
v; j
)] +
w
[(
u; v
)])
All
d
[(
u; v
)]
will then hold the longest broadcast time from
u
to a vertex in the
subtree rooted at
v
. Then from the root down to the leaves set the broadcast time
of vertex
i
to be the farthest time either down one of its children or up through
it’s parent,
b
[
i
]=
max
j
2
N
(
i
)
(
d
[(
i; j
)])
Also, set for
i
’s children,
j
2
C
(
i
)
, broadcast times on the edges now reflecting
upward broadcasts,
d
[(
i; j
)] =
max
k
2
N
(
i
)
;k
6
=
j
(
d
[(
i; k
)]) +
w
[(
i; j
)]
This algorithm makes two passes over the
n
1
edges in the tree and performs
work linear in the degree of each vertex,
d
. The instances in the problem are
stated to be binary trees, so
d
2
O
(1)
and the total time is
O
(
n
)
. (This can be
shown to be
O
(
n
)
for a tree with any degrees by amortizing the work over the
edges, which are set twice and examined three times each).
We can provethe correctness by looking at the content of the variables
d
[(
u; v
)]
.
In the first pass (from the leaves to the root), we claim that each
d
[(
u; v
)]
is
set to the longest broadcast time from
u
to some vertex in the subtree rooted
at
v
(possibly
v
itself). Clearly this is true for the base case when
v
has no
children by setting
d
[(
u; v
)]
to
w
[(
u; v
)]
. Recursively, the maximum downward
broadcast from
u
through
v
,
d
[(
u; v
)]
, is the maximum broadcast from
v
down
any of the children of
v
added to the delay
w
[(
u; v
)]
as the recursion states. This
completes the first pass of the algorithm. Then, when we traverse down the tree
1