# nodes

The `nodes` module provides a comprehensive toolkit for managing multiple graph nodes, enabling linking, updating, type deduction and more.

| Trait               | Value               |
| ------------------- | ------------------- |
| **Module type**     | util                |
| **Implementation**  | C++                 |
| **Graph direction** | directed/undirected |
| **Edge weights**    | weighted/unweighted |
| **Parallelism**     | sequential          |

## Procedures

> **Info**
>
> You can execute this algorithm on [graph projections, subgraphs or portions of the graph](https://memgraph.com/docs/advanced-algorithms/run-algorithms#run-procedures-on-subgraph).

### `relationship_types()`

Returns a list of distinct relationship types of the given node(s) contained
within the given list of types. If the list of types is empty, the procedure
returns all distinct relationship types. Relationship types can also be
directed:
- &lt;type - incoming relationship.
- type> - outgoing relationship.
- type - either way.

#### Input:

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](https://memgraph.com/docs/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run. 
If subgraph is not specified, the algorithm is computed on the entire graph by default.

- `node: int|node|List[int|node]` ➡ The input nodes given as nodes themselves or their IDs.
- `types: List[string] (default = [])` ➡ A list of relationship types to filter by.

#### Output:

- `relationship_types: List[Map]` ➡ Each list element is a map with two keys:
  `node` and `types`. `node` representing the given node and `types` a list of
  distinct relationship types contained within the given list of types for the
  corresponding node.

#### Usage:

The database contains the following graph objects:

```cypher
CREATE (ivan: Intern {name: 'Ivan'})
CREATE (idora: Intern {name: 'Idora'})
CREATE (matija: Intern {name: 'Matija'})
MERGE (ivan)-[:KNOWS]->(idora)
MERGE (matija)-[:HEARS]->(idora)
MERGE (matija)-[:SEES]->(ivan);
```

To get the relationship types for multiple nodes, run the following query:

```cypher
MATCH (n:Intern) 
WITH collect(n) as interns 
CALL nodes.relationship_types(interns, ["<KNOWS", "SEES>", "HEARS"])
YIELD relationship_types 
RETURN relationship_types;
```

Result:

```plaintext
+---------------------------------------+
|   relationship_types                  |
| [                                     |
|     {                                 |
|       "node": {                       |
|          "labels": [                  |
|             "Intern"                  |
|          ],                           |
|          "properties": {              |
|             "name": "Ivan"            |
|          },                           |
|          "type": "node"               |
|       },                              |
|       "types": []                     |
|     },                                |
|     {                                 |
|        "node": {                      |
|           "labels": [                 |
|              "Intern"                 |
|          ],                           |
|           "properties": {             |
|              "name": "Idora"          |
|           },                          |
|           "type": "node"              |
|        },                             |
|       "types": [                      |
|          "HEARS",                     |
|          "KNOWS"                      |
|       ]                               |
|    },                                 |
|    {                                  |
|        "node": {                      |
|           "labels": [                 |
|             "Intern"                  |
|          ],                           |
|          "properties": {              |
|             "name": "Matija"          |
|          },                           |
|          "type": "node"               |
|        },                             |
|        "types": [                     |
|          "SEES",                      |
|          "HEARS"                      |
|       ]                               |
|    }                                  |
| ]                                     |
+---------------------------------------+
```

### `delete()`

Deletes the given node(s) from the graph. Equivalent to running a `DETACH
DELETE` query.

#### Input:

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](https://memgraph.com/docs/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run. 
If subgraph is not specified, the algorithm is computed on the entire graph by default.

- `nodes: int|node|List[int|node]` - Nodes to be deleted given as nodes themselves or their IDs.

#### Usage:

The database contains the following graph objects:

```cypher
CREATE (ivan: Intern {name: 'Ivan'})
CREATE (idora: Intern {name: 'Idora'})
CREATE (matija: Intern {name: 'Matija'})
MERGE (ivan)-[:KNOWS]->(idora)
MERGE (matija)-[:HEARS]->(idora)
MERGE (matija)-[:SEES]->(ivan);
```

The following query will delete all the created nodes and relationships:

```cypher
MATCH (n:Intern) 
WITH collect(n) as interns 
CALL nodes.delete(interns);
```

### `relationships_exist()`

Checks if relationships in the input list exist at the given nodes. Results are
returned as a map, which contains two smaller maps. The first map represents the
node, and the second map represents the relationship status map of the node.
Relationships can be directed, and the syntax for direction specification is:
- &lt;type - incoming relationship.
- type> - outgoing relationship.
- type - both incoming and outgoing.

Any other syntax results in an exception.

#### Input:

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](https://memgraph.com/docs/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run. 
If subgraph is not specified, the algorithm is computed on the entire graph by default.

- `nodes: List[Any]` ➡ A list of input nodes. Elements of the list can be either nodes themselves or their IDs.
- `relationships: List[string]` ➡ A list of relationships to be checked.

#### Output:

- `result: Map` ➡ The result map, containing two smaller maps. The first map
represents the node, and the second represents the status of relationships
checked in the function. Example of the map: `{"Node": {"id": 0, "labels":["Dog"], "properties": {},"type": "node"}, "Relationships_exist_status": {"RUNS": false}}`.

#### Usage:

The database contains the following graph objects:

```cypher
CREATE (d:Dog)-[l:LOVES]->(h:Human)-[t:TAKES_CARE_OF]->(d);
```

The following query will check if relationships in the input list exist at the given nodes:

```cypher
MATCH (d:Dog), (h:Human)
CALL nodes.relationships_exist([d,id(h)], ["<LOVES","FOLLOWS"]) YIELD result RETURN result;
```

Result:

```plaintext
+-----------------------------------------------------------------------------------------------------------------------------------------+
| result                                                                                                                                  |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| {"Node": {"id": 0,"labels": ["Dog"],"properties": {},"type": "node"},"Relationships_exist_status": {"<LOVES": false,"FOLLOWS": false}}  |               
+-----------------------------------------------------------------------------------------------------------------------------------------+
| {"Node": {"id": 1,"labels": ["Human"],"properties": {},"type": "node"},"Relationships_exist_status": {"<LOVES": true,"FOLLOWS": false}} |
+-----------------------------------------------------------------------------------------------------------------------------------------+
```

### `link()`

Links the provided nodes sequentially with the relationship type provided in the
input, essentially creating a linked list of nodes.

#### Input:

- `subgraph: Graph` (**OPTIONAL**) ➡ A specific subgraph, which is an [object of type Graph](https://memgraph.com/docs/advanced-algorithms/run-algorithms#run-procedures-on-subgraph) returned by the `project()` function, on which the algorithm is run. 
If subgraph is not specified, the algorithm is computed on the entire graph by default.

- `nodes: List[Node]` ➡ A list of input nodes that need to be linked.
- `type: string` ➡ The type of relationship that will link two nodes.

#### Usage:

The database contains the following graph objects: 

![](https://memgraph.com/docs/pages/advanced-algorithms/available-algorithms/nodes/graph_before.png)

Run the following query

```cypher
MATCH (h:Human), (c:Cat), (m:Mouse), (e:Elephant) 
CALL nodes.link([e, m, c, h, e],"IS_AFRAID_OF");
```

The database changes to the following state: 

![](https://memgraph.com/docs/pages/advanced-algorithms/available-algorithms/nodes/graph_after.png)
