1
votes

How to write query in Cypher which creates graph-sun? I mean one node in the center, it has edges to each node, however another nodes (outside of the center) are not connected between themself.

Moreover, I would like each node has some random property, like rank. And then, how to write query which find 10 better (given rank) neighbors of center node?

Could anyone give something like that?

1
Hi @CypherFancy! If my answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Thanks! - Bruno Peres

1 Answers

2
votes

You can use a query like this:

create (center:Center),
(:Node{rank : rand()})-[:LINKED_TO]->(center),
(:Node{rank : rand()})-[:LINKED_TO]->(center),
(:Node{rank : rand()})-[:LINKED_TO]->(center),
(:Node{rank : rand()})-[:LINKED_TO]->(center),
(:Node{rank : rand()})-[:LINKED_TO]->(center),
(:Node{rank : rand()})-[:LINKED_TO]->(center),
(:Node{rank : rand()})-[:LINKED_TO]->(center)

to produce this graph:

Graph

Note the use of rand() function to assign a random value to rank property in each node.

And to to get the top nodes based on rank property you can use (top 5, in this case):

match (:Center)<-[:LINKED_TO]-(n:Node)
with n order by n.rank desc limit 5
return n

EDIT

Here a more elegant (but complex) solution to create the "graph-sun" using the APOC Procedure apoc.periodic.iterate. I'm using a parameter to determine the number of neighbors. The docs about apoc.periodic.iterate says:

With apoc.periodic.iterate you provide 2 statements, the first outer statement is providing a stream of values to be processed. The second, inner statement processes one element at a time or with iterateList:true the whole batch at a time.

So in the first statement I'm returning number_of_n items (using range()) function. Then the second statement is executed number_of_n times, creating each neighbor.

CALL apoc.periodic.iterate(
"with range(0, {number_of_n} - 1) as items
unwind items as item
return item",
"merge (center:Center)
create (:Node{rank : rand()})-[:LINKED_TO]->(center)",
{params : {number_of_n:20}})

This query outputs a graph like:

Graph generated using apoc.periodic.iterate