610 likes | 751 Views
Disciplined Software Engineering Lecture #9. Watts S. Humphrey Software Engineering Institute Carnegie Mellon University Pittsburgh, PA 15213 Sponsored by the U.S. Department of Defense. Design Notation - Overview. The importance of notation Boolean algebra Karnaugh maps useful symbols
E N D
Disciplined Software Engineering Lecture #9 • Watts S. Humphrey • Software Engineering Institute • Carnegie Mellon University • Pittsburgh, PA 15213 • Sponsored by the U.S. Department of Defense
Design Notation - Overview • The importance of notation • Boolean algebra • Karnaugh maps • useful symbols • using Boolean functions • Simplification • Formal methods
The Importance of Notation • Many software development defects are caused by misunderstandings and confusion. • The notation used to represent program requirements and designs should be • precise and concise • convenient to use • commonly understood • suitable for representing a broad range of software functions • Formal notations meet these criteria.
Class Properties- Mathematical Notation English Symbol Example Union, sum A or B, A+B Intersection, product A and B, A*B, AB Complement, not ' A', not A All, universal 1 None, empty set 0
Boolean Algebra • The algebra of classes • defines and manipulates class relationships • consistency - whether statements are self- consistent • validity - whether statements represent reality • Class representations • Venn diagrams • basic relationships • the universal and empty sets
Venn Diagrams Humans Humans Non-adult Women Women Non- adult Men Men Adult Men Adult Women Universal Class A’B’C’ A’BC’ AB’C’ ABC’ ABC AB’C A’BC A’B’C
Properties of Classes - 1 • Reflexive: for every x, x<=x • Idempotent: xx=x, x+x=x • Antisymmetric: if x<=y and y<=x then x=y • Transitive: if x<=y and y<=z then x<=z • Commutative: xy=yx, x+y=y+x • Associative: x(yz)=(xy)z, x+(y+z)=(x+y)+z • Distributive: x(y+z)=xy+xz, x+yz=(x+y)(x+z)
Properties of Classes - 2 • Empty and universal classes: • xx’=0, x+x’=1, 0<=x<=1 • x0=0, x+1=1, x+0=x, x1=x • Consistency: x<=y, xy=x, x+y=y • are equivalent • Absorption: x(x+y)=x+xy=x • DeMorgan’s theorem: • (x’)’=x, (xy)’=x’+y’, (x+y)’=x’y’
An Example • Suppose you had a switch that you wanted to be on under the following conditions • A or B or C are on • not when A and B are on • not when A and C are on • You could represent this as follows • On = (A+B+C)*[(A*B)+(A*C)]’
Simplifying Boolean Expressions • Consider the following guidelines when you simplify Boolean expressions • if there are any primed parenthetical expressions, use DeMorgan’s theorem to remove them • look for common terms that can be factored by the distributive law • make substitutions to reduce complexity • attempt to reduce expressions to a union of intersections
Simplification Example - 1 • Use the light bulb example • On = (A+B+C)*[(A*B)+(A*C)]’ • Apply DeMorgan’s theorem • On = (A+B+C)*[(A’+B’)*(A’+C’)] • Now, A’ is common to the last terms so • On = (A+B+C)*[A’+(B’*C’)] • Now, substitute x for B+C, and x’ for B’*C’ • On = (A+x)*(A’+x’)
Simplification Example - 2 • While this expression is fully simplified, it should be converted to a union of intersections • On = (A+x)*(A’+x’) • On = [(A+x)*A’]+[(A+x)*x’] • On = [(A*A’)+(x*A’)]+[(A*x’)+(x*x’)] • On = (x*A’)+(A*x’) • Now, replacing x with B+C • On = [(B+C)*A’]+[A*(B’*C’)] • On = (A*B’*C’)+[A’*(B+C)]
Simplification Exercises • Simplify the following expressions • 1. F = X+(X’*Y) • 2. F = (X+Y)+[(X*Z)+Y] • 3. F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) • 4. F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’
Exercise 1 Answer • Starting with F = X+(X’*Y) • Use the distributive law • F = (X+X’)*(X+Y) • Now, with the property of the universal class • X+X’ = 1 AND 1*(X+Y) = X+Y • So the answer is • F = X+Y
Exercise 2 Answer • Starting with F = (X+Y)+[(X*Z)+Y], • Apply the distributive law to give • F = (X+Y)+(X+Y)*(Z+Y) • Now, factor out the common X+Y term • F = (X+Y)*[1+(Z+Y)] • By the property of the universal class, 1+Z+Y=1 • so the answer is • F = X+Y
Exercise 3 Answer • Starting with F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) • Factor out the common Z’ term • F = Z’*(X’Y’+XY+XY’) • Factoring out the common X term • F = Z’*[X’Y’+X(Y+Y’)] = Z’*(X’Y’+X) • Now, with the distributive law • F = Z’*(X’+X)*(Y’+X) = Z’*(Y’+X)
Exercise 4 Answer • Starting with F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’ • First apply DeMorgan’s theorem, giving • F = (X+Y’Z)*(X+Y’+Z)*(X+Y+Z) • Factoring out the common X term gives • F = X+Y’Z*(Y’+Z)*(Y+Z) • Factoring out the common Z term • F = X+Y’Z*(Z+Y’Y) = X+Y’Z
Karnaugh Maps - 1 • For expressions of 6 variables or less, Karnaugh maps can aid the simplification process. • Karnaugh maps are a structured way to represent all possible combinations of the variables in an expression. • Factoring is then done by associating related terms to achieve simplification.
Karnaugh Maps - 2 • Boolean expressions can be represented in minterm form. • The minterms are the products of the variables in all possible combinations. • The minterms for X and Y are: • X’*Y’, X*Y’, X’*Y, XY • There are 2**n minterms for n variables.
Karnaugh Maps - 3 XY’ X’Z YZ YZ X X 00 01 11 10 00 01 11 10 0 0 X X 1 1 X X YZ XY’+X’Z X 00 01 11 10 0 X X 1 X X
A 4-Variable Karnaugh Map YZ WX 00 01 11 10 00 X 01 X X X 11 X X 10
A 5 Variable Karnaugh Map XYZ VW 110 111 101 100 000 001 011 010 00 01 11 10
Map Factoring - 1 • When two adjacent cells both have 1s, then those cells can be factored, eliminating the variable that is different for the two cells. • For example, the 0100 and 0101 cells can be factored, eliminating the variable z. • This is shown on the following map.
Map Factoring - 2 YZ WX 00 01 11 10 00 X 01 X X X 11 X X 10
Map Factoring - 3 • Similarly, when four adjacent cells have 1s, then those cells can be factored, eliminating the two variables that are different for the four cells. • For example, the 0101, 0111, 1101, and 1111 cells can be factored, eliminating the variables w and y. • This is shown on the following map.
Map Factoring - 4 YZ WX 00 01 11 10 00 X 01 X X X 11 X X 10
Map Factoring - 5 • When all three of the possible factorings are made, the resulting expression is • F = w’yz + wxy’ + xz • This is shown on the following map.
Map Factoring - 6 YZ WX 00 01 11 10 00 X 01 X X X 11 X X 10 W’YZ+W’XY’+XZ
Simplification Exercises • Using Karnaugh Maps, simplify the following expressions • 1. F = X+(X’*Y) • 2. F = (X+Y)+[(X*Z)+Y] • 3. F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) • 4. F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’
Exercise 1 Answer YZ The function is F = X+(X’*Y) X 00 01 11 10 0 0 0 1 1 1 1 1 1 1 YZ Factoring this gives F = X+Y X 00 01 11 10 0 0 0 1 1 1 1 1 1 1
Exercise 2 Answer YZ The function is F = (X+Y)+[(X*Z)+Y] = X+Y+XZ+Y = X+Y+XZ X 00 01 11 10 0 0 0 1 1 1 1 1 1 1 YZ Factoring this gives F = X+Y X 00 01 11 10 0 0 0 1 1 1 1 1 1 1
Exercise 3 Answer YZ The function is F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) = X’Y’Z’+XYZ’+XY’Z’ X 00 01 11 10 0 1 0 0 0 1 1 0 0 1 YZ Factoring this gives F = Y’Z’+XZ’ = Z’(Y’+X) X 00 01 11 10 0 1 0 0 0 1 1 0 0 1
Exercise 4 Answer - 1 X+Y’Z The function is F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’ = (X+Y’Z)*(X+Y’+Z)*(X+Y+Z) This can be covered in 3 maps. YZ X 00 01 11 10 0 0 1 0 0 1 1 1 1 1 X+Y’+Z X+Y+Z YZ YZ X 00 01 11 10 X 00 01 11 10 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1
Exercise 4 Answer - 2 The total function is the product of these maps, or a map with 1s where all 3 maps have 1s and 0s elsewhere. This is YZ X 00 01 11 10 0 0 1 0 0 1 1 1 1 1 Factoring this map gives the answer F = X+Y’Z YZ X 00 01 11 10 0 0 0 1 0 1 1 1 1 1
Factoring Zeros • When factoring 1s, the 1s on a map are covered. This gives the minterm form of the expression. • The same map can be factored by covering the 0s and then applying DeMorgan’s Theorem. • This gives the maxterm form of the expression, or the intersection of unions.
Example of Zeros Factoring The following map could be factored by covering 1s as follows, giving F = X+Y+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 1 Covering the 0s, however, gives F’ = X’Y’Z’ Applying DeMorgan’s Theorem gives the answer F = X+Y+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 1
Another Factoring Example The following map could be factored by covering 1s as follows, giving the minterm result F = XY’+X’Y+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 0 Covering the 0s, however, gives F’ = X’Y’Z’+XYZ’ Applying DeMorgan’s Theorem gives the answer in maxterm form F = (X+Y+Z)*(X’+Y’+Z) = (X+Y)*(X’+Y’)+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 0
Factoring Out Zeros • Sometimes, a 1s factoring will be complicated by one or two badly placed 0s. This can be handled as follows • Factor the map as if the inconvenient 0s were 1s. • Then, treating each of these factors as a small map, factor out the 0s.
Factoring Out 0s Example - 1 Except for two inconvenient 0s, this map has a very simple factoring F = Y’+W’Y = Y’+W’ YZ WX 00 01 11 10 00 1 0 1 1 Because of the 0 at W’X’Y’Z’ and the 0 at W’XYZ, however, we must factor out 0s. 01 1 1 1 0 11 0 1 1 0 10 1 0 0 1
Factoring Out 0s Example - 1 The first factor, Y’, creates the mini map in the variables W, X, and Z. On this map, W’X’Z’ is factored out: F1 = Y’*(W’X’Z’)’ = Y’*(W+X+Z) This factoring is essentially Y’ and not W’X’Z’. YZ WX 00 01 11 10 00 1 0 1 1 01 1 1 1 0 11 0 1 1 0 The second factoring is F2 = W’Y*(XZ)’ = W’Y*(X’+Z’) So the function is F = Y’*(W+X+Z)+W’Y*(X‘+Z’) 10 1 0 0 1
Factoring With More Variables • Karnaugh maps are possible with up to 6 variables. Beyond that, special tricks are needed. • If the expression involves 6 or fewer variables in most of the terms with an occasional additional variable, an approach much like factoring out 0s can be used. • This method is called exception factoring and can be used with any number of variables.
Exception Factoring - 1 • To factor the following function on a 4-variable Karnaugh map • F = AX’Y’+WXY’+W’Y’Z+XY’Z+W’XY’Z’+WX’Y’ • 1. Construct the function, using the variables • W, X, Y, and Z. • 2. For the term with the A, instead of 1, enter an • A in that square. • 3. In factoring out zeros, treat A as A+0.
Exception Factoring - 2 First, construct the map, using only the terms containing W, X, Y, and Z. Next, enter the A term. Put an A wherever there are 0s and nothing where there is a 1. F1 = WXY’+W’Y’Z+XY’Z+W’XY’Z’+WX’Y’ F2 = AX’Y’ YZ YZ WX WX 00 01 11 10 00 01 11 10 00 00 0 0 1 0 0 A A 0 01 01 1 0 1 0 0 0 0 0 11 11 0 1 1 0 0 0 0 0 10 10 1 0 0 1 A 0 0 A
Exception Factoring - 3 First, factor out the 1s, assuming the A was a 1 F1 = Y’ Next, factor out the A term, treating it as A+0 F = Y’*[A+(W’X’Z’)’] = Y’*(A+W+X+Z) YZ WX 00 01 11 10 00 0 A 1 0 01 1 0 1 0 11 0 1 1 0 10 1 0 0 1
Other Useful Symbols • The following basic mathematical symbols are also useful in defining programming functions. • - means “is a member of the set” • - means “is not a member of the set” • - means “for all members of the set” • - means “there exists a member of the • set”
Notation Examples • To update a file when dataset D is a member of the file • (D File) :: Update • To add a number x(i), when every number i is an even integer • i when (i even) :: add x(i) • To clear a register when any number n is negative • i when (i < 0) :: Clear
Function Completeness • A set of functions is complete if it covers all possible conditions. • For a complete set of functions, the Karnaugh map for the sum (or union) of the functions covers all map positions. • There may be multiply-covered map positions.
Function Orthogonality • A set of functions is orthogonal if no two functions cover the same conditions. • This means that the product (intersection) of any function with any other function is zero. • i, j where (i j) => F(i)*F(j) = 0 • The map may not be completely covered.
Complete and Orthogonal • A set of functions is complete and orthogonal if • the functions cover all elements • no two functions cover the same elements
Complete and Orthogonal Functions f, g, h are complete f, g, h are orthogonal 00 01 11 10 00 01 11 10 0 0 f g gh fh f g 1 1 g fh f fg f h g f, g, h are complete and orthogonal 00 01 11 10 0 f f g h 1 g h h f