CS154
Chris Pollett
Jan. 30, 2013
In this class we will frequently examine the efficiency of algorithms. We will use the notions on this slide to do this.
Definition: Let `NN` be the nonnegative integers. Let `f` and `g` be functions from `NN` to `NN`.
For example, `n^2+1 = O(n^2)`.
To see this notice, for all `n geq 1`, `n^2+1 leq n^2+ n^2 leq 2cdot n^2`. So `m=1`, `c=2` work for the above definition.
You might want to convince yourself that: `n^3 = Omega(n^2+n+1)` and `n^3 + n^2= Theta(n^3)`.
Problem 1. Let `A = {1, 4, 5, 9}` and `B = {3, 4, 9}`. Write out fully the elements in each of the following sets (a) `A \cap B` (b) `B - A` (c) `S(A)` (d) `2^B` (e) `A times B`. Give the cardinality of each set.
Answer. (a) `A \cap B = {4, 9}` and `|A \cap B| = 2`, (b) `B - A = {3}` and `|B - A| = 1`, (c) `S(A) = {1, 4, 5, 9, {1, 4, 5, 9}}` and `|S(A)| = 5`, (d) `2^B = {emptyset, {3}, {4}, {9}, {3, 4}, {4,9}, {3,9}, {3,4,8}}` and `|2^B| = 8` (e) `A times B = {(1,3)`,`(1,4)`, `(1,9)`, `(4,3),(4,4), (4,9), (5,3),(5,4), (5,9), (9,3),(9,4), (9,9) }` and `|A times B| = 12`.
Problem 3. Show using only the properties (a)-(d) of this slide that `S^3(0) cdot S^6(0) = S^{18}(0)`.
Answer. The point of this exercise is to realize that axioms for an operation like plus or times should give us an algorithm for computing these operations based on the simpler operations we already have defined. So the two axioms for times say how it is computed in terms of plus and successor, the axioms for plus tell us how plus is defined in terms of successor. To see that `S^3(0) cdot S^6(0) = S^{18}(0)` we will start with the left-hand-side and repeatedly use our rules to expand it until we get the right-hand-side:
`S^3(0) cdot S^6(0) =` (by d)
`S^3(0) cdot S^5(0) + S^3(0) =` (by d -- the first sequence of steps can be viewed as saying multiply 3 times 6 by adding three, six times)
`S^3(0) cdot S^4(0) + S^3(0) + S^3(0) =` (by d)
`S^3(0) cdot S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by d)
`S^3(0) cdot S^2(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by d)
`S^3(0) cdot S(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by d)
`S^3(0) cdot 0 + S^3(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by d)
`((((((0 + S^3(0)) + S^3(0)) + S^3(0)) + S^3(0)) + S^3(0)) + S^3(0)) =` (by c -- and by being less sloppy on parentheses)
`S(0 + S^2(0)) + S^3(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by b -- as there are no more multiplies left we start using properties of addition)
`S(S(0 + S(0))) + S^3(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S(S(S(0 + 0))) + S^3(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S(S(S(0))) + S^3(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by a -- now note left hand term is `S^3(0)`)
`S(S^3(0) + S^2(0)) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S(S(S^3(0) + S(0))) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S(S(S(S^3(0) + 0))) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S^6(0) + S^3(0) + S^3(0) + S^3(0) + S^3(0) =` (by a)
`S(S^6(0) + S^2(0)) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S(S(S^6(0) + S(0))) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S(S(S(S^6(0) + 0))) + S^3(0) + S^3(0) + S^3(0) =` (by b)
`S^9(0) + S^3(0) + S^3(0) + S^3(0) =` (by a)
`S(S^9(0) + S^2(0)) + S^3(0) + S^3(0) =` (by b)
`S(S(S^9(0) + S(0))) + S^3(0) + S^3(0) =` (by b)
`S(S(S(S^9(0) + 0))) + S^3(0) + S^3(0) =` (by b)
`S^(12)(0) + S^3(0) + S^3(0) =` (by a)
`S(S^(12)(0) + S^2(0)) + S^3(0) =` (by b)
`S(S(S^(12)(0) + S(0))) + S^3(0) =` (by b)
`S(S(S(S^(12)(0) + 0))) + S^3(0) =` (by b)
`S^(15)(0) + S^3(0) =` (by a)
`S(S^(15)(0) + S^2(0)) =` (by b)
`S(S(S^(15)(0) + S(0))) =` (by b)
`S(S(S(S^(15)(0) + 0))) =` (by b)
`S^(18)(0)` (by a, we're done!!)
Problem 2. Write down all possible partitions of the set `{1,2,3,4}`
Answer.`{{1,2,3,4}}`, `{{1},{2,3,4}}`, `{{2},{1,3,4}}`, `{{3},{1,2,4}}`, `{{4},{1,2,3}}`, `{{1, 2},{3, 4}}`, `{{1, 3}, {2, 4}}`, `{{1, 4}, {2, 3}}`, `{{1},{2}, {3,4}}`, `{{1},{3}, {2,4}}`, `{{1},{4}, {2,3}}`, `{{2},{3}, {1,4}}`, `{{2},{4}, {1,3}}`, `{{3}, {4}, {1,2}}`, `{{1}, {2}, {3}, {4}}`.
Problem 4. Construct using `^^`, `vv`, `not` gates a boolean function `{0, 1}^4 rightarrow {0, 1}` which returns `1` if and only if at least half of the boolean inputs are `1`.
Answer. Here we are view 1 as true and 0 as false, and otherwise are using our definitions of `^^`, `vv`, `not` from last day. First, let's call this function `f` and make a truth table of this function:
`x_1` | `x_2` | `x_3` | `x_4` | `f` |
---|---|---|---|---|
`0` | `0` | `0` | `0` | `0` |
`0` | `0` | `0` | `1` | `0` |
`0` | `0` | `1` | `0` | `0` |
`0` | `0` | `1` | `1` | `1` |
`0` | `1` | `0` | `0` | `0` |
`0` | `1` | `0` | `1` | `1` |
`0` | `1` | `1` | `0` | `1` |
`0` | `1` | `1` | `1` | `1` |
`1` | `0` | `0` | `0` | `0` |
`1` | `0` | `0` | `1` | `1` |
`1` | `0` | `1` | `0` | `1` |
`1` | `0` | `1` | `1` | `1` |
`1` | `1` | `0` | `0` | `1` |
`1` | `1` | `0` | `1` | `1` |
`1` | `1` | `1` | `0` | `1` |
`1` | `1` | `1` | `1` | `1` |
Two ways we could express this as a boolean formula are: (1) Write a formula that expresses that at least one of the rows where `f` is `1` is true (disjunctive normal form), or (2) Write a formula that expresses that none of the rows where `f` is `0` is true (conjunctive normal form). We'll use approach (1), although it gives a longer formula. This give the formula:
`(not x_1 ^^ not x_2 ^^ x_3 ^^ x_4) vv`
`(not x_1 ^^ x_2 ^^ not x_3 ^^ x_4) vv`
`(not x_1 ^^ x_2 ^^ x_3 ^^ not x_4) vv`
`(not x_1 ^^ x_2 ^^ x_3 ^^ x_4) vv`
`(x_1 ^^ not x_2 ^^ not x_3 ^^ x_4) vv`
`(x_1 ^^ not x_2 ^^ x_3 ^^ not x_4) vv`
`(x_1 ^^ not x_2 ^^ x_3 ^^ x_4) vv`
`(x_1 ^^ x_2 ^^ not x_3 ^^ not x_4) vv`
`(x_1 ^^ x_2 ^^ not x_3 ^^ x_4) vv`
`(x_1 ^^ x_2 ^^ x_3 ^^ not x_4) vv`
`(x_1 ^^ x_2 ^^ x_3 ^^ x_4)`