This repository was archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
[master < path] Write docs for path module #1018
Open
mpintaric55334
wants to merge
9
commits into
master
Choose a base branch
from
E-mage-path-docs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
78d21dd
Write docs for path.expand
mpintaric55334 8248fdf
create path docs
imilinovic 1b0cbd2
Merge branch 'master' of github.com:memgraph/docs into E-mage-path-docs
imilinovic 7d79777
Apply suggestions from code review
vpavicic a379dfa
add rest of path docs
ind1xa 98cdd24
Merge pull request #1024 from memgraph/add-to-paths
ind1xa d211a57
Apply suggestions from code review
vpavicic b809a7f
PR review changes
ind1xa be0fb14
Merge branch 'E-mage-path-docs' of https://github.com/memgraph/docs i…
ind1xa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,300 @@ | ||
--- | ||
id: path | ||
title: path | ||
sidebar_label: path | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
import RunOnSubgraph from '../../templates/_run_on_subgraph.mdx'; | ||
|
||
export const Highlight = ({children, color}) => ( | ||
<span | ||
style={{ | ||
backgroundColor: color, | ||
borderRadius: '2px', | ||
color: '#fff', | ||
padding: '0.2rem', | ||
}}> | ||
{children} | ||
</span> | ||
); | ||
|
||
The `path` module is a module for working with intricate connections in graph-like structures. It provides functions and procedures to navigate and analyze paths, helping users uncover meaningful insights from complex data relationships. | ||
|
||
[](https://github.com/memgraph/mage/tree/main/cpp/path_module) | ||
|
||
| Trait | Value | | ||
| ------------------- | ----------------------------------------------------- | | ||
| **Module type** | <Highlight color="#FB6E00">**algorithm**</Highlight> | | ||
| **Implementation** | <Highlight color="#FB6E00">**C++**</Highlight> | | ||
| **Graph direction** | <Highlight color="#FB6E00">**directed**</Highlight>/<Highlight color="#FB6E00">**undirected**</Highlight> | | ||
| **Edge weights** | <Highlight color="#FB6E00">**weighted**</Highlight>/<Highlight color="#FB6E00">**unweighted**</Highlight> | | ||
| **Parallelism** | <Highlight color="#FB6E00">**sequential**</Highlight> | | ||
|
||
### Procedures | ||
|
||
### `create(start_node, relationships)` | ||
|
||
Creates a path from the given starting node and a list of relationships. Iteratively appends all relationships in the list to the new path until a relationship is null (as a result of optional match) or a relationship from the last node of the path to one of the nodes in the current relationship (the one that isn't the last one in the path) doesn't exist. | ||
|
||
#### Input | ||
|
||
- `start_node: Node` - starting node of the path | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `relationships: Map` - map with the key `rel` that contains a list of the given relationships. See `usage` for more details. | ||
|
||
#### Output | ||
|
||
- `path: Path` - the created path | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Usage | ||
|
||
```cypher | ||
|
||
MERGE (croatia:Country {name: 'Croatia'}) | ||
MERGE (madrid:City {name: 'Madrid'}) | ||
MERGE (kutina:City {name: 'Kutina'}) | ||
MERGE (real:Club {name: 'Real Madrid'}) | ||
MERGE (moslavina:Club {name: 'NK Moslavina'}) | ||
MERGE (kutina)-[:In_country]->(croatia) | ||
MERGE (moslavina)-[:In_city]->(kutina) | ||
MERGE (real)-[:In_city]->(madrid); | ||
``` | ||
```cypher | ||
MATCH (club:Club) OPTIONAL MATCH (club)-[inCity:In_city]->(city:City) OPTIONAL MATCH (city)-[inCountry:In_country]->(:Country) CALL path.create(club, {rel:[inCity, inCountry]}) YIELD path return path; | ||
``` | ||
|
||
```plaintext | ||
+------------------------------------------------------------------------------------------------------------------+ | ||
| path | | ||
+------------------------------------------------------------------------------------------------------------------+ | ||
| (:Club {name: 'Real Madrid'}-[:In_city]->(:City {name 'Madrid'})) | | ||
+------------------------------------------------------------------------------------------------------------------+ | ||
| (:Club {name: 'NK Moslavina'}-[:In_city]->(:City {name 'Kutina'})-[:In_country]->(:Country {name 'Croatia'})) | | ||
+------------------------------------------------------------------------------------------------------------------+ | ||
``` | ||
|
||
### `expand(start, relationships, labels, min_hops, max_hops)` | ||
|
||
Expand from the start node(s) following the given relationships and label filters, from min to max number of allowed hops. Return all paths inside the allowed number of hops, which satisfy relationship and label filters. | ||
|
||
#### Input: | ||
|
||
- `start: any` ➡ node, node ID, or list of nodes and/or node IDs from which the function will expand. | ||
- `relationships: List[string]` ➡ list of relationships which the expanding will follow. | ||
- `labels: List[string]` ➡ list of labels which will define filtering. | ||
- `min_hops: int` ➡ minimum number of hops for a path to be returned. | ||
- `max_hops: int` ➡ maximum number of hops for a path to be returned. | ||
|
||
#### Relationship filters: | ||
|
||
Relationship filters are described in the table below: | ||
|
||
|Option | Explanation | | ||
|--------------------|------------------------------------------------------------------------------| | ||
| `TYPE` | Path will expand with both outgoing and incoming relationships of this type. | | ||
| `<TYPE` | Path will expand with incoming relationships of this type. | | ||
| `TYPE>` | Path will expand with outgoing relationships of this type. | | ||
| `>` | Path will expand with all outgoing relationships. | | ||
| `<` | Path will expand will all incoming relationships. | | ||
|
||
Anything else will result in an exception. | ||
|
||
If the relationship filter is empty, all relationship types are allowed. | ||
|
||
##### Relationship filtering examples: | ||
|
||
- `Relationship list : [<LOVES, >]` : path will expand on all outgoing relationships, and incoming relationship `LOVES`. | ||
- `Relationship list : [<LOVES, LOVES]` : path will expand on incoming relationship `LOVES`, and all directions of relationship `LOVES`, making the first element in the relationship list functionally obsolete. | ||
- `Relationship list : []` : path will expand on all relationships. | ||
|
||
#### Label filter: | ||
|
||
Label filters are described in the table below: | ||
|
||
|Option | Explanation | | ||
|--------------------|------------------------------------------------------------------------------| | ||
| `+LABEL` | Label is added to the whitelist. All nodes in the path must have a label in the whitelist. If the whitelist is empty, it is as if all nodes are whitelisted. | | ||
| `>LABEL` | Label is added to the end list. When end list has labels, only paths ending with these labels will be returned, but they can be expanded further, to return paths ending in nodes with end labels beyond it, but the expansion will only go through nodes with whitelisted labels. Labels in the end list do not have to respect the whitelist. | | ||
| `-LABEL` | Label is added to the blacklist. No node in the path will contain labels in the blacklist. The blacklist takes precedence over all other filters. | | ||
| `/LABEL` | Label is added to the termination list. When termination list contains labels, only paths ending with these labels will be returned, and any further expansion is stopped. Labels in the termination list do not have to respect the whitelist. | | ||
|
||
Any other label syntax is added to the whitelist. For example, `LABEL` will be added to the whitelist as `LABEL`, and `!LABEL` will be added to the whitelist as `!LABEL`. NOTE: when deciding where the label will be added, it is done by looking at the first element of the label. For example, `>LABEL>` will be added to the end list as `LABEL>`. | ||
|
||
##### Label filtering examples: | ||
|
||
Consider the graph provided in the usage section below. In this subsection, number of hops will be limited from 0 to 2, and the starting node will be `Dog`. | ||
|
||
- `Label list: ["/Mouse"]` - this will return all the paths ending with `Mouse`. Because no labels were added to the whitelist, all labels are considered whitelisted. This filtering will return 3 paths: `Dog->Cat->Mouse`, `Dog<-Human->Mouse`, `Dog->Mouse`. | ||
|
||
- `Label list: ["/Mouse", "Cat"]` - now, label `Cat` is added to the whitelist, becoming the only whitelisted label. The meaning of the filter can now be represented as: `"return all paths ending with Mouse, which expand through Cat and Cat only"`. This filtering will return two paths, one where `Dog` connects to the `Mouse` directly(`Dog->Mouse`), and one where `Cat` is included(`Dog->Cat->Mouse`). | ||
|
||
- `Label list: ["/Mouse", "-Cat", "-Human"]` - now, both `Cat` and `Human` are blacklisted, and there is only one eligible path that can reach `Mouse`: `Dog->Mouse`. | ||
|
||
For the final example, the starting node will be `Cat`, and the maximum number of hops will be increased to 4. | ||
|
||
- `Label list: [">Dog", "+Human", "+Wolf"]` - now, only paths ending with `Dog` will be returned, but they can be further expaned through the nodes with whitelisted labels. This filtering returns 3 paths: `Cat<-Dog`, `Cat<-Dog<-Human->Wolf->Dog`, `Cat<-Dog<-Wolf<-Human->Dog`. | ||
|
||
|
||
#### Usage: | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In this section, usage with Cypher will be demonstrated. | ||
|
||
##### Graph used in examples: | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This is the graph used in examples. | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<Tabs | ||
groupId="example" | ||
defaultValue="cypher" | ||
values={[ | ||
{label: 'Step 1: Cypher for creation', value: 'cypher'}, | ||
{label: 'Step 2: Graph visualization', value: 'visualization'}, | ||
] | ||
}> | ||
|
||
<TabItem value="cypher"> | ||
|
||
```cypher | ||
CREATE (w:Wolf)-[ca:CATCHES]->(d:Dog), (c:Cat), (m:Mouse), (h:Human); | ||
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human) | ||
WITH w, d, c, m, h | ||
CREATE (d)-[:CATCHES]->(c) | ||
CREATE (c)-[:CATCHES]->(m) | ||
CREATE (d)-[:FRIENDS_WITH]->(m) | ||
CREATE (h)-[:OWNS]->(d) | ||
CREATE (h)-[:HUNTS]->(w) | ||
CREATE (h)-[:HATES]->(m); | ||
``` | ||
</TabItem> | ||
|
||
<TabItem value="visualization"> | ||
|
||
<img src={require('../../data/query-modules/cpp/path/graph.png').default}/> | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
##### Usage example 1: | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Expand from `Dog` on outgoing relationship `CATCHES` and incoming relationship `HATES`, with `Mouse` and `Human` being labels in end list. Whitelist is empty, hence, all labels are whitelisted. | ||
|
||
|
||
<Tabs | ||
groupId="usageExample1" | ||
defaultValue="cypher" | ||
values={[ | ||
{label: 'Step 1: Cypher syntax', value: 'cypher'}, | ||
{label: 'Step 2: Graph visualization', value: 'visualization'}, | ||
{label: 'Step 3: Table results', value: 'results'}, | ||
] | ||
}> | ||
|
||
<TabItem value="cypher"> | ||
|
||
```cypher | ||
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human) | ||
CALL path.expand(d,["CATCHES>","<HATES"],[">Mouse", ">Human"],0,4) YIELD result RETURN result; | ||
``` | ||
</TabItem> | ||
|
||
<TabItem value="visualization"> | ||
|
||
<img src={require('../../data/query-modules/cpp/path/graph_usage1.png').default}/> | ||
|
||
</TabItem> | ||
|
||
|
||
<TabItem value="results"> | ||
|
||
| result | | ||
|------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `{"nodes":[{"id":1,"labels":["Dog"],"properties":{},"type":"node"},{"id":2,"labels":["Cat"],"properties":{},"type":"node"},{"id":3,"labels":["Mouse"],"properties":{},"type":"node"}],"relationships":[{"id":1,"start":1,"end":2,"label":"CATCHES","properties":{},"type":"relationship"},{"id":2,"start":2,"end":3,"label":"CATCHES","properties":{},"type":"relationship"}],"type":"path"}`| | ||
| `{"nodes":[{"id":1,"labels":["Dog"],"properties":{},"type":"node"},{"id":2,"labels":["Cat"],"properties":{},"type":"node"},{"id":3,"labels":["Mouse"],"properties":{},"type":"node"},{"id":4,"labels":["Human"],"properties":{},"type":"node"}],"relationships":[{"id":1,"start":1,"end":2,"label":"CATCHES","properties":{},"type":"relationship"},{"id":2,"start":2,"end":3,"label":"CATCHES","properties":{},"type":"relationship"},{"id":6,"start":4,"end":3,"label":"HATES","properties":{},"type":"relationship"}],"type":"path"}`| | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
|
||
##### Usage example 2: | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Expand from `Dog` only on incoming relationships. Also, `Human` is blacklisted. | ||
|
||
<Tabs | ||
groupId="usageExample2" | ||
defaultValue="cypher" | ||
values={[ | ||
{label: 'Step 1: Cypher syntax', value: 'cypher'}, | ||
{label: 'Step 2: Graph visualization', value: 'visualization'}, | ||
{label: 'Step 3: Table results', value: 'results'}, | ||
] | ||
}> | ||
|
||
<TabItem value="cypher"> | ||
|
||
```cypher | ||
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human) | ||
CALL path.expand(d,["<"],["-Human"],0,4) YIELD result RETURN result; | ||
``` | ||
</TabItem> | ||
|
||
<TabItem value="visualization"> | ||
|
||
<img src={require('../../data/query-modules/cpp/path/graph_usage2.png').default}/> | ||
|
||
</TabItem> | ||
|
||
|
||
<TabItem value="results"> | ||
|
||
| result | | ||
|------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `{"nodes":[{"id":1,"labels":["Dog"],"properties":{},"type":"node"},{"id":0,"labels":["Wolf"],"properties":{},"type":"node"}],"relationships":[{"id":0,"start":0,"end":1,"label":"CATCHES","properties":{},"type":"relationship"}],"type":"path"}`| | ||
|
||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
|
||
##### Usage example 3: | ||
vpavicic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Expand from `Dog` and `Mouse`. `Cat` is the termination label, and the maximum number of hops is 1. Also, mouse is passed as ID, to demonstrate that capability of expand function. | ||
|
||
<Tabs | ||
groupId="usageExample3" | ||
defaultValue="cypher" | ||
values={[ | ||
{label: 'Step 1: Cypher syntax', value: 'cypher'}, | ||
{label: 'Step 2: Graph visualization', value: 'visualization'}, | ||
{label: 'Step 3: Table results', value: 'results'}, | ||
] | ||
}> | ||
|
||
<TabItem value="cypher"> | ||
|
||
```cypher | ||
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human) | ||
CALL path.expand([d, id(m)],[],["/Cat"],0,1) YIELD result RETURN result; | ||
``` | ||
</TabItem> | ||
|
||
<TabItem value="visualization"> | ||
|
||
<img src={require('../../data/query-modules/cpp/path/graph_usage3.png').default}/> | ||
|
||
</TabItem> | ||
|
||
|
||
<TabItem value="results"> | ||
|
||
| result | | ||
|------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `{"nodes":[{"id":1,"labels":["Dog"],"properties":{},"type":"node"},{"id":2,"labels":["Cat"],"properties":{},"type":"node"}],"relationships":[{"id":1,"start":1,"end":2,"label":"CATCHES","properties":{},"type":"relationship"}],"type":"path"}`| | ||
| `{"nodes":[{"id":3,"labels":["Mouse"],"properties":{},"type":"node"},{"id":2,"labels":["Cat"],"properties":{},"type":"node"}],"relationships":[{"id":2,"start":2,"end":3,"label":"CATCHES","properties":{},"type":"relationship"}],"type":"path"}`| | ||
|
||
</TabItem> | ||
|
||
</Tabs> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this sounds like a politician wrote it :D You told me nothing :D At this point I have no idea what I will be able to do with this module or is it for me or my use case... I need to read on and check procedures and that's annoying :))