Duplicate a subgraph

Hello,
can I duplicate the following graph in neo4j? and if it is possible, then what cyphers can can me?


Have a lovely day :)

Hello @verachkaverachka :slight_smile:

If you want to duplicate this graph in another database?

You can use the Graph Data Science plugin.

First, you create the in-memory graph:

CALL gds.graph.project('graph', '*', '*')
YIELD graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels

Then, you export the graph to a new database:

CALL gds.graph.export('graph', { dbName: 'mydatabase' })

Finally, you can create the new database and use it:

:use system
CREATE DATABASE mydatabase;
:use mydatabase

Regards,
Cobra

thank you so much for your answers :slight_smile:
and if I want to duplicate this graph in the same databse? is it possible?
and if it is, then which query can help me?

You can have a look at APOC plugin with clone nodes functions or clone subgraph functions. I think the apoc.refactor.cloneNodesWithRelationships() function is what you need.

Maybe this query will help you:

MATCH (n)
CALL apoc.refactor.cloneNodesWithRelationships([n])
YIELD input, output
RETURN *
1 Like

Hello ,I've installed the APOC library, and tried the suggested query.
this one:
MATCH (n)
CALL apoc.refactor.cloneNodesWithRelationships([n])
YIELD input, output
RETURN *what i've tried to get was this whole graph twice , like to saparated similar graphs:


what i've actually got is something weird that looks like this:

What I try to do is find a query that will duplicate my original graph and i'll gey two of these.
What query can I do to solve it?
have a great day.
Vera

Can you share your dataset please?

Where get I get it from?(the dataSet) so I can send it here?

I can try to export the data I have to one of these:

or dataSet means something else?

The dataset is the data, can you share queries to recreate the graph?

I create the graph with the help of java (Springboot Dao) it starts with an entity of CellType that represents the type of a node I'm going to use(operation or input ) and with the first celltype(that must be a type of input) I connect it to a "real cell" and I call it "cellInstaces".(they also will appear in my frontend later without all the attributes). This cellInstance is going to be connected to another cellInstance from Celltype(again cellInstance must have a type-cellType) of function because in the end my purpose is to run a simulator on this graph and calculate stuff from function to function. Then, I connect an entity named a subgraphInstance to cellInstance(that represents the input)to my cellInstance. and all the nodes that are connected to this first cellInstance we will see them.
the query I use to create cell instance is this one


The query to connect between cellInstances is

I did use a query when I wanted to get my graph that looks like this:

and if you meant they tables that represents the data of this graph for example:


it look like this:
{
"identity": 0,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 5.6,
"xCoordinate": 3.7
}
}
{
"identity": 20,
"labels": [
"SubGraphInstance"
],
"properties": {
"name": "Graph1"
}
}
{
"identity": 77,
"labels": [
"Variable"
],
"properties": {
"variableName": "x"
}
}
{
"identity": 78,
"labels": [
"Function"
],
"properties": {
"expression": "x"
}
}
{
"identity": 79,
"labels": [
"CellType"
],
"properties": {
"name": "Input Cell",
"transformType": "INPUT_TO_ANALOG"
}
}
{
"identity": 84,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 6.6,
"xCoordinate": 4.7
}
}
{
"identity": 85,
"labels": [
"Variable"
],
"properties": {
"variableName": "x"
}
}
{
"identity": 86,
"labels": [
"Variable"
],
"properties": {
"variableName": "y"
}
}
{
"identity": 87,
"labels": [
"Function"
],
"properties": {
"expression": "x+y"
}
}
{
"identity": 88,
"labels": [
"CellType"
],
"properties": {
"name": "Addition ",
"transformType": "ANALOG_TO_ANALOG"
}
}
{
"identity": 89,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 6.6,
"xCoordinate": 4.7
}
}
{
"identity": 90,
"labels": [
"Connection"
],
"properties": {
"delay": 1
}
}
{
"identity": 91,
"labels": [
"Connection"
],
"properties": {
"delay": 1
}
}

You can try this query to duplicate all nodes and their relationships:

MATCH (n)
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

You can also add a WHERE clause to select nodes and their relationships from of a subgraph:

MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

There is more documentation here.

1 Like

I will try it and let you know today! ,thank you so so much for trying to help me understand !! :slight_smile:

1 Like

yeahi! it works!
thank you so much for helping me :heart:

1 Like

Hello!
Can I duplicate my graph by his name or id with a query?for example if I have a few different subgraphs(When I say subgraphisntance i mean the node and all his connected elements) and I want to duplicate all the elements of a specific subgraph in my DB and not duplicate them all, is there a way to do it with apoc? Like you showed me with the first query but this time finding the whole subgraph by his id or name?
Vera

Hello @verachkaverachka :slight_smile:

It's in the answer I gave you, the second query with the WHERE clause should do what you want.

Regards,
Cobra

1 Like

I tried the second one, and got only 1 duplicated node which was Graph1 that represents subGraphInstance, my problem is that i got it without all the connected elements of this node and I need them too

All the nodes of the subgraph must be tagged with the property, not only one node.