0% found this document useful (0 votes)
5 views

Data Structure Lect14

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structure Lect14

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Discrete Structure

Lecture 14
Topics
 Recursive Algorithm for Computing gcd

 Counting

 Product Rule

 Example of Sum Rule

 Sum Rule

 Examples of Sum Rule


Recursive Algorithm for Computing gcd
We can base a recursive algorithm on the reduction gcd(a, b) = gcd(b mod a, a)
and the condition gcd(0, b) = b when b > 0. This produces the procedure in
Algorithm 3, which is a recursive version of the Euclidean algorithm
Example of Recursive Algorithm for gcd

Find the gcd (a,b) = gcd (b mod a, a) when input a=5, b=8.
With this input,
gcd(5, 8) = gcd(8 mod 5, 5) = gcd(3, 5).
gcd(3, 5) = gcd(5 mod 3, 3) = gcd(2, 3),
gcd(2, 3) = gcd(3 mod 2, 2) = gcd(1, 2),
gcd(1, 2) = gcd(2 mod 1, 1) = gcd(0, 1).

Finally, to find gcd(0, 1) it uses the first step with a = 0 to find that gcd(0, 1) = 1.
Consequently, the algorithm finds that gcd(5, 8) = 1.
Recursive Algorithm for Computing gcd(a, b)

procedure gcd(a, b: nonnegative integers with a < b)

if a = 0 then return b

else return gcd(b mod a, a)

{output is gcd(a, b)}


Counting

 There are two basic Counting principles

 The Product Rule

 The Sum Rule

 The Subtraction Rule

 The Division Rule


Product Rule

Suppose that a procedure can be broken down into a sequence of two


tasks. If there are n1 ways to do the first task and for each of these ways
of doing the first task, there are n2 ways to do the second task, then
there are n1n2 ways to do the procedure.
Example 1
A new company with just two employees, Nasir and Saleem, rents a floor of a
building with 12 offices. How many ways are there to assign different offices to
these two employees?

Solution:
The procedure of assigning offices to these two employees consists of assigning an
office to Nasir, which can be done in 12 ways, then assigning an office to Saleem
different from the office assigned to Nasir, which can be done in 11 ways.

By the product rule, there are 12 · 11 = 132 ways to assign offices to these two
employees.
Example 2:

The chairs of an auditorium are to be labeled with an uppercase English letter


followed by a positive integer not exceeding 100. What is the largest number of
chairs that can be labeled differently?

Solution:
The procedure of labeling a chair consists of two tasks, namely, assigning to the
seat one of the 26 uppercase English letters, and then assigning to it one of the
100 possible integers. The product rule shows that there are 26 · 100 = 2600
different ways that a chair can be labeled. Therefore, the largest number of
chairs that can be labeled differently is 2600.
Example 3:

There are 32 microcomputers in a computer center. Each microcomputer has 24


ports. How many different ports to a microcomputer in the center are there?

Solution:
The procedure of choosing a port consists of two tasks, first picking a
microcomputer and then picking a port on this microcomputer. Because there are
32 ways to choose the microcomputer and 24 ways to choose the port no matter
which microcomputer has been selected, the product rule shows that there are
32 · 24 = 768 ports.
Example 4:

How many different bit strings of length seven are there?

Solution:
Each of the seven bits can be chosen in two ways, because each bit is either 0 or 1.
Therefore, the product rule shows there are a total of 27 = 128 different bit strings
of length seven.
Example 5

 How many different license plates can be made if each plate contains a
sequence of three uppercase English letters followed by three digits (and no
sequences of letters are prohibited, even if they are obscene)?

Solution: There are 26 choices for each of the three uppercase English letters
and ten choices for each of the three digits. Hence, by the product rule there are
a total of
26 · 26 · 26 · 10 · 10 · 10 = 17,576,000
possible license plates.
ABB962
The Sum Rule

If a task can be done either in one of n1 ways or in one of n2 ways, where


none of the set of n1 ways is the same as any of the set of n2 ways, then
there are n1 + n2 ways to do the task.
Example 1 of Sum Rule

A student can choose a computer project from one of three lists. The three lists
contain 23, 15, and 19 possible projects, respectively. No project is on more than
one list.
How many possible projects are there to choose from?

Solution: The student can choose a project by selecting a project from the first
list, the second list, or the third list.
Because no project is on more than one list, by the sum rule there are
23 + 15 + 19 = 57 ways to choose a project.
Example 2
 Suppose that either a member of the mathematics faculty or a student who is
a mathematics major is chosen as a representative to a university committee.
How many different choices are there for this representative if there are 37
members of the mathematics faculty and 83 mathematics majors and no one
is both a faculty member and a student?

Solution:
There are 37 ways to choose a member of the mathematics faculty and there are
83 ways to choose a student who is a mathematics major. Choosing a member of
the mathematics faculty is never the same as choosing a student who is a
mathematics major because no one is both a faculty member and a student. By
the sum rule it follows that there are
37 + 83 = 120 possible ways to pick this representative.
The Subtraction Rule

If a task can be done in either n1 ways or n2 ways, then the number of ways to
do the task is n1 + n2 minus the number of ways to do the task that are common
to the two different ways.

|A1 ∪ A2|=|A1|+|A2|−|A1 ∩ A2|


Example of Subtraction
A computer company receives 350 applications from computer graduates for a job planning a
line of new Web servers. Suppose that 220 of these applicants majored in computer science,
147 majored in business, and 51 majored both in computer science and in business. How
many of these applicants majored neither in computer science nor in business?
Solutions
To find the number of these applicants who majored neither in computer science nor in
business, we can subtract the number of students who majored either in computer science or
in business (or both) from the total number of applicants.
Let A1 be the set of students who majored in computer science and A2 the set of students
who majored in business. Then A1 ∪ A2 is the set of students who majored in computer
science or business (or both), and A1 ∩ A2 is the set of students who majored both in
computer science and in business. By the subtraction rule the number of students who
majored either in computer science or in business (or both) equals
|A1 ∪ A2|=|A1|+|A2|−|A1 ∩ A2| = 220 + 147 − 51 = 316.
We conclude that 350 − 316 = 34 of the applicants majored neither in computer science nor
in business.
Division Rule

There are n/d ways to do a task if it can be done using a procedure that can be
carried out in n ways, and for every way w, exactly d of the n ways correspond to
way w.
Example of Division Rule

How many bit strings of length four do not have two consecutive 1s?

Solution:
The binary combination, displays all bit strings of length four without two
consecutive 1s. We see that there are eight bit strings of length four without two
consecutive 1s.
0001, 0010, 0100, 1000,1001,1010, 0101, 0000

You might also like