Download Examples for discussion 12 Week of November 10, 2008 1. Find the

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Big O notation wikipedia , lookup

Abuse of notation wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Large numbers wikipedia , lookup

Arithmetic wikipedia , lookup

Series (mathematics) wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Collatz conjecture wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Transcript
Examples for discussion 12
Week of November 10, 2008
1. Find the symmetric and transitive closures of relation R defined on A = {a, b, c, d, e}.
R = {(a, b), (a, c), (a, e), (b, a), (b, c), (c, a), (c, b), (d, a), (e, d)}
2. Let A and B be nonempty sets, let f: A  B be a function and let R = {(a, b) | f(a) = f(b)}.
Prove that R is an equivalence relation on A and describe the equivalence classes of R.
3. If n(A) = 30 and the equivalence relation R on A partitions A into equivalence classes A 1, A2,
and A3, where n(A1) = n(A2) = n(A3), how many ordered pairs are in R?
4.
Complexity
a. Examples of big-O and big- proofs and disproofs:
1) n lg n  O(n2) but n2  O(n lg n)
2) 2n2 + n – 7  (n2) Do both the big-O and big- proofs
3) If f(n)  O(g(n)) and g(n)  O(h(n)) then f(n)  O(h(n))
b. For each of the following find the smallest integer n such that f(x)  O(xn).
1) (x! + 2x)(x3 + log(x2 + 1))
2) x•lg(x2 + 1) + x2•lg x
c. Prove that the sum of the first n integers is O(n2)—do it without using the n(n+1)/2 formula.
d. Algorithms A and B each take one second on 20 data items. If A uses c•n 2 time for n items
and B uses d•2n for n items, how long does each algorithm take on 40 data items? on 100
data items?
5. Algorithm analysis
a. Consider the following program segment. Set up and solve a summation to determine how
many times the print statement is executed.
for i  1 to 20 do
for j  1 to i do
for k  1 to j do
print(i*j + k)
b. Design and analyze a pseudocode algorithm to find the sum of the even numbers in a list of
length n. Analyze the number of assignments made in both the best and worst cases.
integer sum, i, j
sum  1
i1
while (i  n and ai is odd)
ii+1
if i  n then
sum  ai
for j  i+1 to n
if even(aj) then sum  sum + aj
return (sum)
else
return(“There are no even numbers”)
(1)
(2)
(3)
(4)
(5)
(6)
Steps (1) and (2) each have one assignment
Step (3)—if a1 is even, then there are no assignments in (4)
if all integers are odd the assignment in (4) is made n times
Step (5)—if there is at least one even number one assignment is made
Step (6)—If the list has only even numbers, the assignment is made n – 1 times.
Best case: the first number and no others are even
total number of assignments: 3 (steps (1), (2) and (5))
Worst case: all numbers are even. Assignments are made in steps
(1), (2), (5) and (6) for a total of n + 2---[1 + 1 + 1 + n-1 in for loop
(Note: if all numbers are odd then n + 2 assignments are also made: steps (1), (2) and the
n increments in step (4) )
c. Write and analyze a pseudocode algorithm to find the product of the largest and smallest
even integers in the list a1, a2, …, an. The algorithm should return -1 (or some other odd
negative value) if there are no even numbers since 0 or a negative even integer could be the
actual product of the largest and smallest even numbers on the list. If there is just one even
number, k, return it. Determine how many comparisons of list items the algorithm requires
in both the best and worst cases.