You are on page 1of 4

Introduction to Algorithms - Ex1

Shai Wyborski - 032526865

October 16, 2010

Question 1
Let G be a circular directed weighted graph with n nodes (n being the amount
of gas stations), s.t. the sum of all weights is exactly 0.
We can correspond each gas station to a node such that:

• each two nodes are neighbours i their matching gas stations are neigh-

• the weight of a node is the distance which could be travelled with the gas
in the station less the length of the road to the next station

Thus, reducing the original problem to proving that

∃e ∈ G s.t. ∀e0 ∈ G : w (e? ) ≥ 0
e? ∈[e,e0 ]

where w is the weight function and [e, e0 ] is the (unanimously dened) simple
line between e and e.
Let us assume that no such e exists, thus that

∀e ∈ G : ∃e0 ∈ G s.t w (e? ) < 0
e? ∈[e,e0 ]

For the simplicity of the proof well say that e is negatively distant from e0 .
Let e0 ∈ G be any node, according to our assumption there is an e1 which is
negatively distant from e0 , and an e2 which is negatively distant from e1 and
so on.

Let us create a succession of n+1 such nodes, since there are only n gas stations,
we know that ∃i, j s.t. i 6= j and ei = ej .

Thus the succession (ei , ..., ej ) starts and ends at the same place, and let us
assume it goes over the entire circle k times, thus, the total weight traversed
along this succession is
k w(e)

thus, according to our assumption

k w(e) = w (e? ) + ... + w (e? ) < 0 =⇒ w(e) < 0
e∈G e? ∈[ei ,ei+1 ] e? ∈[ej−1 ,ej ] e∈G

which contradicts the way G was originally dened.

Thus such a node e exists which proves the hypothesis.

Question 2
I suggest this algorithm (which I like to call a reverse Kruskal):

• Sort the edges from the heaviest to the lightest

• Go over the edges in that order, and remove it from the graph i it doesn't
divide it into two connectivity components (could be checked in o(|E|))

Correctness proof :
First we must prove that RK indeed returns a tree. The constraints promise
that the output graph is simply connected.

Let us assume that the output contains a circle, and let us denote vj as the
heaviest edge in the circle. Since vj is the rst edge traversed by RK, it's still
containt in a circle, which means removing it will not divide the graph into
two connectivity components, thus having the algorithm remove it, which is a

This proves the output is indeed a spanning tree, let us prove that it has a
minimal maximal edge.

The correctness is trivial in case removing a1 (or any ai s.t. w(ai ) = w(a1 ))
divides the graph into two connectivity components, as a1 (or one of it's dopel-
gangers) must stay thus making any spanning tree a solution.

Assuming that is not the case, let us denote a solution as the ordered set of
edges removed from the graph, and denote the output of our algorithm as RK .
Let B be the set of optimal solutions and let S ∈ B , there is a maximal j s.t.
∀j ≤ i : vj ∈ S , assuming that RK is not optimal means that there exists a
minimal j ≤ i s.t. vj ∈
/ RK .

But since S is a solution we know that after removing v1 , ..., vj−1 from G, we get
a connected graph, and furthermore, since vj ∈ S we know that G − {v1 , ..., vj }
is still connected.

Hence, after removing the edges {v1 , ..., vj−1 } RK would remove vj , proving
that vj ∈ RK , in contradiction.

Thus it's proved that RK ∈ S .

Question 3
Let us consider a spanning tree of G, and let v∈G be a leaf in that tree.

That means, that ∀u1 , u2 6= v ∈ G there exists a (u1, u2)−trail in G which

doesn't contain v , and by proxy it doesn't contain any of the edges it's connected

Thus, removing v and all if it's edges, doesn't harm the connectivity of the

Question 4
• Go over each of the numbers in the array from x1 to xn , if the current
number isn't contained in any segment add it to the output set.

Proof of correctness:
Since the algorithm will add any number not yet covered it's obvious that the
output covers all numbers in the array.

Let us denote our output as A = {y1 , ..., yk }.

Let B be a set of optimal solutions, and let us assume that A∈
/ B.
The assumption implies that the optimal solution is of size m < k.
Let j be the maximal value such that ∃S ∈ B s.t. {y1 , ..., yj } ⊂ S , thus S =
{y1 , ..., yj , cj+1 , ..., cm }.
Let us denote by xl the minimal element of the input which isn't contained in
any of the segments described by {y1 , ..., yj }.
From the correctness of the algorithms we know that xl ∈ [cj+1 , cj+1 + 1] and
xl ∈ [yj+1 , yj+1 + 1].
But if we examine the elements larger than xl which are contained in the same
segments, we realize that in case of A they are all contained in [xi , yj+1 + 1] and
in case of S they are all contained in [xi , cj+1 + 1].

But from the denition of our algorithm we get that xi = cj+1 thus [xi , yj+1 +
1] ⊆ [xi , cj+1 + 1] (as the length of the rightmost segment is 1, which is the
maximal length, and they both have the same starting point).

This proves that {y1 , ..., yj+1 , cj+2 , ..., cm } as also a solution, in contradiction
with j being maximal.

Thus proving that A∈B as required