Debuging all the code computing and sorting equivalence classes - debugging

I have this function compute an equivalence class
let eq_class m i =
let column = m.(i)
and set = ref [] in
Array.iteri begin fun j l ->
if j = i || column.(j) && m.(j).(i) then
set := j :: !set else ignore l
end column;
!set;;
and this function to collect all the classes are equivalence
let eq_classes m =
let classes = ref [] in
Array.iteri begin fun e _ ->
if not (List.exists (List.mem e) !classes) then
classes := eq_class m e :: !classes
end m;
!classes;;
I have this function to compare two equivalence classes:
let cmp_classes m c c' = if c = c' then 0 else
match c, c' with
| i :: _, j :: _ -> if m.(i).(j) then 1 else -1
| _ -> assert false
After I used this function to sort it by using List.sort
let sort_eq_classes m = List.sort (cmp_classes m);;
My matrix is an boolean matrix, and I computed it with transitive closure.
let transClosure m =
let n = Array.length m in
for k = 0 to n - 1 do
let mk = m.(k) in
for i = 0 to n - 1 do
let mi = m.(i) in
for j = 0 to n - 1 do
mi.(j) <- max mi.(j) (min mi.(k) mk.(j))
done;
done;
done;
m;;
let tc = transClosure matrix
let eq = eq_classes tc
let sort_eq = sort_eq_classes tc eq
I tested with many counter example to test all these functions, for example with the graph (matrix)
EDIT
matrix:
a <-> b c <-> d
|
v
e
matrix_2:
a <-> b -> e -> f
| |
v v
h <------- g
| |
v v
u k
I input the boolean matrix:
let matrix =
[|
[|false; true; false; false; false|];
[|true; false; false; false; false|];
[|false; false; false; true; false|];
[|false; false; true; false; false|];
[|false; false; false; false; false|];
|];;
let matrix_2 =
[|
[| false; false; false; false; false; false; false; false |];
[| false; false; false; false; false; false; false; false |];
[| false; false; false; true; false; false; true; false |];
[| false; false; true; false; true; false; false; false |];
[| true; false; false; false; false; true; false; false |];
[| false; true; false; false; false; false; true; false |];
[| false; false; false; false; false; false; false; true |];
[| false; false; false; false; false; false; false; false |];
|]
output of matrix 1:
equivalence classes: e d c b a
sort equivalence classes: e d c b a
output of matrix 2:
equivalence classes: u h g e b a k f
sort equivalence classes : u h k g f e b a
And the result is correct order like I expected. But when I test it with my data, which is an xsds data, more complicated depended relations. It output for me a wrong order. I had test with function transform to boolean matrix from xsds, and tested transitive closure, it is correct. So I think may be their is some bugs in my functions, (eq_class) or (cmp_classes).
Could you please help me to see what wrong in these code?

The problem is in cmp_classes function.
From your source code:
(* We check that if two elements are in the same equivalence class they are
equal (0); if they have a path from i to j then i < j (-1)
otherwise i > j (1). We assumes that: each equivalence class only
appears once and each equivalence class contains at least one
element. *)
let cmp_classes m c c' = if c = c' then 0 else
match c, c' with
| i :: _, j :: _ -> if m.(i).(j) then 1 else -1
| _ -> assert false
Obviously, your code doesn't fulfill your requirement. Several errors could be mentioned:
If m.(i).(j) = true, you don't compare between i and j at all.
If m.(i).(j) = false, you suppose to compare other combinations than i and j, but here you wrongly returns -1.
You only compare using the heads of c and c' and ignore other elements while you suppose to use any pair of elements in two lists before reaching a conclusion.
In your small examples, you got correct results because equivalence classes often have one element. Since they are collected in the order that there is no path from former ones to later ones, your returning of -1 is fortunately correct. It's no longer the case with arbitrary input from xsd trees.
It's not difficult to fix the function if you can define it clearly. Now I still don't know the order of two equivalence classes without any path connecting them together ({a,b} and {c, d} classes in matrix).
In order that you can test your fix more easily, this is a small example which will produce a wrong order:
a <-> b c <-> d
^ ^ ^ ^
| | | |
e e e e

Related

Tail recursive solution in Scala for Linked-List chaining

