You are on page 1of 5

Introduction to algorithms - Ex2

Shai Wyborski

October 29, 2010

Question 1
a.

This won't work, counter example:


{[0, 5], [4, 7], [6, 10]}

The described algorithm will return {[4, 7]} while the best solution is {[0, 5], [6, 10]}

b.

This won't work either, counter example:

{[0, 1], [1, 2], [1, 2], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [5, 6], [5, 6], [6, 7]}
(we'll assume that similar segments which are denoted twice describe dierent
tasks at similar times)
The algorithm will rst choose [3, 4], as it intersects with the fewest tasks, and
will remain with

{[0, 1], [1, 2], [1, 2], [1, 2], [5, 6], [5, 6], [5, 6], [6, 7]}
this set can be divided to
{[0, 1], [1, 2], [1, 2], [1, 2]} , {[5, 6], [5, 6], [5, 6], [6, 7]}

as there are no intersections between them.


Since each of the segmants in both sets intersect all the other segments, the
output will be at most three tasks.
However, a better solution is
{[0, 1], [2, 3], [4, 5], [6, 7]}

1
Question 2
a.

Let a1 > ... > ak be a succession of values such that


∀1 < i ≤ k : ak ≤ 2ak−1

We want an output l1 , l2 , ..., lk s.t:

• li ≥ 0
Pk
• i=1 ak lk = n

i=1 li is minimal
Pk

and let us use the following algorithm

• For each k , starting from one, set lk = max {l ∈ N : l · ak ≤ n} and n =


n − lk ak

Let us prove correctness.


Let l1 , ..., lk be our solution, and let us assume that = L.
Pk
i=1 li

Let o1 , ..., ok be an optimal solution, and let us assume that ok = M < L.


Pk
i=1

Let us dene Di = oi − ji .
Let i = min {i : oi 6= li }. It's obvious that oi < li (as oi > li =⇒
Pi
j=1 oj > n
from the denition of the algorithm), and that i 6= k.
We know that Di > 0, let us notice that
k   k
X Di X
0= aj Dj ≤ ai−1 + (Di mod2)ai + aj Dj
j=i
2 j=i+1

we can dene the solution


  Di 
 oj + 2
 j =i−1
?
oj = Di mod2 j=i
otherwise

oj

we get that
X X X X
o?j ≤ o j − oi + D i = oj − oi + (oi − ji ) < oj

which is a contradicition to o1 , ..., ok being optimal.

2
b.

We'll use the coins 6, 5, 3, 1. Trying to return 10 shekels using our algorithm
will result in the output 1, 0, 1, 1 while a better output would be 0, 2, 0, 0.

Question 3
Let us denote wi = pi
ai .
For every test ei the amount of points we get for each hour we spent learning
for it is wi · 100.
Since wi is independent of other grades, or how many hours spent on each test,
we just want to choose the highest among the wi such that xaii < 1, when xi is
the amount of hours we already spent studying for that test.
The output will be the succession x1 , ..., xn .
The algorithm:

• Create an array of x1 , ..., xn


• replace each ei with apii and sort them in decending order
• for each i, while xi < T and xaii < 1 do xi = xi + 1
P

Proof of correctness: Trivial, every hour we choose to study for the test which
is most benecial, which is exactly like choosing the largest k out of n numbers.
Choosing the largest one k times is just ne.
Running time: Sorting the array is o(n log n), after the sorting we do
P constant
time calculations until we run out of hours or time, which is o (T + ei ai ).
The overall running time would be o(n log n + T + ei ai ).
P

Question 4
a.

The given is not a matroid, as it doesn't satisfy the augmentation property


(‫)תכונת ההחלפה‬.
Let S = {(1, 3), (2, 4), (3, 5), (4, 6), (5, 7)}.
The A = {(1, 3), (3, 5), (5, 7)} , B = {(2, 4), (4, 6)} ∈ I and |A| > |B|, yet each
element in A intersects with an element in B .

3
b.

This is indeed a metroid.


Hereditary: Let A ∈ I and let B ⊂ A, then ∀i = 1, ..., k : B ∩ Si ⊆ A ∩ Si =⇒
|B ∩ Si | ≤ |A ∩ Si | ≤ 1 =⇒ B ∈ I .
Augmentation: Let A, B ∈ I s.t. |A| < |B|. Since A intersects with exactly
|A| of the sets {Si }, and B intersects with exactly |B| of them, there has to be
∃j : A ∩ Sj = ∅ =6 B ∩ Sj . Let x ∈ B ∩ Sj , then (A ∪ {x}) ∈ I as needed.

c.

Let S = {1, 2, 3, 4}, I1 = {{1, 2}, {1, 3}, {1}, {2}, {3}}, I2 = {{1, 2}, {2, 3}, {1}, {2}, {3}}.
Then (S, I1 ) and (S, I2 ) are matroids, however
I = {X1 ∩ X2 : X1 ∈ I1 and X2 ∈ I2 } = {{1, 2}, {1}, {2}, {3}}

Since |{1, 2}| > |{3}| yet {1, 3}, {2, 3} ∈/ I we get that the augmentation isn't
satised.

d.

This is a matroid.
Hereditary: Let A ∈ I , and let B ⊂ A. Let x ∈/ A\B , since M1 and M2 are
matroids, X1 \{x} ∈ I1 and X2 \{x} ∈ I2 , thus
(X1 \{x}) ∪ (X2 \{x}) ∈ I

since S is nite we know that |A\B| is nite, and we can repeat this process for
every element in A\B until we get that
(X1 \(A\B)) ∪ (X2 \(A\B)) = (X1 ∪ X2 ) \(A\B) = A\(A\B) = B ∈ I

Question 5
Hereditary: Let A ∈ I and let B ⊂ A. Since A doesn't contain a circle, neither
does B , which proves B ∈ I .
Augmentation: Let A, B ∈ I such that |A| < |B|. A ∪ {x} ∈ I i {x} gaps two
connectivity components in A, we need to prove that such an x exists in B .
Let us assume that every x ∈ B\A closes a circle in A, thus, none of them
gap between two connectivity components in A, and since A ∩ B ⊂ A, and
B = A ∪ (B\A) we get that B has at least as much connectivity components as
A.

4
However, we know that B is acyclic, which means that A ∩ B ( A since A ∩ B =
A =⇒ (A ∩ B) ∪ {x} ⊂ B which means B closes a cycle.
Since this is true for every x ∈ B\A, we know that |A ∩ B| ≤ |A| − |B\A|, thus
|B| = |(A ∩ B) ∪ (B\A)| = |(B\A)| + |(A ∩ B)| ≤ |(B\A)| |A| − |B\A| = |A|

which is a contradiction.

Question 6
b.
• Build a bipartite graph (L, R, E) where L and R are empty.

• Go over the women in decending payment order, add the woman to L


and the rst found man which isn't already matched to R, add a vertex
between them.
• If we encounter a woman whose entire list was already matched:

 We denote that woman as C.


 we go over all the better paying women an ascending order (as in,
going back in the list) - for each women we check if she is matched
to a man in the current woman's list.
 If so we go over her men - if one of them isn't matched we match her
with him and C with her current match.

This algorithm always holds a perfect matching, and it's correctness is almost
immediate from a.

You might also like