Upload
imchan
View
538
Download
3
Embed Size (px)
Citation preview
4123402 : 3(2-2) unit Discrete Mathematics and structure
• Euclidean Algo. • Recursive Algo.• Recurrence Relation
• Euclidean Algo. • Recursive Algo.• Recurrence Relation
Discrete Mathematics
The Language of ComputerThe Language of Computer
The Euclidean Algo.The Euclidean Algo.
Discrete Mathematics
4
การหารลงตว
The Euclidean Algo.
หาตวหารรวมมากของจานวนเตม 2 ตว เชน m,n และ m,n <>0
จานวนเตมบวกทใหญทสด ซงหาร m,n ลงตวทงค
a = ตวตง (dividend),b= ตวหาร (divisor) , q = ผลหาร ( quotient) เปนจานวนเตม และ b <> 0 โดยท a = bqจะไดวา b หาร a ลงตว เขยน b | a เชน 3 | 21 = 7
b | a
5
สรปนยามของการหารลงตว
กาหนดให a, b ,q และ r เปนจานวนเตมb | a หมายความวา b หาร a ลงตว เมอ a=bq
a เรยกวา ตวตง (dividend)b เรยกวา ตวหาร (divisor) ซง b ≠ 0q เรยกวา ผลหาร (quotient)
b | a หมายความวา b หาร a ไมลงตว เมอ a = bq+rr เรยกวา เศษ (remainder) ซง 0<r<a
q = b | a และ r = a mod b
6
Division
7
Example Divisors
Q: Which of the following is true?
1. 77 | 72. 7 | 773. 24 | 244. 0 | 245. 24 | 0
A :
8
ทฤษฎบท ถา a เปนจานวนเตมบวก ไมใชคาลบ , b เปนจานวนเตมบวก ,r = a mod b
และ a = bq +r , 0 ≤ r ≤ bจะได
gcd(a,b) = gcd(b,r)
Euclidean algorithm : common divisor
Example :1. gcd(20,15) 2. gcd(105,30)3. gcd(20,80) 4. gcd(150,60)
9
a,b gcd(a,b)
a b
a b
10
Example : Euclidean Algorithm
Ex 1 gcd(20,15):
0515 mod 5
= 02
51520 mod 15
= 51
1520-0
yxr = x mod yStep
11
Ex 2gcd(33,77):
Example : Euclidean Algorithm
12
Example : Euclidean Algorithm
13
Example : Euclidean Algorithm
14
Example : Euclidean Algorithm
15
Q : Find the following gcd’s (ดสไลดท10 เปนต.ย.)
1. gcd(11,77) 2. gcd(24,36)
3. gcd(24,25) 4. gcd(504,396)
5. gcd(30,105)
Homework
16
Euclidean algorithm : GCD
17
ในกรณท gcd(a,b) = 1 หมายความวา a และ b ไมม
ตวหารรวมใดเลยยกเวน 1
เราเรยก a และ b วา เฉพาะสมพทธ (relatively prime)
Example.gcd(13,31) = 1 13 และ 31 เปนจานวนซงเฉพาะสมพทธ
Relatively prime
18
Least Common Multiple
บทนยาม
ตวคณรวมนอย ของจานวนเตมบวก a และ b หมายถง จานวนเตมบวกเลกทสดซงหารดวย b ลงตว ตวคณรวมนอย ของ a และ b ใชสญลกษณ lcm(a,b)
ทฤษฎบท
ให a และ b เปนจานวนเตมบวก จะได
lcm(a,b) = ab / gcd(a,b)
19
Least Common Multiple
Q: Find the lcm :
1. lcm(10,100)2. lcm(7,5)3. lcm(9,21)
20
Q : Find the following lcm’s6. lcm(11,77) 7. lcm(24,36)
8. lcm(24,25) 9. lcm(504,396)
10. lcm(30,105)Q: จงหาจานวนเตม q และ r เพอให a = bq + r, 0≤r<b
11. a=45 , b=6 12. a=106, b=1213. a=66 , b=11 14. a=221 , b=1715. a=0 , b=31
Homework
Discrete Mathematics
The Language of Computer:The Language of Computer:EoFEoF
Recursive Algo.and
Recurrence Relation.
Recursive Algo.and
Recurrence Relation.
Discrete Mathematics
23
Recursive Algorithms
factorial of n defined by
1 if n = 0
n! =
n(n-1)(n-2)…3•2•1 if n ≥ 1
24
Example:
3! = ?
6 ! = ?
โปรดสงเกตวา Factorial n สามารถเขยนในเทอมของตวมนเอง
n! = n(n-1)(n-2)…2•1
= n•(n-1)!
Example:
5! = ?
Recursive Algorithms
25
26
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Compute 5!
27
f(5)=5·f(4)
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
28
f(4)=4·f(3)
f(5)=5·f(4)
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
29
f(3)=3·f(2)
f(4)=4·f(3)
f(5)=5·f(4)
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
30
f(3)=3·f(2)
f(4)=4·f(3)
f(5)=5·f(4)
f(2)=2·f(1)
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
31
f(3)=3·f(2)
f(4)=4·f(3)
f(5)=5·f(4)
f(2)=2·f(1)
f(1)=1·f(0)
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
32
33
f(3)=3·f(2)
f(4)=4·f(3)
f(5)=5·f(4)
f(2)=2·f(1)
1·1=1
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
34
f(3)=3·f(2)
f(4)=4·f(3)
f(5)=5·f(4)
2·1=2
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
35
3·2=6
f(4)=4·f(3)
f(5)=5·f(4)
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
36
4·6=24
f(5)=5·f(4)
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
37
5·24=120
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
38
Recursive Algo:FactorialJava:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Java:long factorial(int n){
if (n<=0) return 1;return n*factorial(n-1);
}
Return 5! = 120
39
A robot can take steps of 1 meter or 2 meters.
We write an algorithm to calculate the number of ways the robot can walk n meters. As examples:
Example 2 Recursive Algo:Robot Walking
Distance Sequence of steps Number of Ways to walk
1 1 1
2 1, 1 or 2 2
3 1,1,1 or 1,2 or 2,1 3
4 1,1,1,1 or 1,1,2 or1,2,1 or 2,1,1 or 2,2 5
40
41
Input: nOutput: walk(n)
procedure robot_walk(n)
if n = 1 or n = 2 then return(n)
return(robot_walk(n-1) + robot_walk(n-2))
end robot_walk
Recursive Algo:Robot Walking
The Language of ComputerThe Language of ComputerRecurrence Relation.Recurrence Relation.
Discrete Mathematics
43
Recurrence Relation
Example :
44
Recurrence Relation
เขยนความสมพนธเวยนเกดของ an
หาคาตอบของ an ในรปแบบปด
รปแบบปดของ an
ปญหาการนบมคาตอบคอ an
เราใชความสมพนธเวยนเกดในการแทนคาตอบของปญหาการนบ จากนนจงคอยหาผลเฉลยทเขยนในรปแบบปดอกทหนง
45
Recurrence Relation
46
Example 1:
Initial condition a0 = 1
Recurrence formula: a n= 1 + 2a n-1 for n > 1First few terms are: ?
Example 2: Initial conditions a0 = 1, a1 = 2
Recurrence formula: a n= 3(a n-1 + a n-2) for n > 2First few terms are: ?
Examples of recurrence relations
47
Example 3: ให {an} คอลาดบซงไดแทนดวยความสมพนธเวยนบงเกดคอ an = an-1 – an-2 สาหรบ n>1 โดยท a0 = 1 และ a1 = 2 จงหาคา ของ a3 และ a4
Examples of Recurrence relations
48
เราสามารถเขยนผลเฉลยของความสมพนธเวยนบงเกดใหอยในรป n ได เชน กาหนดความสมพนธเวยนบงเกดดงน an = an-1 + 1
โดยม ลาดบ an = n+1 เปนผลเฉลยของความสมพนธเวยนบงเกด
Example 1: จงหาวาลาดบ {an} เปนผลเฉลยของความสมพนธเวยนบงเกด an = 2an-1 – an-2 สาหรบ n ≥ 2 เมอ 1) an = 3n สาหรบ n ทกตว ซงเปนจานวนเตม ไมเปนลบหรอไม2) ตอบคาถามเดยวกนเมอ an = 2n
3) ตอบคาถามเดยวกนเมอ an = 5
การเปนผลเฉลยของ Recurrence relations
49
Example 2: ชายคนหนง นาเงน 10,000 บาท ฝากธนาคาร เปนบญชประเภทประจา ไดรบดอกเบย 11% ตอป คดดอกเบยทบตนทกป หลงจาก 30 ป ชายคนนจะมเงนในบญชจานวนเทาใด?
Examples of Recurrence relations
50
Leonardo Fibonacci ป 1175 เปนนกคณตศาสตร
ไดทาการศกษาดานตวเลข พบความสมพนธ ของตวเลขเชงอนกรม โดยนาตวเลขทตดกนมาบวกกน จะไดตวเลขทมคาเพม
ตามลาดบแบบไมรจบ
คาถามดงเดมทเปนตนตอของ Fibonacci Sequence คอ ปญหาการขยายพนธของกระตาย
Fibonacci Sequence
51
คาถาม “เรมตนมกระตายเพยงคหนง (สองเพศ) กระตายคนจะยง
ไมใหลก จนกวามนจะมอายครบ 2 เดอน หลงจากมอายได 2 เดอน
กระตายแตละคจะมลกอก 1 คในแตละเดอน เมอเวลาผานไป
k เดอนจะมกระตายกค (สมมตวากระตายพนธนอายยนมาก ๆ ไม
ตายเลย !)”
fk = fk-1 + fk-2 สาหรบ k ≥ 2 , f0 = 0, f1 = 1
Examples of Fibonacci Sequence
52
Towers of HanoiStart with three pegs numbered 1, 2 and 3 mounted on a board, n disks of different sizes with holes in their centers, placed in order of increasing size from top to bottom.
Object of the game: find the minimum number of moves needed to have all n disks stacked in the same order in peg number 3.
53
Start with all disks stacked in peg 1 with the smallest at the top and the largest at the bottom
Use peg number 2 for intermediate steps
Only a disk of smaller diameter can be placed
on top of another disk
Rules of the game:Towers of Hanoi
54
Game ends when all disks are stacked in peg number 3 in the same order they were stored at the start in peg number 1.
Let Hn denote the number of moves needed to solve the Tower of Hanoi problem with n disks. Set up a recurrence relation for the sequence {Hn }
Start End
End of game:Towers of Hanoi
55
เทคนคการหาสตรชดแจง ของความสมพนธเวยนบงเกด
ทฤษฎบท 1
Sn = aSn-1 + bSn-2
a and b are constants
Defined S0 and S1
Solving Recurrence Relations
56
Case 1 : b = 0
Sn = anS0 , สาหรบทกคา n ∈ N
Case 2 : a = 0
S2n = bnS0
Solving Recurrence Relations (cont.)
Example :
57
Case 3 : a ≠ 0 and b ≠ 0
Sn = crn , สาหรบคาคงท c บางตว
ถาสงนเปนจรง
rn = arn-1 + brn-2
พสจนสตร
Solving Recurrence Relations (cont.)
58
ทฤษฎบท 2
Sn = aSn-1 + bSn-2
ทมสมการลกษณะเฉพาะ
x2 – ax –b = 0
เมอ a และ b เปนคาคงท ไมใชศนย
Solving Recurrence Relations (cont.)
59
(a) ถาสมการลกษณะเฉพาะ มผลเฉลยเปน r1 และ r2 แตกตางกนจะได
Sn = c1r1n + c2r2
n
(b) ถาสมการลกษณะเฉพาะ มผลเฉลยเพยงหนงตวเทานน คอ r จะได
Sn = c1rn + c2•nrn
Example :
Solving Recurrence Relations (cont.)
60
Linear Homogeneous Recurrence Relations of Degree k with Constant Coefficients
Example :
ความสมพนธเวยนบงเกด Pn = (1.11)Pn-1
- ความสมพนธเวยนบงเกดเอกพนธเชงเสนขององศา เทากบ หนงความสมพนธเวยนบงเกด fn = fn-1 + fn-2
- ความสมพนธเวยนบงเกดเอกพนธเชงเสนขององศา เทากบ สองความสมพนธเวยนบงเกด an = an-5
- ความสมพนธเวยนบงเกดเอกพนธเชงเสนขององศา เทากบ หา
61
Solving Linear Homogeneous Recurrence Relations with Constant Coefficients
พจารณาผลเฉลยของรปแบบ an = rn , r is constant
an = rn คอผลเฉยของความสมพนธเวยนบงเกดดงน
an = c1an-1 + c2an-2 +…+ ckan-k
characteristic equation (สมการลกษณะเฉพาะ)
characteristic roots (คาเจาะจง)
62
Solving Linear Homogeneous Recurrence Relations with Constant Coefficients (cont.)
Example :
63
Example :
Example :
Solving Linear Homogeneous Recurrence Relations with Constant Coefficients (cont.)
64Example :
Solving Linear Homogeneous Recurrence Relations with Constant Coefficients (cont.)
65
Homework1. จงหาหาเทอมแรกของลาดบ ซงนยามโดยความสมพนธเวยนบงเกด
an = nan-1 + a2n-2 , a0=1, a1=1
2. จงแสดงใหเหนวาลาดบ an = (-4)n คอผลเฉลยของความสมพนธเวยนเกด an = -3an-1 + 4an-2
3. สมมตวาจานวนแบคทเรยใน โคโลนแหงหนง เพมจานวนเปน 3 เทาทกชวโมง1) จงหาความสมพนธเวยนบงเกด สาหรบ จานวนแบคทเรย หลงผานไป n ชวโมง
2) ถามแบคทเรยจานวน 100 ตว เมอตอนเรมตน โคโลนใหม หลงจาก 10 ชวโมงจะมจานวนแบคทเรย ในโคโลน จานวนเทาใด
4. จงหาสตรชดแจง สาหรบ Sn เมอ S0=3 และ Sn= -2Sn-1 สาหรบ n≥1
5. จงหาผลเฉลยของ ความสมพนธเวยนบงเกด an=5an-1-6an-2 for n≥2,a0=1,a1=0
Discrete Mathematics
The Language of Computer:The Language of Computer:EoFEoF