I wanted to write a tail-recursive solution for the following problem on Leetcode -
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contains a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
*Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)*
*Output: 7 -> 0 -> 8*
*Explanation: 342 + 465 = 807.*
Link to the problem on Leetcode
I was not able to figure out a way to call the recursive function in the last line.
What I am trying to achieve here is the recursive calling of the add function that adds the heads of the two lists with a carry and returns a node. The returned node is chained with the node in the calling stack.
I am pretty new to scala, I am guessing I may have missed some useful constructs.
/**
* Definition for singly-linked list.
* class ListNode(_x: Int = 0, _next: ListNode = null) {
* var next: ListNode = _next
* var x: Int = _x
* }
*/
import scala.annotation.tailrec
object Solution {
def addTwoNumbers(l1: ListNode, l2: ListNode): ListNode = {
add(l1, l2, 0)
}
//#tailrec
def add(l1: ListNode, l2: ListNode, carry: Int): ListNode = {
var sum = 0;
sum = (if(l1!=null) l1.x else 0) + (if(l2!=null) l2.x else 0) + carry;
if(l1 != null || l2 != null || sum > 0)
ListNode(sum%10,add(if(l1!=null) l1.next else null, if(l2!=null) l2.next else null,sum/10))
else null;
}
}
You have a couple of problems, which can mostly be reduced as being not idiomatic.
Things like var and null are not common in Scala and usually, you would use a tail-recursive algorithm to avoid that kind of things.
Finally, remember that a tail-recursive algorithm requires that the last expression is either a plain value or a recursive call. For doing that, you usually keep track of the remaining job as well as an accumulator.
Here is a possible solution:
type Digit = Int // Refined [0..9]
type Number = List[Digit] // Refined NonEmpty.
def sum(n1: Number, n2: Number): Number = {
def aux(d1: Digit, d2: Digit, carry: Digit): (Digit, Digit) = {
val tmp = d1 + d2 + carry
val d = tmp % 10
val c = tmp / 10
d -> c
}
#annotation.tailrec
def loop(r1: Number, r2: Number, acc: Number, carry: Digit): Number =
(r1, r2) match {
case (d1 :: tail1, d2 :: tail2) =>
val (d, c) = aux(d1, d2, carry)
loop(r1 = tail1, r2 = tail2, d :: acc, carry = c)
case (Nil, d2 :: tail2) =>
val (d, c) = aux(d1 = 0, d2, carry)
loop(r1 = Nil, r2 = tail2, d :: acc, carry = c)
case (d1 :: tail1, Nil) =>
val (d, c) = aux(d1, d2 = 0, carry)
loop(r1 = tail1, r2 = Nil, d :: acc, carry = c)
case (Nil, Nil) =>
acc
}
loop(r1 = n1, r2 = n2, acc = List.empty, carry = 0).reverse
}
Now, this kind of recursions tends to be very verbose.
Usually, the stdlib provide ways to make this same algorithm more concise:
// This is a solution that do not require the numbers to be already reversed and the output is also in the correct order.
def sum(n1: Number, n2: Number): Number = {
val (result, carry) = n1.reverseIterator.zipAll(n2.reverseIterator, 0, 0).foldLeft(List.empty[Digit] -> 0) {
case ((acc, carry), (d1, d2)) =>
val tmp = d1 + d2 + carry
val d = tmp % 10
val c = tmp / 10
(d :: acc) -> c
}
if (carry > 0) carry :: result else result
}
Scala is less popular on LeetCode, but this Solution (which is not the best) would get accepted by LeetCode's online judge:
import scala.collection.mutable._
object Solution {
def addTwoNumbers(listA: ListNode, listB: ListNode): ListNode = {
var tempBufferA: ListBuffer[Int] = ListBuffer.empty
var tempBufferB: ListBuffer[Int] = ListBuffer.empty
tempBufferA.clear()
tempBufferB.clear()
def listTraversalA(listA: ListNode): ListBuffer[Int] = {
if (listA == null) {
return tempBufferA
} else {
tempBufferA += listA.x
listTraversalA(listA.next)
}
}
def listTraversalB(listB: ListNode): ListBuffer[Int] = {
if (listB == null) {
return tempBufferB
} else {
tempBufferB += listB.x
listTraversalB(listB.next)
}
}
val resultA: ListBuffer[Int] = listTraversalA(listA)
val resultB: ListBuffer[Int] = listTraversalB(listB)
val resultSum: BigInt = BigInt(resultA.reverse.mkString) + BigInt(resultB.reverse.mkString)
var listNodeResult: ListBuffer[ListNode] = ListBuffer.empty
val resultList = resultSum.toString.toList
var lastListNode: ListNode = null
for (i <-0 until resultList.size) {
if (i == 0) {
lastListNode = new ListNode(resultList(i).toString.toInt)
listNodeResult += lastListNode
} else {
lastListNode = new ListNode(resultList(i).toString.toInt, lastListNode)
listNodeResult += lastListNode
}
}
return listNodeResult.reverse(0)
}
}
References
For additional details, you can see the Discussion Board. There are plenty of accepted solutions, explanations, efficient algorithms with a variety of languages, and time/space complexity analysis in there.

