# Graph algorithms 3.5.13 source code

**URL:** https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990
**Category:** Graph Data Science / Graph Analytics
**Created:** [December 18, 2019, 9:33pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990 "2019-12-18T21:33:28Z")
**Posts on this page:** 11
**Page:** 1

<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 18, 2019, 9:33pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/1 "2019-12-18T21:33:28Z")

</div>

I noticed that the triangleCount algorithm sometimes throws ArrayIndexOutOfBoundsException. This seems to be fixed in 3.5.13.0 and for that reason I moved to 3.5.13. But then noticed that now jaccard similarity algorithm sometimes throws ArithmeticException. I was going to look at the code and see if I can understand why that happens but looks like the githup repo does not have the code for 3.5.13. The latest version in the repo is 3.5.4. Does that mean the source code of the newer versions is not open?

---

<div class="post-metadata">

### Author: ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)
#### Post date: [December 19, 2019, 9:48am UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/2 "2019-12-19T09:48:31Z")

</div>

Can you create a GH issue for the exceptions you see?

We are working on making the code available again, it's currently undergoing some internal restructuring / modifications.

---

<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 19, 2019, 3:21pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/3 "2019-12-19T15:21:14Z")

</div>

Thanks and yes @michael.hunger I already did that:

> <https://github.com/neo4j/neo4j/issues/12363>
>
> \*\*Server spec:\*\*
> \- Neo4j version: 3.5.12
> \- Graph algorithms version: 3.5.12.0 …or 3.5.13.0
> \- Operating system: macOS Sierra
> 
> \*\*Steps to reproduce:\*\*
> 1. Install neo4j with the above spec. Don't forget to add the graph algorithms jar file.
> 2. Run the following cyphers to create a small sample graph
> \`\`\`
> merge (a:Test {name:"a"})   
> merge (t1:Test {name:"t1"})   
> merge (t2:Test {name:"t2"})   
> merge (t3:Test {name:"t3"})   
> merge (t4:Test {name:"t4"})   
> merge (t5:Test {name:"t5"})   
> merge (t6:Test {name:"t6"})   
> merge (t7:Test {name:"t7"})   
> merge (t8:Test {name:"t8"})   
> merge (t9:Test {name:"t9"})   
> merge (t10:Test {name:"t10"})   
> merge (t11:Test {name:"t11"})   
> merge (t12:Test {name:"t12"})   
> merge (t13:Test {name:"t13"})   
> merge (t14:Test {name:"t14"})   
> merge (t15:Test {name:"t15"})   
> merge (t16:Test {name:"t16"})   
> merge (t17:Test {name:"t17"})   
> merge (t18:Test {name:"t18"})   
> merge (t19:Test {name:"t19"})   
> merge (t20:Test {name:"t20"})   
> merge (t21:Test {name:"t21"})   
> merge (t22:Test {name:"t22"})   
> merge (t23:Test {name:"t23"})   
> merge (t24:Test {name:"t24"})   
> merge (t25:Test {name:"t25"})   
> merge (t26:Test {name:"t26"})
> merge (a)-\[:CONNECTED\_TO\]-\>(t1)
> merge (a)-\[:CONNECTED\_TO\]-\>(t2)
> merge (a)-\[:CONNECTED\_TO\]-\>(t3)
> merge (a)-\[:CONNECTED\_TO\]-\>(t4)
> merge (a)-\[:CONNECTED\_TO\]-\>(t5)
> merge (a)-\[:CONNECTED\_TO\]-\>(t6)
> merge (a)-\[:CONNECTED\_TO\]-\>(t7)
> merge (a)-\[:CONNECTED\_TO\]-\>(t8)
> merge (a)-\[:CONNECTED\_TO\]-\>(t9)
> merge (a)-\[:CONNECTED\_TO\]-\>(t10)
> merge (a)-\[:CONNECTED\_TO\]-\>(t11)
> merge (a)-\[:CONNECTED\_TO\]-\>(t12)
> merge (a)-\[:CONNECTED\_TO\]-\>(t13)
> merge (a)-\[:CONNECTED\_TO\]-\>(t14)
> merge (a)-\[:CONNECTED\_TO\]-\>(t15)
> merge (a)-\[:CONNECTED\_TO\]-\>(t16)
> merge (a)-\[:CONNECTED\_TO\]-\>(t17)
> merge (a)-\[:CONNECTED\_TO\]-\>(t18)
> merge (a)-\[:CONNECTED\_TO\]-\>(t19)
> merge (a)-\[:CONNECTED\_TO\]-\>(t20)
> merge (a)-\[:CONNECTED\_TO\]-\>(t21)
> merge (a)-\[:CONNECTED\_TO\]-\>(t22)
> merge (a)-\[:CONNECTED\_TO\]-\>(t23)
> merge (a)-\[:CONNECTED\_TO\]-\>(t24)
> merge (a)-\[:CONNECTED\_TO\]-\>(t25)    
> merge (a)-\[:CONNECTED\_TO\]-\>(t26)
> merge (b:Test {name:"b"}) 
> merge (b)-\[:CONNECTED\_TO\]-\>(t1)
> \`\`\`
> 3. Find jaccard similarity between nodes \`a\` and \`b\`:
> \`\`\`
> match (a:Test {name:"a"})-\[ie:CONNECTED\_TO\]-\>(t:Test)
> with \[{item:id(a), categories: collect(distinct id(t))}\] as source\_data, collect(distinct id(a)) as source\_id
> match (b:Test {name:"b"})-\[ie:CONNECTED\_TO\]-\>(t:Test)
> with \[{item:id(b), categories: collect(distinct id(t))}\] as target\_data, source\_data, source\_id, collect(distinct id(b)) as target\_id
> CALL algo.similarity.jaccard(source\_data+target\_data, {similarityCutoff:0.01, sourceIds:source\_id , targetIds: target\_id, write:true, writeRelationshipType:'SIMILAR', writeProperty: 'jaccardSimilarity'})
> YIELD nodes, similarityPairs, write, writeRelationshipType, writeProperty, min, max, mean, stdDev, p25, p50, p75, p90, p95, p99, p999, p100
> return nodes, similarityPairs, write, writeRelationshipType, writeProperty, min, max, mean, stdDev, p25, p50, p75, p90, p95, p99, p999, p100
> \`\`\`
> 
> \*\*Expected behaviour:\*\*
> Returns jaccard similarity between the two nodes and add an edge of type \`SIMILAR\` between the nodes
> 
> \*\*Actual behaviour\*\*
> Throws an exception and shows the following error message:
> \`Failed to invoke procedure algo.similarity.jaccard: Caused by: java.lang.ArithmeticException: / by zero\`
> 
> \*\*Further explanation\*\*
> I noticed that if I set the \`similarityCutoff\` threshold to zero or \`write\` to False, it works. Or if I use \`algo.similarity.jaccard.stream\`, it will still work. It looks like when it needs to add the a new edge to the graph then it fails. 
> If I use version 3.5.11 of the graph algorithms library, it seems to work.
> 
> \* As a side note, I noticed that if I set the \`similarityCutoff\` to zero, it never writes the result back to the graph. This is not documented in neo4j manual. In fact there are some examples in the manual for \`algo.similarity.jaccard.stream\` where \`similarityCutoff:0.0\` which makes you to think that you can do the same with \`algo.similarity.jaccard\`

---

<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 19, 2019, 3:38pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/4 "2019-12-19T15:38:46Z")

</div>

@michael.hunger Are `algo.nodeSimilarity` and `algo.similarity.jaccard` using the same libraries behind the scene? I am thinking maybe if I use `algo.nodeSimilarity` instead of `algo.similarity.jaccard` it may not give me that ArithmeticException anymore.

---

<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 20, 2019, 9:38am UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/5 "2019-12-20T09:38:04Z")

</div>

Hi Shan!

We're in the process of moving the labs code into a product supported library, which should be released in the next month or two. We're deprecating Jaccard in favor of `nodeSimilarity` which uses the Jaccard similarity scoring function, but is a much more performant implementation 🙂

Look for open sourced code in the next few weeks as we get ready for a major release - I'll post on the forums as soon as it's available!

---

<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 20, 2019, 3:34pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/6 "2019-12-20T15:34:37Z")

</div>

Hi Alicia,

Thanks for your reply. I am glad to hear lab graph algorithms are going to be officially supported. Thanks for letting us know.  
Looking forward to the release.

Seyed

---

<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: [January 21, 2020, 6:59pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/7 "2020-01-21T18:59:38Z")

</div>

Hi Alicia,

I noticed that `nodeSimilarity` does not support `sourceId` and `targetId` whereas `jaccardSimilarity` does. Is there any workaround for that?

Thanks,  
Seyed

---

<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: [January 22, 2020, 7:04pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/8 "2020-01-22T19:04:40Z")

</div>

@shan - when using a cypher projection? The syntax is source/target, eg:

```auto
CALL algo.nodeSimilarity.stream(
     'MATCH(n) WHERE n:Person OR n:ItemType RETURN id(n) as id', 
     'MATCH (p:Person)-[:PURCHASED]->(e:Item)-[:INSTANCE_OF]->(m:ItemType) RETURN id(n) as source, id(m) as target',
{graph:'cypher', direction:'outgoing'})

