6.12. Pretty graphs

This section is showing how to create some of the named pretty graphs on Wikipedia.

Star Graph

The graph is created by first creating a center node, and then once per element in the range, creates a leaf node and connects it to the center.

Query 

CREATE (center)
FOREACH (x IN range(1,6)| CREATE (leaf),(center)-[:X]->(leaf))
RETURN id(center) AS id

The query returns the id of the center node.

Result

id

1 row Nodes created: 7 Relationships created: 6

0

Figure 6.11. Graph

Try this query live  none CREATE (center) FOREACH (x IN range(1,6)| CREATE (leaf),(center)-[:X]->(leaf)) RETURN id(center) AS id

Wheel graph

This graph is created in a number of steps:

  • Create a center node.
  • Once per element in the range, create a leaf and connect it to the center.
  • Connect neighboring leafs.
  • Find the minimum and maximum leaf and connect these.
  • Return the id of the center node.

Query 

CREATE (center)
FOREACH (x IN range(1,6)| CREATE (leaf { count:x }),(center)-[:X]->(leaf))
WITH center
MATCH (large_leaf)<--(center)-->(small_leaf)
WHERE large_leaf.count = small_leaf.count + 1
CREATE (small_leaf)-[:X]->(large_leaf)
WITH center, min(small_leaf.count) AS min, max(large_leaf.count) AS max
MATCH (first_leaf)<--(center)-->(last_leaf)
WHERE first_leaf.count = min AND last_leaf.count = max
CREATE (last_leaf)-[:X]->(first_leaf)
RETURN id(center) AS id

The query returns the id of the center node.

Result

id

1 row Nodes created: 7 Relationships created: 12 Properties set: 6

0

Figure 6.12. Graph

Try this query live  none CREATE (center) foreach( x in range(1,6) | CREATE (leaf {count:x}), (center)-[:X]->(leaf) ) WITH center MATCH (large_leaf)<--(center)-->(small_leaf) WHERE large_leaf.count = small_leaf.count + 1 CREATE (small_leaf)-[:X]->(large_leaf) WITH center, min(small_leaf.count) as min, max(large_leaf.count) as max MATCH (first_leaf)<--(center)-->(last_leaf) WHERE first_leaf.count = min AND last_leaf.count = max CREATE (last_leaf)-[:X]->(first_leaf) RETURN id(center) as id

Complete graph

To create this graph, we first create 6 nodes and label them with the Leaf label. We then match all the unique pairs of nodes, and create a relationship between them.

Query 

FOREACH (x IN range(1,6)| CREATE (leaf:Leaf { count : x }))
WITH *
MATCH (leaf1:Leaf),(leaf2:Leaf)
WHERE leaf1.count < leaf2.count
CREATE (leaf1)-[:X]->(leaf2)

Nothing is returned by this query

Result

0 rows Nodes created: 6 Relationships created: 15 Properties set: 6 Labels added: 6

(empty result)

Figure 6.13. Graph

Try this query live  none FOREACH (x IN range(1,6)| CREATE (leaf:Leaf { count : x })) WITH * MATCH (leaf1:Leaf),(leaf2:Leaf) WHERE leaf1.count < leaf2.count CREATE (leaf1)-[:X]->(leaf2)

Friendship graph

This query first creates a center node, and then once per element in the range, creates a cycle graph and connects it to the center.

Query 

CREATE (center)
FOREACH (x IN range(1,3)| CREATE (leaf1),(leaf2),(center)-[:X]->(leaf1),(center)-[:X]->(leaf2),(leaf1)-[:X]->(leaf2))
RETURN ID(center) AS id

Result

id

1 row Nodes created: 7 Relationships created: 9

0

Figure 6.14. Graph

Try this query live  none CREATE (center) FOREACH (x IN range(1,3)| CREATE (leaf1),(leaf2),(center)-[:X]->(leaf1),(center)-[:X]->(leaf2), (leaf1)-[:X]->(leaf2)) RETURN ID(center) AS id