First-order logic formula - logic

If I want to express in first-order logic that 'the element(s) in the set with the smallest radius has the value 0', would the following be right?
∀ e1 ∈ S. ∀ e2 ∈ S. Radius e1 ≤ Radius e2 ⇒ Value e1 = 0?
Are the variables correctly quantified?
Thanks

Just to clarify with parentheses, what you wrote is usually taken to mean:
\forall e1 \in S. (\forall e2 \in S. (Radius e1 <= Radius e2 --> Value e1 = 0))
This statement asserts that the value of every element is 0. Here's how: Pick an arbitrary e1, now pick e2 = e1, and we have: Radius e1 <= Radius e1 --> Value e1 = 0. Since the antecedent (thing before the -->) is true, we have Value e1 = 0. And since we made no assumptions about e1, we have forall e \in S. Value e = 0.
The problem is that your parentheses are off.
\forall e1 \in S. (\forall e2 \in S. Radius e1 <= Radius e2) --> Value e1 = 0
In order for the antecedent to be true now, e1's radius has to be less than or equal to every (as opposed to any) other radius, which seems like what you intended.

I think you want an exists
\exists e_1 . (\forall e_2 radius(e_1) <= radius(e_2)) and (radius(e_1) = 0)
I'm not sure about the precedence in the formula, but now that I think I understand the question, maybe you want (where M is the minimality condition radius(e_1) < radius(e_2))
\forall e_1 . ((\forall e_2 . M) -> value e_1 = 0)
I think your previous formula may be wrong for the following reason. Suppose you have elements with radii { 0, 1, 2 }, and values equal to radii. Then, you will have a case where 1 <= 2, but the value is not zero. If I'm interpreting your original formula correctly,
\forall e_1 . \forall e_2 . P(e_1, e_2)
Then this counterexample provides a case where P is false, therefore the entire formula fails (but the example should be true).

What you wrote is also true if there are no elements with the smallest radius. If this is desired, you are correct; if not, you need to add a clause to that effect:
(\forall e1 \in S. \forall e2 \in S. Radius e1 <= Radius e2 --> Value e1 = 0) \and (\exists e1 \in S. \forall e2 \in S. Radius e1 <= Radius e2)

Related

confusing question of maximum possible weight of a subsequence

The weight of a sequence a0, a1, …, an-1 of real numbers is defined as
a0+a1/2+…+ aa-1/2n-1. A subsequence of a sequence is obtained by
deleting some elements from the sequence, keeping the order of the
remaining elements the same. Let X denote the maximum possible weight
of a subsequence of a0, a1, …,an-1 and Y the maximum possible weight
of a subsequence of a1, a2, …,an-1. Then X is equal to (A) max(Y,
a0+Y) (B) max(Y, a0+Y/2) (C) max(Y, a0+2Y) (D) a0+Y/2
Answer: (B)
Explanation: Using concepts of Dynamic Programming, to find the
maximum possible weight of a subsequence of X, we will have two
alternatives:
Do not include a0 in the subsequence: then the maximum possible weight will be equal to maximum possible weight of a subsequence of
{a1, a2,….an} which is represented by Y
Include a0: then maximum possible weight will be equal to a0 + (each number picked in Y will get divided by 2) a0 + Y/2. Here you can
note that Y will itself pick optimal subsequence to maximize the
weight.
Final answer will be Max(Case1, Case2) i.e. Max(Y, a0 + Y/2). Hence
B).
Why is the 2nd alternative Y/2 using Dynamic programming?
As per my understanding, the alternatives are:
without a0,
= the maximum possible weight of a subsequence of a1, a2, …,an-1 = Y
with a0,
= a0 + the maximum possible weight of a subsequence of a1, a2, …,an-1 = a0 + Y (but in above explaination it takes Y/2. Why?)
According to the question:
the weight of the subsequence a0, a1, a2,..., an-1
is:
X = a0 + a1/2 + a2/4 .... + an-1/2^(n-1)
and, the weight of the subsequence (with a0 not included) a1, a2, a3,..., an-1
is:
Y = a1 + a2/2 .... + an-1/2^(n-2)
Now, to get X from Ywe can observe that:
X = a0 + a1/2 + a2/4 .... + an-1/2^(n-1)
=> X = a0 + (a1/2 + a2/4 .... + an-1/2^(n-1))
=> X = a0 + 1/2(a1 + a2/2 .... + an-1/2^(n-2))
=> X = a0 + 1/2(Y)
Now, applying Dynamic Programming, the max weight of the subsequence a0, a1, a2,..., an-1 will be max(Y, X) = max(Y, a0 + Y/2)
Hence option B is correct.
Without a0, the subsequence sum is
Y = a1 + a2/2 + a3/4 ....
With a0 included, the sum becomes
a0 + a1/2 + a2/4 + a3/8 ... = a0 + [1/2 * (a1 + a2/2 + a3/4 ...)] = a0 + Y/2
So the correct answer would be option B.

