computational boolean algebra - school of information...

102
Pingqiang Zhou ShanghaiTech University Computational Boolean Algebra

Upload: buithuy

Post on 08-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Pingqiang Zhou

ShanghaiTech University

Computational Boolean Algebra

Announcements

2

Written assignment #1 is out.

Due: March 24th, in class.

Programming assignment #1 is out.

Due: March 24th, 11:59PM.

Online open course video available at “coursera”

By Prof. Rob Rubentar, head of CS department at UIUC

https://www.coursera.org/course/vlsicad

Online Course – from UIUC

3

1 1 0 0

0 1 1 0

0 1 1 0

1 1 0 1

Karnaugh Map

4

xyzw

00

01

11

10

00 01 11 10

Computational Boolean AlgebraMotivation

5

Background

You’ve done Boolean algebra, hand manipulations, Karnaugh

maps to simplify…

But this is not sufficient for real designs!

Example: a multiplier of two 16-bit numbers

It has 32 inputs.

Its Karnaugh map has 232 = 4,294,967,296 squares

This is too big!

There must be a better way…

Need a Computational Approach

6

Need algorithmic, computational strategies for Boolean stuff.

Need to be able to think of Boolean objects as data structures + operators

What will we study?

Decomposition strategies

Ways of decomposing complex functions into simpler pieces.

A set of advanced concepts and terms you need to be able to do this.

Computational strategies

Ways to think about Boolean functions that let them be manipulated by programs.

Interesting applications

When you have new tools, there are some useful new things to do.

Computational Boolean Algebra: Topics

7

Cofactors

Boolean difference

Quantification

Tautology

Advanced Boolean AlgebraUseful Analogy to Calculus

8

In calculus, you can represent complex functions like 𝑒𝑥

using simpler functions.

If you can only use 1, 𝑥, 𝑥2, 𝑥3, … as the pieces ...

… turns out 𝑒𝑥 = 1 + 𝑥 +𝑥2

2!+

𝑥3

3!+⋯

It corresponds to the Taylor series expansion.

𝑓 𝑥 = 𝑓 0 +𝑓′(0)

1!+

𝑓′′(0)

2!+

𝑓′′′(0)

3!+⋯

Question: Anything like this for Boolean functions?

Yes. It is called Shannon Expansion.

9

Shannon Expansion

10

Proposed by Claude Shannon, the father of information

theory.

Suppose we have a function 𝐹(𝑥1, 𝑥2, … , 𝑥𝑛).

Define a new function if we set one of the 𝑥𝑖 = 𝑐𝑜𝑛𝑠𝑡

𝐹(𝑥1, 𝑥2, … , 𝑥𝑖 = 1,… , 𝑥𝑛)

𝐹(𝑥1, 𝑥2, … , 𝑥𝑖 = 0,… , 𝑥𝑛)

Example: 𝐹 𝑥, 𝑦, 𝑧 = 𝑥𝑦 + 𝑥 𝑧 + 𝑦( 𝑥𝑧 + 𝑧)

𝐹 𝑥 = 1, 𝑦, 𝑧 =

𝐹 𝑥, 𝑦 = 0, 𝑧 =

𝑦 + 𝑧 + 𝑦 𝑧

𝑥 𝑧

Note: this is a new function, that no longer depends on the variable 𝑥𝑖.

Shannon Expansion: Cofactors

11

Turns out to be an incredibly useful idea.

It is also known as Shannon cofactor with respect to 𝑥𝑖.

We write 𝐹(𝑥1, 𝑥2, … , 𝑥𝑖 = 1,… , 𝑥𝑛) as 𝐹𝑥𝑖. We call it positive cofactor.

We write 𝐹(𝑥1, 𝑥2, … , 𝑥𝑖 = 0,… , 𝑥𝑛) as 𝐹 𝑥𝑖. We call it negative cofactor.

Often, just write them as 𝐹(𝑥𝑖 = 1) and 𝐹(𝑥𝑖 = 0).

Why are these useful functions to get from 𝐹?

Shannon Expansion Theorem

12

Why we care: Shannon Expansion Theorem

Given any Boolean function 𝐹(𝑥1, 𝑥2, … , 𝑥𝑛) and pick any

𝑥𝑖 in 𝐹’s inputs, 𝐹 can be represented as

𝐹 𝑥1, 𝑥2, … , 𝑥𝑛 = 𝑥𝑖 ∙ 𝐹 𝑥𝑖 = 1 + 𝑥𝑖 ∙ 𝐹(𝑥𝑖 = 0)

Proof:

Consider any 𝑥1, 𝑥2, … , 𝑥𝑛 ∈ {0,1}𝑛

If 𝑥𝑖 = 1

𝐹 … , 𝑥𝑖 = 1,… = 𝐹 𝑥𝑖 = 1 = 1 ∙ 𝐹 𝑥𝑖 = 1 + 1 ∙ 𝐹(𝑥𝑖 = 0)

If 𝑥𝑖 = 0𝐹 … , 𝑥𝑖 = 0,… = 𝐹 𝑥𝑖 = 0 = 0 ∙ 𝐹 𝑥𝑖 = 1 + 0 ∙ 𝐹(𝑥𝑖 = 0)

Shannon Expansion: Another View

13

function F

𝑥1

𝑥𝑛

𝑥𝑖

…… 𝐹

function

F

𝑥1

𝑥𝑛

0

…… 𝑥𝑖

function

F

𝑥1

𝑥𝑛

1

…… 𝑥𝑖

MUX

0

1

𝑥𝑖

𝐹 = 𝑥𝑖 ∙ 𝐹 𝑥𝑖 = 1 + 𝑥𝑖 ∙ 𝐹(𝑥𝑖 = 0)

Same as

Shannon Expansion: Multiple Variables

14

Can do it on more than one variable, too.

Just keep on applying the theorem on each variable.

Example: Expand 𝐹 𝑥, 𝑦, 𝑧, 𝑤 around 𝑥 and 𝑦 First, expand around x:

𝐹 𝑥, 𝑦, 𝑧, 𝑤 = 𝑥 ∙ 𝐹 𝑥 = 1 + 𝑥 ∙ 𝐹(𝑥 = 0) Then, expand cofactors 𝐹 𝑥 = 1 and 𝐹 𝑥 = 0 around 𝑦:𝐹 𝑥 = 1 = 𝑦 ∙ 𝐹 𝑥 = 1, 𝑦 = 1 + 𝑦 ∙ 𝐹(𝑥 = 1, 𝑦 = 0)𝐹 𝑥 = 0 = 𝑦 ∙ 𝐹 𝑥 = 0, 𝑦 = 1 + 𝑦 ∙ 𝐹(𝑥 = 0, 𝑦 = 0)

Final result:

