# How to find similarity between two graphs in Neo4j?

**URL:** https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387
**Category:** Graph Data Science / Graph Analytics
**Created:** [December 1, 2019, 3:06pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387 "2019-12-01T15:06:26Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![ahmedmelmoselhy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ahmedmelmoselhy/32/3134_2.png) [@ahmedmelmoselhy](https://community.neo4j.com/u/ahmedmelmoselhy)
#### Post date: [December 1, 2019, 3:06pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/1 "2019-12-01T15:06:26Z")

</div>

Hello,  
I want to import 2 separate graphs into Neo4j and then to find the similarity between them. How to do that?  
I had a look at the Jaccard Similarity procedure and it seems that the 2 graphs should be connected in some way.  
But what if they are not?

Also, I have found that one of the algorithms used for graph similarity/ isomorphism is Maximum Common Subgraph. Is there a way to use it in Neo4j?

Thanks in advance.

---

<div class="post-metadata">

### Author: ![roberto1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/roberto1/32/9505_2.png) [@roberto1](https://community.neo4j.com/u/roberto1)
#### Post date: [December 1, 2019, 3:31pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/2 "2019-12-01T15:31:19Z")

</div>

["Graph similarity involves determining the degree of similarity between these two graphs (a number between 0 and 1). Intuitively, since we know the node correspondences, the same node in both graphs would be similar if its neighbors are similar (and its connectivity, in terms of edge weights, to its neighbors). Again, its neighbors are similar if their neighborhoods are similar, and so on."](https://people.eecs.berkeley.edu/~aramdas/reports/DBreport.pdf)

You need to share some elements between the two graph for example if you're talking about customers and e-costumers there are some properties that share like gender, zip, contact number. etc  
maybe you could share a little more of info about the graphs and the model that you want to do.

kind regards, Roberto

---

<div class="post-metadata">

### Author: ![shan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shan/32/7767_2.png) [@shan](https://community.neo4j.com/u/shan)
#### Post date: [December 2, 2019, 3:56pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/3 "2019-12-02T15:56:36Z")

</div>

Can you explain a bit more about what you mean by similarity between 2 graphs?  
Those OOTB similarity algorithms (e.g., jaccard) are meant to be used to find similarity between nodes. You might be able to use them for other purposes though.

---

<div class="post-metadata">

### Author: ![ahmedmelmoselhy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ahmedmelmoselhy/32/3134_2.png) [@ahmedmelmoselhy](https://community.neo4j.com/u/ahmedmelmoselhy)
#### Post date: [December 3, 2019, 12:44am UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/4 "2019-12-03T00:44:11Z")

</div>

Thanks [Roberto](https://community.neo4j.com/u/roberto1) for the reply and for the document.  
My goal is to have two separate graphs (and I know previously that there similar ) and I want to calculate the overall similarity or change that happens from one to another.  
Example:

 ![](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/c/c209f90f017874c96ceba03c73e34b3f28eb5612.jpeg)  
Here, I have a chemical compound 1 with nodes as (H,C, etc) and edges as bonds (Ionic, etc). which means I have heterogeneity in nodes and edges. Then I have another chemical compound 2 (assumably) almost similar, with small changes ( e.g. changing one H node to C node, same as for the edges)  
My goal is to compare these two standalone graphs and to come up with a percentage of similarity.

Hope that my idea is clear.

Thanks in advance.  
Ahmed.

---

<div class="post-metadata">

### Author: ![ahmedmelmoselhy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ahmedmelmoselhy/32/3134_2.png) [@ahmedmelmoselhy](https://community.neo4j.com/u/ahmedmelmoselhy)
#### Post date: [December 3, 2019, 12:49am UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/5 "2019-12-03T00:49:15Z")

</div>

Thanks [shan](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/3) for your reply.  
By the similarity between 2 graphs: can be thoughts also as is I have two identical standalone graphs and then I tweaked some nodes/edges here and there. So, I want to measure how similar this new (tweaked) version of the graph to the original one.  
I have also mentioned an example in my second reply above.

Hope this will help in clarifying my idea.

Thanks in advance.  
Ahmed

---

<div class="post-metadata">

### Author: ![niclasko](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/niclasko/32/14871_2.png) [@niclasko](https://community.neo4j.com/u/niclasko)
#### Post date: [December 3, 2019, 8:01am UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/6 "2019-12-03T08:01:13Z")

</div>

If you want to compare the degree of overlap in relationship sequences, then you could check out the BLAST algorithm from bioinformatics ([BLAST (biotechnology) - Wikipedia](https://en.wikipedia.org/wiki/BLAST_(biotechnology)))

You can also represent your graphs as contingency matrices and compare those using a singular value decomposition of the contingency matrices for example. That doesn't take into account the relationship sequences however.

Yet another idea is to do a graph 2 vec ([DeepWalk: Implementing Graph Embeddings in Neo4j](https://neo4j.com/blog/deepwalk-implementing-graph-embeddings-in-neo4j/)). This allows you to compare nodes based on their contextual information (i.e. relationship neighborhood similarity).

Just some ideas...

---

<div class="post-metadata">

### Author: ![ahmedmelmoselhy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ahmedmelmoselhy/32/3134_2.png) [@ahmedmelmoselhy](https://community.neo4j.com/u/ahmedmelmoselhy)
#### Post date: [December 3, 2019, 8:55am UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/7 "2019-12-03T08:55:17Z")

</div>

Great ideas @niclasko  
Thank you very much.  
I will try them and will put my feedback here.

---

<div class="post-metadata">

### Author: ![shan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shan/32/7767_2.png) [@shan](https://community.neo4j.com/u/shan)
#### Post date: [December 4, 2019, 4:01pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/8 "2019-12-04T16:01:31Z")

</div>

A new graph algorithm was released yesterday that might help you with your usecase:

> **[Node Similarity - Neo4j Graph Data Science](https://neo4j.com/docs/graph-data-science/1.1/algorithms/node-similarity/)**
>
> This section describes the Node Similarity algorithm in the Neo4j Graph Data Science library.

> The Node Similarity algorithm compares a set of nodes based on the nodes they are connected to. Two nodes are considered similar if they share many of the same neighbors. Node Similarity computes pair-wise similarities based on the Jaccard metric.

---

<div class="post-metadata">

### Author: ![ahmedmelmoselhy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ahmedmelmoselhy/32/3134_2.png) [@ahmedmelmoselhy](https://community.neo4j.com/u/ahmedmelmoselhy)
#### Post date: [December 4, 2019, 11:20pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/9 "2019-12-04T23:20:31Z")

</div>

That's great!  
Thank you very much, [shan](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/8)  
I am gonna use it for sure.

---

<div class="post-metadata">

### Author: ![marvin-hansen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/marvin-hansen/32/7559_2.png) [@marvin-hansen](https://community.neo4j.com/u/marvin-hansen)
#### Post date: [December 5, 2019, 10:01am UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/10 "2019-12-05T10:01:18Z")

</div>

Use geometric deep learning to correctly classify sub-graph patterns.

> **[Deep Graph Library](https://www.dgl.ai/pages/about.html)**
>
> Library for deep learning on graphs

[https://docs.dgl.ai/tutorials/basics/4\_batch.html](https://docs.dgl.ai/tutorials/basics/4_batch.html)

Once classified, you just search for any sort of subgraph pattern and you are effectively done.

Beyond that, you can perform link/relation prediction with DGL to figure out which molecule is most likely to change state e.g. changing one H node to C node int order to bind to another molecule depending on its context.

[https://docs.dgl.ai/tutorials/basics/1\_first.html](https://docs.dgl.ai/tutorials/basics/1_first.html)

In case you are short on structures to classify, you can just generate molecules structures similar but slightly different to your target structure by using GAN's.

> **[Deep learning for molecular design—a review of the state of the art](https://pubs.rsc.org/en/content/articlelanding/2019/me/c9me00039a#!divAbstract)**
>
> In the space of only a few years, deep generative modeling has revolutionized how we think of artificial creativity, yielding autonomous systems which produce original images, music, and text. Inspired by these successes, researchers are now applying...

> **[paper18.pdf](https://ceur-ws.org/Vol-2350/paper18.pdf)**
>
> 1251.09 KB

> **[Creating Molecules from Scratch I: Drug Discovery with Generative Adversarial...](https://medium.com/neuromation-blog/creating-molecules-from-scratch-i-drug-discovery-with-generative-adversarial-networks-9d42cc496fc6)**
>
> We’ve got great news: the very first paper with official Neuromation affiliation has appeared! This work, “3D Molecular Representations…

---

<div class="post-metadata">

### Author: ![ahmedmelmoselhy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ahmedmelmoselhy/32/3134_2.png) [@ahmedmelmoselhy](https://community.neo4j.com/u/ahmedmelmoselhy)
#### Post date: [December 6, 2019, 4:50pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/11 "2019-12-06T16:50:08Z")

</div>

Awesome!  
Very useful resources and ideas.  
Thank you very much [marvin-hansen](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/10).  
I will back with my feedback after trying them.

---

<div class="post-metadata">

### Author: ![alicia.frame](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/alicia.frame/32/4507_2.png) [@alicia.frame](https://community.neo4j.com/u/alicia.frame)
#### Post date: [December 9, 2019, 4:14pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/12 "2019-12-09T16:14:06Z")

</div>

@ahmedmelmoselhy - if you're working in the chemistry space, check out the neo4j/rdkit integration project: [GitHub - rdkit/neo4j-rdkit](https://github.com/rdkit/neo4j-rdkit)

That should let you do structure/substructure and similarity searches using RDKit's framework.

Neo4j doesn't ship any out-of-the-box graph isomorphism or graph edit distance algorithms, but the RDKit tools should get you where you need to be 🙂

---

<div class="post-metadata">

### Author: ![ahmedmelmoselhy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ahmedmelmoselhy/32/3134_2.png) [@ahmedmelmoselhy](https://community.neo4j.com/u/ahmedmelmoselhy)
#### Post date: [December 10, 2019, 3:19pm UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/13 "2019-12-10T15:19:42Z")

</div>

okay. 🙂  
I am trying the other mentioned techniques and may use it as well.  
thanks [alicia.frame](https://community.neo4j.com/u/alicia.frame)  
I may also try to implement some _common subgraph algorithm_ and hope it will work.

---

<div class="post-metadata">

### Author: ![roberto1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/roberto1/32/9505_2.png) [@roberto1](https://community.neo4j.com/u/roberto1)
#### Post date: [December 15, 2019, 3:14am UTC](https://community.neo4j.com/t/how-to-find-similarity-between-two-graphs-in-neo4j/12387/14 "2019-12-15T03:14:01Z")

</div>

Also you could try put the properties of the atoms in the relationships and use it as a weights. And use something like page rank
