cancel
Showing results for 
Search instead for 
Did you mean: 

All permutations where node type is distinct in each

stummu
Node

 

What I'm trying to do is generate all the permutations of a collection of nodes but where each node type only appears once per permutation.

For example if I have a graph of u:User, l:Location, d:Device nodes, lets say 3 nodes of each type.

What I want to be able to generate is a collection that might look something like this

User ID: 1, Location ID: 1, Device ID: 1
User ID: 1, Location ID: 1, Device ID: 2
User ID: 1, Location ID: 1, Device ID: 3
User ID: 1, Location ID: 2, Device ID: 1
User ID: 1, Location ID: 2, Device ID: 2
User ID: 1, Location ID: 2, Device ID: 3

And so on, until I have all combinations.

What I don't want to happen is for a valid combination to include any of the node types more than once, so a combination can only have one user, one location and one device.

At the moment I'm using apoc.coll.combinations, but I don't seem to be able to work out a way to stop it from making the node type distinct.

If I were to run 

MATCH (l:Location)-[]-(p:Policy)-[]-(ur:UserRisk)
WITH COLLECT({l:l,ur:ur}) as coll
WITH apoc.coll.combinations(coll,1,size(coll)) as combColl
RETURN combColl
I'd run out of memory because valid combos would be all the locations and all the user risks.
 
Any help would be massively welcome
 
1 REPLY 1

glilienfield
Ninja
Ninja

Does this work?  sequential 'unwind' operations product a Cartesian product of the data. 

with [1,2] as userIds, [10,11] as locationIds, [100,110] as deviceIds
unwind userIds as userId
unwind locationIds as locationId
unwind deviceIds as deviceId
return userId, locationId, deviceId

Screen Shot 2022-10-01 at 11.33.16 AM.png 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.