5.14. Pretty graphs

5.14.1. Star graph
5.14.2. Wheel graph
5.14.3. Complete graph
5.14.4. Friendship graph

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

5.14.1. 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

7


Try this query live. (0) create (center) foreach( x in range(1,6) | create (leaf), (center)-[:X]->(leaf) ) return id(center) as id;

Figure 5.11. Graph


5.14.2. 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

7


Try this query live. (0) 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

Figure 5.12. Graph


5.14.3. Complete graph

For this graph, a root node is created, and used to hang a number of nodes from. Then, two nodes are selected, hanging from the center, with the requirement that the id of the first is less than the id of the next. This is to prevent double relationships and self relationships. Using said match, relationships between all these nodes are created. Lastly, the center node and all relationships connected to it are removed.

Query. 

CREATE (center)
FOREACH (x IN range(1,6)| CREATE (leaf { count : x }),(center)-[:X]->(leaf))
WITH center
MATCH (leaf1)<--(center)-->(leaf2)
WHERE id(leaf1)<id(leaf2)
CREATE (leaf1)-[:X]->(leaf2)
WITH center
MATCH (center)-[r]->()
DELETE center,r;

Nothing is returned by this query.

Result

Nodes created: 7
Relationships created: 21
Properties set: 6
Nodes deleted: 1
Relationships deleted: 6

(empty result)


Try this query live. (0) create (center) foreach( x in range(1,6) | create (leaf {count : x}), (center)-[:X]->(leaf) ) WITH center MATCH (leaf1)<--(center)-->(leaf2) WHERE id(leaf1)<id(leaf2) CREATE (leaf1)-[:X]->(leaf2) WITH center MATCH (center)-[r]->() DELETE center,r;

Figure 5.13. Graph


5.14.4. 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

The id of the center node is returned by the query.

Result

id
1 row
Nodes created: 7
Relationships created: 9

7


Try this query live. (0) 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

Figure 5.14. Graph