𝐹 𝑥, 𝑦, 𝑧, 𝑤= 𝑥𝑦 ∙ 𝐹 𝑥 = 1, 𝑦 = 1 + 𝑥 𝑦 ∙ 𝐹 𝑥 = 1, 𝑦 = 0+ 𝑥𝑦 ∙ 𝐹 𝑥 = 0, 𝑦 = 1 + 𝑥 𝑦 ∙ 𝐹(𝑥 = 0, 𝑦 = 0)

Shannon Cofactors: Multiple Variables

15

There is notation for these multiple-variable expansions as well.

Shannon cofactor with respect to 𝑥𝑖 and 𝑥𝑗:

Write 𝐹(𝑥1, … , 𝑥𝑖 = 1,… , 𝑥𝑗 = 0,… , 𝑥𝑛) as 𝐹𝑥𝑖𝑥𝑗.

The same for any number of variables 𝑥𝑖 , 𝑥𝑗 , 𝑥𝑘 , …

Notice that order does not matter: (𝐹𝑥)𝑦= (𝐹𝑦)𝑥= 𝐹𝑥𝑦.

For the previous example:

𝐹 𝑥, 𝑦, 𝑧, 𝑤 = 𝑥𝑦 ∙ 𝐹𝑥𝑦 + 𝑥 𝑦 ∙ 𝐹𝑥 𝑦 + 𝑥𝑦 ∙ 𝐹𝑥𝑦 + 𝑥 𝑦 ∙ 𝐹𝑥 𝑦

Again, remember: each of the cofactors is a function, not a

number.

𝐹𝑥𝑦 = 𝐹(𝑥 = 1, 𝑦 = 1, 𝑧, 𝑤) is a Boolean function of only 𝑧and 𝑤.

Next Question: Properties of Cofactors

16

What else can you do with cofactors?

Suppose you have 2 functions 𝐹(𝑋) and 𝐺(𝑋), where 𝑋 =(𝑥1, 𝑥2, … , 𝑥𝑛).

Suppose you make a new function 𝐻, from 𝐹 and 𝐺, say…

𝐻 = 𝐹

𝐻 = 𝐹 ∙ 𝐺, i.e., 𝐻 𝑋 = 𝐹 𝑋 ⋅ 𝐺(𝑋)

𝐻 = 𝐹 + 𝐺, i.e., 𝐻 𝑋 = 𝐹 𝑋 + 𝐺(𝑋)

𝐻 = 𝐹⨁𝐺, i.e., 𝐻 𝑋 = 𝐹(𝑋)⨁𝐺(𝑋)

Question: can you tell anything about 𝐻’s cofactors from

those of 𝐹 and 𝐺?

𝐹 ⋅ 𝐺 𝑥 = what? 𝐹 𝑥 = what?

Nice Properties of Cofactors

17

Cofactors of 𝐹 and 𝐺 tell you everything you need to know.

Complements

𝐹 𝑥 = (𝐹𝑥)

In English: cofactor of complement is complement of cofactor.

Binary Boolean operators

𝐹 ⋅ 𝐺 𝑥 = 𝐹𝑥 ⋅ 𝐺𝑥 cofactor of AND is AND of cofactors

𝐹 + 𝐺 𝑥 = 𝐹𝑥 + 𝐺𝑥 cofactor of OR is OR of cofactors

𝐹⨁𝐺 𝑥 = 𝐹𝑥⨁𝐺𝑥 cofactor of XOR is XOR of cofactors

Very useful! Can often help in getting cofactors of complex

formulas.

Combinations of Cofactors

18

Now consider operations on cofactors themselves.

Suppose we have 𝐹(𝑋), and get 𝐹𝑥 and 𝐹𝑥 𝐹𝑥 ⨁ 𝐹𝑥=?

𝐹𝑥 ⋅ 𝐹𝑥 =?

𝐹𝑥 + 𝐹𝑥 =?

Turns out these are all useful new functions.

Indeed, they even have names!

Next: let’s go look at these interesting, useful new functions.

Computational Boolean Algebra: Topics

19

Cofactors

Boolean difference

Quantification

Tautology

Calculus Revisited: Derivatives

20

Remember how you defined derivatives?

Suppose you have 𝑦 = 𝑓(𝑥).

How to compute?

𝑑𝑓(𝑥)

𝑑𝑥= lim

Δ𝑥→0

𝑓 𝑥+Δx −𝑓(𝑥)

Δx

𝑦 = 𝑓(𝑥)

𝑥

Defined as slope of curve as

a function of point 𝑥.

Boolean Derivatives

21

So, do Boolean functions have “derivatives”?

Actually, yes. Trick is how to define them...

Basic idea

For real-valued 𝑓(𝑥),𝑑𝑓

𝑑𝑥tells how 𝑓 changes when 𝑥 changes.

For 0,1-valued Boolean function, we cannot change 𝑥 by small

delta.

Can only change 0 1, but can still ask how 𝑓 changes with 𝑥 ...

For Boolean function 𝑓(𝑥), define

𝜕𝑓

𝜕𝑥= 𝑓𝑥 ⊕𝑓𝑥

Boolean Derivatives

22

𝜕𝑓

𝜕𝑥= 𝑓𝑥 ⊕𝑓𝑥

Compare value of 𝑓 when 𝑥 = 0 against when 𝑥 = 1.

𝜕𝑓

𝜕𝑥== 1 if and only if 𝑓(𝑥 = 0) is different from 𝑓(𝑥 = 1).

𝜕𝑓

𝜕𝑥is also known as Boolean difference.

If function 𝑓 is constant (𝑓 = 1 or 𝑓 = 0 for all inputs), then 𝜕𝑓 𝜕𝑥 = 0 for any 𝑥.

Boolean Difference

23

Boolean difference also behaves sort of like regular

derivatives...

Order of variables does not matter

( 𝜕𝑓 𝜕𝑥) 𝜕𝑦 = ( 𝜕𝑓 𝜕𝑦) 𝜕𝑥

Derivative of XOR is XOR of derivatives

𝜕(𝑓 ⊕ 𝑔)

𝜕𝑥=𝜕𝑓

𝜕𝑥⊕

𝜕𝑔

𝜕𝑥 Like addition

Boolean Difference

24

But some things are just more complex

Derivatives of (𝑓 ⋅ 𝑔) and (𝑓 + 𝑔) do not work the same...

Why?

Because AND and OR on Boolean values do not always behave

like ADDITION and MULTIPLICATION on real numbers.

Boolean Difference: Gate-Level View

25

Consider simple examples for 𝜕𝑓 𝜕𝑥.

Inverter: 𝑓 = 𝑥

𝑓𝑥 = 0, 𝑓𝑥 = 1, 𝜕𝑓 𝜕𝑥 = 𝑓𝑥 ⊕𝑓𝑥 = 1

AND: 𝑓 = 𝑥𝑦

𝑓𝑥 = 𝑦, 𝑓𝑥 = 0, 𝜕𝑓 𝜕𝑥 = 𝑓𝑥 ⊕𝑓𝑥 = 𝑦

