0% found this document useful (0 votes)
9 views34 pages

Chapter 5 - Counting

Chapter 5 covers counting techniques including the product rule, inclusion-exclusion principle, and advanced counting methods such as recurrence relations. It discusses practical examples like password combinations, bit strings, and the Tower of Hanoi problem. Additionally, it highlights the complexity of divide-and-conquer algorithms and their applications in finding maximum values and sorting.

Uploaded by

chunggardian222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views34 pages

Chapter 5 - Counting

Chapter 5 covers counting techniques including the product rule, inclusion-exclusion principle, and advanced counting methods such as recurrence relations. It discusses practical examples like password combinations, bit strings, and the Tower of Hanoi problem. Additionally, it highlights the complexity of divide-and-conquer algorithms and their applications in finding maximum values and sorting.

Uploaded by

chunggardian222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CHAPTER 5 - COUNTING

• Choose a quiz password: ***


* can be chosen from {a, b, c, d}
• How many possible passwords ?
Product rule
Task = task 1 AND task 2

2
Task 1: choose one


Task 2: choose one

4
Product rule
THE PRODUCT RULE

Task = task1 ➔ task 2 ➔ task 3 ➔ … ➔ task k


task 1: n1 ways
task 2: n2 ways

Task k: nk ways
----------------------------
Product rule: n1.n2…nk ways to do the task
Product rule
Example 1. A new company with just two
employees, A and B, rents a floor of a building
with 12 offices.
How many ways are there to assign different
offices to these two employees?
➔ 2 tasks: 12. 11
Example 2. How many different bit strings of
length seven are there?
➔ 7 tasks: 2.2.2.2.2.2.2 =27
How many functions are there from {a, b, c}
to {1, 2, 3, 4, 5} ?

function Input Output

A• •1
B• •2
C• •3
•4
•5
Example – counting passwords
• Each user on a computer system has a
password, which has properties:
• six to eight characters long
• character is an uppercase letter or a digit
• contain at least one digit
• How many possible passwords are there?
• Result = (366 – 266) + (367-267) + (368 – 268)
all all invalid cases = cases without digit

6 characters ******
Exercises
1/ If there are 5 multiple-choice questions on
an exam, each having four possible answers,
how many different sequences of answers are
there?
2/ In how many ways can a teacher seat 5
girls and 3 boys in a row seats if a boy must be
seated in the first and a girl in the last seat?
Exercises
1/ How many positive divisors does 120 have?

2/ A = {1, 2, 3, 4, 5, 6}
a. How many subsets of A can be
constructed?
b. How many subsets of A that contain
1?
c. How many subsets neither contain 3
nor 4?
How many integers between 10
and 30 inclusive are divisible by
3 or 7?
The principle of Inclusion-
exclusion

|AB| = |A| + |B| - |AB|

•a
•f
A
•b •d B
•e
•c

| AB | = |A| + |B| - |AB| = 4 + 3 - 1


The principle of Inclusion-
exclusion
• How many bit strings of length eight either
start with a 1 bit or end with the two bits 00?

1******* 1*****00 ******00

27 ways 26 ways
25 ways

• Result = 27 + 26 - 25
Advanced counting techniques

• P0: initial deposit


• r: interest rate per year
• Pn: amount of money after n years
• Pn = (1+r)Pn-1
• Pn = (1+r)nP0
Advanced counting techniques
Readings:
• Tower of Hanoi problem:
Q1. How many moves if n = 5?
Q2. How to move?
• Solution of a recurrence relation:
Q3. is an = 3n a solution of an = 2an-1 – an-2?
• Divide and conquer algorithm:
Q4. How to estimate complexity of a divide and
conquer algorithm?
Q5. If f(n) = 2f(n/2) + 1 ➔ f(n) is O(?)
The Tower of Hanoi Problem- Ex5, page 452

How many steps


this problem is
solved if there is n
disks on the peg 1?
Tower of Hanoi – 3 disks
Peg1 Peg2 Peg3

• 3 disks → 7 steps
Tower of Hanoi problem
• n = 1 disk  H1 = 1 step
• n = 2 disks:
Peg1 → Peg3
Peg1 → Peg2
Peg2  Peg3
 H2 = 3 steps
• n = 3 disks  H3 = 7 steps
• n disks  Hn = ? // number of steps for n disks
How many steps
Tower of Hanoi problem -

for n = 4 and more?


Peg1 Peg2 Peg3

3 disks: H3 = 7 steps

1 disk: 1 step 3 disks: H3 = 7 steps

Number of steps for 4 disks:


