Docsity

Graph Algorithms Sample Questions - Homework 2 | CSE 202, Assignments of Computer Science

Toggle sidebar
bg1
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
pf3

Partial preview of the text

Download Graph Algorithms Sample Questions - Homework 2 | CSE 202 and more Assignments Computer Science in PDF only on Docsity!

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 , define the

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 algorithm to 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 ), a variable 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 )] = maxj 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] = maxj 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 )] = maxk 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 prove the 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

and are at vertex u, we maintain that d[(par ent(u); u)] holds the broadcast time

from u to the farthest node in the subtree T subtr ee(u), and the d[(u; v )] are

still the down-broadcast times. This is trivially true for the root, since it has no

parent and T subtr ee(r oot) = ;. Recursively, the farthest node from v in

T subtr ee(u) must come from the parent side of u or from one of its children,

which the recursion provides. Finally, the farthest node from i is either connected

to i through its parent or its children. These farthest broadcast times are exactly

those contained in the d variables for the neighbors of i and the broadcast times

are thus correct.

2. Prove that a graph is bartite if and only if it has no odd cycles.

) Assume that G has an odd cycle C = (c 1 ; c 2 ; : : : ; cn ; c 1 = cn+1 ) (n is odd).

Since G is bipartite, we can place verticies with edges between them into

two disjoint sets, l ef t and r ig ht. Arbitrarily select c 1 on the l ef t. Since

consecutive verticies in the path ci ; ci+1 are connected, ci+1 must be on

the side ci is not. Therefore, since c 1 is in l ef t, all even verticies must

be in r ig ht. But c 1 = cn+1 is an even vertex in the chain and must be in

r ig ht; c 1 is already in l ef t and since r ig ht and l ef t are disjoint we have a

contradiction.

( Constructively, for each connected component begin with an arbitrary vertex

i. Place i in l ef t. Place each neighbor of j 2 l ef t in r ig ht and then each

neighbor of j 2 r ig ht in l ef t repeatedly until all verticies are in l ef t or

r ig ht. We claim there is no edge between i; j 2 l ef t or i; j 2 r ig ht.

Assume this is not true and WLOG there is an edge i; j 2 l ef t and j was

added after i. Necessarily, j has a set of neighbors, J 1 , in r ig ht and J 1

has a set of neighbors, J 2 , in l ef t. If i 2 J 2 then this is an odd cycle

contradicting the assumption. Else, this repeats, J 2 k  l ef t; J 2 k +1 

r ig ht. Since this is a connected component (and j was clearly not added to

l ef t by our process as a neighbor of i), for some k , we must have i 2 J 2 k

and thus we have a cycle (i; j; j 1 ; : : : ; j 2 k ), where ji 2 Ji and j 2 k = i.

This cycle is of odd length contradicting the assumption.

3. Maximum matching in a forest

Lemma In a forest, F , if a leaf node, u, exists and has a neighbor v , then a

maximum matching on F exists that contains (u; v ).

Proof Assume a different maximum matching N is found on the forest not con-

taining the edge (u; v ). That matching must necessarily contain an edge

(v ; x) where x 6 = u (otherwise, it is not a maximum matching since (u; v )

can be added to it). Further no other edge (v ; y ), y 6 = x is in the match-

ing since that would violate the matching condition. Therefore, we can

remove (v ; x) and replace it with (u; v ) without changing the cardinality of

the matching and without violating the matching condition.

Algorithm proof (Inductively on the number of edges) If F has no edges then

all matchings are of zero cardinality and are maximum. Then applying the

above theorem, take any leaf u with neighbor v and a maximum matching,