```

If you're looking for something equivalent to the `sourceIds` and `targetId` parameters, where you could pass a vector specifying which you want to compare, we don't explicitly support that input in nodeSimilarity. You'll want to specify the node labels for source and target either directly or via the cypher loader.

Hope that helps!

---

<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: [January 23, 2020, 3:50pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/9 "2020-01-23T15:50:10Z")

</div>

Thanks very much @alicia.frame.  
Yes I am using cypher projection and I meant `sourceIds` and `targetIds`.  
Just as a feedback, the good thing about having those parameters is that sometimes you have a graph, you find similarity between nodes, then add some new nodes/edges to your graph, and now you want to only calculate similarity between the newly added nodes and the old ones. Recalculating all those similarities every time a new node is added to the graph could be inefficient if you have a large graph.

As another difference between the new `nodeSimilarity` and the old `jaccardSimilarity`, the former adds two edges between every pair of nodes (`a-->b` and `a<--b`) whereas the latter was smart enough to just add one edge. Adding two same similarity edges with the same score that are different only in their directions does not carry that much information.

---

<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: [January 24, 2020, 5:21pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/10 "2020-01-24T17:21:36Z")

</div>

@shan - thanks for the feedback! I've added it to our backlog so we keep track of it when we talk about enhancements 🙂

WRT your first question, we _just_ open sourced the code for the graph data science library, ahead of our preview release in February: [GitHub - neo4j/graph-data-science: Source code for the Neo4j Graph Data Science library of graph algorithms.](https://github.com/neo4j/graph-data-science). It's still a work in progress, but if you want to see the underlying code or open issues etc, this will be the place for it.

---

<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: [January 24, 2020, 6:30pm UTC](https://community.neo4j.com/t/graph-algorithms-3-5-13-source-code/12990/11 "2020-01-24T18:30:40Z")

</div>

That's awesome. Thanks a lot @alicia.frame  
Looking forward to the its official release 🙂
