Skip to content

Instantly share code, notes, and snippets.

@moxious
Created January 22, 2022 14:14
Show Gist options
  • Save moxious/26261447a24eb877844ce7d7907b4a27 to your computer and use it in GitHub Desktop.
Save moxious/26261447a24eb877844ce7d7907b4a27 to your computer and use it in GitHub Desktop.
Node art component cypher
/* Create a snake */
WITH 500 as snakeSize, 3 as snakeLinkage
/* Create the nodes... */
FOREACH (id IN range(0,snakeSize) | CREATE (:SnakeNode {id:id}))
/* Link each node to the previous {snakeLinkage} nodes, creating a lattice */
WITH ['A', 'B', 'C'] as labels, snakeSize, snakeLinkage
MATCH (n:SnakeNode), (n2:SnakeNode)
WHERE n2.id > n.id and n2.id < n.id + snakeLinkage
with n, n2, labels
call apoc.create.setLabels(n, ['SnakeNode', labels[n.id%3]])
yield node with node as n, n2, labels
call apoc.create.setLabels(n2, ['SnakeNode', labels[n2.id%3]])
yield node with node as n2, n
create (n)-[:link]->(n2);
/* Clusters */
WITH [
{ id: 0, name: "Red" },
{ id: 1, name: "Blue" },
{ id: 2, name: "Black" },
{ id: 3, name: "Green" }
] as clusters,
1000 as totalNodeCount
FOREACH (x in range(0, totalNodeCount) | CREATE (m:Member { id: x }))
WITH clusters
UNWIND clusters as cluster
CREATE (c) SET c += cluster
WITH c CALL apoc.create.setLabels(c, ['ClusterCenter', c.name])
YIELD node RETURN count(node);
/* Link to clusters */
MATCH (cc:ClusterCenter)
WITH cc
MATCH (m:Member)
WHERE m.id % 4 = cc.id
MERGE (m)-[:IN]->(cc);
/* In-group links */
WITH 3 as inGroupRatio
MATCH (cc:ClusterCenter)<-[:IN]-(m:Member)
WITH collect(m) as clusterMembers, cc, inGroupRatio
WITH
apoc.coll.randomItems(clusterMembers,
toInteger(inGroupRatio*size(clusterMembers)), false) AS froms,
apoc.coll.randomItems(clusterMembers,
toInteger(inGroupRatio*size(clusterMembers)), false) AS tos
UNWIND apoc.coll.zip(froms, tos) as pair
CALL apoc.create.relationship(pair[0], 'INGROUP', {}, pair[1])
YIELD rel RETURN COUNT(rel);
/* Out-group links */
/* TOO MUCH */
WITH 0.001 as outGroupRatio
MATCH (cc1:ClusterCenter), (cc2:ClusterCenter)
WHERE id(cc1)<id(cc2)
WITH cc1, cc2, outGroupRatio
MATCH (cc1)<-[:IN]-(m:Member),
(cc2)<-[:IN]-(m2:Member)
WITH
collect(m) as cluster1Members,
cc1,
collect(m2) as cluster2Members,
cc2,
outGroupRatio
WITH
apoc.coll.randomItems(cluster1Members,
toInteger(outGroupRatio*size(cluster1Members)), false) AS froms,
apoc.coll.randomItems(cluster2Members,
toInteger(outGroupRatio*size(cluster2Members)), false) AS tos
UNWIND apoc.coll.zip(froms, tos) as pair
CALL apoc.create.relationship(pair[0], 'OUTGROUP', {}, pair[1])
YIELD rel RETURN COUNT(rel);
/* Interstitial links */
WITH 0.002 as outGroupRatio
MATCH (cc1:ClusterCenter), (cc2:ClusterCenter)
WHERE id(cc1)<id(cc2)
WITH cc1, cc2, outGroupRatio
MATCH (cc1)<-[:IN]-(m:Member),
(cc2)<-[:IN]-(m2:Member)
WITH
collect(m) as cluster1Members,
cc1,
collect(m2) as cluster2Members,
cc2,
outGroupRatio
WITH
apoc.coll.randomItems(cluster1Members,
toInteger(outGroupRatio*size(cluster1Members)), false) AS froms,
apoc.coll.randomItems(cluster2Members,
toInteger(outGroupRatio*size(cluster2Members)), false) AS tos
UNWIND apoc.coll.zip(froms, tos) as pair
CREATE (i:Interstitial { from: pair[0].id, to: pair[1].id })
WITH i, pair
CALL apoc.create.relationship(pair[0], 'INTERSTITIAL', {}, i)
YIELD rel
WITH i, pair
CALL apoc.create.relationship(i, 'INTERSTITIAL', {}, pair[1])
YIELD rel
RETURN COUNT(rel);
/* Link snake to cluster (experimental; dependent on snake length, careful) */
MATCH (starters:SnakeNode) WHERE starters.id < 3
WITH starters
MATCH (enders:SnakeNode) WHERE enders.id > 292
WITH starters, enders
MATCH (midpoint1:SnakeNode) WHERE midpoint1.id > 193 and midpoint1.id < 196
WITH starters, enders, midpoint1
MATCH (midpoint2:SnakeNode) WHERE midpoint2.id > 69 and midpoint2.id < 72
MATCH (cc:ClusterCenter { name: "Red" })
MATCH (cc2:ClusterCenter { name: "Black" })
MATCH (cc3:ClusterCenter { name: "Green" })
MATCH (cc4:ClusterCenter { name: "Blue" })
MERGE (starters)-[:CONNECTED]->(cc)
MERGE (enders)-[:CONNECTED]->(cc2)
MERGE (midpoint1)-[:CONNECTED]->(cc3)
MERGE (midpoint2)-[:CONNECTED]->(cc4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment