Computer science is not about machines, in the same way that astronomy is not about telescopes. There is an essential unity of mathematics and computer science.
-- Edsger Dijkstra
🎯 Objectives:
- Model Ideas into flowcharts and pseudocode
- Know right from wrong with logic
- Count stuff
- Calculate probabilities
It is very easy for our brains to overflow with fact and ideas, dump all important stuff on paper.
There are different ways to help you break down a problem into smaller processable chunks:
- Flowcharts
- Pseudocode
- Math Modeling (important to express abstract ideas)
- write states and instruction steps inside rectangles
- write decision steps, where the process may go different ways, inside diamonds
- never mix an instruction step with a decision step
- connect sequential steps with an arrow
- mark the start and end of the process
human-friendly code that expresses computational processes
# max of three numbers
function maximum (A, B, C)
if A > B
if A > C
max <- A
else
max <- C
else
if B > C
max <- B
else
max <- C
return max
Code: max-of-three-numbers.js
💡 Tip:
stand on the shoulders of giants who created these tools
a set of concepts that represents a problem and its characteristics
Livestock Fence
Your farm has two types of livestock. You have 100 units of barbed wire to make a rectangular fence for the animals, with a straight division for separating them.
How do you frame the fence in order to maximize the pasture's area?
Solution:
Quadratic equation:
$$ A = w \times l $$ $$ 100 = 2w + 3l $$ $$ l = \frac{100-2w}{3} $$ $$ A = \frac{100}{3}w - \frac{2}{3}w^2 $$
Topics covered in this section:
- Logic statements
- Operators
- Special Algebra
variables and operators represent validity of things, expressing
true
orfalse
values
!A
or~A
A | ~A |
---|---|
T | F |
F | T |
Dependency between variables
A -> B
implies thatB
depends onA
to betrue
Example: If it rains, then I'll take my umbrella
Notice:
A -> B
is NOT equivalent toB -> A
this is called inverse errorNotice:
A -> B
is equivalent to!A | B
A | B | A -> B |
---|---|---|
T | T | T |
T | F | F |
F | T | T |
F | F | T |
A
true
premise implies atrue
conclusion.T -> T = T
A
true
premise can NOT imply afalse
conclusion.T -> F = F
You can conclude anything from a
false
assumption.F -> * = T
If
A -> B
then the contrapositive is!B -> !A
Example: If You love me, I'll kiss you
Notice:
A -> B
is the same as!B -> !A
Example: I won't kiss you if you don't love me
A <-> B
expresses thatA
depends onB
and viceversaExample: I'll kiss you only if you love me or Only if you love me I'll kiss you
A | B | A <-> B |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | T |
A & B
or$A \bigwedge B$
A | B | A & B |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | F |
A | B
or$A \bigvee B$
A | B | A | B |
---|---|---|
T | T | T |
T | F | T |
F | T | T |
F | F | F |
A XOR B
Notice:
A XOR B
is equivalent to!(A <-> B)
A | B | A X B |
---|---|---|
T | T | F |
T | F | T |
F | T | T |
F | F | F |
Notice:
A X B
isT
if eitherA
orB
isT
, but not both.
A | B | !A | A -> B | A <-> B | A & B | A | B | A xor B |
---|---|---|---|---|---|---|---|
✓ | ✓ | ✗ | ✓ | ✓ | ✓ | ✓ | ✗ |
✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ | ✓ |
✗ | ✓ | ✓ | ✓ | ✗ | ✗ | ✓ | ✓ |
✗ | ✗ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ |
NOT
AND
,OR
,XOR
IMPLIES
,EQUIVALENCE
Boolean Algebra simplifies logical expressions.
Boolean
comes from George Bool.
A AND (B AND C) = (A AND B) AND C
A OR (B OR C) = (A OR B) OR C
A AND (B OR C) = (A AND B) OR (A AND C)
A OR (B AND C) = (A OR B) AND (A OR C)
!(A AND B) = !A OR !B
!A AND !B = !(A OR B)
Problem: Hot Server
columns for each variable
rows to represent possible combinations of variable states
Note:
A truth table with30
variables can have more than a billion rows.
$2^{30} = 1,073,741,824$
v1 | v2 | v3 |
---|---|---|
✅ | ✅ | ✅ |
✅ | ✅ | ❌ |
✅ | ❌ | ✅ |
✅ | ❌ | ❌ |
❌ | ✅ | ✅ |
❌ | ✅ | ❌ |
❌ | ❌ | ✅ |
❌ | ❌ | ❌ |
3
variables =8
possible combinations:
$2^3 = 8$
Problem: Fragile System
perform logic operations on electric current
Counting and Logic belong to an important field to computer science called Discrete Mathematics.
"Past events never affect the outcome of an independent event."
- xkcd: An Apple for a Dollar
- Programmer's Life (@ProgrammersLife) | Twitter
- Wolfram|Alpha: Computational Intelligence
- Solving the Zebra Puzzle with Boolean Algebra
- Discrete Mathematics and its Applications, 7th Edition