DM_lect_12-boolean algebra

Download Report

Transcript DM_lect_12-boolean algebra

461191 Discrete Math
Lecture 12: Boolean Algebra
San Ratanasanya
CS, KMUTNB
Today’s topics




Review of Trees
Boolean Algebra
Quizzes
Final Exam is in 3 weeks! Please be
prepared!
2
Trees





No circuit
No multiple edges
No loops
Simple Graph
Tree คือ Connected Undirected Graph ที่ไม่มี Simple Circuit
สำหรับ Undirected Graph ที่ไม่มี Simple Circuit แต่ไม่ Connect
กัน เรำสำมำรถเรี ยกว่ำเป็ น Tree หลำยอัน และในกรณี น้ ี เรำเรี ยก
Tree เหล่ำนั้นว่ำ Forest
3
Rooted Tree







เป็ น Tree ที่มีหนึ่ง Vertex กำหนดให้เป็ น Root และทุกๆ Edge จะมีทิศทำง
ออกมำจำก Root นั้น
Root
Ancestor, Descendant
Parent, Child, Sibling
Internal Vertex, Leaf
Subtree
Level, Height
4
More Trees



M-ary Tree คือ Tree ที่ทุกๆ Internal Vertex มี Childrenไม่เกิน m children
และจะเรี ยกว่ำเป็ น Full M-ary Tree ก็ต่อเมื่อ ทุกๆ Internal Vertex มี m
children
Ordered Root Tree คือ Rooted Tree ที่ Children ในแต่ละ Internal Vertex
มีกำรจัดลำดับ ซึ่งปกติเรำจะจัดเรี ยงจำกซ้ำยไปขวำ
Balanced Tree คือ Tree ที่มี Leaf อยูท่ ี่ Level h หรื อ h-1 เท่ำนั้น
5
Examples
6
Tree Properties




Tree ทีม
่ ี n vertex จะมี n-1 edges
Full m-ary Tree ทีม
่ ี i Internal Vertices จะมี vertex ทัง้ หมด n = mi + 1
vertex
 ถ้า Full m-ary Tree มี n vertices, จะมี i = (n - 1)/m internal
vertices และ l = ((m - 1)n + 1)/m leaves
 ถ้า Full m-ary Tree มี i internal vertices, จะมี n = mi + 1 vertices
และ l = (m - 1)i + 1 leaves
 ถ้า Full m-ary Tree มี l leaves, จะมี n = (ml – 1)/(m – 1) vertices
และ i = (l - 1)/(m - 1) internal vertices
M-ary Tree ทีม
่ ีความสูง h จะมี leaf ได้มากทีส
่ ด
ุ เท่ากับ mh
M-ary Tree ทีม
่ ีความสูง h และมี l leaves, จะได้วา่ h  logml
ถ้า
เป็ น Full M-ary Balanced Tree, h = logml
7
Tree Modeling and
Applications




Binary Search Tree
Decision Tree
Prefix Code: Huffman Coding
Game Trees
8
Tree Traversal



Preorder
Inorder
Postorder
723*-4^93/+ = ?
9
Spanning Tree (ต ้นไม ้ทอดข ้าม)


ถ้ำให้ G เป็ น Simple Graph แล้ว เรำจะเรี ยก Tree ที่ประกอบไปด้วย vertex ทุก
vertex ใน G ว่ำ Spanning Tree ซึ่ งก็คือ Subgraph ของ G นัน่ เอง จะเห็นได้วำ่ เรำ
สำมำรถหำ Spanning Tree ของ G ได้หลำยอัน
เรำสำมำรถหำ Spanning Tree ได้จำก Depth-First Search หรื อ Breadth-First
Search
a
e
a
b
c
f
d
g
a
e
b
f
c
d
g
b
c
d
a
b
c
d
g
e
G
f
c
d
a
e
b
f
g
e
f
g
10
Minimum Spanning Tree

ถ้ำหำกว่ำ Graph มีน้ ำหนักแล้ว Spanning Tree ก็จะมีน้ ำหนักด้วย ดังนั้น Minimum
Spanning Tree คือ Spanning Tree ที่มีน้ ำหนักน้อยที่สุด ซึ่ งคำนวณจำกผลรวมของ
น้ ำหนักของเส้นเชื่อมทั้งหมด โดยทัว่ ไปเรำจะหำ Minimum Spanning Tree จำก