OR: 𝑓 = 𝑥 + 𝑦

𝑓𝑥 = 1, 𝑓𝑥 = 𝑦, 𝜕𝑓 𝜕𝑥 = 𝑓𝑥 ⊕𝑓𝑥 = 𝑦

XOR: 𝑓 = 𝑥 ⊕ 𝑦

𝑓𝑥 = 𝑦, 𝑓𝑥 = 𝑦, 𝜕𝑓 𝜕𝑥 = 𝑓𝑥 ⊕𝑓𝑥 = 1

Meaning: When 𝜕𝑓 𝜕𝑥 = 1, then 𝑓 changes if 𝑥 changes!

Interpreting the Boolean Difference

26

What does 𝜕𝐹(𝑎, 𝑏, … , 𝑤, 𝑥) 𝜕𝑥 = 1 mean?

If you apply a pattern of inputs (𝑎, 𝑏, … ,𝑤) that makes 𝜕𝐹 𝜕𝑥 = 1, then any change in 𝑥 will force a change in

output 𝐹.

Combinational

Logic

𝑎𝑏

𝑤

𝑥

𝐹(𝑎, 𝑏, … ,𝑤, 𝑥)

Boolean Difference: Example

27

What is 𝜕𝑐𝑜𝑢𝑡 𝜕𝑐𝑖𝑛 = 1?

𝑐𝑜𝑢𝑡 𝑐𝑖𝑛 = 1 = 𝑎 + 𝑏

𝑐𝑜𝑢𝑡 𝑐𝑖𝑛 = 0 = 𝑎𝑏

𝜕𝑐𝑜𝑢𝑡 𝜕𝑐𝑖𝑛 = 𝑐𝑜𝑢𝑡 𝑐𝑖𝑛 = 1 ⊕ 𝑐𝑜𝑢𝑡 𝑐𝑖𝑛 = 0= 𝑎 + 𝑏 ⊕ 𝑎𝑏 = 𝑎 ⊕ 𝑏

Make sense?

𝑎 ⊕ 𝑏 = 1 ⟹ 𝑎 ≠ 𝑏

1-Bit Full

Adder

𝑎𝑏

𝑐𝑖𝑛𝑐𝑜𝑢𝑡

𝑠 𝑠 = 𝑎 ⊕ 𝑏⊕ 𝑐𝑖𝑛

𝑐𝑜𝑢𝑡 = 𝑎𝑏 + 𝑎𝑐𝑖𝑛 + 𝑏𝑐𝑖𝑛

Boolean Difference: Summary

28

Boolean difference explains under what situations an input-

change can cause output-change for a Boolean function 𝑓.

𝜕𝑓 𝜕𝑥 is another Boolean function, but it does not depend

on 𝑥!

It cannot, because it is made out of cofactors with respect to 𝑥,

which eliminate all the 𝑥 and 𝑥 terms by setting them to

constants.

Very useful! (we will see more, later…)

Computational Boolean Algebra: Topics

29

What you know

Shannon expansion lets you decompose a Boolean function

Combinations of cofactors do interesting things, e.g, the

Boolean difference

What you don’t know

Other combinations of cofactors that do useful things

The big ones: Quantification operators

Applications: Being able to do something impressive

Computational Boolean Algebra: Topics

30

Cofactors

Boolean difference

Quantification

Tautology

AND of 𝐹𝑥 and 𝐹 𝑥: Universal Quantification

31

AND the cofactors: 𝐹𝑥𝑖 ⋅ 𝐹 𝑥𝑖 Name: Universal Quantification of function 𝐹 with respect to

variable 𝑥𝑖.

Represented as: ∀𝑥𝑖 𝐹 (𝑥1, 𝑥2, … , 𝑥𝑖−1, 𝑥𝑖+1, … , 𝑥𝑛)

∀𝑥𝑖 𝐹 is a new function

It does not depend on 𝑥𝑖!

“∀” sign is the “for all”

symbol from logic.

function

F𝑥

function

F

AND

𝑥

0

1 𝐹𝑥

𝐹 𝑥

∀𝑥 𝐹

OR of 𝐹𝑥 and 𝐹 𝑥: Existential Quantification

32

OR the cofactors: 𝐹𝑥𝑖 + 𝐹 𝑥𝑖 Name: Existential Quantification of function 𝐹 with respect to

variable 𝑥𝑖.

Represented as: ∃𝑥𝑖 𝐹 (𝑥1, 𝑥2, … , 𝑥𝑖−1, 𝑥𝑖+1, … , 𝑥𝑛)

∃𝑥𝑖 𝐹 is a new function

It does not depend on 𝑥𝑖!

“∃” sign is the “there exists”

symbol from logic.

∃𝑥 𝐹

function

F𝑥

function

F𝑥

0

1 𝐹𝑥

𝐹 𝑥

OR

Both ∀𝑥𝑖 𝐹 and ∃𝑥𝑖 𝐹 do not

depend on 𝑥𝑖

Quantification Notation Makes Sense…

33

function

F𝑥

function

F

AND

𝑥

0

1 𝐹𝑥

𝐹 𝑥

∀𝑥 𝐹

∃𝑥 𝐹

function

F𝑥

function

F𝑥

0

1 𝐹𝑥

𝐹 𝑥

OR

When does ∀𝑥 𝐹(all vars except 𝑥) = 1?

When does ∃𝑥 𝐹(all vars except 𝑥) = 1?

This input pattern of the other vars

makes 𝐹 = 1 for all values of 𝑥.

There exists a value of 𝑥 that makes 𝐹 = 1for this input pattern of the other vars.

Quantification: Gate-Level View

34

Consider simple examples for (∀𝑥 𝑓) and (∃𝑥 𝑓).

Inverter: 𝑓 = 𝑥

𝑓𝑥 = 0, 𝑓𝑥 = 1, ∀𝑥 𝑓 = 𝑓𝑥𝑓𝑥 = 0, ∃𝑥 𝑓 = 𝑓𝑥 + 𝑓𝑥 = 1

AND: 𝑓 = 𝑥𝑦

𝑓𝑥 = 𝑦, 𝑓𝑥 = 0, ∀𝑥 𝑓 = 𝑓𝑥𝑓𝑥 = 0, ∃𝑥 𝑓 = 𝑓𝑥 + 𝑓𝑥 = 𝑦

OR: 𝑓 = 𝑥 + 𝑦

𝑓𝑥 = 1, 𝑓𝑥 = 𝑦, ∀𝑥 𝑓 = 𝑓𝑥𝑓𝑥 = 𝑦, ∃𝑥 𝑓 = 𝑓𝑥 + 𝑓𝑥 = 1

XOR: 𝑓 = 𝑥 ⊕ 𝑦

