User Defined Function in neo4j - neo4j

I have the following function that I found in this Reference.
I want to use this function to find the minimum dominating set. How can I execute this in neo4j?
function CYPHERGreedyAlgorithm
MATCH(h)
SET h.whiteness = 1
SET h.blackness = 0
WITH h
OPTIONAL MATCH(j)
WHERE NOT (j){>()
SET j.blackness = 1
SET j.whiteness = 0
WITH j
MATCH (n){>(m)
WHERE n.blackness <> 1
WITH collect(m) as neighbourhood, n
WITH reduce(totalweight = n.whiteness, j in neighbourhood | totalweight + j.whiteness) as weightings, n
WITH n, weightings
ORDER BY weightings desc limit 1
MATCH (n1){>(m1)
WHERE n1.blackness <> 1
WITH collect(m1) as neighbourhood, n1
WITH reduce(totalweight = n1.whiteness, j in neighbourhood | totalweight + j.whiteness) as weightings, n1
WITH n1, weightings
ORDER BY weightings desc limit 1
MATCH(n1){>(m1)
WHERE m1.blackness <> 1
SET n1.blackness = 1
SET n1.whiteness = 0
SET m1.whiteness = 0
WITH n1
MATCH (k)
WHERE k.whiteness = 1
RETURN count(distinct(k)) as countOfRemainingWhiteNodes
end function

Related

Finding the "GAP" in node values ? or next?

Let say I have a nodes with values a multiples of 10. I want to find the first GAP in the values.
Here is how I would do it in numpy :
> np.where(np.diff([11,21,31,51,61,71,91]) > 10)[0][0] + 2
> 4 i.e. 41
How would I do this in Cypher... ?
match (n) where n.val % 10 = 1
with n.val
order by val ....???
I'm using RedisGraph.
PS>
if no GAP it should return the next value i.e. biggest + 10, if possible !
I'm not sure if this is the most performant solution, but you can accomplish this using a combination of collect() and list comprehensions:
MATCH (n) WHERE n.val % 10 = 1 WITH n.val AS val ORDER BY n.val // collect ordered vals
WITH collect(val) AS vals // combine vals into array
WITH vals, [idx IN range(0, size(vals) + 1) WHERE vals[idx + 1] - vals[idx] > 10] AS gaps // find first index with diff > 10
RETURN vals[gaps[0]] + 10 // return missing value
To additionally return the next-biggest value if no gaps are found, change the RETURN clause to use a CASE statement:
RETURN CASE size(gaps) WHEN 0 THEN vals[-1] + 10 ELSE vals[gaps[0]] + 10 END

how are the index applied in upvalue + lua

I'm trying to understand how are index applied in upvalue but unable to get so as led me to ask this question over here.
function newCounter ()
local t = 10
local n = 0
local k = 0
return function ()
l = t
k = n
n = n + 1
return n
end
end
counter = newCounter()
counter()
counter()
print("<==============>")
local i = 1
repeat
name, val = debug.getupvalue(counter, i)
if name then
print ("index", i, name, "=", val)
if (name == "n") then
debug.setupvalue(counter,2,10)
end
i = i + 1
end -- if
until not name
When I run this I get following o/p
index 1 _ENV = table: 0x7f8203c03ea0
index 2 t = 10
index 3 k = 1
index 4 n = 2
Note : I was assuming the o/p to be (based on the order in which they are initialise)
index 1 t = 10
index 2 k = 1
index 3 n = 2
index 4 _ENV = table: 0x7f8203c03ea0
Can any provide me an info as to what is the logical way to find the correct index associate with a given upvalue.
Every chunk in Lua starts with _ENV as its first upvalue.

Is an admisible heuristic always monotone (consistent)?

For the A* search algorithm, provided an heuristic h, supose h is admisible.
That is:
h(n) ≤ h*(n) for every node n, where h* is the real cost from n to goal.
Does this ensure the heuristic is monotone?
That is:
f(n) ≤ g(n') + h(n') for every sucesor n' of n, where f(n)= h(n) + g(n) and g(n) is the accumulated cost.
No.
Assume you have three successor states s1, s2, s3 and a goal state g so that s1 -> s2 -> s3 -> g.
s1 is the starting node.
Consider also the following values for h(s) and h*(s) (i.e. true cost):
h(s1) = 3 , h*(s1) = 6
h(s2) = 4 , h*(s2) = 5
h(s3) = 3 , h*(s3) = 3
h(g) = 0 , h*(g) = 0
Following the only path to the goal we can have that:
g(s1) = 0, g(s2) = 1, g(s3) = 3, g(g) = 6, coinciding with the true cost above.
Although the heuristic function is admissible (h(s) <= h*(s)), f(n) will not be monotonic. For instance f(s1) = h(s1) + g(s1) = 3 while f(s2) = h(s2) + g(s2) = 5 with f(s1) < f(s2). Same holds between f(s2) and f(s3).
Of course this means you have a quite uninformative heuristic.

Runtime of while loop pseudocode

I have a pseudocode which I'm trying to make a detailed analysis, analyze runtime, and asymptotic analysis:
sum = 0
i = 1
while (i ≤ n){
sum = sum + i
i = 2i
}
return sum
My assignment requires that I write the cost/runtime for every line, add these together, and find a Big-Oh notation for the runtime. My analysis looks like this for the moment:
sum = 0 1
long i = 1 1
while (i ≤ n){ log n + 1
sum = sum + i n log n
i = 2i n log n
}
return sum 1
=> 2 n log n + log n + 4 O(n log n)
is this correct ? Also: should I use n^2 on the while loop instead ?
Because of integer arithmetic, the runtime is
O(floor(ln(n))+1) = O(ln(n)).
Let's step through your pseudocode. Consider the case that n = 5.
iteration# i ln(i) n
-------------------------
1 1 0 5
2 2 1 5
3 4 2 5
By inspection we see that
iteration# = ln(i)+1
So in summary:
sum = 0 // O(1)
i = 1 // O(1)
while (i ≤ n) { // O(floor(ln(n))+1)
sum = sum + i // 1 flop + 1 mem op = O(1)
i = 2i // 1 flop + 1 mem op = O(1)
}
return sum // 1 mem op = O(1)

Creating a precision matrix for Gaussian markov random field

I am currently trying to create a precision matrix for a Gaussian markov random field. Lets say I have random variables in a spatial grid of 6x6. Then I will have a precision matrix of 36x36.
Now suppose that I have a neighbor hood of 3x3, then my precision matrix will be
Q= nnbs[1] -1 0 0 0 0 -1.......0
-1 nnbs[2] -1 0 0 0 0 ......0
0 -1 nnbs[3] -1 0 0 0 ......0
...................................................
...................................................
and so on. Can anyone suggest me how can I code this precision matrix. I mean if I change the window size/neighborhood size to 5x5, then I will have a new precision matrix. How can I code this? where nnbs is the number of neighbors of that element
rows=20;
columns=20;
%Random initialization
data=zeros(1000,3);
index=1;
value=-1;
%3x3 neighborhood
%For each element the neighbors are accessible within 1 hop so neighbors=1
neighbors=1;
for i=1:rows
for j=1:columns
for k=1:neighbors
%same row right
if j+k <= columns
data(index,1) = (i-1)*columns+j;
data(index,2) = ((i-1)*columns) + (j+k);
data(index,3) = value;
index=index+1;
end
%same row left
if j-k >= 1;
data(index,1) = (i-1)*columns+j;
data(index,2) = ((i-1)*columns) + (j-k);
data(index,3) = value;
index=index+1;
end
end
%row below -> bottom left right
for k=i+1:i+neighbors
if k <= rows
%bottom
data(index,1) = (i-1)*columns+j;
data(index,2) = (k-1)*columns + j;
data(index,3) = value;
index=index+1;
for l=1:neighbors
%right
if j+l <= columns
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) + (j+1);
data(index,3) = value;
index=index+1;
end
%left
if j-l >= 1;
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns)+(j-1);
data(index,3) = value;
index=index+1;
end
end
end
end
%row above top left right
for k=i-1:i-neighbors
if k >= 1
%top
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) +j;
data(index,3) = value;
index=index+1;
for l=1:neighbors
%right
if j+l <= columns
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) + (j+1);
data(index,3) = value;
index=index+1;
end
%left
if j-k >= 1;
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) + (j-1);
data(index,3) = value;
index=index+1;
end
end
end
end
end
end
%Get the values for the diagonal elements(which is equal to the number of
%neighbors or absolute sum of the nondiagonal elements of the corresponding
%row)
diagonal_values = zeros(rows*columns,3);
for i=1:rows*columns
pointer=find(data(:,1) == i);
diag_value=abs(sum(data(pointer,3)));
diagonal_values(i,1) = i;
diagonal_values(i,2) = i;
diagonal_values(i,3) = diag_value;
end
data(index:index+rows*columns-1,:)=diagonal_values(:,:);
Q = sparse(data(:,1), data(:,2), data(:,3), rows*columns, rows*columns);
I tried something like this but I don't think this is the most efficient of ways. I think there should be a better way.
A bit too late but it may be useful for someone else :
Your precision matrix is a linear combination of kronecker product of symmetric Toeplitz matrix : to each neighbour type corresponds a kronecker product of 2 Toeplitz matrix.
More info about toeplitz Matrix
Example :
you want a precision matrix only with the horizontal link for each pixel
Writing I_n the identity matrix of size n and H_{n,p} the Symmetric Toeplitz matrix of dimension [n n] filled with 0 everywhere excepts on the p-th diagonals
H_{4,2} =
0 1 0 0
1 0 1 0
0 1 0 1
0 0 1 0
In Matlab :
H_nonSym_n_p = toeplitz(zeros(n,1), [[zeros(1,p-1) 1] zeros(1,n-p)]) ;
H_sym_n_p = H_nonSym + H_nonSym' ;
Then, if you have a [n m] image and if you want to code the horizontal neighbour of every pixel, you can express it through the kronecker product (hope the LaTeX-like code will work): Q = - I_n \otimes \H_{n,2}.
And finally, to get your nnbs : something like Q = Q - diag(sum(Q,2))...
Now, if you want other links, for instance 2 horizontal and 2 vertical links : Q = - I_n \otimes \H_{n,2} - I_n \otimes \H_{n,3} - \H_{n,2} \otimes I_{n} - \H_{n,3} \otimes I_{n}.
and again Q = Q - diag(sum(Q,2))
Note that the diagonal neighbours are a bit more difficult to produce but it is still represented by a kronecker product of toeplitz matrix (maybe non-symmetric this time).

Resources