Prim’s Algorithm หรื อ
Kruskal’s Algorithm
หลักกำรของทั้ง 2 วิธีกำรดังกล่ำวคล้ำยกัน ต่ำงกันตรงที่ Kruskal’s จะเลือก edge ที่มี
น้ ำหนักน้อยที่สุดโดยไม่สนใจว่ำจะสำมำรถเชื่อมต่อกับ edge ที่มีอยูแ่ ล้วได้หรื อไม่ก่อน
Kruskal’s จะมีประสิ ทธิ ภำพดีกว่ำ Prim’s เมื่อมีจำนวน edge น้อยๆ หรื อกรำฟมี
ลักษณะแผ่กว้ำง (Sprase)
11
Examples
a
2
3
e
a
2
3
e
3
1
4
4
i
b
f
4
3
g
3
j
3
h
k
Prim’s Algorithm
2
3
g
3
4
3
j
5
3
1
k
h
l
a
2
e
l
i
b
3
1
4
4
3
1
2
3
d
1
3
5
4
f
c
d
1
2
2
3
c
3
1
4
i
b
f
3
g
j
5
3
4
3
d
1
2
2
3
c
h
3
1
k
l
Kruskal’s Algorithm
12
Boolean Algebra
Boolean Algebra

้
เป็ นกฎทีใ่ ชในการท
างานกับ set { 0, 1 } หรือ { F, T }

่ x, y
Boolean Literal = Boolean Variable เชน

การดาเนินการทีใ่ ชบ่้ อยทีส
่ ด
ุ 3 อย่าง ได ้แก่

Complement


10
1·1 = 1,
1·0 = 0,
0·1 = 0,
0·0 = 0
0+1 = 1,
0+0 = 0
Boolean sum (OR)


and
Boolean product (AND)


0 1
1+1 = 1,
1+0 = 1,
ลาดับของตัวดาเนินการ (Precedence): complement, AND, OR
14
Example: 1 0  (0  1)
1 0  (0  1)  1 0  1
 1 0  0
 00
0
15
Boolean Algebra v.s.
Compound Proposition
1 0  (0  1)  0
(T  F )  ( F  T )  F
16
Boolean Identities
17
Boolean Identities
18
Exercise
1
จงหาค่าของนิพจน์ตอ
่ ไปนี้
คำตอบ:
2
A) 1 , B) 1, C) 0, D) 0
จงหาค่าของ x ทีท
่ าให้นิพจน์เหล่านี้เป็ นจริง
คำตอบ:
A)
B)
C)
D)
X=0
X= 0
X = 0,1
None
19
Exercise
3
คำตอบ:
0·1 + 1·1
0+1 = 1
จงเปลีย่ นสมการในข้อ a) ให้เป็ นประพจน์ทม
ี่ ีความหมายเดียวกัน
โดยให้ 0 = False, 1 = True, Boolean sum = disjunction,
Boolean product = conjunction, Boolean complementation = negation,
และ Equal sign = Equivalence sign
คำตอบ:
(~TΛ~F) V (T·~F) = T
20
Exercise
4
คำตอบ:
21
22
Exercise
5
4
Answer:
23
Exercise
Answer:
Discrete Mathematics for Computer Science
24
Exercise
6
Answer: 1. X=0, Y=0
2. X=1, Y=1
7
Answer:
25
Exercise
8
Answer: a)
b)
c)
d)
x
0
1
26
Miscelleneous



Functional Completeness คือ set ของ Boolean Operator ที่สำมำรถแสดง Boolean Function ได้ครบ
ทุกรู ปแบบ เช่น {+, , ¯}, {+, ¯}, {, ¯} แต่ {+, ¯} ไม่ใช่ Functionally Complete
จำนวนของ Boolean Function = subset ของ power set ของ จำนวน literal (degree)
ภำวะคู่กนั (Duality) ed ของ Boolean expression e สร้ำงได้จำก e โดยกำร


เปลี่ยน + เป็ น 
เปลี่ยน 0 เป็ น 1

ถ้ำ e1 = e2 แล้ว e1d = e2d

Examples