𝑓𝑥 = 𝑦, 𝑓𝑥 = 𝑦, ∀𝑥 𝑓 = 𝑓𝑥𝑓𝑥 = 0, ∃𝑥 𝑓 = 𝑓𝑥 + 𝑓𝑥 = 1

Make sense?

Extends to More Variables in Obvious Way

35

Like Boolean difference, can do with respect to more than 1

variable

Suppose we have 𝐹(𝑥, 𝑦, 𝑧, 𝑤).

∀𝑥𝑦 𝐹 𝑧,𝑤 = ∀𝑥 ∀𝑦 𝐹 = 𝐹𝑥𝑦 ∙ 𝐹𝑥𝑦 ∙ 𝐹𝑥𝑦 ∙ 𝐹𝑥 𝑦

∃𝑥𝑦 𝐹 𝑧,𝑤 = ∃𝑥 ∃𝑦 𝐹 = 𝐹𝑥𝑦 + 𝐹𝑥𝑦 + 𝐹𝑥𝑦 + 𝐹𝑥 𝑦

Remember:

(∀𝑥 𝐹), (∃𝑥 𝐹), and 𝜕𝐹 𝜕𝑥 are all functions.

… but they are functions of all the variables except 𝑥.

Quantification Example

36

Consider the following circuit, it adds 𝑥 =0 or 𝑥 =1 to

a 2-bit number 𝑎1𝑎0.

It’s just a 2-bit adder, but instead of 𝑏1𝑏0 for the second

operand, it is just 0𝑥.

It has a carry-in 𝑑 and produces a carry-out 𝑐.

Hence, 𝑐 is function of 𝑎1, 𝑎0, 𝑑 and 𝑥.

Questions:

What is ∀𝑎1𝑎0 𝑐 𝑥, 𝑑 ?

What is ∃𝑎1𝑎0 𝑐 𝑥, 𝑑 ? adder𝐴0𝐴1 𝐵1 𝐵0

𝑎1 𝑎0 𝑥

𝑑𝑐

0

Quantification Example

37

What is ∀𝑎1𝑎0 𝑐 𝑥, 𝑑 ?

A function of only 𝑥 and 𝑑. Makes a 1 for values of 𝑥 and 𝑑 that

make a carry 𝑐 = 1 for all values of inputs 𝑎1 and 𝑎0.

What is ∃𝑎1𝑎0 𝑐 𝑥, 𝑑 ?

A function of only 𝑥 and 𝑑. Makes a 1 for values of 𝑥 and 𝑑 that

make a carry 𝑐 = 1 for some value of inputs 𝑎1 and 𝑎0, i.e.,

there exists some 𝑎1 and 𝑎0 that for this 𝑥 and 𝑑, 𝑐 = 1.

adder𝐴0𝐴1 𝐵1 𝐵0

𝑎1 𝑎0 𝑥

𝑑𝑐

0

Quantification Example

38

Compute ∀𝑎1𝑎0 𝑐 𝑥, 𝑑

𝑐𝑎1𝑎0 ∙ 𝑐𝑎1 𝑎0 ∙ 𝑐 𝑎1𝑎0 ∙ 𝑐 𝑎1 𝑎0= 0

Compute ∃𝑎1𝑎0 𝑐 𝑥, 𝑑

𝑐𝑎1𝑎0 + 𝑐𝑎1 𝑎0 + 𝑐 𝑎1𝑎0 + 𝑐 𝑎1 𝑎0= 𝑥 + 𝑑

adder𝐴0𝐴1 𝐵1 𝐵0

𝑎1 𝑎0 𝑥

𝑑𝑐

0

𝑐 = 𝑎1(𝑎0𝑥 + 𝑎0𝑑 + 𝑥𝑑)

Need four cofactors:

• 𝑐𝑎1𝑎0 =

• 𝑐𝑎1 𝑎0 =

• 𝑐 𝑎1𝑎0 =

• 𝑐 𝑎1 𝑎0 =

𝑥 + 𝑑𝑥𝑑

00

Quantification Example

39

∀𝑎1𝑎0 𝑐 𝑥, 𝑑 = 0

Make sense: No values of 𝑥 and 𝑑 that make 𝑐 = 1independent of 𝑎1 and 𝑎0

∃𝑎1𝑎0 𝑐 𝑥, 𝑑 = 𝑥 + 𝑑

Make sense: If at least one of 𝑥 and 𝑑 = 1, then there

exists 𝑎1 and 𝑎0 that let 𝑐 = 1.

adder𝐴0𝐴1 𝐵1 𝐵0

𝑎1 𝑎0 𝑥

𝑑𝑐

0

𝑐 = 𝑎1(𝑎0𝑥 + 𝑎0𝑑 + 𝑥𝑑)

Quantification Application:

Logic Network Repair

40

Suppose that some one specified a logic block for you to

implement: 𝑓 𝑎, 𝑏 = 𝑎𝑏 + 𝑏

…but you implemented it wrong: in particular, you got ONE

gate wrong.

Goal

Can we deduce how precisely to change this gate to restore

correct function?

Go with this very trivial test case to see how mechanics work...

𝑎𝑏 + 𝑏

Specified

𝑓 𝑎, 𝑏

𝑎

𝑏

AND

??

Implemented

𝑎𝑏

𝑔 𝑎, 𝑏

Network Repair

41

Clever trick: Replace our suspect gate by a 4-to-1 MUX with 4

arbitrary new vars 𝑑0, 𝑑1, 𝑑2, 𝑑3.

By cleverly assigning values to 𝑑0, 𝑑1, 𝑑2, 𝑑3, we can fake any gate.

Question is: what are the right values of 𝑑𝑖’s so 𝑔 is repaired, i.e.,

𝑔 = 𝑓?

AND

??

𝑎𝑏 𝑔 𝑎, 𝑏

𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3

AND𝑎𝑏

𝑠1 𝑠0

MU

X

00011011

𝑑0𝑑1𝑑2𝑑3

𝑠1

𝑠0

Aside: Faking a Gate with a MUX

42

You can do any function of 2 vars with one 4-to-1

multiplexor (MUX).

𝑦

𝑥𝑠1 𝑠0

MU

X

00011011

0111

OR𝑥

𝑦

Aside: Faking a Gate with a MUX

43

You can do any function of 2 vars with one 4-to-1

multiplexor (MUX).

𝑦

𝑥𝑠1 𝑠0

MU

X

00011011

1110

𝑥

𝑦NAND

Network Repair: Using Quantification

44

Next trick: XNOR 𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3 with the specification

𝑓 𝑎, 𝑏 .

Note: 𝑧 𝑎, 𝑏, 𝑑0, 𝑑1, 𝑑2, 𝑑3 = 1only when 𝐺 == 𝑓.

𝑎𝑏 + 𝑏𝑓 𝑎, 𝑏

𝑎

𝑏

AND𝑎𝑏

𝑠1 𝑠0M

UX

00011011

𝑑0𝑑1𝑑2𝑑3

