Neo4j - extract all subgraphs from neo4j database
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a similar question to the one asked here, however, the solution proposed didn't work for me.
My Neo4j database has a lot of sub-graphs, each one of them contains varying number of nodes.
I would like to extract some kind of a list which will contain all sub-graphs separated.
In the example given, I would like to get 3 groups, where each group contains all the nodes in the sub-graph which the group represents.
How can I get all groups of nodes by Cypher query?
All the solutions that I have found for this problem require "root" node which I don't have in this case.
neo4j cypher
add a comment |
I have a similar question to the one asked here, however, the solution proposed didn't work for me.
My Neo4j database has a lot of sub-graphs, each one of them contains varying number of nodes.
I would like to extract some kind of a list which will contain all sub-graphs separated.
In the example given, I would like to get 3 groups, where each group contains all the nodes in the sub-graph which the group represents.
How can I get all groups of nodes by Cypher query?
All the solutions that I have found for this problem require "root" node which I don't have in this case.
neo4j cypher
add a comment |
I have a similar question to the one asked here, however, the solution proposed didn't work for me.
My Neo4j database has a lot of sub-graphs, each one of them contains varying number of nodes.
I would like to extract some kind of a list which will contain all sub-graphs separated.
In the example given, I would like to get 3 groups, where each group contains all the nodes in the sub-graph which the group represents.
How can I get all groups of nodes by Cypher query?
All the solutions that I have found for this problem require "root" node which I don't have in this case.
neo4j cypher
I have a similar question to the one asked here, however, the solution proposed didn't work for me.
My Neo4j database has a lot of sub-graphs, each one of them contains varying number of nodes.
I would like to extract some kind of a list which will contain all sub-graphs separated.
In the example given, I would like to get 3 groups, where each group contains all the nodes in the sub-graph which the group represents.
How can I get all groups of nodes by Cypher query?
All the solutions that I have found for this problem require "root" node which I don't have in this case.
neo4j cypher
neo4j cypher
edited Nov 23 '18 at 13:55
Guy Coder
16.3k44287
16.3k44287
asked Nov 23 '18 at 13:51
DanDan
53
53
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could use graph algorithms plugin and specifically connected components algorithm to label all the isolated subgraphs in your graph and then export them and group them later when exported by set id.
Example:
create a sample graph
MERGE (nAlice:User {id:'Alice'})
MERGE (nBridget:User {id:'Bridget'})
MERGE (nCharles:User {id:'Charles'})
MERGE (nDoug:User {id:'Doug'})
MERGE (nMark:User {id:'Mark'})
MERGE (nMichael:User {id:'Michael'})
MERGE (nAlice)-[:FRIEND]->(nBridget)
MERGE (nAlice)-[:FRIEND]->(nCharles)
MERGE (nMark)-[:FRIEND]->(nDoug)
MERGE (nMark)-[:FRIEND]->(nMichael);
Run connected components (unionFind) algorithm and return nodes in the same subgraph:
CALL algo.unionFind.stream('User', 'FRIEND', {})
YIELD nodeId,setId
RETURN setId,collect(algo.getNodeById(nodeId)) AS nodes_from_specific_subgraph
This will return:
╒═══════╤══════════════════════════════════════════════════╕
│"setId"│"specific_subgraph" │
╞═══════╪══════════════════════════════════════════════════╡
│4 │[{"id":"Doug"},{"id":"Mark"},{"id":"Michael"}] │
├───────┼──────────────────────────────────────────────────┤
│0 │[{"id":"Alice"},{"id":"Bridget"},{"id":"Charles"}]│
└───────┴──────────────────────────────────────────────────┘
added an example
– Tomaž Bratanič
Nov 23 '18 at 19:50
Thank you! I wasn't familiar with the graph lgorithm plugin. I have followed your example and that worked for me!
– Dan
Nov 24 '18 at 10:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447957%2fneo4j-extract-all-subgraphs-from-neo4j-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use graph algorithms plugin and specifically connected components algorithm to label all the isolated subgraphs in your graph and then export them and group them later when exported by set id.
Example:
create a sample graph
MERGE (nAlice:User {id:'Alice'})
MERGE (nBridget:User {id:'Bridget'})
MERGE (nCharles:User {id:'Charles'})
MERGE (nDoug:User {id:'Doug'})
MERGE (nMark:User {id:'Mark'})
MERGE (nMichael:User {id:'Michael'})
MERGE (nAlice)-[:FRIEND]->(nBridget)
MERGE (nAlice)-[:FRIEND]->(nCharles)
MERGE (nMark)-[:FRIEND]->(nDoug)
MERGE (nMark)-[:FRIEND]->(nMichael);
Run connected components (unionFind) algorithm and return nodes in the same subgraph:
CALL algo.unionFind.stream('User', 'FRIEND', {})
YIELD nodeId,setId
RETURN setId,collect(algo.getNodeById(nodeId)) AS nodes_from_specific_subgraph
This will return:
╒═══════╤══════════════════════════════════════════════════╕
│"setId"│"specific_subgraph" │
╞═══════╪══════════════════════════════════════════════════╡
│4 │[{"id":"Doug"},{"id":"Mark"},{"id":"Michael"}] │
├───────┼──────────────────────────────────────────────────┤
│0 │[{"id":"Alice"},{"id":"Bridget"},{"id":"Charles"}]│
└───────┴──────────────────────────────────────────────────┘
added an example
– Tomaž Bratanič
Nov 23 '18 at 19:50
Thank you! I wasn't familiar with the graph lgorithm plugin. I have followed your example and that worked for me!
– Dan
Nov 24 '18 at 10:19
add a comment |
You could use graph algorithms plugin and specifically connected components algorithm to label all the isolated subgraphs in your graph and then export them and group them later when exported by set id.
Example:
create a sample graph
MERGE (nAlice:User {id:'Alice'})
MERGE (nBridget:User {id:'Bridget'})
MERGE (nCharles:User {id:'Charles'})
MERGE (nDoug:User {id:'Doug'})
MERGE (nMark:User {id:'Mark'})
MERGE (nMichael:User {id:'Michael'})
MERGE (nAlice)-[:FRIEND]->(nBridget)
MERGE (nAlice)-[:FRIEND]->(nCharles)
MERGE (nMark)-[:FRIEND]->(nDoug)
MERGE (nMark)-[:FRIEND]->(nMichael);
Run connected components (unionFind) algorithm and return nodes in the same subgraph:
CALL algo.unionFind.stream('User', 'FRIEND', {})
YIELD nodeId,setId
RETURN setId,collect(algo.getNodeById(nodeId)) AS nodes_from_specific_subgraph
This will return:
╒═══════╤══════════════════════════════════════════════════╕
│"setId"│"specific_subgraph" │
╞═══════╪══════════════════════════════════════════════════╡
│4 │[{"id":"Doug"},{"id":"Mark"},{"id":"Michael"}] │
├───────┼──────────────────────────────────────────────────┤
│0 │[{"id":"Alice"},{"id":"Bridget"},{"id":"Charles"}]│
└───────┴──────────────────────────────────────────────────┘
added an example
– Tomaž Bratanič
Nov 23 '18 at 19:50
Thank you! I wasn't familiar with the graph lgorithm plugin. I have followed your example and that worked for me!
– Dan
Nov 24 '18 at 10:19
add a comment |
You could use graph algorithms plugin and specifically connected components algorithm to label all the isolated subgraphs in your graph and then export them and group them later when exported by set id.
Example:
create a sample graph
MERGE (nAlice:User {id:'Alice'})
MERGE (nBridget:User {id:'Bridget'})
MERGE (nCharles:User {id:'Charles'})
MERGE (nDoug:User {id:'Doug'})
MERGE (nMark:User {id:'Mark'})
MERGE (nMichael:User {id:'Michael'})
MERGE (nAlice)-[:FRIEND]->(nBridget)
MERGE (nAlice)-[:FRIEND]->(nCharles)
MERGE (nMark)-[:FRIEND]->(nDoug)
MERGE (nMark)-[:FRIEND]->(nMichael);
Run connected components (unionFind) algorithm and return nodes in the same subgraph:
CALL algo.unionFind.stream('User', 'FRIEND', {})
YIELD nodeId,setId
RETURN setId,collect(algo.getNodeById(nodeId)) AS nodes_from_specific_subgraph
This will return:
╒═══════╤══════════════════════════════════════════════════╕
│"setId"│"specific_subgraph" │
╞═══════╪══════════════════════════════════════════════════╡
│4 │[{"id":"Doug"},{"id":"Mark"},{"id":"Michael"}] │
├───────┼──────────────────────────────────────────────────┤
│0 │[{"id":"Alice"},{"id":"Bridget"},{"id":"Charles"}]│
└───────┴──────────────────────────────────────────────────┘
You could use graph algorithms plugin and specifically connected components algorithm to label all the isolated subgraphs in your graph and then export them and group them later when exported by set id.
Example:
create a sample graph
MERGE (nAlice:User {id:'Alice'})
MERGE (nBridget:User {id:'Bridget'})
MERGE (nCharles:User {id:'Charles'})
MERGE (nDoug:User {id:'Doug'})
MERGE (nMark:User {id:'Mark'})
MERGE (nMichael:User {id:'Michael'})
MERGE (nAlice)-[:FRIEND]->(nBridget)
MERGE (nAlice)-[:FRIEND]->(nCharles)
MERGE (nMark)-[:FRIEND]->(nDoug)
MERGE (nMark)-[:FRIEND]->(nMichael);
Run connected components (unionFind) algorithm and return nodes in the same subgraph:
CALL algo.unionFind.stream('User', 'FRIEND', {})
YIELD nodeId,setId
RETURN setId,collect(algo.getNodeById(nodeId)) AS nodes_from_specific_subgraph
This will return:
╒═══════╤══════════════════════════════════════════════════╕
│"setId"│"specific_subgraph" │
╞═══════╪══════════════════════════════════════════════════╡
│4 │[{"id":"Doug"},{"id":"Mark"},{"id":"Michael"}] │
├───────┼──────────────────────────────────────────────────┤
│0 │[{"id":"Alice"},{"id":"Bridget"},{"id":"Charles"}]│
└───────┴──────────────────────────────────────────────────┘
edited Nov 23 '18 at 19:50
answered Nov 23 '18 at 14:26
Tomaž BrataničTomaž Bratanič
1,9472820
1,9472820
added an example
– Tomaž Bratanič
Nov 23 '18 at 19:50
Thank you! I wasn't familiar with the graph lgorithm plugin. I have followed your example and that worked for me!
– Dan
Nov 24 '18 at 10:19
add a comment |
added an example
– Tomaž Bratanič
Nov 23 '18 at 19:50
Thank you! I wasn't familiar with the graph lgorithm plugin. I have followed your example and that worked for me!
– Dan
Nov 24 '18 at 10:19
added an example
– Tomaž Bratanič
Nov 23 '18 at 19:50
added an example
– Tomaž Bratanič
Nov 23 '18 at 19:50
Thank you! I wasn't familiar with the graph lgorithm plugin. I have followed your example and that worked for me!
– Dan
Nov 24 '18 at 10:19
Thank you! I wasn't familiar with the graph lgorithm plugin. I have followed your example and that worked for me!
– Dan
Nov 24 '18 at 10:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447957%2fneo4j-extract-all-subgraphs-from-neo4j-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown