如何在neo4j中找到两个图之间的相似性

时间:2019-04-15 12:33:44

标签: neo4j cypher

在下面的图中,A,B,C,D,E,G,H,I,J,K是主图,L,M,N,O,P,Q,R是子图。首先,我尝试使子图存在于主图中,因此,得到了子图,例如D,E,G,H。现在我想在L,M,N,O,P,Q,R之间应用余弦相似度图和D,E,G,H。

Two Graphs comparison in Neo4j

Code for creating the graph :
MERGE (a:Node {name:'A', score:1})
MERGE (b:Node {name:'B', score:2})
MERGE (c:Node {name:'C', score:3})
MERGE (d:Code {name:'D', score:4}) 
MERGE (e:Code {name:'E', score:5}) 
MERGE (g:Code {name:'G', score:7}) 
MERGE (h:Code {name:'H', score:8}) 
MERGE (i:Node {name:'I', score:9})
MERGE (j:Node {name:'J', score:10})
MERGE (k:Node {name:'K', score:11})

MERGE (a)-[:Connects {score:1}]->(b)
MERGE (a)-[:Connects {score:2}]->(c)
MERGE (a)-[:Connects {score:3}]->(d)
MERGE (b)-[:Connects {score:4}]->(c)
MERGE (b)-[:Connects {score:5}]->(d)
MERGE (b)-[:Connects {score:6}]->(j)
MERGE (c)-[:Connects {score:7}]->(d)
MERGE (c)-[:Connects {score:8}]->(e)
MERGE (d)-[:Connects {score:10}]->(g)
MERGE (d)-[:Connects {score:11}]->(h)
MERGE (e)-[:Connects {score:14}]->(g)
MERGE (e)-[:Connects {score:15}]->(h)
MERGE (g)-[:Connects {score:20}]->(h)
MERGE (g)-[:Connects {score:21}]->(i)
MERGE (g)-[:Connects {score:22}]->(j)
MERGE (i)-[:Connects {score:23}]->(j)
MERGE (i)-[:Connects {score:24}]->(k)
MERGE (j)-[:Connects {score:25}]->(k)

CREATE (l:Test {name:'L', score:4})
CREATE (m:Test {name:'M', score:5})
CREATE (n:Test {name:'N', score:6})
CREATE (o:Test {name:'O', score:7})
CREATE (p:Test {name:'P', score:8})
CREATE (q:Test {name:'Q', score:12})
CREATE (r:Test {name:'R', score:13})

CREATE (l)-[:Connects {score:10}]->(o)
CREATE (l)-[:Connects {score:11}]->(p)
CREATE (l)-[:Connects {score:12}]->(n)
CREATE (m)-[:Connects {score:13}]->(n)
CREATE (m)-[:Connects {score:14}]->(o)
CREATE (m)-[:Connects {score:15}]->(p)
CREATE (n)-[:Connects {score:16}]->(o)
CREATE (n)-[:Connects {score:17}]->(p)
CREATE (n)-[:Connects {score:26}]->(q)
CREATE (n)-[:Connects {score:27}]->(r)
CREATE (o)-[:Connects {score:20}]->(p)

我是neo4j密码查询的新手,请建议如何在图(L,M,N,O,P,Q,R图和D,E,G,H)之间应用余弦相似度。

要获得我在密码查询下面使用的子图:

MATCH (n) where n.score IN [4,5,6,7,8,12,13] AND NONE(l IN labels(n) WHERE l=~'Tes.*')
MATCH path = (n)-[l]->(m) where m.score IN [4,5,6,7,8,12,13]
UNWIND nodes(path) as node
RETURN  node

我得到了下面的子图

Sub graph from main graph

请建议如何在图(L,M,N,O,P,Q,R图和D,E,G,H)之间应用余弦相似度。

1 个答案:

答案 0 :(得分:0)

您几乎可以得到余弦相似度。您只需要收集不同的分数即可。供参考,也请访问此网站。 https://neo4j.com/docs/graph-algorithms/current/algorithms/similarity-cosine/

MATCH (n) where n.score IN [4,5,6,7,8,12,13] AND NONE(l IN labels(n) WHERE l=~'Tes.*')
MATCH (t: Test)
MATCH path = (n)-[l]->(m) where m.score IN [4,5,6,7,8,12) AS similarity,13]
UNWIND nodes(path) as node
RETURN  algo.similarity.cosine(
        collect(node.score), collect(m.score)) AS similarity

结果:

similarity
0.9733970633316996