EXNOR

𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3

𝑧 𝑎, 𝑏, 𝑑0, 𝑑1, 𝑑2, 𝑑3

Using Quantification

45

What do we need?

Values of 𝑑0, 𝑑1, 𝑑2, 𝑑3 that make 𝑧 = 1 for all possible

values of inputs 𝑎, 𝑏.

There are values of 𝑑0, 𝑑1, 𝑑2, 𝑑3 that let

∀𝑎𝑏 𝑧 𝑑0, 𝑑1, 𝑑2, 𝑑3 = 1

The above equation is universal quantification of function 𝑧with respect to 𝑎, 𝑏!

Any pattern of (𝑑0, 𝑑1, 𝑑2, 𝑑3) that makes

∀𝑎𝑏 𝑧 𝑑0, 𝑑1, 𝑑2, 𝑑3 = 1will do it!

Network Repair via Quantification

46

𝑎𝑏 + 𝑏𝑓 𝑎, 𝑏

𝑎

𝑏

AND𝑎𝑏

𝑠1 𝑠0

MU

X00011011

𝑑0𝑑1𝑑2𝑑3

XNOR

𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3

𝑧 𝑎, 𝑏, 𝑑0, 𝑑1, 𝑑2, 𝑑3

𝑠1 = 𝑏, s0 = 𝑎𝑏

𝐺 = 𝑑0 𝑠1 𝑠0 + 𝑑1 𝑠1𝑠0 + 𝑑2𝑠1 𝑠0+𝑑3𝑠1𝑠0

Network Repair via Quantification

47

As a result

𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3 = 𝑑0 𝑎𝑏 + 𝑑1𝑎𝑏 + 𝑑2 𝑏

𝑓 𝑎, 𝑏 = 𝑎𝑏 + 𝑏

𝑧 𝑎, 𝑏, 𝑑0, … , 𝑑3 = 𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3 ⨁𝑓 𝑎, 𝑏

We want to get

∀𝑎𝑏 𝑧 𝑑0, 𝑑1, 𝑑2, 𝑑3= 𝑧 𝑎 𝑏 ∙ 𝑧 𝑎𝑏 ∙ 𝑧𝑎 𝑏 ∙ 𝑧𝑎𝑏

Network Repair via Quantification

48

𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3 = 𝑑0 𝑎𝑏 + 𝑑1𝑎𝑏 + 𝑑2 𝑏

𝑓 𝑎, 𝑏 = 𝑎𝑏 + 𝑏

𝑧 𝑎, 𝑏, 𝑑0, … , 𝑑3 = 𝐺 𝑎, 𝑏, 𝑑0, … , 𝑑3 ⨁𝑓 𝑎, 𝑏

𝑧 𝑎 𝑏 = 𝐺 𝑎 𝑏 ⨁𝑓 𝑎 𝑏 = 𝑑2 ⨁1 = 𝑑2

𝑧 𝑎𝑏 = 𝐺 𝑎𝑏 ⨁𝑓 𝑎𝑏 = 𝑑0 ⨁0 = 𝑑0

𝑧𝑎 𝑏 = 𝐺𝑎 𝑏 ⨁𝑓𝑎 𝑏 = 𝑑2 ⨁1 = 𝑑2 𝑧𝑎𝑏 = 𝐺𝑎𝑏 ⨁𝑓𝑎𝑏 = 𝑑1 ⨁1 = 𝑑1

∀𝑎𝑏 𝑧 𝑑0, 𝑑1, 𝑑2, 𝑑3 = 𝑧 𝑎 𝑏 ∙ 𝑧 𝑎𝑏 ∙ 𝑧𝑎 𝑏 ∙ 𝑧𝑎𝑏 = 𝑑0𝑑1𝑑2

Network Repair via Quantification

49

Finally, we obtain ∀𝑎𝑏 𝑧 𝑑0, 𝑑1, 𝑑2, 𝑑3 = 𝑑0𝑑1𝑑2

To repair, we should find values of 𝑑0, 𝑑1, 𝑑2, 𝑑3 so that

∀𝑎𝑏 𝑧 𝑑0, 𝑑1, 𝑑2, 𝑑3 = 1

Not hard: 𝑑0 = 0, 𝑑1 = 1, 𝑑2 = 1, 𝑑3 = 𝑋(don′t care)

Network Repair

50

Does 𝑑0 = 0, 𝑑1 = 1, 𝑑2 = 1, 𝑑3 = 𝑋 work?

Case 1: 𝑑0 = 0, 𝑑1 = 1, 𝑑2 = 1, 𝑑3 = 1

𝑎𝑏 + 𝑏

Specified

𝑓 𝑎, 𝑏

𝑎

𝑏

AND𝑎𝑏

𝑠1 𝑠0

MU

X

00011011

𝑑0𝑑1𝑑2𝑑3

0111

MUX is an OR gate. Expected!

Network Repair

51

Does 𝑑0 = 0, 𝑑1 = 1, 𝑑2 = 1, 𝑑3 = 𝑋 work?

Case 2: 𝑑0 = 0, 𝑑1 = 1, 𝑑2 = 1, 𝑑3 = 0

𝑎𝑏 + 𝑏

Specified

𝑓 𝑎, 𝑏

𝑎

𝑏

AND𝑎𝑏

𝑠1 𝑠0

MU

X

00011011

𝑑0𝑑1𝑑2𝑑3

0110

MUX is an XOR gate.

Unexpected but works!

Network Repair: Summary

52

This example is tiny...

But in a real example, you have a big network – 100 inputs, 50,000 gates.

When the design doesn’t work, it is a major hassle to go through the design to fix it.

This gives a mechanical procedure to answer: Can we change 1 gate to repair?

What we haven’t seen yet: Computation strategy to mechanically find inputs to make

∀𝑎𝑏 𝑧 𝑑0, 𝑑1, 𝑑2, 𝑑3 = 1 This computation is called Boolean Satisfiability (SAT).

We will see how to solve Boolean SAT problem efficiently later.

Review

53

Cofactors

Positive cofactor: 𝐹(𝑥1, 𝑥2, … , 𝑥𝑖 = 1,… , 𝑥𝑛)

Negative cofactor: 𝐹(𝑥1, 𝑥2, … , 𝑥𝑖 = 0,… , 𝑥𝑛)

Shannon expansion theorem𝐹 𝑥1, 𝑥2, … , 𝑥𝑛 = 𝑥𝑖 ∙ 𝐹 𝑥𝑖 = 1 + 𝑥𝑖 ∙ 𝐹(𝑥𝑖 = 0)

Combinations of cofactors

Boolean difference: 𝜕𝑓

𝜕𝑥= 𝑓𝑥 ⊕𝑓𝑥

Another Boolean function, but it does not depend on 𝑥.

Explain under what situations on other variables that an input change can cause output change for a Boolean function.

