views:

352

answers:

8

Could you please explain the deMorgan rules as simply as possible (e.g. with a secondary school background) ?

+4  A: 

It is just a way to restate truth statements, which can provide simpler ways of writing conditionals to do the same thing.

In plain English:
When something is not This or That, it is also not this and not that.
When something is not this and that, it is also not this or not that.

Note: Given the imprecision of the English language on the word 'or' I am using it to mean a non-exclusive or in the preceding example.

For example the following pseudo-code is equivalent:
If NOT(A OR B)...
IF (NOT A) AND (NOT B)....

IF NOT(A AND B)...
IF NOT(A) OR NOT(B)...

JohnFx
I would rather phrase logical or as "either not this, (or) not that, or both" or "not this, not that, or neither". Though, it might be simpler and more productive at this level to concretely explain parenthetical grouping and use your second example!
Anonymous
A: 

Don't know what secondary school background means, these days. 'Twas a long time ago for me. Did you do:

  • binary & truth tables
  • sets?

It's all covered here but I guess you looked there before you came here.

What is it you're having trouble with? If you can tell us what you're trying to do, it might help.

serialhobbyist
I have no troubles with the deMorgan rules, I'm just idling and I want to put some questions that newbie programmers could find useful. Since newbie programmers can be normally in the range 10-15, I am looking for an answer that explain things easily.
Stefano Borini
+2  A: 

Looking over some of the answers, I think I can explain it better by using conditions that are actually related to each other.

DeMorgan's Law refers to the fact that there are two identical ways to write any combination of two conditions - specifically, the AND combination (both conditions must be true), and the OR combination (either one can be true). Examples are:

Part 1 of DeMorgan's Law

Statement: Alice has a sibling.
Conditions: Alice has a brother OR Alice has a sister.
Opposite: Alice is an only child (does NOT have a sibling).
Conditions: Alice does NOT have a brother, AND she does NOT have a sister.

In other words: NOT [A OR B] = [NOT A] AND [NOT B]

Part 2 of DeMorgan's Law

Statement: Bob is a car driver.
Conditions: Bob has a car AND Bob has a license.
Opposite: Bob is NOT a car driver.
Conditions: Bob does NOT have a car, OR Bob does NOT have a license.

In other words: NOT [A AND B] = [NOT A] OR [NOT B].

I think this would be a little less confusing to a 12-year-old. It's certainly less confusing than all this nonsense about truth tables (even I'm getting confused looking at all of those).

Aaronaught
+1  A: 

"He doesn't have either a car or a bus." means the same thing as "He doesn't have a car, and he doesn't have a bus."

"He doesn't have a car and a bus." means the same thing as "He either doesn't have a car, or doesn't have a bus, I'm not sure which, maybe he has neither."

Of course, in plain english "He doesn't have a car and a bus." has a strong implication that he has at least one of those two things. But, strictly speaking, from a logic standpoint the statement is also true if he doesn't have either of them.

Formally:

  • not (car or bus) = (not car) and (not bus)
  • not (car and bus) = (not car) or (not bus)

In english, 'or' tends to mean a choice, that you don't have both things. In logic, 'or' always means the same as 'and/or' in English.

Here's a truth table that shows how this works:

First case: not (cor or bus) = (not car) and (not bus)

 c | b || c or b | not (c or b) || (not c) | (not b) | (not c) and (not b)
---+---++--------+--------------++---------+---------+--------------------
 T | T ||    T   |      F       ||    F    |    F    |        F
---+---++--------+--------------++---------+---------+--------------------
 T | F ||    T   |      F       ||    F    |    T    |        F
---+---++--------+--------------++---------+---------+--------------------
 F | T ||    T   |      F       ||    T    |    F    |        F
---+---++--------+--------------++---------+---------+--------------------
 F | F ||    F   |      T       ||    T    |    T    |        T
---+---++--------+--------------++---------+---------+--------------------

Second case: not (car and bus) = (not car) or (not bus)

 c | b || c and b | not (c and b) || (not c) | (not b) | (not c) or (not b)
---+---++---------+---------------++---------+---------+--------------------
 T | T ||    T    |       F       ||    F    |    F    |        F
---+---++---------+---------------++---------+---------+--------------------
 T | F ||    F    |       T       ||    F    |    T    |        T
---+---++---------+---------------++---------+---------+--------------------
 F | T ||    F    |       T       ||    T    |    F    |        T
---+---++---------+---------------++---------+---------+--------------------
 F | F ||    F    |       T       ||    T    |    T    |        T
---+---++---------+---------------++---------+---------+--------------------
Omnifarious
+1  A: 

DeMorgan's Law allows you to state a string of logical operations in different ways. It applies to logic and set theory, where in set theory you use complement for not, intersection for and, and union for or.

DeMorgan's Law allows you to simplify a logical expression, performing an operation that is rather similar to the distributive property of multiplication.

So, if you have the following in a C-like language

if !(x || y || z) { /* do something */ }

It is logically equivalent to:

if (!x && !y && !z)

It also works like so:

if !(x && !y && z)

turns into

if (!x || y || !z)

And you can, of course, go in reverse.

The equivalence of these statements is easy to see using something called a truth table. In a truth table, you simply lay out your variables (x, y, z) and list all the combinations of inputs for these variables. You then have columns for each predicate, or logical expression, and you determine for the given inputs, the value of the expression. Any university curriculum for computer science, computer engineering, or electrical engineering will likely drive you bonkers with the number and size of truth tables you must construct.