H4 = 2H3 + 1 = 15
Tower of Hanoi problem with n
disks
• Hn= 2Hn-1 + 1 // recurrence relation
• H1 = 1 // initial condition
•  Hn = 2n – 1, n  1 // solution
• n = 64 → H64 = 264-1
(=18 446 744 073 709 551 615)
1 move/sec → more than 500 billion years.
• Complexity O(2n)
How many bit strings of length n
that not have two consecutive 0s
• n : length of bit string
• an: number of such bit strings of length n
n all 2n bit strings of length n an
1 0, 1 a1 = 2
2 00, 01, 10, 11 a2 = 3
3 000, 001, 010, 011, a3 = 5
100, 101, 110, 111
4 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, a4 = 8
1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111
… … …
n an-2: number of such strings begin with 0 (in fact, an = an-2 + an-
01) 1
an-1: number of such strings begin with 1 (Fibonacci)
Recurrence Relations
• Recurrence relation:
• Hn = 2Hn-1 + 1
• an = an-1 + an-2

• Initial conditions:
• H1=1
• a1=2, a2=3
Recurrence Relations
• Example: Determine whether {an} = 3n is a solution
of the recurrence relation
an= 2an-1 – an-2, n ≥ 2?
• an = 3n  an-1 = 3(n-1) and an-2 = 3(n-2)
• The right-hand side = 2an-1 – an-2 = 2.3(n-1) – 3(n-2)
• The right-hand side = 3n = an
• Left-hand side = Right-hand side
•  an is a solution of the recurrence relation
Divide-and-Conquer Algorithms
and recurrence Relations

• Divide: Dividing a problem into one or more


instances of the same problem of smaller size

• Conquer: Using the solutions of the smaller


problems to find a solution of the original
problem, perhaps with some additional work.
Recurrence Relations for Binary Search
procedure bsearch(x, i, j) need f(n)
if i > j comparisons

return 0
else
m= (i+j)/2
if x= am
return m
else if x< am
bsearch(x, i, m-1) need f(n/2)
else comparisons
bsearch(x, m+1, j)

f(n) = f(n/2) +2
Recurrence Relations for Finding Maximum of a
sequence
procedure findmax(i,j: integer ,ai,a2+1,…,aj: need f(n)
comparisons
integers)
if i=j
return (ai) f(n) = 2f(n/2) +1
else
m:= (i+j)/2
max1:= findmax (i,m,ai,ai+1,…,am) need f(n/2)
comparisons
need f(n/2)
max2:= findmax (m+1,j,am+1,am+2,…,aj) comparisons
if max1 > max2
return max1
else
return max2
Theorem. [ … ]
f(n)= af(n/b) + c ➔

a=1 c=2

b=2

Ex.
f(n) = f(n/2) + 2

➔ f(n) is O(logn)

?
O(logn) time complexity
Theorem. [ … ]
f(n)= af(n/b) + c ➔

What is big-O time


complexity of the
findmax algorithm?
Complexity of merge sort

f(n) = 2f(n/2) + n

a = bd: f(n) is O(n1logn)


A Demonstration: Closest-Pair Problem

• n points in the plane. How to determine the closest-pair of points?


• (1) Determine the distance of every pair of points.
• (2) Determine the pair of points that have minimum distance.
• ➔ C(n,2)= n(n-1)/2= O(n2)
• Michal Samos proposed an approach that is O(nlogn) only.
• Michal Samos’s approach
(1) Sorting points in order of increasing x coordinates → O(nlog(n))
(2) Sorting points in order of increasing y coordinates → O(nlog(n))
An Demonstration: Closest-Pair Problem
(3) Using recursive approach 16 points
to divide the problem into 2
subproblem with n/2 points
(left and right points based
on x coordinates). Let ℓ is
the line that partitions two
subproblems. If there is any
point on this dividing line, we
decide these points among
the two parts if necessary)
(4) Finding out closest-pair of
points in two side ( dL, dR)
(5) Let d=min(dL, dR)
8 points 8 points
An Demonstration: Closest-Pair Problem

(6) Studying area [l-d,l+d].


This area may be
contains the result.
(7) Because we sorted
points by their y
coordinate. We
examine for points p in
the strip of width 2d
that has the line l as its
center with upward
direction.
An Demonstration: Closest-Pair Problem

Total number of
points in the strip
does not exceed n
and there are at
most 8 points,
including p, can lie
in or on the 2dxd
rectangle.

➔A point will be computed with 7 others.


➔At most 7n distances need to be compare with d to find the
minimum distance between points.
➔ The increasing function f(n) satisfies the recurrence relation :
f(n) = 2f(n/2) + 7n
➔ By the Master Theorem, it follows that f(n) is O(nlogn)
Summary
• Basic counting rules:
• The Product rule
• The Sum rule
• Inclusion-exclusion principle
• Advanced counting technique: recurrence
relations
– Tower of Hanoi
– Count bit strings of length n that satisfy some
properties
• Complexity of Divide and conquer algorithms
– Finding max, binary search in a recursive version
– Merge sort
• THANKS

You might also like