Universal quantification: ∀𝑥 𝑓 = 𝑓𝑥 ∙ 𝑓𝑥 Existential quantification: ∃𝑥 𝑓 = 𝑓𝑥 + 𝑓𝑥 Quantification Application: Network Repair

Computational Boolean Algebra: Topics

54

Cofactors

Boolean difference

Quantification

Tautology

Important Example of Computation:

Tautology Checking

55

Tautology: a Boolean function is 1 for every input pattern.

We are going to look at how to do tautology checking,

i.e. whether a Boolean function 𝑓 is 1 for every input.

Why study this problem?

To show a representation, i.e., a data structure, for a

Boolean function 𝑓.

To show an important computational strategy: recursion

How hard is this problem?

Very, very hard!

What happens if you are given a sum-of-product expression

with 50 variables and 800 products?

Start with: Representation

56

We use a simple, early representation scheme for functions

Represent a function as a set of OR’ed product terms, i.e., a

sum of products (SOP).

Each product term is also called a cube, e.g., 𝑎 𝑏𝑐 is a cube.

Why call it cube?

𝑎

𝑏

𝑐

000 100

110010

001 101

111011 Circle all vertices that let

𝑓 = 1.Boolean space

of 3 variables

How does a product

term look like?

Properties of Cubes

57

In what follows, we refer to product term as cube.

For each variable 𝑥, 𝑥 and 𝑥 do not appear simultaneously.

However, for each variable 𝑥, it is possible that none of 𝑥 and 𝑥appears in the cube.

If for each variable 𝑥, one of 𝑥 and 𝑥 appears in the cube. The

cube is a minterm.

The number of vertices

in a cube is 2𝑘.

𝑎

𝑏

𝑐

000 100

110010

001 101

111011Boolean space

of 3 variables

Positional Cube Notation (PCN)

58

We represent a cube using Positional Cube Notation (PCN).

One slot per variable.

In slot for variable 𝑥:

put “1”, if cube has 𝑥 in it;

put “0”, if cube has 𝑥 in it;

put “-”, if cube has no 𝑥 or 𝑥 in it.

Example: In a Boolean space on three variables 𝑎, 𝑏, 𝑐

𝑏:

𝑎 𝑐:

[ ]

[ ]

𝑎 𝑏 𝑐

Positional Cube Notation (PCN)

59

To represent cube in program, we need to encode “1”, “0”,

and “-”.

We need at least two bits to encode three values.

One encoding: “01” to encode “1”; “10” to encode “0”;

“11” to encode “-”.

Example: In a Boolean space on three variables 𝑎, 𝑏, 𝑐

𝑏:

𝑎 𝑐:

[11 10 11]

[01 11 10]

𝑎 𝑏 𝑐

[ - 0 - ]

[ 1 - 0 ]

𝑎 𝑏 𝑐

PCN Cube List

60

A sum-of-products (SOP) expression of a Boolean

function is also called as a cover of cubes.

We present a cover of cubes using a list of cubes in

positional cube notation.

Example: 𝑓 = 𝑎 + 𝑏𝑐 + 𝑎𝑏

𝑎

𝑏𝑐

𝑎𝑏

[10 11 11]

𝑎 𝑏 𝑐

[11 01 01]

[01 01 11]

Tautology Checking

61

How do we approach tautology checking as a

computation?

Input: a list of cube in PCN representing an SOP expression of 𝑓

Output: Yes, when 𝑓 is always 1; No, when 𝑓 is not always 1.

Cofactors to rescue

Great result: 𝑓 is a tautology if and only if 𝑓𝑥 and 𝑓 𝑥 are both

tautologies.

This makes sense:

If function 𝑓 ≡ 1, then cofactors both obviously ≡ 1.

If both cofactors ≡ 1, then 𝑓 = 𝑥 ∙ 𝑓𝑥 + 𝑥 ∙ 𝑓𝑥 = 𝑥 + 𝑥 = 1

Recursive Tautology Checking

62

Suggests a recursive computation strategy:

If you cannot tell immediately that f is a tautology, go try to see if each cofactor is a tautology.

What else do we need?

Selection rules: which 𝑥 is good to pick to split on?

Termination rules: how do we know when to stop splitting, so we can answer that the function at this node of tree is tautology or not?

Mechanics: how hard is it to actually obtain the cofactors?

𝑓 = 1??

Split on 𝑥(𝑓𝑥 = 1AND 𝑓𝑥 = 1)??

𝑓𝑥 = 1 𝑓𝑥 = 1

Recursive Cofactoring

63

Do mechanics first (easy!). For each cube in the list:

If you want positive cofactor w.r.t. var 𝑥, look at 𝑥 slot in each

cube:

[… 10 …] just remove this cube from list, since it’s a

term with 𝑥.

[… 01 …] just make this slot 11 (don’t care), since we

will strike 𝑥 from the product term.

[… 11 …] just leave this alone, since this term doesn’t

have any 𝑥/ 𝑥 in it.

𝑓 = 𝑎𝑏𝑑 + 𝑏 𝑐

𝑎𝑏𝑑

𝑏 𝑐

[01 01 11 01]

[11 01 10 11]

𝑓𝑎

[11 01 11 01]

[11 01 10 11]

𝑓𝑐

[01 01 11 01]

Recursive Cofactoring

64

Do mechanics first (easy!). For each cube in the list:

If you want negative cofactor w.r.t. var 𝑥, look at 𝑥 slot in

each cube:

[… 01 …] just remove this cube from list, since it’s a

term with 𝑥.

[… 10 …] just make this slot 11 (don’t care), since we

will strike 𝑥 from the product term.

[… 11 …] just leave this alone, since this term doesn’t

have any 𝑥/ 𝑥 in it.

𝑓 = 𝑎𝑏𝑑 + 𝑏 𝑐

𝑎𝑏𝑑

𝑏 𝑐

[01 01 11 01]

[11 01 10 11]

𝑓 𝑐

[01 01 11 01]

[11 01 11 11]

Unate Functions

65

Selection/termination, another trick: Unate functions Special class of Boolean functions

𝑓 is unate if a SOP representation only has each literal in exactly one polarity, either all true, or all complemented

𝑓 is positive unate in var 𝑥: if changing x 01 but keeping other variables constant (no matter what values they are), keeps 𝑓 constant or makes 𝑓 change 01.

𝑓 is negative unate in var 𝑥: if changing x 01 but keeping other variables constant (no matter what values they are), keeps 𝑓 constant or makes 𝑓 change10.

Function that is not unate is called binate.

Examples:

Is 𝑎𝑏 + 𝑎 𝑐𝑑 + 𝑐𝑑 𝑒 unate?Is 𝑥𝑦 + 𝑥𝑦 + 𝑥𝑦 𝑧 + 𝑧 unate?Is 𝑓 = 𝑎𝑏 positive unate?

Unate Functions

66

Analogous to monotonic continuous functions.