So why learn them? I think the biggest reason in computing is that it can improve readability of larger logical expressions. Some people don't like using logical not ! in front of expressions, as they think it can confuse someone if they miss it. The impact of using DeMorgan's Law on the gate level of chips is useful, however, because certain gate types are faster, cheaper, or you're already using a whole integrated circuit for them so you can reduce the number of chip packages required for the outcome.

Corey D
That was a lot of writing.
Corey D
+2  A: 

If you're a police officer looking for underage drinkers, you can do one of the following, and De Morgan's law says they amount to the same thing:

FORMULATION 1 (A AND B)

If they're under the age limit AND drinking an alcoholic beverage, arrest them.

FORMULATION 2 (NOT(NOT A OR NOT B))

If they're over the age limit OR drinking a non-alcoholic beverage, let them go.

This, by the way, isn't my example. As far as I know, it was part of a scientific experiment where the same rule was expressed in different ways to find out how much of a difference it made in peoples' ability to understand them.

d__
+2  A: 

We have two values: T and F.

We can combine these values in three ways: NOT, AND, and OR.

NOT is the simplest:

  • NOT T = F
  • NOT F = T

We can write this as a truth table:

when given.. | results in...
============================
           T | F
           F | T

For conciseness

x | NOT x
=========
T | F
F | T

Think of NOT as the complement, that is, it turns one value into the other.

AND works on two values:

x y | x AND y
=============
T T | T
T F | F
F T | F
F F | F

AND is T only when both its arguments (the values of x and y in the truth table) are T and F otherwise.

OR is T when at least one of its arguments is T:

x y | x OR y
=============
T T | T
T F | T
F T | T
F F | F

We can define more complex combinations. For example, we can write a truth table for x AND (y OR z), and the first row is below.

x y z | x AND (y OR z)
======================
T T T | ?

Once we know how to evaluate x AND (y OR z), we can fill in the rest of the table.

To evaluate the combination, evaluate the pieces and work up from there. The parentheses show which parts to evaluate first. What you know from ordinary arithmetic will help you work it out. Say you have 10 - (3 + 5). First you evaluate the part in parentheses to get

10 - 8

and evaluate that as usual to get the answer, 2.

Evaluating these expressions works similarly. We know how OR works from above, so we can expand our table a little:

x y z | y OR z | x AND (y OR z)
===============================
T T T | T      | ?

Now it's almost like we're back to the x AND y table. We simply substitute the value of y OR z and evaluate. In the first row, we have

T AND (T OR T)

which is the same as

T AND T

which is simply T.

We repeat the same process for all 8 possible values of x, y, and z (2 possible values of x times 2 possible values of y times 2 possible values of z) to get

x y z | y OR z | x AND (y OR z)
===============================
T T T | T      | T
T T F | T      | T
T F T | T      | T
T F F | F      | F
F T T | T      | F
F T F | T      | F
F F T | T      | F
F F F | F      | F

Some expressions may be more complex than they need to be. For example,

x | NOT (NOT x)
===============
T | T
F | F

In other words, NOT (NOT x) is equivalent to just x.

DeMorgan's rules are handy tricks that let us convert between equivalent expressions that fit certain patterns:

  • NOT (x AND y) = (NOT x) OR (NOT y)
  • NOT (x OR y) = (NOT x) AND (NOT y)

(You might think of this as how NOT distributes through simple AND and OR expressions.)

Your common sense probably already understands these rules! For example, think of the bit of folk wisdom that "you can't be in two places at once." We could make it fit the first part of the first rule:

NOT (here AND there)

Applying the rule, that's another way of saying "you're not here or you're not there."

Exercise: How might you express the second rule in plain English?

For the first rule, let's look at the truth table for the expression on the left side of the equals sign.

x y | x AND y | NOT (x AND y)
=============================
T T | T       | F
T F | F       | T
F T | F       | T
F F | F       | T

Now the righthand side:

x y | NOT X | NOT Y | (NOT x) or (NOT y)
========================================
T T | F     | F     | F
T F | F     | T     | T
F T | T     | F     | T
F F | T     | T     | T

The final values are the same in both tables. This proves that the expressions are equivalent.

Exercise: Prove that the expressions NOT (x OR y) and (NOT x) AND (NOT y) are equivalent.

Greg Bacon
Your answer walks you through logic algebra. Occasionally it gets a bit complex but it's very complete. It's almost ok, probably with some sectioning it would be even better. I don't know if it would pass the 15yo test, but for a smart 15yo I guess it's very fine.
Stefano Borini
+1  A: 

Draw a simple Venn diagram, two intersecting circles. Put A in the left and B in the right. Now (A and B) is obviously the intersecting bit. So NOT(A and B) is everything that's not in the intersecting bit, the rest of both circles. Colour that in.

Draw another two circles like before, A and B, intersecting. Now NOT(A) is everything that's in the right circle (B), but not the intersection, because that's obviously A as well as B. Colour this in. Similarly NOT(B) is everything in the left circle but not the intersection, because that's B as well as A. Colour this in.

Two drawings look the same. You've proved that NOT(A and B) = NOT(A) or NOT(B). T'other case is left as an exercise for the student.

Bob Moore
this I like, it's very pictorial
Stefano Borini