OCaml shared variable

I wish to create two functions : the first one generates a new integer and the second one reset the generator
let a = ref 0 in
let f () = a := !a + 1 and
g () = a := 0; ();;
The REPL shows "Error: Syntax error" on the last two semicolons.
The second let also needs to be followed by an in (see the manual), e.g.,
let inc, reset =
let a = ref 0 in
let f () = a := !a + 1; !a
and g () = a := 0
in (f, g);;
But maybe you want something more like this:
let new_counter () =
let a = ref 0 in
let f () = a := !a + 1; !a
and g () = a := 0
in (f, g);;

Fastest solution for all possible combinations, taking k elements out of n possible with k>2 and n large

I am using MATLAB to find all of the possible combinations of k elements out of n possible elements. I stumbled across this question, but unfortunately it does not solve my problem. Of course, neither does nchoosek as my n is around 100.
Truth is, I don't need all of the possible combinations at the same time. I will explain what I need, as there might be an easier way to achieve the desired result. I have a matrix M of 100 rows and 25 columns.
Think of a submatrix of M as a matrix formed by ALL columns of M and only a subset of the rows. I have a function f that can be applied to any matrix which gives a result of either -1 or 1. For example, you can think of the function as sign(det(A)) where A is any matrix (the exact function is irrelevant for this part of the question).
I want to know what is the biggest number of rows of M for which the submatrix A formed by these rows is such that f(A) = 1. Notice that if f(M) = 1, I am done. However, if this is not the case then I need to start combining rows, starting of all combinations with 99 rows, then taking the ones with 98 rows, and so on.
Up to this point, my implementation had to do with nchoosek which worked when M had only a few rows. However, now that I am working with a relatively bigger dataset, things get stuck. Do any of you guys think of a way to implement this without having to use the above function? Any help would be gladly appreciated.
Here is my minimal working example, it works for small obs_tot but fails when I try to use bigger numbers:
value = -1; obs_tot = 100; n_rows = 25;
mat = randi(obs_tot,n_rows);
while value == -1
posibles = nchoosek(1:obs_tot,i);
[num_tries,num_obs] = size(possibles);
num_try = 1;
while value == 0 && num_try <= num_tries
check = mat(possibles(num_try,:),:);
value = sign(det(check));
num_try = num_try + 1;
end
i = i - 1;
end
obs_used = possibles(num_try-1,:)';
Preamble
As yourself noticed in your question, it would be nice not to have nchoosek to return all possible combinations at the same time but rather to enumerate them one by one in order not to explode memory when n becomes large. So something like:
enumerator = CombinationEnumerator(k, n);
while(enumerator.MoveNext())
currentCombination = enumerator.Current;
...
end
Here is an implementation of such enumerator as a Matlab class. It is based on classic IEnumerator<T> interface in C# / .NET and mimics the subfunction combs in nchoosek (the unrolled way):
%
% PURPOSE:
%
% Enumerates all combinations of length 'k' in a set of length 'n'.
%
% USAGE:
%
% enumerator = CombinaisonEnumerator(k, n);
% while(enumerator.MoveNext())
% currentCombination = enumerator.Current;
% ...
% end
%
%% ---
classdef CombinaisonEnumerator < handle
properties (Dependent) % NB: Matlab R2013b bug => Dependent must be declared before their get/set !
Current; % Gets the current element.
end
methods
function [enumerator] = CombinaisonEnumerator(k, n)
% Creates a new combinations enumerator.
if (~isscalar(n) || (n < 1) || (~isreal(n)) || (n ~= round(n))), error('`n` must be a scalar positive integer.'); end
if (~isscalar(k) || (k < 0) || (~isreal(k)) || (k ~= round(k))), error('`k` must be a scalar positive or null integer.'); end
if (k > n), error('`k` must be less or equal than `n`'); end
enumerator.k = k;
enumerator.n = n;
enumerator.v = 1:n;
enumerator.Reset();
end
function [b] = MoveNext(enumerator)
% Advances the enumerator to the next element of the collection.
if (~enumerator.isOkNext),
b = false; return;
end
if (enumerator.isInVoid)
if (enumerator.k == enumerator.n),
enumerator.isInVoid = false;
enumerator.current = enumerator.v;
elseif (enumerator.k == 1)
enumerator.isInVoid = false;
enumerator.index = 1;
enumerator.current = enumerator.v(enumerator.index);
else
enumerator.isInVoid = false;
enumerator.index = 1;
enumerator.recursion = CombinaisonEnumerator(enumerator.k - 1, enumerator.n - enumerator.index);
enumerator.recursion.v = enumerator.v((enumerator.index + 1):end); % adapt v (todo: should use private constructor)
enumerator.recursion.MoveNext();
enumerator.current = [enumerator.v(enumerator.index) enumerator.recursion.Current];
end
else
if (enumerator.k == enumerator.n),
enumerator.isInVoid = true;
enumerator.isOkNext = false;
elseif (enumerator.k == 1)
enumerator.index = enumerator.index + 1;
if (enumerator.index <= enumerator.n)
enumerator.current = enumerator.v(enumerator.index);
else
enumerator.isInVoid = true;
enumerator.isOkNext = false;
end
else
if (enumerator.recursion.MoveNext())
enumerator.current = [enumerator.v(enumerator.index) enumerator.recursion.Current];
else
enumerator.index = enumerator.index + 1;
if (enumerator.index <= (enumerator.n - enumerator.k + 1))
enumerator.recursion = CombinaisonEnumerator(enumerator.k - 1, enumerator.n - enumerator.index);
enumerator.recursion.v = enumerator.v((enumerator.index + 1):end); % adapt v (todo: should use private constructor)
enumerator.recursion.MoveNext();
enumerator.current = [enumerator.v(enumerator.index) enumerator.recursion.Current];
else
enumerator.isInVoid = true;
enumerator.isOkNext = false;
end
end
end
end
b = enumerator.isOkNext;
end
function [] = Reset(enumerator)
% Sets the enumerator to its initial position, which is before the first element.
enumerator.isInVoid = true;
enumerator.isOkNext = (enumerator.k > 0);
end
function [c] = get.Current(enumerator)
if (enumerator.isInVoid), error('Enumerator is positioned (before/after) the (first/last) element.'); end
c = enumerator.current;
end
end
properties (GetAccess=private, SetAccess=private)
k = [];
n = [];
v = [];
index = [];
recursion = [];
current = [];
isOkNext = false;
isInVoid = true;
end
end
We can test implementation is ok from command window like this:
>> e = CombinaisonEnumerator(3, 6);
>> while(e.MoveNext()), fprintf(1, '%s\n', num2str(e.Current)); end
Which returns as expected the following n!/(k!*(n-k)!) combinations:
1 2 3
1 2 4
1 2 5
1 2 6
1 3 4
1 3 5
1 3 6
1 4 5
1 4 6
1 5 6
2 3 4
2 3 5
2 3 6
2 4 5
2 4 6
2 5 6
3 4 5
3 4 6
3 5 6
4 5 6
Implementation of this enumerator may be further optimized for speed, or by enumerating combinations in an order more appropriate for your case (e.g., test some combinations first rather than others) ... Well, at least it works! :)
Problem solving
Now solving your problem is really easy:
n = 100;
m = 25;
matrix = rand(n, m);
k = n;
cont = true;
while(cont && (k >= 1))
e = CombinationEnumerator(k, n);
while(cont && e.MoveNext());
cont = f(matrix(e.Current(:), :)) ~= 1;
end
if (cont), k = k - 1; end
end