A monotonically non-decreasing function: whenever

𝑥2 ≥ 𝑥1, we have 𝑓(𝑥2) ≥ 𝑓(𝑥1).

Example, for a Boolean function 𝑓 positive unate in 𝑥:

Boolean function 𝑓

𝑎𝑏

𝑤

𝑥

𝑓(𝑎, 𝑏, … ,𝑤, 𝑥)

0 1

0 0, or

1 1, or

0 1

Checking Unateness

67

How to check unateness?

Not easy if 𝑓 is represented using truth table.

Very easy if 𝑓 is represented as an SOP.

Suppose that 𝑓 is represented as an SOP. Then 𝑓 is unate in

var 𝑥 if …

For all the cubes that contain var 𝑥, 𝑥 appears in exactly one

polarity, either all true, or all complemented.

E.g., 𝑓 = 𝑎𝑏 + 𝑎 𝑐𝑑 + 𝑐𝑑 𝑒 is unate in 𝑎, 𝑏, 𝑐, 𝑑, 𝑒.

A sufficient condition only!! If not satisfied, may be either

unate or not unate.

E.g., 𝑓 = 𝑥𝑦 + 𝑥𝑦 + 𝑥𝑦 𝑧 + 𝑧 is unate in 𝑦, 𝑧, but may or

may NOT be unate in 𝑥. (Actually, it is unate in 𝑥!)

Checking Unate Functions with Cube-List

68

Suppose you have a cube-list for 𝑓.

A cube-list is unate if for each var 𝑥 and for all the cubes that

contain var 𝑥, 𝑥 only appears in one polarity, not both.

Again, just a sufficient condition.

E.g., 𝑓 = 𝑎 + 𝑏𝑐 + 𝑎𝑐 is unate.

E.g., 𝑓 = 𝑎 + 𝑏𝑐 + 𝑏𝑐 may or may NOT be unate.

Easier to see if draw the cube-list vertically.

𝑎 + 𝑏𝑐 + 𝑎𝑐

[01 11 11]𝑎

𝑏𝑐𝑎𝑐

[11 01 01]

[01 11 01]

𝑎 + 𝑏𝑐 + 𝑏𝑐

𝑎

𝑏𝑐

𝑏𝑐

[01 11 11]

[11 01 01]

[11 10 01]

Using Unate Functions in Tautology

Checking

69

It is very easy to check a unate cube-list for tautology:

Unate cube-list for 𝑓 is tautology iff it contains a cube whose

elements are all don’t care: [11 11 … 11].

Question: what exactly is [11 11 ... 11] as a product term?

This result actually makes sense...

Cannot make a 1 with only product terms where all literals are

in just one polarity!

𝑎 + 𝑏𝑐 + 𝑏𝑐

𝑎

𝑏𝑐

𝑏𝑐

[01 11 11]

[11 01 01]

[11 10 01]

Termination Rules Using Unateness

70

If we have a unate cube-list, we can check for tautology

directly.

Rule 1: The function is a tautology if cube-list has an all-don’t-

care cube [11 11 … 11].

Why? function = stuff + 1 + stuff = 1.

Rule 2: The function is NOT tautology if cube-list does not

have any all-don’t-care cube [11 11 … 11].

Why? Unate cube-list for the function is tautology iff it

contains all-don’t-care cube.

a b c

01 11 10

11 01 10

01 01 10

Other Possible Termination Rules

71

Lots more possible termination rules. For example:

Rule 3: The function is tautology if cube list has single var

cube that appears in both polarities.

Why? function = stuff + 𝑥 + 𝑥 + stuff = 1

Selection Rule

72

We can’t use easy termination rules unless cube-list is unate.

Selection rule...? Pick splitting var to make unate cofactors.

Strategy: pick “most non-unate (binate)” var as split var.

Pick binate var with the most product terms dependent on it

Why? Simplify more cubes.

If a tie, pick var with minimum |#true_var − #compl_var|

Why? Left subtree and right subtree are balanced.

x y z w

01 01 01 01

10 11 01 01

10 11 11 10

01 01 11 01

x: binate, in 4 cubes, |true-compl| = |2-2|=0

y: unate

z: unate

w: binate, in 4 cubes, |true-compl| = |3-1|=2

Recursive Tautology Checking: Done!

73

tautology(f represented as cubelist) {/* check if we can terminate recursion */if (f is unate) {

apply unate tautology termination rules directlyif (= =1) return (1)else return (0)

}else if (any other termination rules, like rule 3, work?) {

return the appropriate value if = =1 or = =0}else { /* canot tell -- find splitting variable */

x = most-not-unate variable in freturn ( tautology( f(x=1) ) && tautology( f(x=0) ) )

}}

Algorithm

Recursive Tautology CheckingExample

74

Example: 𝑓 = 𝑎𝑏 + 𝑎𝑐 + 𝑎 𝑏 𝑐 + 𝑎

a b c

01 01 11

01 11 01

01 10 10

10 11 11

• 𝑓 unate?

• No

• Which var to pick?

• Most non-unate var: 𝑎

𝑓

𝑓𝑎 𝑓 𝑎

a b c

11 01 11

11 11 01

11 10 10

------------

a b c

------------

------------

------------

11 11 11

• 𝑓𝑎 unate?

• No

• Need to further split

• 𝑓 𝑎 unate?

• Yes!

• Tautology?

• Yes!

• Terminate!

Recursive Tautology CheckingExample

75

𝑓𝑎

a b c

11 01 11

11 11 01

11 10 10

• 𝑓𝑎 unate?

• No

• Which var to pick?

• Either 𝑏 or 𝑐• For example, we pick 𝑏

𝑓𝑎 𝑏𝑓𝑎𝑏

a b c

11 11 11

11 11 01

------------

a b c

------------

11 11 01

11 11 10

• 𝑓𝑎𝑏 unate?

• Yes!

• Tautology?

• Yes! (contain all-

don’t-care cube)

• Terminate!

• 𝑓𝑎 𝑏 unate?

• No

• Can we terminate?

• Yes, due to Rule 3

Recursive Tautology CheckingExample

76

The recursion tree we finally get is

The tree has tautologies at all leaves, so 𝑓 is tautology.

Note: if any leaf is NOT tautology, 𝑓 is NOT!

𝑓

𝑓𝑎 𝑓 𝑎 = 1

𝑓𝑎 𝑏 = 1𝑓𝑎𝑏 = 1

Recursive Tautology CheckingSummary

77

This strategy is so general and useful it has a name: Unate Recursive Paradigm (URP).

Again, we see that cofactors are important and useful.

Boolean difference, quantifications; real applications like logic network repair

Representations (data structures) for Boolean functions are critical.

Truth tables, Karnaugh maps, Boolean expressions cannot be manipulated by software.

See one real representation: cube-list, positional cube notation

78

Complementary Materials

Representation of Digital Circuits

79