ถ้ำ x(x+y) = x แล้ว x + xy = x
Boolean Functions ของ degree 6 จะมีจำนวน = 226 = 264 = 18,446,744,073,709,551,616
{ | } (NAND) และ {  } (NOR) เป็ น functionally complete หรื อไม่
27
Logical Gates
Not Gate
28
Logic Gates
29
Combinational Circuits
30
Exercises
31
Adders
Half Adder
Full Adder
32
Adders
33
Representing Boolean Functions

Definitions





Literal (สัญพจน์) หมำยถึง ตัวแปร Boolean ใดๆ หรื อ ส่วนเติมเต็มของตัวแปรนั้นๆ
Minterm ของ ตัวแปร Boolean x1, x2, x3, …, xn คือ ผลคูณ Boolean y1, y2, y3, …, yn เมื่อ yi = xi หรื อ yi = xi
 ดังนั้น minterm ก็คือผลคูณของ n literals เมื่อแต่ละ literal แทนแต่ละตัวแปร
 minterm จะมีท้ ง
ั หมด 2n สำหรับ n ตัวแปร
Maxterm ของตัวแปร Boolean x1, x2, x3, …, xn คือผลบวก Boolean y1, y2, y3, …, yn เมื่อ yi = xi หรื อ yi = xi
 ดังนั้น maxterm ก็คือผลบวกของ n literals เมื่อแต่ละ literal แทนแต่ละตัวแปร
Sum-of-Products Expansion (Disjunctive Normal Form)
Product-of-Sums Expansion (Conjunctive Normal Form)
Example
จงหำ minterm ที่มีค่ำเป็ น 1 เมื่อ
Answer:
34
Sum-of-Products expansion
ผลรวมของ minterm ที่แสดงถึง Boolean Function เรี ยกว่ำ กำรขยำยผลรวม
ของผลคูณ (Sum-of-Products expansion) หรื อ Disjunctive Normal Form
Solution 1
35
Sum-of-Products expansion
Solution 2
36
Product-of-sums expansion
ผลคูณของ maxterm ที่แสดงถึง Boolean Function เรี ยกว่ำกำรขยำยผลคูณของ
ผลรวม (Product-of-Sums expansion) หรื อ Conjunctive Normal Form
37
Minimizing Circuits



เป้ ำหมำยของกำรลดรู ปวงจร (Minimizing Circuits) ก็เพื่อลดจำนวนของ Logic Gate
เพื่อให้เหลือแต่ที่จำเป็ นกับวงจรเท่ำนั้น โดยที่ไม่ทำให้ Boolean Function
เปลี่ยนแปลง ซึ่ งกำรลดรู ปวงจรนี้ จะทำให้จำนวน transistor ที่ใช้ลดลง ขนำดของ
chip เล็กลง และ รำคำก็ถกู ลงด้วย
เรำสำมำรถใช้ DNF ในกำรลดรู ปวงจรได้
แต่ในบทนี้เรำจะศึกษำกำรใช้ แผนผังคำร์ โนห์ (Karnaugh Map or K-Map) และ
Quine-McCluskey Method ในกำรลดรู ปวงจร
38
39
K-Map

K-Map เป็ นกำรใช้แผนภำพที่แสดง minterm ของ boolean function และใช้ในกำรจับกลุ่มของ
minterm เพื่อที่จะทำกำรลดรู ปของ minterm โดยมีหลักสำคัญดังนี้




กำรจับกลุ่มจะต้องจับระหว่ำง cell ที่อยูต่ ิดกันเท่ำนั้น นัน่ คือ cell ที่มี literal ต่ำงกันเพียง 1 ตัว
ให้จบั กลุ่มที่ใหญ่ที่สุดก่อน โดยสมำชิกของกลุ่มจะมีจำนวนได้ตำม 2i เมื่อ i > 0
จำกนั้นทำกำรกำจัด literal ที่มีกำรเปลี่ยนแปลงในแต่ละกลุ่มออก เหลือไว้แต่ literal ที่ไม่มีกำรเปลี่ยนแปลง
ในกำรจับกลุ่มควรคำนึงถึง glitch (สัญญำณหลอก) ที่จะเกิดขึ้นเมื่อทำกำรลดรู ปวงจรแล้วด้วย โดยพยำยำมจับกลุ่ม
ให้ไม่เกิด glitch หรื อเกิดน้อยที่สุด (รำยละเอียดใน Digital Circuit) นัน่ คือกำรจับกลุ่มโดยไม่ให้มี minterm ตัว
เดียวกันเป็ นสมำชิกของหลำยกลุ่มมำกเกินไป หรื อจับกลุ่มให้ได้จำนวนกลุ่มน้อยที่สุด
40
Implicant
Essential Prime Implicant
Prime Implicant
41
42
K-Map : Don’t Care Conditions
43
Quine-McCluskey