An operator riddle

While converting a project from Python to C#, I found some interesting differences in the syntax families. Yet I got stuck, still unable to understand and comprehend the dissimilar behavior of comparison operator in C#.
During the course of curing this curiosity, I considered few languages of C-syntax family; C, C++, C#, Java, Javascript.. and verified the behavior. Here is how it transpired:
Let a=2, b=3, c=4, d=5;
Now, consider the following expressions:
a < a < a // returns true
c < b < a // returns true
c > b > a // returns false
a < c > b // returns false
If it was due to the right-associativity, then the following code in JavaScript shouldn't act like:
console.info(a < false); // returns false
console.info(a < a); // returns false
console.info(a < a < a); // returns true, as opposed to returning false
Here is the C/C++ version
int main(){
int a=2, b=3, c=4, d=5;
printf("%s\n","false\0true"+6*(a < a < a)); // returns true
printf("%s\n","false\0true"+6*(c < b < a)); // returns true
printf("%s\n","false\0true"+6*(c > b > a)); // returns false
printf("%s\n","false\0true"+6*(a < c > b)); // returns false
return 0;
}
Except in Python, where
a < a < a // returns false
c < b < a // returns false
c > b > a // returns true
a < c > b // returns true
Can anyone explain why C-family of languages and Python are computing the expressions differently?
Because Python uses a slightly interpretation of your input:
Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.
This means your lines will be interpreted as
a < a < a = a < a and a < a // returns false
c < b < a = c < b and b < a // returns false
c > b > a = c > b and b > a // returns true
a < c > b = a < c and c > b // returns true
In C-style languages, an comparison expression will evaluate to either false (integer value 0) or true (integer value 1). So in C it will behave like
a < a < a = (a < a) < a = 0 < a // returns true
c < b < a = (c < b) < a = 0 < a // returns true
c > b > a = (c > b) > a = 1 > a // returns false
a < c > b = (a < c) > b = 0 > b // returns false
Note that almost all languages define operators with a boolean return value, but since boolean values can be implicit converted to zero or one the proposition above is still valid:
// C++ example
struct myComparableObject{
int data;
bool operator<(const myComparableObject& o){
return data < o.data;
}
};
myComparableObject a, b;
a.data = 2;
b.data = 3;
int c = 5;
a < b; // true
a < c; // error, a cannot be converted to int / unknown operator
a.data < c; // true
a < b < c; // true, as this is equal to
// (a < b) < c = false < c = 0 < c
For example JavaScript will actually use ToNumber in order to compare two non-string objects, see [ECMAScript p78, 11.8.5 The Abstract Relational Comparison Algorithm], where ToNumber(false) is zero and ToNumber(true) === 1.
The comparison x < y, where x and y are values, produces true, false, or undefined[...]
Let px be the result of calling ToPrimitive(x, hint Number).
Let py be the result of calling ToPrimitive(y, hint Number).
If it is not the case that both Type(px) is String and Type(py) is String, then
a. Let nx be the result of calling ToNumber(px). Because px and py are primitive values evaluation
order is not important.
b. Let ny be the result of calling ToNumber(py).
c. If nx is NaN, return undefined.
d. If ny is NaN, return undefined.
e. If nx and ny are the same Number value, return false.
f. If nx is +0 and ny is -0, return false.
g. If nx is -0 and ny is +0, return false.
h. If nx is +infty, return false.
i. If ny is +infty, return true.
j. If ny is -infty, return false.
k. If nx is -infty, return true.
l. If the mathematical value of nx is less than the mathematical value of ny —note that these
mathematical values are both finite and not both zero— return true. Otherwise, return false.
It's because a < a < a is evaluated like ((a < a) < a) which then becomes 0 < a which is true (when a >= 0)
If you run the following, the first expression changes to false.
int main(){
int a=0, b=3, c=4, d=5;
printf("%s\n","false\0true"+6*(a < a < a)); // returns false
printf("%s\n","false\0true"+6*(c < b < a)); // returns true
printf("%s\n","false\0true"+6*(c > b > a)); // returns false
printf("%s\n","false\0true"+6*(a < c > b)); // returns false
return 0;
}
Whereas in Python, a < a < a becomes a < a and a < a. So it's not comparing the result of a < a. If you add parentheses to your statements, you'll get the C-like behavior again.
(a < a) < a ## returns true
(c < b) < a ## returns true
(c > b) > a ## returns false
(a < c) > b ## returns false