We see how to represent an SOP of a Boolean function.

…but we will also deal with digital circuits.

How can we represent digital circuits?

As we know, there are two types of digital circuits

Combinational circuits

Sequential circuits

Two Types of Digital Circuits

80

Combinational circuits

Current outputs only depend on current inputs, not on the

previous inputs.

Sequential circuits

Current outputs depend on the previous inputs.

Combinational

Logic

Primary

Inputs

Primary

Outputs

Memory Elements,

e.g., Flip-Flops

Sequential Circuits

81

The combinational part of a sequential circuit is critical.

For the combinational part of a sequential circuit,

its inputs include primary inputs and the outputs of the

memory part.

its outputs include primary outputs and the inputs to the

memory part.

Combinational

Logic

Primary

Inputs

Primary

Outputs

Memory Elements,

e.g., Flip-Flops

Representation of Combinational

Circuits

82

Represented as a directed graph.

Inputs, outputs, and gates nodes

Wires directed edges.

Why directed edges? Signal flow has direction.

AND1

AND3

OR1 OR2

AND2

x2

yx3

x4

x1 x1

x2

x3

x4

A1

A3

A2 O2

O1

y

Representation of Combinational

Circuits

83

Since a combinational circuit has no loops, the corresponding

graph is a directed acyclic graphs (DAG).

DAG: A directed graph with no cycles.

AND1

AND3

OR1 OR2

AND2

x2

yx3

x4

x1 x1

x2

x3

x4

A1 A3

A2 O2

O1

y

Traversal of Combinational Circuits

84

Many operations on combinational circuit need to traverse it.

Example: obtain the output given an input pattern.

AND1

AND3

OR1 OR2

AND2

x2

yx3

x4

x10

1

1

1

0

1

1

0

1

How to approach this kind of traversal as a computation?

Answer: Topological Sorting.

Topological Sorting

85

Observation on computing the output of a combinational circuit:

The output of each gate can be computed only when all of its input

are known. We have to obtain its inputs before obtaining output.

In other words, if there is a wire (edge) from gate (node) 𝑢 to 𝑣,

then gate (node) 𝑢 should be visited before gate (node) 𝑣.

This is exactly topological sorting.

0

1

1

1

0

1

1

0

1

AND1

AND3

OR1 OR2

AND2

x2

yx3

x4

x1

One possible visiting order:

𝑥1, 𝑥2, 𝑥3, 𝑥4, AND1, OR1,

AND2, AND3, OR2, 𝑦.

Topological Sorting

86

Topological sorting is not necessarily unique:

A, G, D, B, E, C, F and A, B, G, D, E, F, C are both topological sorting.

Are the following orderings topological sorting?

A, B, E, G, D, C, F

A, G, B, D, E, F, C

A B C

D E F

G

Topological Sorting: Algorithm

87

Based on a queue.

Algorithm:

1. Compute the in-degrees of all nodes. (in-degree: number of

incoming edges of a node.)

2. Enqueue all in-degree 0 nodes into a queue.

3. While queue is not empty

1. Dequeue a node 𝑣 from the queue and visit it.

2. Decrement in-degrees of node 𝑣’s neighbors.

3. If any neighbor’s in-degree becomes 0, enqueue it into the

queue.

Topological Sorting AlgorithmExample

88

A B C D E F G

0 1 2 2 2 1 0

A B C

D E F

G

Queue

In-degrees

Order

Enqueue A and G

Topological Sorting AlgorithmExample

89

A B C D E F G

0 1 2 2 2 1 0

A B C

D E F

G

Queue

A

G

In-degrees

Order

Dequeue A, visit A, and decrement

in-degrees of A’s neighbors.

Topological Sorting AlgorithmExample

90

A B C D E F G

0 1 0 2 2 1 2 1 0

A B C

D E F

G

Queue

G

In-degrees

Order

A

Enqueue B

Topological Sorting AlgorithmExample

91

A B C D E F G

0 0 2 1 2 1 0

A B C

D E F

G

Queue

G

B

In-degrees

Order

A

Dequeue G, visit G, and decrement

in-degrees of G’s neighbors.

Topological Sorting AlgorithmExample

92

A B C D E F G

0 0 2 1 0 2 1 0

A B C

D E F

G

Queue

B

In-degrees

Order

A G

Enqueue D

Topological Sorting AlgorithmExample

93

A B C D E F G

0 0 2 0 2 1 0

A B C

D E F

G

Queue

B

D

In-degrees

Order

A G

Dequeue B, visit B, and decrement

in-degrees of B’s neighbors.

Topological Sorting AlgorithmExample

94

A B C D E F G

0 0 2 1 0 2 1 1 0

A B C

D E F

G

Queue

D

In-degrees

Order

A G B

Dequeue D, visit D, and decrement

in-degrees of D’s neighbors.

Topological Sorting AlgorithmExample

95

A B C D E F G

0 0 1 0 1 0 1 0

A B C

D E F

G

Queue

In-degrees

Order

A G B D

Enqueue E

Topological Sorting AlgorithmExample

96

A B C D E F G

0 0 1 0 0 1 0

A B C

D E F

G

Queue

In-degrees

Order

A G B D

E

Dequeue E, visit E, and decrement

in-degrees of E’s neighbors.

Topological Sorting AlgorithmExample

97

A B C D E F G

0 0 1 0 0 0 1 0 0

A B C

D E F

G

Queue

In-degrees

Order

A G B D E

Enqueue C and F

Topological Sorting AlgorithmExample

98

A B C D E F G

0 0 0 0 0 0 0

A B C

D E F

G

Queue

In-degrees

Order

A G B D E

C

F

Dequeue C, visit C, and decrement

in-degrees of C’s neighbors.

Topological Sorting AlgorithmExample

99

A B C D E F G

0 0 0 0 0 0 0

A B C

D E F

G

Queue

In-degrees

Order

A G B D E C

F

Dequeue F, visit F, and decrement

in-degrees of F’s neighbors.

Topological Sorting AlgorithmExample

100

A B C D E F G

0 0 0 0 0 0 0

A B C

D E F

G

Queue

In-degrees

Order

A G B D E C F

Queue is now empty. Done!

Circuit Netlist Format

101

We also need to store circuits as a text file.

… to be processed by different programs, e.g., layout synthesis

tool, SPICE, schematic viewer, etc.

Such files essentially store a netlist of gates.

Many formats exist:

Berkeley Logic Interchange Format (BLIF)

Structured Verilog Format

Bench Format

Example: Bench Format

102

INPUT(x1)

INPUT(x2)

INPUT(x3)

INPUT(x4)

OUTPUT(y)

g1=AND(x1,x2)

g2=OR(x3,x4)

g3=AND(x3,x4)

g4=AND(g1,g2)

y=OR(g3,g4)

AND1

AND3

OR1 OR2

AND2

x2

yx3

x4

x1 g1

g2

g3

g4