เนื่องจำก K-Map ไม่สำมำรถนำมำใช้กบั กำรลดรู ปวงจรที่มี literal จำนวนมำก
(degree  4) ได้อย่ำงมีประสิ ทธิ ภำพเท่ำที่ควรเนื่องจำกต้องเสี ยเวลำในกำรวำด
แผนภำพ ในช่วง 1950s Quine และ McCluskey ได้คิดวิธีกำรแก้ไขปั ญหำนี้ โดยไม่
ต้องใช้แผนภำพ
วิธีกำรนี้แบ่งออกเป็ น 2 ส่ วน



หำ minterm ทั้งหมดที่น่ำจะนำมำใช้ในกำรลดรู ปวงจรได้ โดยจะต้องมี literal ต่ำงกันเพียง 1
ตัวเท่ำนั้น และทำกำรจับกลุ่มพร้อมลดรู ปให้เหลือ n-2 literal
ทดสอบว่ำ minterm ใดที่สมควรนำมำใช้ในกำรลดรู ปวงจร โดยทำกำรตรวจสอบจำกกลุ่มที่จบั
กันแล้วในข้ำงต้นว่ำต้องใช้กลุ่มใดบ้ำงเพื่อให้ครอบคลุม minterm ทั้งหมดและไม่ทำให้
boolean function เปลี่ยนไป
ขั้นตอนโดยละเอียดของวิธีกำรนี้สำมำรถศึกษำได้จำก Sec. 11.4 page 779
44
Example

จงทำกำรลดรู ปของ Boolean Function xyz  x yz  xyz  x yz  x y z ด้วยวิธี QuineMcCluskey
pair up
pair up
pair up
45
 z  xy
46
Example

จงใช้ Quine-McCluskey เพื่อทำกำรลดรู ป DNF ต่อไปนี้
wxy z  wxyz  wxy z  wxyz  wx yz  wxyz  wx yz
47
=
wz  wy z  wxy หรือ wz  wy z  xyz
48
Homework 11

11.1.11, 11.2.12, 11.1.24-27,11.2.7-11, 11.2.15-16, 11.3.10-11,
11.3.15-18, 11.4.12, 11.4.25, 11.4.30-32
49
Programming Assignment 4
1.
2.
3.
4.
5.
6.
จงเขียนโปรแกรมเพื่อหำ Shortest path ด้วย Dijkstra’s Algorithm
จงเขียนโปรแกรมเพื่อหำ Minimum Spanning Tree ด้วย Prim’s และ Kruskal’s Algorithm
จงเขียนโปรแกรมเพื่อถอดรหัส Huffman โดยใช้ตำรำงในแบบฝึ กหัด 10.2.27
จงเขียนโปรแกรมเพื่อแสดง Boolean Function ที่เป็ นไปได้ท้ งั หมดสำหรับ Boolean Function ที่มี
degree ไม่เกิน 3
จงเขียนโปรแกรมเพื่อตรวจสอบควำมถูกต้องของข้อควำมตำมหลักภำษำในแบบฝึ กหัด 12.3.22
จงเขียนโปรแกรมเพื่อตรวจสอบควำมถูกต้องของข้อควำมตำมหลักภำษำในแบบฝึ กหัด 12.3.48
วิธีการส่ งงาน:
- ส่งทำง e-mail ใช้ชื่อ Subject ว่ำ 461191 Programming Assignment 3
- ให้ทำกำรบีบอัดไฟล์ต่ำงๆเป็ นไฟล์เดียว โดยใช้ format เป็ น zip หรื อ rar
- ไฟล์ที่ทำกำรบีบอัดให้ใช้ชื่อไฟล์เป็ นรหัสนศ.
Each program should be submitted with a report showing
its code and how it works (some screen shorts plus descriptions).
50