Which expression is equivalent? - expression

This is for a study guide by the way:
I would appreciate the help. I tried using De Morgan's Laws but I don't think it applies here. I am new to CS1 and trying to understand which relates.
Consider the following expression: (A > B) && (C <= B)
Assume that A, B, and C are integer variables. Which of the expressions given below is (are) equivalent to the one given above?
I. !(A < B) && !(C >= B)
II. (A > B) && (B > C)
III. !((A <= B) || (B < C))

Ok, first is not good because A > B is not same to !(A < B) (!(A <= B) should be, you can try 3 and 3 for A and B to check it).
Second is not good, as (C <= B) is not same to (B > C) ((B >= C) should be, try again 3 and 3 as values).
The third one can be converted to !(A <= B) && !(B < C).
!(A <= B) is equal to A > B;
and !(B < C) we can convert into B >= C or C <= B, it's equal.
So, third answer is good.

Related

Expression Tree of logical expression

I need to transform this: (a < b)∧(b < c)∨(c < d)
expression into an expression tree, but I can't figure out a way that looks correct. Here is what I got:
I'm predicting this will be a long answer, because I will go through the general thinking process for achieving a solution. For the inpatient, the solution is at the end.
Is your solution correct?
Well, it depends. Your picture would be totally correct if ∨ took precedence over ∧, in which case you would need to apply ∧ only after (b < c) ∨ (c < d) has a result. It would also be correct if you forced precedence using parenthesis like so:
(a < b) ∧ ( (b < c) ∨ (c < d) )
That said, when talking about operator precedence, typically ∧/and has precedence over ∨/or. When precedence is the same, the evaluation happens from left to right, meaning the right side depends on the result of the left side.
The highest the precedence of an operator, the lower it appears in the tree.
The rest of the answer will assume the usual operator precedence.
How to approach this problem?
The best approach to this sort of problems is to decompose the expression. Even better, if we decompose using the prefix/polish notation, it will be more natural to build up the tree later.
Given: (a < b) ∧ (b < c) ∨ (c < d)
Let's decompose it into parts:
x = (a < b), which translates to prefix: < a b
y = (b < c), which translates to prefix: < b c
z = (c < d), which translates to prefix: < c d
We now have 3 inequality expressions decomposed as x, y and z.
Now to the logical operators.
i = x ∧ y, which translates to prefix: ∧ x y
j = i ∨ z, which translates to prefix: ∨ i z
We now have 2 logical expressions decomposed as i and j. Note how they depend on x, y, z. But also, that j depends on i. Dependencies are important because you know tree leaves have no dependencies.
How to build the tree?
To sum up, this is what we decomposed from the original expression:
x = < a b
y = < b c
z = < c d
i = ∧ x y
j = ∨ i z
Let's approach it bottom-up.
Considering the dependencies, the leaves are obviously the most independent elements: a, b, c and d.
Let's build the bottom of the tree considering all appearances of these independent elements in the decomposition we just made (b and c appear twice, we put them twice).
a b b c c d
Now let's build x, y and z which depends only on a, b, c and d. I'll be using / and \ for building my ASCII art as equivalent to your picture lines.
x y z
< < <
/ \ / \ / \
a b b c c d
Now as we've seen, i depends only on x and y. So we can put it there now. We can't add j just yet, because we need i to be there first.
i
∧
/ \
x y z
< < <
/ \ / \ / \
a b b c c d
Now we are just missing j, which depends on i and z.
j
∨
/ \
/ \
i \
∧ \
/ \ \
x y z
< < <
/ \ / \ / \
a b b c c d
And we have a full expression tree. As you see, each dependency level will result in a tree level.
To be completely accurate, a Breadth-First Search in this tree would have to consider that z is on the same level of i, so the correct representation of the tree would have to put z one level higher:
j
∨
/ \
/ \
i z
∧ <
/ \ / \
x y c d
< <
/ \ / \
a b b c
Just one more note, for it to be completely clear, for the expression (a < b) ∧ ( (b < c) ∨ (c < d) ), the decomposition result would be:
x = < a b
y = < b c
z = < c d
j = ∨ y z
i = ∧ x j
Which would, in turn, result in the tree in your picture.
Hope this helps your future endeavours on building expression trees.

Algorithm to determine if number is between two numbers in modular arithmetic

