Discrete Mathematics I
Class Description
This class is called Mathematical Concepts in Computing I at my college and covers propositions/predicates, sequences, number theory, set theory, functions, matrices, basic proof techniques, combinatorics, counting, relations, and trees/graphs.
Book Used: Discrete Mathematics and Its Applications by Kenneth Rosen
The book had some nice examples and problems, but sometimes explanations were bad.
These are my notes and are primarily meant to be used as a reference for me only, so there’s probably stuff that I got wrong. Comment if you find mistakes.
Helpful websites and resources I used for the class:
- Kimberly Brehm’s Youtube Channel. Pretty good explanations.
- https://math.berkeley.edu/~arash/55/ : Condensed version of book chapters. A bit hard to read sometimes.
- https://www.slader.com/: For solutions to book problems
Counting and Combinatorics (Chapter 6)
Counting (6.1)
Stuff based on https://youtu.be/zG9Y8hnXZOc
Sum Rule
Definition: If task A can be performed in m ways and task B in n ways, and the tasks are disjoint (the tasks don’t affect one another), then A OR B can be performed in m + n
ways.
Example: If we roll two dice, one green and one purple, how many ways are there to get a sum of 7 or 11?
Ways to get 7:
(1, 6), (2, 5), (3, 4)
(6, 1), (5, 2), (4, 3)
Ways to get 11:
(5, 6)
(6, 5)
OR means +
and using the Sum Rule.
So add the number of ways to get 7 and the number of ways to get 11: 6 + 2 = 8
8 is number of ways to get either a 7 or 11.
Example: 8 Republicans and 5 Democrats are nominated. How many possible winners are there.
Answer: 8 + 5 = 13 possibilities.
Product Rule
Definition: Task A (m ways) and task B (n ways). Tasks A AND B can be performed in m
Example: 8 Republicans and 5 Democrats nominated for president. How many possibilities for a pair of candidates (running against each other with one from each party)?
Answer: 8 * 5 = 40 possible pairs
Explanation: For every republican there’s 5 democrats to pair with. Draw a tree diagram if you want.
Example: Student ID made up of 3 letters followed by 2 digits. How many possible IDs?
Answer: 26 * 26 * 26 * 10 * 10
Example: Student ID made up of 3 letters followed by 2 digits. How many possible IDs with no duplicate numbers/letters?
Answer: 26 * 25 * 24 * 10 * 9
Explanation: First letter has all possibilities open, the second letter has all possibilities minus the first letter, the third has all possibilities minus first and second, and so on.
Example: Student ID made up of 3 letters followed by 2 digits. How many possible IDs with even number of A's (including zero A's)?
Answer: ( 25 * 25 * 25 * 10 * 10) + (1 * 1 * 25 * 10 * 10) + (1 * 25 * 1 * 10 * 10) + (25 * 1 * 1 * 10 * 10 * 10 )
Explanation: Add the following up
Opposite/Complement
Sometimes it’s easier to find the opposite case and subtract that from all possibilities since there might be overlapping cases or too many of the possibilities you’re looking for.
Example: A student ID is made up of 3 letters followed by 2 digits. How many have some repetition?
Answer: 26^3 * 10 * 10 - 26 * 25 * 10 * 9
Explanation: Find the number of cases that don’t have any repetition. Just subtract the number of distinct IDs from total possible IDs. We already solved those two above.
Other Problems
Determine the number of 6-digit integers (no leading zeros) in which:
Example: No repetitions
Answer: 9 * 9 * 8 * 7 * 6 * 5
Explanation: (10 - 1) * (10 - 1) * (10 - 1 - 1) * (10 - 1 - 1 -1) ... etc.
The first digit has all possible digits except zero, the second digit has all possibilities except the first digit, the third has all possibilities except first and second, etc.
Example: Repetitions allowed
Answer: 9 * 10 * 10 * 10 * 10 * 10
Explanation: All six digits have all possible digits open except first digit. First digit has all digits as possibilities except zero.
Example: No repetitions, even number
Explanation:
There are two cases: If the last digit is zero or one of (2, 4, 6, 8). Last digit has to be even.
For the case that last digit is non-zero even integer:
8 * 8 * 7 * 6 * 5 * 4
Last digit has to be even but not zero (leaving 4 choices (2, 4, 6, 8). First digit has all possible digits minus the last digit and zero. Second digit has all possible digits minus last digit and first digit. Third digit has all possible digits minus first digit, second digit, and last digit. Etc.
For the case that last digit is zero:
Last digit has to be zero. First digit can be any digit except zero. Second can be any digit except first and zero. Etc.
Add the two cases together to get the final answer.
Answer: 8 * 8 * 7 * 6 * 5 * 4 + 9 * 8 * 7 * 6 * 5 * 1
Example: Repetitions allowed, even number
Answer: 9 * 10 * 10 * 10 * 10 * 5
Explanation: Last digit has to one of the even digits. First digit can be any digit except zero. All other digits can be any digit.
Example: No repetitions, divisible by 5
Explanation: The number has to end with 0 or 5 if it is divisible by 5. So we split this problem into two cases: first case is if last digit is 0 and second case is if the last digit is 5.
This is how many possible combinations end in zero: (10 - 1) * (10 - 2) * (10 - 3) * (10 - 4) * (10 - 5) * (1)
. The last digit has to be zero. The first digit can be any digit except zero. The second digit can be any digit except the first digit and zero. Etc.
This is how many possible combinations end in five: (10 - 1 - 1) * (10 - 2) * (10 - 3) * (10 - 4) * (10 - 5) * (1)
. The last digit has to be five. The first digit can be any digit except zero and five. The second digit can be any digit except the first digit and five. Etc.
Add these two subproblems to get the answer.
Answer: 9*8*7*6*5*1 + 8*8*7*6*5*1
Example: No repetitions, divisible by 4
Explanation: Numbers that are divisible by 4 either end in 4 or 8 or end in one of the following 2-digit combos: 04, 08, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96
. Any n-digit number (where n > 2) will end with one of these combo if they’re divisible by 4. Since the first digit can’t be zero, divide the 2-digit combos into two groups: one group with zeros and one without.
Group without zeros (12, 16, 24, 28, 32, 36, 48, 52, 56, 64, 68, 72, 76, 84, 92, 96): 16 options.
So we get 7 * 7 * 6 * 5 * 16
since the first digit can be any digit except zero and the two last digits, the second digit can be any digit except the first and last two, etc. and last two digits can be any from the “Group without zeros.”
Group with zeros (04, 08, 20, 40, 60, 80): 6 options.
So we get 8 * 7 * 6 * 5 * 6
since the first digit can be any digit except the two last digits, the second digit can be any digit except the first and last two, etc. and last two digits can be any from the “Group with zeros.”
To get the final answer add the two: 7 * 7 * 6 * 5 * 16 + 8 * 7 * 6 * 5 * 6
Pigeonhole Principle (6.2)
Permutations and Combinations (6.3)
Based on https://youtu.be/EHMDRUSOubM and https://youtu.be/bT8JoK1D9sM
Permutations
Definition: Arrangement of objects where order matters
For n letters there are the following distinct permutations:
Example: How many ways to arrange letters A, B, C, D, and E
in groups of 3?
Answer:
Example: From a pool of 10 candidates, how many arrangements of president, vice president, and secretary:
Answer:
Permutation With Repetition
If you have
Example: How many ways to arrange letters in “Oboe” (if O and o are considered the same letter)?
Oboe Obeo Oeob Oebo Oobe Ooeb boeO boOe boOe beoO eObo eOob ebOo
obOe obeO oeOb oebO oObe oOeb bOeo bOoe bOoe beOo eobO eoOb eboO
While there are 24 possible arrangements, there are only 12 UNIQUE ones since o is repeated.
To solve we do
Permutations Problems
-
How many ways can you arrange letters in the word “banana”?
Answer: There are 6 letters total and repeats twice and repeats thrice. -
a) How many arrangements for letters in “sociological”?
Answer: 12 letters total, 3o
's, 2l
's, 2c
's, and 2i
's -
b) In how many arrangements are A and G adjacent (think of “ag” as a letter and “ga” as a letter) in sociological?
Answer: If we treat “ag” and “ga” as one letter, then we have 11
letters.”
It’s the same as above except we multiply by 2 since there can be ag or ga. -
c) In how many arrangements are all the vowels adjacent in sociological?
Answer: We have 6 vowels (3o
's, 2i
's, and 1a
). We have 6 consonants.
First term is vowels and second term is treats the block of vowels as one “letter” with the rest of the consonants. Use product rule and the technique for dealing with duplicates.
Combinations
Definition: If we have n distinct objects and want to choose k of these objects without caring about the order, you can use the following formula:
Note that
Example: 5 white balls are chosen from 69 white balls, and 1 red ball from 26 red balls.
- Find the number of combinations of white balls.
Answer: AND means use product rule. - Find the number of combinations of white balls.
Answer: - Find the number of combinations.
Answer: Use product rule
Combinations with Permutations
How many arrangements of the letters “MISSISSIPPI” have no consecutive S’s?
Answer:
Remove the 4 S
's and notice how there are now 8 spots to put the S
's in between the letters. So we have a combination where we can use the following
We can also permute the other letters that are not S’s (7 letters with 4 duplicate I’s and 2 duplicate P’s):
The final solution:
Binomial Theorem (6.4)
Entries for Pascal’s triangle
The binomial theorem:
Relations (Chapter 9)
Relations and Properties (9.1)
Definition:
Relation from A to B
Notation: If a R B
or a ~ b
Example: |A| = 10, |B| = 10
The number of possible relations from A to B is
Example: Write a list of elements of R
A = {0, 1, 2}
and B = {a, b}
There are a total of 64 possible relations from A to B
Some possible elements of R:
R = {(0, a), (0, b), (1, a), (2, b)...}
Reflexive
A relation is reflexive if
A relation is irreflexive if
Example:
Since
Example: A = Set of all people
, S = {(a, b) | a is the parent of b
Irreflexive since a is the parent of a
is always false.
In a digraph, if a graph is reflexive, all nodes have a loop that points back to itself.
In a matrix, the diagonal should all be 1’s if reflexive. If it is irreflexive, then the diagonal should be all 0’s.
Symmetric
R is symmetric if
Look for “two way street” in digraph
If
Antisymmetric
R is antisymmetric if
Asymmetric
Both irreflexive and asymmetric
Transitive
Relations Properties Problems
Example: Find the properties the relation R where
It’s only symmetric since xx = 0
isn’t always true. It isn’t transitive since if y
was 0 and both x
and z
were non-zero that wouldn’t be the case).
Composition
Matrix:
Example:
Answer:
Explanation: Since a = b
and b > c
. This implies a > c
, which is
Subtraction
Example:
Answer:
Explanation: Since
Symmetric Difference
Complement
Let R be a relation. Then
Representing Relations (9.3)
You can use a digraph or matrix to represent a relation.
Closures of Relations (9.4)
Closure: The smallest relation with a property P that also contains the original relation R. The new relation should have the property and also have the original relation. P can be symmetry, transitivity, reflexivity, etc.
Reflexive Closure
Closure:
Matrix:
Symmetric Closure
Closure:
Matrix:
Transitive Closure
Closure:
Matrix:
Where
While it says we have to multiply matrixes together forever, we can stop multiplying when no new edges are introduced.
Warshall’s Algorithm
A more efficient way of getting the above (transitive closure).
Example:
Graphs (Chapter 10)
Graphs and Graph Models (10.1)
A graph G = (V, E) consists of V, a nonempty set of vertices/nodes and E, a set of edges.
Can be represented using adjacency matrix or adjacency list.
|E| = Number of edges
|V| = Number of vertices
Different types: Directed and undirected
Simple Graph: Undirected graph with no loops and no multiple edges.
Trees (Chapter 11)
Tree: Acyclic undirected graph (No cycles).
T = (V, E)
|E| = |V| - 1
deg(v) = Number of edges incident
Total degrees = 2
Miscallaneous
Fast Multiplication: Karatsuba’s Algorithm
Notes based on https://youtu.be/-dfsxsiGoC8 since the video goes more in-depth than my class did.
- The way we learned to multiply in elementary school was not the fastest way
- The traditional multiplication algorithm we use takes
multiplications.
Lety = 9876
andz = 1234
and maken = 4
(number of digits).
Regular Multiplication Algoritm
Multiplying y * z with the regular algorithm:
Step 1. There are 4 multiplications done in this step. 4 * 6
, 7 * 4
, 8 * 4
, and 9 * 4
.
9876 x 1234 --------
Step 2. Again, there are 4 multiplications done in this step. 3 * 6
, 7 * 3
, 8 * 3
, and 9 * 3
.
9876 x 1234 --------
Step 3. Again, there are 4 multiplications done in this step. 2 * 6
, 7 * 2
, 8 * 2
, and 9 * 2
.
9876 x 1234 --------
Step 4. Again, there are 4 multiplications done in this step. 1 * 6
, 7 * 1
, 8 * 1
, and 9 * 1
.
9876 x 1234 --------
Since there are four steps each with 4 multiplications done, the regular algorithm has a total of 16 multiplications done, indicating an
Divide and Conquer Approach
y = 9876
Let c = 98
and d = 76
z = 1234
Let a = 12
and b = 34
Then y = 98 * 100 + 76
and z = 12 * 100 + 34
.
This means that
We can generalize the multiplication of y and z as follows:
We now have 4 multiplications: ac
, ad
, bc
, and bd
.
The above figure shows that ad
and bc
are both a max of n digits long since a
, b
, c
, and d
are all n/2
digits in length. Thus an n
digit number added to another n
digit number takes n
additions. We use
Multiplying by any power by 10 is just a shift to the left. Example, if n = 4
and X
represents any non-zero digit:
When we add all the terms we get the following:
XXXX0000 0000XXXX + 00XXXX00 ----------
XXXXXXXX + 00XXXX00 ----------
Notice that only n
digits overlap
Thus we only have to do n
additions and we can represent them in terms of atomic additions to get
Since ad + bc
took yz
is
Recurrence Relation
We use the formula ac
, ad
, bc
, or bd
is not a multiplication between single digits (i.e. not atomic), then we have to do a recursive call and use the formula again by further splitting up the numbers in half until we get atomic multiplication (i.e. when we get multiplication between two one digit numbers). The base case is when we get “atomic multiplication.” The
We have 4 multiplications done for each recursive call:
Let M(n)
represent the recursive relation describing the result for multiplying numbers of length n
.
At each recursive call, we need to get the results of the 4 multiplications (
Find Additions at Each Level
Representing the recursive calls using a tree (number of additions at each level shown on right hand side):
n 1(2𝛼n)
n/2 n/2 n/2 n/2 4(2𝛼(n/2))
n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 16(2𝛼(n/4))
The tree continues on until the base case is reached.
We see that the number of subproblems/additions at each level is i
is depth of the tree. We also see that the number of digits gets halved each time we do a recursive call, so at each level, the number of additions is
Find Total Number of Multiplications
Since the base case is when the input to M(n) is 1 and the input to M(n) gets halved, we can find the total number of levels by taking the log base 2 of n, where n is the number of total number of digits. Since there’s
Sum up Everything
The following represents the solution for our whole problem:
Note: The summation is only applied to the first term to get the total number of additions.
The bounds for the summation end at -1
. Furthermore, the additions only occur when n for M(n) is not 1 (e.g. when not the base case), since at the base case, we just return a constant without doing any further non-atomic operations. We only sum the first term since we have to do additions at every recursive call and we also already summed the multiplications up for each level in the previous subsection.
This just evaluates to the following:
which means this is still
Karatsuba’s Thing
Take the formula from before:
And rewrite part of the second term:
Annotated above is how many digits each term will have. Adding two numbers
Now we have the following result:
Notice how we only have 3 multiplications now: ac
, (a + b)(c + d)
, and bd
. We increased the number of additions to 4 as a tradeoff as shown below.
The stuff below shows how whole expression takes X
denotes a non-zero digit:
When we add all the terms we get the following:
XXXX0000 0000XXXX + 00XXXX00 ----------
XXXXXXXX + 00XXXX00 ----------
Notice that only n
digits overlap
Thus we only have to do n
additions and we can represent them in terms of atomic additions to get
Since the second term ((a + b)(c + d) - ad - cb
) took yz
is
Revised Recurrence Relation:
Do the same analysis and stuff as before:
Tree Diagram:
n 1(4𝛼n)
n/2 n/2 n/2 3(4𝛼(n/2))
n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 9(4𝛼(n/4))
Summation:
With some magic and simplication:
The runtime is
This is Karatsuba Multiplication.