14.1 What is an algorithm

Shared by: vmarcelo
-
Stats
views:
9
posted:
10/31/2008
language:
English
pages:
15
Document Sample
scope of work template
							                    Solutions to Chapter 14 Exercises
          in Discrete Mathematics by Norman L. Biggs;
                        2nd Edition 2002

               14.1        What is an algorithm?

14.1.1
The following layout describes the usual algorithm for multiplying an n-digit
integer x by a single-digit integer y, in base 10 notation.
                      xn−1 xn−2 . . . x2 x1 x0    x
                ×                           y     y
                    cn cn−1 . . . c2 c1           (carrying digits)
                 pn pn−1 pn−2 . . . p2 p1 p0      xy

Write down the formulae which determine the digits of xy and the carrying
digits.

Solution Using t and u to denote the tens-digit and the units-digit, as in the
text, we have

                           c1 = t(x0 y),   p0 = u(x0 y);

           ci+1 = t(xi y + ci ),   pi = u(xi y + ci )   (1 ≤ i ≤ n − 1);

                                     pn = cn .
                  Solutions to Chapter 14 Exercises
           in Discrete Mathematics by Norman L. Biggs;
                         2nd Edition 2002



14.1.2
Explain carefully how the algorithm described in Ex. 14.1.1 can be extended to
yield the usual ‘long multiplication’ algorithm for multiplying an n-digit integer
by an m-digit integer (n ≥ m ≥ 1).

Solution In the usual method of long multiplication, the multiplier y, in base
10 notation, is

                             y = ym−1 ym−2 · · · y1 y0 .
The products

                       x × ym−1 ,    x × ym−2 , . . . , x × y0
are computed by the method given in Ex. 14.1.1, and 0s are inserted to give the
products

                x × 10m−1 ym−1 ,    x × 10m−2 ym−2 , . . . , x × y0 .
   These are then added, using the method explained in the Example, to give
x × y.
                   Solutions to Chapter 14 Exercises
           in Discrete Mathematics by Norman L. Biggs;
                         2nd Edition 2002

             14.2       The language of programs

14.2.1
Suppose that initially the values of the identifiers x, y, z are 46, 23, 78, respec-
tively. Write down the values of the identifiers after the execution of the follow-
ing commands.

 (a) x:= y + z
 (b) x:= x + 12
 (c) z:= z - 17


Solution

 (a) x      y     z
     101    23    78
 (b) x      y     z
     58     23    78
 (c) x      y     z
     46     23    61
                 Solutions to Chapter 14 Exercises
          in Discrete Mathematics by Norman L. Biggs;
                        2nd Edition 2002



14.2.2
Use a table such as Table 14.2.1 to work out the values of x, y, z after the
execution of the following commands, when the initial values in each case are
as in Ex. 14.2.1.

 (a) begin x:= 27; y:= 13 end
 (b) begin z:= 25; x:= y - 12; y:= z + 6 end
 (c) begin x:= x + 28; y:= x; x:= y - 14; y:= x + z end


Solution The tables are as follows.

 (a) x     y     z
     46    23    78
     27    23    78
     27    13    78
 (b) x     y     z
     46    23    78
     46    23    25
     11    23    25
     11    31    25
 (c) x      y     z
     46     23   78
     74     23   78
     74     74   78
     60     74   78
     60    138   78
                  Solutions to Chapter 14 Exercises
            in Discrete Mathematics by Norman L. Biggs;
                          2nd Edition 2002

              14.3      Algorithms and programs

14.3.1
Tabulate the values of x, y, and z in the version of the Euclidean algorithm given
above, when the initial values of s and t are 725 and 441.

Solution

 x     y     z

725   441
441   284   441
284   157   284
157   127   157
127    30   127
 30     7    30
  7     2     7
  2     1     2
  1     0     1
                  Solutions to Chapter 14 Exercises
           in Discrete Mathematics by Norman L. Biggs;
                         2nd Edition 2002



14.3.2
What value of ans is computed by the following algorithm?

                         sum:= 0;
                         for i:= 1 to 17       do
                           sum:= sum + i;
                         ans:= sum


