Discrete Mathematics with Graph Theory (3rd Edition) 244

Discrete Mathematics with Graph Theory (3rd Edition) 244 -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
242 Solutions to Review Exercises Step 5: If F = 0, output "transitive" and stop; otherwise output "not transitive" and stop. The loops in Step 4 are executed t 3 times, each iteration requiring one addition and two compar- isons. So there are at most 3t 3 additions and comparisons. The algorithm is O(t 3 ). (e) Step 1: Set F = O. Step 2: while F = 0 for i = 1 to t - 1 for j = i + 1 to t if aj = ai and bj -# bi set F = 1. end while Step 3: If F = 1 output "Not a function"; else output ''The relation is a function." When i = 1, the j-Ioop in Step 2 requires 2(t - 1) comparisons. When i = 2, the loop requires 2(t-2) comparisons, and so on. The number of comparisons is 2[(t-1) + (t-2) + . . ·+2+ 1] = t(t - 1), so the algorithm is O(t 2 ). 4. (a) The best sorting algorithm (for example, a merge sort) is O(n log n). (b) There certainly are better ways to proceed. We could, for example, apply just the first two it- erations of a bubble sort, or use the following
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: O(n) algorithm (which requires at most n -3 comparisons. ) Initially we set Ml to be the larger of al, a2 and M2 the lesser. Then, in tum, we compare M2 with each ai. When ai > M 2, we check whether or not ai is also larger than M l . Eventually, Ml is the largest and M2 the second largest integer. Step 1: if al > a2, set Ml = al and M2 = al; else set Ml = a2 and M2 = al. Step 2: ifn > 2, fori = 3 to n, ifai > M2 if ai > Mlo replace Ml by ai and M2 by M l ; else replace M2 by ai. Step 3: output Ml and M 2. 5. Step 1: Let M = al and m = al. Step 2: for i = 2 to n, if ai > M, replace M by ai; if ai < m, replace m by ai; Step 3: Output ''The largest number is M and the smallest is m." 6. Step 1: Set n = O. Step 2: If x > 0 while x> n replace n by n + 1; output n - 1; else while -x > n replace n by n + 1; output -n 7. (a) 1 k + 2k + ... + n k :::; n k + n k + ... n k = n(nk) = nk+l, so the result follows with c = 1 and no = 1....
View Full Document

Ask a homework question - tutors are online