How to add ports to nodes in an Agraph_t?

I'm trying to add ports to nodes, but apparently I'm missing something.
aginit();
Agraph_t *g = agopen("g", AGFLAG_DIRECTED);
Agsym_t *symRankdir = agraphattr(g, "rankdir", "LR");
Agsym_t *symShape = agnodeattr(g, "shape", "Mrecord");
Agsym_t *symLabel = agnodeattr(g, "label", "");
Agnode_t *n1 = agnode(g, "n1");
n1->attr[1] = "n1|<p1>p1|<p2>p2";
Agnode_t *n2 = agnode(g, "n2");
n2->attr[1] = "n2|<p1>p1|<p2>p2";
Agedge_t *e = agedge(g, n1, n2);
e->u.tail_port.defined = true;
e->u.tail_port.name = "p1";
e->u.head_port.defined = true;
e->u.head_port.name = "p2";
FILE *fp = fopen(argv[1], "w");
agwrite(g, fp);
Output:
digraph g {
graph [rankdir=LR];
node [shape=Mrecord];
n1 [label="n1|<p1>p1|<p2>p2"];
n2 [label="n2|<p1>p1|<p2>p2"];
n1 -> n2;
}
The edge in the output should be n1:p1 -> n2:p2. What needs to be set in the code to make that happen?
Replace this --
e->u.tail_port.defined = true;
e->u.tail_port.name = "p1";
e->u.head_port.defined = true;
e->u.head_port.name = "p2";
-- with this --
#define TAILX 1
#define HEADX 2
agxset(e, TAILX, "p1");
agxset(e, HEADX, "p2");
(I figured it out from looking at the Graphviz source code -- lib/graph/parser.y and lib/graph/libgraph.h.)

Resources