ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Extract Specific Subgraph by Node and Relationship Type

Cypher (Neo4j)

Goal -- WPM

Ready
Exercise Algorithm Area
1/**
2* Extracts a subgraph based on a primary node label and a specific relationship type.
3* Includes all nodes connected by these relationships, regardless of their label.
4*/
5
6// Define the primary node label and the relationship type to filter by
7WITH 'Company' AS primaryNodeLabel, 'EMPLOYS' AS relationshipType
8
9// Match the primary nodes
10MATCH (primaryNode)
11WHERE labels(primaryNode) CONTAINS primaryNodeLabel
12
13// Find relationships of the specified type and the nodes they connect to
14// Using OPTIONAL MATCH to ensure we capture all primary nodes, even if they have no matching relationships.
15OPTIONAL MATCH (primaryNode)-[r:EMPLOYS]-(connectedNode)
16WHERE type(r) = relationshipType
17
18// Collect all unique nodes and relationships that form the subgraph
19// This includes the primary nodes and any connected nodes, plus the specific relationships.
20WITH collect(DISTINCT primaryNode) AS primaryNodes,
21collect(DISTINCT r) AS specificRelationships,
22collect(DISTINCT connectedNode) AS connectedNodes
23
24// Combine all nodes into a single collection for the subgraph
25WITH primaryNodes + specificRelationships AS subgraphElements
26
27// Return the collected elements that constitute the subgraph
28RETURN subgraphElements
29
30// Edge case handling:
31// - If no nodes with the primaryNodeLabel are found, primaryNodes will be empty.
32// - If no relationships of relationshipType are found, specificRelationships will be empty.
33// - If connectedNode is null (due to OPTIONAL MATCH and no relationship), it will be filtered out by collect(DISTINCT ...).
34// - The final RETURN will correctly yield an empty list if no elements are found.
Algorithm description viewbox

Extract Specific Subgraph by Node and Relationship Type

Algorithm description:

This query extracts a specific subgraph by focusing on nodes of a primary label and relationships of a particular type. It includes all nodes that are part of these specified relationships, even if those nodes do not match the primary label. This is useful for understanding the immediate network context around a set of entities, such as mapping all employees and their direct connections within a company graph.

Algorithm explanation:

The query first sets the `primaryNodeLabel` and `relationshipType`. It then matches nodes that have the `primaryNodeLabel`. An `OPTIONAL MATCH` is used to find relationships of the specified `relationshipType` connected to these primary nodes, and importantly, it also captures the `connectedNode` regardless of its label. The `WITH` clause then collects distinct primary nodes, the specific relationships, and the connected nodes. These collections are combined to form the `subgraphElements` which are then returned. The time complexity is roughly O(N + M) where N is the number of nodes matching the primary label and M is the number of relationships of the specified type. Space complexity is O(N' + M') where N' and M' are the sizes of the projected subgraph. Edge cases include no primary nodes found, no matching relationships found, or primary nodes with no connections of the specified type.

Pseudocode:

1. Define the primary node label and the relationship type for the subgraph.
2. Match nodes that have the primary node label.
3. Optionally, match relationships of the specified type connected to these primary nodes, and capture the connected nodes.
4. Collect all unique primary nodes, the specific relationships, and the connected nodes.
5. Combine all collected nodes into a single list.
6. Return the combined list of nodes and relationships representing the subgraph.