cancel
Showing results for
Did you mean:

## User Defined Function

Node Clone

Hello

I have the following function that I found in this Ref

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``````

3 REPLIES 3
Ninja

First, you can easily create functions with apoc.
After that, it is best to move to Java without apoc.

This is a sample code on the APOC doc.

``````CALL apoc.custom.declareProcedure(
'UNWIND range(0, \$power) AS power
RETURN \$input ^ power AS answer'
);``````

Custom, Cypher Based Procedures and Functions
https://neo4j.com/labs/apoc/4.4/cypher-execution/cypher-based-procedures-functions/

User-defined functions
https://neo4j.com/docs/java-reference/current/extending-neo4j/functions/

Node Clone

Thanks for your reply. My question is: Should I program the algorithm in Java? Why I can’t use the Cypher that I mentioned in the question? Please I need help in this to solve the SDN controller placement problem using neo4j and the dominating set algorithm.

Node Clone

I found this link Minimum Dominating Set . Can this be a good reference to add this algorithm to the GDS library?