Discrete Mathematics Chapter 3 Sec 3.1-3.8

advertisement
Islamic University of Gaza
Faculty of Engineering
Department of Computer Engineering
Fall 2011
ECOM 2311: Discrete Mathematics
Eng. Ahmed Abumarasa
Discrete Mathematics
Chapter 3
Sec 3.1-3.8
The Fundamentals: Algorithms, the Integers, and Matrices

3.1: Algorithms:
 Exercises
1. 3.1.2Determine which characteristics of an algorithm the following procedures have and which
they lack.
a)
Sol:
This procedure is not finite.
b)
Sol:
This procedure is not effective, because the step m: = 1/n cannot be performed
n = 0, which will eventually be the case.
when
c)
Sol:
This procedure lacks definiteness, since the value of i is never set.
d)
Sol:
This procedure lacks definiteness, since the statement does not tell whether x is to be
set equal to a or to b.
2. 3.1.4 Describe an algorithm that takes as input a list of n integers and produces as output the
largest difference obtained by subtracting an integer in the list from the one following it.
Solution:
Procedure largest difference (x: integer, a1, a2… an distinct integers)
max_dif := - ∞
For i=1 to n-1
If ai+1 – ai > max_dif then
max_dif:= ai+1 – ai
end
{the output is max_dif }
3. 3.1.9 A palindrome is a string that reads the same forward and backward. Describe an
algorithm for determining whether a string of n characters is a palindrome.
E.g. radar, reviver
Solution:
Procedure palindrome (a1a2a3… an: String)
result: = true
For i=1 to └ n/2┘
If ai ≠ an – i +1 then
Begin
result: = false
Break the loop;
end
end
4. 3.1.12 Describe an algorithm that uses only assignment statements that replaces the triple (x, y,
z) with (y, z, x). What is the minimum number of assignment statements needed?
Solution:
Temp:= x
x:= y
y:= z
z:= temp

3.2: The Growth of Functions:






Let f and g be functions from the set of integers or the set of real numbers to the set of
real numbers. We say that f(x) is O (g(x)) if there are constants C and k such that
Exercises:
1. 3.2.1 Determine whether each of these functions is O(x).
Solution:
A
B
C
D
E
f
Yes, f(x) < x when x>10.
Yes, f(x) < 5x, when x >1
No, f(x) cannot be < x.
Yes, f(x) < x whenever x >10
Yes, f(x) ≤ x all the time
Yes, f(x) < 2x
2. 3.2.20 Give a big-O estimate for each of these functions. For the function g in your estimate f(x)
is O (g), use a simple function g of smallest order.
Solution:
Using THEOREM 2 and 3:
A
B
C
Max(n3.log n , n3.log n ) = O( n3.log n)
Max(2n, n2 ) * Max(3n, n3 ) = 2n *3n = O(6n)
3. 3.2.33 Show that if f and g are real-valued functions such that f(x) is O (g(x)), then for every
positive integer k, fk(x) is O(gk(x)).
Solution:
From theorem 3
Let h(x) = fk(x)
So h(x) = f(x)* f(x)* f(x) ….* f(x) (k times).
Then h(x) is O(g(x)* g(x)* g(x)…. g(x))
So h(x) is O(g(x)k)
so fk(x) O(g(x)k)

3.3 Complexity of Algorithms:
Download