I'm trying to write a function that answers the question: if you start counting at a and stop counting at b, is c in that range (aka is c between a and b).
Normally a < c && c < b would suffice, but I'm in modular arithmetic:
Counter-clockwise is increasing.
Green colors: are values for c where the algorithm should indicate true (where c is between a and b)
Blue colors: are values for c where the algorithm should indicate false (where c is not between a and b) (which happens to be the same as where c is between b and a)
The simple a < c && c < b fails where the range of a and b crosses over 0.
For example, say A = 300 and B = 45. If C is 10 then the function should return true: 300, 301, 302 ... 359, 0, 1, 2, 3 ... 8, 9, 10, 11, 12 ... 43, 44, 45. Therefore, 10 is between 300 and 45 in mod 360.
Ultimately, what I'm trying to determine is if one hue is between two other hues, where hues are specified in degrees around a color wheel (which is a mod 360 system). It would be great if the answer was in terms of mod n so it would solve the general case and not be specific to my problem though.
First calculate a mod n, b mod n, and c mod n.
If a < b then we need to check that a < c && c < b. This is the simple case where modular arithmetic doesn't play a huge role.
Because [a, b] and [b, a] form disjoint regions, instead of trying to deal with the problem of crossing 0, we can test the reverse for cases where b < a. If b < c && c < a is true, c is actually between b and a and therefore not between a and b.
Code sample:
a = a % n; // % = mod
b = b % n;
c = c % n;
if (a < b) {
if (a < c && c < b) return true;
else return false;
} else { // b < a
if (b < c && c < a) return false; // if in [b, a] then not in [a, b]
else return true;
}
bool between = C<A ^ C<B ^ B<A;
The number will be between the given two numbers if either of following three conditions is satisfied.
Condition 1:
c mod n > a mod n && c mod n < b mod n
Condition 2:
a mod n > b mod n && c mod n < b mod n.
Condition 3:
a mod n > b mod n && c mod n > a mod n.

how to minimize this logic (or-gate CNF)?

I'm writing down the truth table for the OR-gate in a circuit satisfiability problem (this has to do with reduction of the 3-satisfiability problem).
I have:
a b c c = (a OR b)
1 1 1 1
1 1 0 0
1 0 1 1
1 0 0 0
0 1 1 1
0 1 0 0
0 0 1 0
0 0 0 1
So if I take the rows with 0 in the column c = (a OR b), and negate the a,b,c then i get the four clauses:
!a AND !b AND c
a AND !b AND !c
a AND !b and c
a AND b AND !c
I'm trying to minimize these clauses. I know that the right answer is:
c OR !a
c OR !b
c OR !a or !b
How do I minimize those four clauses? is there a program online? I used wolfram, but it didn't output the right answer, so if anyone has a second to help that would be amazing
It is easy to see that the last column is almost the same as c column except two last rows.
And for the two last rows it have values from column c swapped. So we can write as:
if (a ∨ b) then c else ¬c
if c then t else f can be represented as CNF (c ∨ t) ∧ (¬c ∨ f) so the formula above will look like:
(a ∨ b ∨ ¬c) ∧ (¬(a ∨ b) ∨ c)
= (a ∨ b ∨ ¬c) ∧ ((¬a ∧ ¬b) ∨ c)
= (a ∨ b ∨ ¬c) ∧ (¬a ∨ c) ∧ (¬b ∨ c)
And the last one is exactly what you want.

Proving the Associativity of OR

I need help proving the following:
(a ∨ b) ∨ c = a ∨ (b ∨ c)
I don't want the answer... just a hint that will help me understand the process of proving this.
Thank you.
Why not just prove it by doing all possible values of a, b and c = True, False? -- there are only 2^3 = 8 different cases.
Here's a start, for a=T, b=F, c=T
(a v b) v c = a ∨ (b ∨ c)
(T v F) v T = T v (F v T)
T v T = T v T
T = T
(However, this isn't really a programming question...)
What is your axiom set?
Not knowing the set, you could build a truth table

Which one is faster and why?

(n >= 3 ) && (n <= 99)
OR
n `elem` [3..99]
Which one is faster and why?
The first one is faster
(n >= 3) && (n <= 99)
it is doing 3 operations
n >= 3
n <= 99
and
Where as the elem is looking up the item in the array, so is doing upto (99 - 3) * 2 operations.
index = 0
isFound = false
array[] = { 3, 4, 5, 6, ... 98, 99 }
while isFound == false
isFound = (n == array[index++])
(n >= 3) && (n <= 99) is faster as it involves only two trivial comparisons. If the compiler/interpreter does not do any kind of real black magic optimization it has to construct the list ([3..99]) because lazy evaluation cannot be used (normally "pulling" the next value until you're done, which would have a complexity of O(n/2) in this case).
These two expressions don't mean the same thing. A subtle difference is that one relies on Ord and the other on Enum:
> :t \n -> (n >= 3) && (n <= 99)
\n -> (n >= 3) && (n <= 99) :: (Num a, Ord a) => a -> Bool
> :t \n -> n `elem` [3..99]
\n -> n `elem` [3..99] :: (Num a, Enum a) => a -> Bool
So, for example, if n is 3.14159, then the first test will pass, but the second won't:
> (pi >= 3) && (pi <= 99)
True
> pi `elem` [3..99]
False
Further, while the four Prelude Num instances (Int, Integer, Float, and Double) are all instances of both Ord and Enum, it is possible to imagine a numeric type that is an instance of Ord but not Enum. In such a case, then the second test wouldn't even be legal.
Hence, in general, the compiler can't optomize the second to be as fast as the first unless it knows for a given type, that it is Ord and that all ordered values in the range are also in the list enumeration created by enumFromTo. For Float and Double this isn't true, and for Int and Integer there is no way for the compiler to derive it, the compiler and library programmers would have to hand code it and ensure it held in all cases.
Depends on the machine and compiler (or interpreter).

Resources