Solution The algorithm computes the sum
                                   17
                                         i,
                                   i=1

which is equal to 153.
                  Solutions to Chapter 14 Exercises
            in Discrete Mathematics by Norman L. Biggs;
                          2nd Edition 2002

    14.4       Proving that an algorithm is correct

14.4.1
The following algorithm is intended to calculate the product of two positive
integers.

                     x:= m; y:= n; z:= 0;
                     while y > 0 do
                       begin z:= z + x; y:= y - 1       end
                     p:= z

Show that

 (a) the algorithm terminates,
 (b) the value of z + x ∗ y is invariant under the command begin z := z + x;
     y := y − 1 end, and
 (c) the value assigned to p is the product of the initial values m and n.


Solution

 (a) The sequence of values of y is altered only by the command y := y − 1 and
     so it forms a strictly decreasing sequence. By the fundamental property
     of integers, the sequence eventually becomes negative, and the algorithm
     terminates.
 (b) Suppose that the values of x, y, z before and after one execution of the
     begin end command are x, y, z; x , y , z . Then

                      z + x y = (z + x) + x(y − 1) = z + xy.

 (c) The initial value of z + x ∗ y is m ∗ n, so this is also the final value. But
     the final value of y is 0, so the final value of z is m ∗ n, which is the value
     assigned to p.
                  Solutions to Chapter 14 Exercises
           in Discrete Mathematics by Norman L. Biggs;
                         2nd Edition 2002



14.4.2
Tabulate the values of a, b and c during the course of the following algorithm
when the given values of m and n are (a) 31 and 12, (b) 15 and 77.

                 a:= 0; b:= n; c:= m;
                 while b > 0 do
                    begin while b mod 2 = 0 do
                    begin b:= b div 2; c:= 2 * c end;
                   a:= a + c; b:= b - 1
                   end;
                 q:= a


Solution
(a)
                                     a   b  c
                                     0   12 31
                                         6  62
                                         3 124
                                   124   2
                                         1 248
                            q =    372   0


(b)
                                    a   b      c
                                     0  77     15
                                    15  76
                                        38     30
                                        19     60
                                    75  18
                                        9     120
                                   195   8
                                        4     240
                                        2     480
                                        1     960
                           q =     1155 0
                     Solutions to Chapter 14 Exercises
              in Discrete Mathematics by Norman L. Biggs;
                            2nd Edition 2002

                  14.5       Efficiency of algorithms

14.5.1
Suppose we wish to find the least member of a finite set of integers {x1 , x2 , . . . , xn }
using the algorithm indicated in Ex. 14.3.3.

  (i) Suggest a good measure of the size of an instance.
 (ii) How many comparisons are needed?
 (iii) What is the worst case with respect to the number of assignment com-
       mands required?


Solution

  (i)      The obvious choice is n, the number of elements in the set.
 (ii)      Each one of x2 , x3 , . . . , xn is compared with the current minimum, so
         n − 1 comparisons are required.
 (iii)     The worst case is when

                                    x1 > x2 > · · · > xn ,

         because then each comparison much be followed by the assignment of a
         new value to the current minimum.
                     Solutions to Chapter 14 Exercises
             in Discrete Mathematics by Norman L. Biggs;
                           2nd Edition 2002

             14.6      Growth rates: the O notation

14.6.1
Find a function g(n) (of the form AB ) such that f (n) is O(g(n)), in each of the
cases:
                 n
  (i) f (n) =      ;
                 3
                5n3 + 6
 (ii) f (n) =           ;
                 n+2

                 3f (n − 1) (n > 1),
(iii) f (n) =
                 2          (n = 1);

(iv) f (n) = n!.


Solution

  (i) We have

                               n       1                   1
                                   =     n(n − 1)(n − 2) ≤ n3 .
                               3       6                   6
      Hence the expression is O(n3 ).
 (ii) Here we have

                            5n3 + 6   5n3 + 6
                                    ≤         = 5n2 + 6/n ≤ 6n2 ,
                             n+2         n
      for all n ≥ 2. Hence the expression is O(n2 ).