Question about the Extended Euclidean Algorithm

So about the Extended Euclidean algorithm, the algorithm that finds, a, b, and d in the equation am+bn=d, where m and n are two positive integers, d is their GCD, and a and b are two integers (not necessarily positive)
So the book I am following has the following instructions for implementing this algorithm:
E1. [Initialize.] Set a′ ← b ← 1, a ← b′ ← 0, c ← m, d ← n.
E2. [Divide.] Let q and r be the quotient and remainder, respectively,
of c divided by d. (We have c = qd + r and 0 ≤ r < d.)
E3. [Remainder zero?] If r = 0, the algorithm terminates;
we have in this case am + bn = d as desired.
E4. [Recycle.] Set c ← d, d ← r, t ← a′, a′ ← a, a ← t − qa, t ← b′, b′ ← b, b ← t − qb,
and go back to E2.
My problem is that I don't particularly understand all the math around this algorithm, I understand the normal Euclidean algorithm well enough, so I understand half of the extended one as well. What I don't understand is the need for a' and b' as well t (I get that t is a temporary variable but I don't get the a = t - qa and b = t - qb part). That as well as the initialization of the variables a, a', b, and b' as well as the swapping of the values between them on each iteration. Can anybody help me bridge these gaps in my understanding of this algorithm?

Relation among Diameter and Radius in an Undirected Graph?

we consider only graphs that are undirected. The diameter of a graph is the maximum, over all choices of vertices s and t, of the shortest-path distance between s and t . (Recall the shortest-path distance between s and t is the fewest number of edges in an s-t path.) Next, for a vertex s, let l(s) denote the maximum, over all vertices t, of the shortest-path distance between s and t. The radius of a graph is the minimum of l(s) over all choices of the vertex s.
with radius r and the diameter d which of the following is always hold? choose the best answer.
1) r >= d/2
2) r <= d
we know the (1) and (2) always hold and in any reference book that written.
my challenge is this problem mentioned on Entrance Exam and just one of (1) or (2) should be true, the OP says choose the best answer and after the exam answer sheet wrote (1) is the best choice. How can verify me, why the (1) is better than (2).
They both are indeed True.
Don't let an exam with ambiguous questions weaken your concepts.
Well as for the Proof:
First of all 2nd inequality is quite trivial (from the definition itself)
Now the 1st one
d <= 2*r
Let z be a central vertex, then:
e(z)=r
Now,
diameter = d(x,y) [d(x,y) denotes distance between some vertex x & y]
d(x,y) <= d(x,z) + d(z,y)
d(x,y) <= d(z,x) + d(z,y)
d(x,y) <= e(z) + e(z) [this can be an upper bound as e(z)>=d(z,u) for all u]
diameter <= 2*r
They both hold.
2) should be clear.
1) holds using the triangle inequality. We can use this property because distances on graphs are a metric (http://en.wikipedia.org/wiki/Metric_%28mathematics%29). Using Let d(x, z) = diameter(G) and let y be a center of G (i.e. there exists a vertex v in G such that d(y, v) = radius(G)). Because d(y, v) = radius(G) and d(y, v) = d(v, y), we know that d(v, z) <= radius(G). Then we have that diameter(G) = d(x, z) <= d(y, v) + d(v, z) <= 2*radius(G).
The OP defined the shortest-path distance between s and t as "the fewest number of edges in an s-t path". This makes things simpler.
We may write the definitions in terms of some pseudocode:
def dist(s, t):
return min([len(path)-1 for path starts with s and ends with t])
r = min([max([dist(s, t) for t in V]) for s in V])
d = max([max([dist(s, t) for t in V]) for s in V])
where V is the set of all vertexes.
Now (2) is obviously true. The definition itself tells this: max always >= min.
(1) is slightly less obvious. It requires at least a few steps to prove.
Suppose d = dist(A, B), and r = dist(C, D), we have
dist(C, A) + dist(C, B) >= dist(A, B),
otherwise the length of path A-C-B would be smaller than dist(A, B).
From the definition of r, we know that
dist(C, D) >= dist(C, A)
dist(C, D) >= dist(C, B)
Hence 2 * dist(C, D) >= dist(A, B), i.e., 2 * r >= d.
So which one is better? This depends on how you define "better". If we consider something non-trivially correct (or not so obvious) to be better than something trivially correct, then we may agree that (1) is better than (2).

Dijkstra Algorithm on a graph modeling a network

We have a directed graph G = (V, E) for a comm. network with each edge having a probability of not failing r(u, v) (defined as edge weight) which lies in interval [0, 1]. The probabilities are independent, so that from one vertex to another, if we multiply all probabilities, we get the the probability of the entire path not failing.
I need an efficient algorithm to find a most reliable path from one given vertex to another given vertex (i.e., a path from the first vertex to the second that is least likely to fail). I am given that log(r · s) = log r + log s will be helpful.
This is what I have so far -:
DIJKSTRA-VARIANT (G, s, t)
for v in V:
val[v] ← ∞
A ← ∅
Q ← V to initialize Q with vertices in V.
val[s] ← 0
while Q is not ∅ and t is not in A
do x ← EXTRACT-MIN (Q)
A ← A ∪ {x}
for each vertex y ∈ Adj[x]
do if val[x] + p(x, y) < val[y]:
val[y] = val[x] + p(x, y)
s is the source vertex and t is the destination vertex. Of course, I have not exploited the log property as I am not able to understand how to use it. The relaxation portion of the algorithm at the bottom needs to be modified, and the val array will capture the results. Without log, it would probably be storing the next highest probability. How should I modify the algorithm to use log?
Right now, your code has
do if val[x] + p(x, y) < val[y]:
val[y] = val[x] + p(x, y)
Since the edge weights in this case represent probabilities, you need to multiply them together (rather than adding):
do if val[x] * p(x, y) > val[y]:
val[y] = val[x] * p(x, y)
I've changed the sign to >, since you want the probability to be as large as possible.
Logs are helpful because (1) log(xy) = log(x) + log(y) (as you said) and sums are easier to compute than products, and (2) log(x) is a monotonic function of x, so log(x) and x have their maximum in the same place. Therefore, you can deal with the logarithm of the probability, instead of the probability itself:
do if log_val[x] + log(p(x, y)) > log_val[y]:
log_val[y] = log_val[x] + log(p(x, y))
Edited to add (since I don't have enough rep to leave a comment): you'll want to initialize your val array to 0, rather than Infinity, because you're calculating a maximum instead of a minimum. (Since you want the largest probability of not failing.) So, after log transforming, the initial log_val array values should be -Infinity.
In order to calculate probabilities you should multiply (instead of add) in the relaxation phase, which means changing:
do if val[x] + p(x, y) < val[y]:
val[y] = val[x] + p(x, y)
to:
do if val[x] * p(x, y) < val[y]:
val[y] = val[x] * p(x, y)
Using the Log is possible if the range is (0,1] since log(0) = -infinity and log(1) = 0, it means that for every x,y in (0,1]: probability x < probability y than: log(x) < log(y). Since we are maintaining the same relation (between probabilities) this modification will provide the correct answer.
I think you'll be able to take it from here.
I think I may have solved the question partially.
Here is my attempt. Edits and pointers are welcome -:
DIJKSTRA-VARIANT (G, s, t)
for v in V:
val[v] ← 0
A ← ∅
Q ← V to initialize Q with vertices in V.
val[s] ← 1
while Q is not ∅ and t is not in A
do x ← EXTRACT-MAX (Q)
A ← A ∪ {x}
for each vertex y ∈ Adj[x]
do if log(val[x]) + log(p(x, y)) > log(val[y]):
log(val[y]) = log(val[x]) + log(p(x, y))
Since I am to find the highest possible probability values, I believe I should be using >. The following questions remain -:
What should the initial values in the val array be?
Is there anything else I need to add?
EDIT: I have changed the initial val values to 0. However, log is undefined at 0. I am open to a better alternative. Also, I changed the priority queue's method to EXTRACT-MAX since it is the larger probabilities that need to be extracted. This would ideally be implemented on a binary max-heap.
FURTHER EDIT: I have marked tinybike's answer as accepted, since they have posted most of the necessary details that I require. The algorithm should be as I have posted here.

Existence of a permutation under constraints (Interview Street - Manipulative Numbers)

I am trying to solve this problem: https://www.interviewstreet.com/challenges/dashboard/#problem/4f9a33ec1b8ea
Suppose that A is a list of n numbers ( A1, A2, A3, ... , An) and B ( B1, B2, B3, .. ,Bn ) is a permutation of these numbers. We say B is K-Manipulative if and only if its following value:
M(B) = min( B1 Xor B2, B2 Xor B3, B3 Xor B4, ... , Bn-1 Xor Bn, Bn Xor B1 ) is not less than 2^K.
You are given n number A1 to An, You have to find the biggest K such that there exists a permutation B of these numbers which is K-Manipulative.
Input:
In the first line of the input there is an integer N.
In the second line of input there are N integers A1 to An
N is not more than 100.
Ai is non-negative and will fit in 32-bit integer.
Output:
Print an integer to the output being the answer to the test. If there is no such K print -1 to the output.
Sample Input
3
13 3 10
Sample Output
2
Sample Input
4
1 2 3 4
Sample Output
1
Explanation
First Sample test
Here the list A is {13, 3, 10}. One possible permutation of A is, B = (10, 3, 13).
For B, min( B1 xor B2, B2 xor B3, B3 xor B1 ) = min( 10 xor 3, 3 xor 13, 13 xor 10 ) = min( 9, 14, 7 ) = 7.
So there exist a permutation B of A such that M(B) is not less than 4 i.e 2^2. However there does not exist any permutation B of A such that M(B) is not less than 8 ie 2^3. So the maximum possible value of K is 2.
==================================================================================
Here are the attempts I have made so far.
Attempt 1: Greedy Algorithm
Place the input in an array A[1..n]
Compute the value M(A). This gives the location of the min XOR value (i, (i + 1) % n)
Check whether swapping A[i] or A[(i + 1) % n] with any other element of the array increases the value of M(A). If such an element exists, make the swap.
Repeat steps 2 & 3 until the value M(A) cannot be improved.
This gives a local maxima for sure, but I am not sure whether this gives the global maxima.
Attempt 2: Checking for the existence of a permutation given neighbor constraints
Given input A[1..n], For i = 1..n and j = (i+1)..n compute x_ij = A[i] XOR A[j]
Compute the max(x_ij). Note that 2^p <= max(x_ij) < 2^(p+1) for some p.
Collect all x_ij such that x_ij >= 2^p. Note that this collection can be treated as a graph G with nodes {1, 2, .. n} and nodes i and j have an undirected edge between them if x_ij >= 2^p.
Check whether the graph G has a cycle which visits each node exactly once. If such a cycle exists, k = p. Otherwise, let p = p - 1, goto step 3.
This gives the correct answer, but note that in step 4 we are essentially checking whether a graph has a hamiltonian cycle which is a very hard problem.
Any hints or suggestions?
It is possible to solve this problem without going deep into graph theory.
Key inference
The property suggested by rich, is the key to solving this problem:
Following up on my comment: B1 Xor B2 < 2^K if and only if B1 and B2
agree on all but the K low order bits
Based on the above property, we only need to find the highest k for which there are at most n/2 occurrences of each unique higher order bits for every A[i].
In other words, amongst the group of values A[i] >> k, iff each of those values is repeated at most n/2 times, there exists a k-manipulative permutation with all the XOR values >= (2^k).
Why n/2
Suppose if you do have more than n/2 occurrences of any of the unique higher order bits, it is NOT possible to obtain a permutation B, with all the cyclic XORs being non-zero, i.e. there will be at least one B[i] XOR B[(i+1) % N] with all the higher order bits becoming zero, hence making M(B) < 2^k
Pseudocode
k = -1
for (bit = (MAX_BITS - 1) to 0) {
HashMap count
for (i = 0 to N - 1) {
higherOrderVal = A[i] >> bit
if (higherOrderVal exists in count) {
count[higherOrderVal] += 1
}
else {
count[higherOrderVal] = 1
}
}
isValid = true
for (element in count) {
if (element > N/2) {
isValid = false
break
}
}
if (isValid) {
k = bit
break
}
}
The time complexity for this solution is O(M * N) where M is the constant factor representing the maximum number of bits used to represent the numbers (32-bits, 64-bits, etc.), and N is the size of the input array A.
Following up on my comment: B1 Xor B2 < 2^K if and only if B1 and B2 agree on all but the K low order bits, so G has the very special structure of being complete multipartite, with partition labels consisting of all but the K low order bits. A complete multipartite graph is Hamiltonian if and only if there is no majority partition. Plug this fact into Attempt 2.
Greedy Approach #priority_queue
firstly insert all pairs xor value and consecutive index i and j ->pq.push(tuple(v[i]^v[j],i,j))
now pop the first maxm xor value and set the both index i and j
now again pop that maxm xor value which is comes from the i or j
this operation perform up to 1 to n
then return nth poped xor value

Resources