Você está na página 1de 4

Solutions to Homework 3

October 21, 2011


Problem 1
Given a sequence of n distinct integers (a
1
, a
2
, . . . , a
n
), note that one pass of
the BubbleSort algorithm swaps the pair (a
i
, a
i+1
) if a
i
> a
i+1
successively for
i = 1, 2, ..., n 1. We know that after n 1 passes, the sequence is sorted in
increasing order.
Show that n 1 passes are necessary, i.e. give an example of a (initial)
sequence that remains unsorted even after n 2 rounds.
Proof. Let (a
1
, a
2
, . . . , a
n
) = (n, n 1, . . . , 1). At round i, the rst element
(ni +1) is moved to the right place. After n2 rounds, the sequence will be
(2, 1, 3, 4, . . . , n 1, n), which is still unsorted.
Problem 2
For this problem and the next assume that every pair of people are (mutual)
friends or enemies. Show that in a group of 10 people, there are either 3 mutual
friends or 4 mutual enemies.
Proof. Assuming that there are no 3 mutual friends, we need to show there
exists 4 mutual enemies. For an arbitrary one person , if has 4 friends,
then any two of them must be enemies in order to satisfy that there are no
mutual 3 friends, therefore we have 4 mutual enemies; if has < 4 friends, then
has 6 enemies. By Ramsey number R(3, 3) = 6, we know that among the 6
enmies of A, there are 3 mutual enemies (by the assumption that there are no
3 mutal friends). Adding , we obtain 4 mutual enemies.
Problem 3
Let n 2. Show that in a group of n people, there must be two people who
have exactly the same number of friends.
Proof. The number of friends falls in 0, 1, . . . , n 1. In order to sasify the
condition that there are no people with the same number of friends, the numbers
of friends of n shoud be 0, 1, . . . , n 1. There is a contradiction. Because since
there is one person who is the friend of all people, there will be no person has
number of friends 0.
Problem 4
An arithmetic progression of length three is an ordered triple (a, b, c) of integers
such that for some positive integer d, b = a+d and c = a+2d. How many arith-
metic progressions of length three are there such that all three of its elements
are in the set 1, 2, 3, . . . , 99, 100?
Proof. Fix a. Lets count how many d can we choose to from an arithmetic
progression (a, a+d, a+2d) such that d+2a [n], i.e., d
100a
2
|. Therefore,
1
the number is

1a100

100 a
2
|
=

1a50

100 2a
2
| +

1a50

100 (2a 1)
2
|
=

1a50
100 2a
2
+

1a50
100 2a
2
= 2

1a50
(50 a)
= 2450.
Problem 5
(1) Let p be a prime and n 1. Show that the largest power of p that divides n!
is precisely (the sum is actually nite since beyond a point, all terms are zero):

n
p
| +
n
p
2
| +
n
p
3
| + . . .
(2)When 1000! is written as a decimal integer, how many zeroes does it have
at the end?
Proof. (1) The largest power of p that divides n! is the same as that in

1in
i =

1in,p|n
i =

1in/p
pi = p
n/p
n/p|!,
Repeat this precedure, we obtain the desired conclusion.
(2) The number of zeros is equal to the largest power of 10 that divides
1000!, which is the minimum between the largest power of 2 and 5 that divides
1000!. Since

1000
5
| +
1000
5
2
| +
1000
5
3
| + . . . = 249,
there are 249 zeros in 1000!.
Problem 6
An interval [a, b] is the set of all real numbers between (and including) a and b.
Given n intervals,
[a
1
, b
1
], [a
2
, b
2
], . . . , [a
n
, b
n
],
design an O(nlog n) time algorithm to decide whether there exists a pair of
intervals that overlap (i.e. share a point).
Proof. First, sort the intervals by the left end point in time O(nlog n) and
denote n intervals by [a

i
, b

i
]. Calculate values M
i
= max
ji
b

i
in time O(n).
Its easy to there exists overlap if and only if [a

i
, M
i
] [a

i+1
, b

i+1
] ,= for some
1 i n 1, which can also be determined in O(n) time.
2
Problem 6
Given a set of m(m1) +1 distinct integers A = a
1
, a
2
, ..., a
m(m1)
+1, show
that there exists a subset B A such that
[B[ = m.
The average of integers in B is also an integer.
Proof. There are only m distinct residues mod m and m(m+1)+1 integers. By
pigeonhole principle, there are
m(m1)+1
m
| = m integers with the same residu
mod m, and their sum is divisible by m.
Challenge Problem 1
Assuming that only equality checks are allowed, design an O(n) time algorithm
to check if there is an element which occurs more than n/2 times in an array
containing n elements. Note that the elements are not necessarily integers and
the only operation allowed is checking whether two elements are equal.
Proof. Assume occurs more than n/2 times in set A. The key observation is
that, if will still be occur more than (n 2)/2 times if two dierent elements
in A are removed. The rest is left to the reader.
Challenge Problem 2
Given a mn matrix of integers such that every row is strictly increasing (from
left to right), and every column is strictly increasing (from top to bottom),
design an O(m + n) time algorithm to test if a given integer b is contained in
the matrix.
Proof. Denote by M(i, j) the element in the row i and column j. Scan the rst
row from right to left, and stop at the rst element which is b, say M(1, c
1
).
If M(1, c
1
) = b, we have done. Otherwise, move to the second row, and scan
from the M(2, c
1
), and move left until we meet the rst element which is b,
say M(2, c
2
). If M(2, c
2
) = b, we have done. Otherwise, move to the third row
and repeat above precedure.
The correctness and the time complexity analysis are left to the reader.
3

Você também pode gostar