MATH 302 – Discrete Mathematics – Section 501 Homework 1

advertisement
Dr. Timo de Wolff
Institute of Mathematics
www.math.tamu.edu/~dewolff/Fall14/math302.html
MATH 302 – Discrete Mathematics – Section 501
Homework 1
Fall 2014
Due: Friday, September 12th, 2014, 9:10 a.m.
When you hand in your homework, do not forget to add your name and your UIN.
Exercise 1. The following pseudocode describes the algorithm InsertionSort, which sorts
a given list of real numbers:
Input: a1 , . . . , an real numbers with n ≥ 2.
Output: a1 , . . . , an in increasing order.
begin
for j := 2 to n do
i := 1
while aj > ai do
i := i + 1
m := aj
for k := 0 to j − i − 1 do
aj−k := aj−k−1
ai := m
Algorithm 1: InsertionSort
Let L := (6, 4, 3, 1, 2, 5) be a list. Describe, how InsertionSort sorts L step by step. Also
give i and m for every step of j.
Hint: A description of and an example for InsertionSort is given in the textbook on
page 197/198.
Exercise 2. Write a pseudocode for an algorithm that takes a list L of natural numbers
as input and returns the maximal odd number in L as output. You may use the expression
“[...] is (resp. is not) odd” in your pseudocode (e.g., “ if x is odd then”).
Exercise 3. Show that the following statements hold (i.e., give witnesses and show the
required inequality).
1
1. f (x) = 2x + 3 is O(x).
2. f (x) = x2 + 14x + 5 is O(x2 ).
3. f (x) = x2 − 15 is Ω(x).
4. f (x) = 2x · log(x) is O(x2 ) (you may use that log(x) < x for every x > 0).
5. f (x) = x3 + 5x2 − 5x is Θ(x3 ).
Exercise 4. Let fk (n) : N → N be the function, which maps n to 1k + 2k + · · · + nk for
some given positive integer k > 1. Show that fk (n) is O(nk+1 ).
Exercise 5. Let f and g be functions (from R or Z to R or Z). Show: If f (x) is O(g(x))
then g(x) is Ω(f (x)).
Exercise 6. (Bonus Exercise) Let gr (n) : N → N be the function, which is for every
positive integer r > 1 given by
gr (n) =
+
..
.
1 + 12 + · · · + 1r
2 + 22 + · · · + 2r
..
.
+ n + n2 + · · · + nr .
Show that gr (n) is O(nr+1).
Hint: Let for every k > 1 fk (x) be defined as in Exercise 3. Express gr (x) as sum of
fk (x) for suitable k.
2
Download