(iii) 3n .
(iv) nn .
                    Solutions to Chapter 14 Exercises
            in Discrete Mathematics by Norman L. Biggs;
                          2nd Edition 2002



14.6.2
Show that for any positive constants C0 , C1 , . . . , Ck , the expression

                          C0 + C1 n + C2 n2 + · · · + Ck nk

is O(nk ). Prove that the expression is not O(nk−1 ).

Solution Since ni ≤ nk for all natural numbers n and 0 ≤ i ≤ k, it follows
that

               C0 + C1 n + · · · + Ck nk ≤ (C0 + C1 + · · · + Ck )nk ,
and so the left-hand side is O(nk ).
Suppose the left-hand side is O(nk−1 ), that is, for some constant A,

                C0 + C1 n + · · · + Ck nk ≤ Ank−1      for all n ≥ n0 .
Then, since all the terms are positive,
                                                        A
                        Ck nk ≤ Ank−1 ,      that is Ck ≤ .
                                                        n
But this is clearly only true for a finite number of n, specifically n ≤ A/Ck .
                  Solutions to Chapter 14 Exercises
           in Discrete Mathematics by Norman L. Biggs;
                         2nd Edition 2002

             14.7       Comparison of algorithms

14.7.1
Estimate the time taken to compute n100 000 using Algorithms A and B, on the
assumption that we can carry out one multiplication per second.

Solution Using Algorithm A requires about 100 000 seconds. A rough esti-
mate can be found using the fact that there are 3600 seconds in one hour, which
implies that 100 000 seconds is roughly 30 hours.
   Using Algorithm B requires about

                2(log2 100 000) = 2 × log2 (105 ) = 10 × log2 (10)
seconds. Since log2 (10) is about 3.6, this is roughly 36 seconds.
                  Solutions to Chapter 14 Exercises
           in Discrete Mathematics by Norman L. Biggs;
                         2nd Edition 2002

      14.8      Introduction to sorting algorithms

14.8.1
Describe the result of each ‘pass’ of the bubble sort algorithm when the given
list is 4, 3, 9, 5, 1, 2, 7, 8, 6.

Solution

                      After first   pass :   3   4   5   1   2   7   8   6   9
                    After second   pass :   3   4   1   2   5   7   6   8   9
                     After third   pass :   3   1   2   4   5   6   7   8   9
                    After fourth   pass :   1   2   3   4   5   6   7   8   9

The remaining passes result in no changes.
                  Solutions to Chapter 14 Exercises
             in Discrete Mathematics by Norman L. Biggs;
                           2nd Edition 2002



14.8.2
Use insertion sort (by hand!) to arrange the following list in increasing order:
516, 207, 321, 581, 762, 163, 921, 105, 721, 813, 316, 188, 733, 909, 281, 312,
871, 950, 135, 888, 417.

Solution The list is built up as follows:

                           516
                           207,   516
                           207,   321,   516
                           207,   321,   516,    581

and so on.
                    Solutions to Chapter 14 Exercises
             in Discrete Mathematics by Norman L. Biggs;
                           2nd Edition 2002



14.8.6
An alternative sorting algorithm is known as selection sorting. In this method,
at the jth pass we select the smallest of xj , xj+1 , . . . , xn and (if it is not xj ) we
switch it with xj . Describe how this method operates when the given list is as
in Ex. 14.8.1. How many comparisons and how many switches are needed, in
general?

Solution

                         After first pass : 1 3 9 5 4 2 7 8 6
                       After second pass : 1 2 9 5 4 3 7 8 6
                        After third pass : 1 2 3 5 4 9 7 8 6

and so on.

   There are n − j comparisons in the jth pass, and so the total number is
                                                   1
                                                     n(n − 1).
                   (n − 1) + (n − 2) + · · · + 2 + 1 =
                                                   2
   There is (at most) one switch for each pass, so the total number is at most
n − 1.

						
Related docs