Training GDS Graph Algorithms Two - tomgeudens/practical-neo4j GitHub Wiki
Context: Cut-and-paste commands for the Graph Data Science - Graph Algorithms session.
Prerequisite: This document will assume you have a Neo4j instance running and are connected to it either with the Neo4j Browser. You also need to have the Game of Thrones database loaded.
Setup
Make sure you're in the correct database
:use gameofthrones
Similarity
Simply Jaccard
RETURN gds.alpha.similarity.jaccard([1,2,3],[1,2,2,3])
Thrown bipartites in the pot
CALL gds.graph.create ('got-character-related-entities', ['Person', 'Book', 'House', 'Culture'], '*');
Cool to 20 degrees
CALL gds.nodeSimilarity.stream('got-character-related-entities',{
degreeCutoff: 20,
similarityCutoff: 0.45})
YIELD node1, node2, similarity
RETURN gds.util.asNode(node1).name as character1, gds.util.asNode(node2).name as character2, similarity
ORDER BY similarity DESC;
Degree of a node
MATCH (p:Person {name: "Gregor Clegane"})
RETURN apoc.node.degree(p), apoc.node.degree.in(p), apoc.node.degree.out(p);
Better than a LIMIT
CALL gds.nodeSimilarity.stream('got-character-related-entities',{
degreeCutoff: 20,
topN: 10,
topK: 1})
YIELD node1, node2, similarity
RETURN gds.util.asNode(node1).name as character1, gds.util.asNode(node2).name as character2, similarity
ORDER BY similarity DESC;
Clear the slate
MATCH (:Person)-[s:SIMILARITY]->(:Person) DELETE s;
Run with write
CALL gds.nodeSimilarity.write('got-character-related-entities',{
degreeCutoff: 20,topN: 10, topK: 1,
writeRelationshipType: 'SIMILARITY',
writeProperty: 'character_similarity'});
Check the result
MATCH sg=(:Person {name: "Gregor Clegane"})-[:SIMILARITY]-() RETURN sg;
Path finding
Gormon to Manfrey - All connections are equal
MATCH (start:Person{name:'Gormon Tyrell'})
MATCH (end:Person{name:'Manfrey Martell'})
CALL gds.beta.shortestPath.dijkstra.stream({
nodeProjection: 'Person',
relationshipProjection: {
INTERACTS_SEASON4: {
type: 'INTERACTS_4',
orientation: 'UNDIRECTED'}},
sourceNode: id(start),
targetNode: id(end),
path: true}) YIELD totalCost, nodeIds, path
RETURN path,nodeIds,totalCost;
Gormon to Manfrey - But some are more equal than others
MATCH (start:Person{name:'Gormon Tyrell'})
MATCH (end:Person{name:'Manfrey Martell'})
CALL gds.beta.shortestPath.dijkstra.stream({
nodeProjection: 'Person',
relationshipProjection: {
INTERACTS_SEASON4: {
type: 'INTERACTS_4',
orientation: 'UNDIRECTED'}},
relationshipProperties: 'weight',
sourceNode: id(start),
targetNode: id(end),
relationshipWeightProperty: 'weight',
path: true}) YIELD totalCost, nodeIds, path
RETURN path,nodeIds,totalCost;
Gormon to Manfrey - Yen-K Style
MATCH (start:Person{name:'Gormon Tyrell'})
MATCH (end:Person{name:'Manfrey Martell'})
CALL gds.beta.shortestPath.yens.stream({
nodeProjection: 'Person',
relationshipProjection: {
INTERACTS_SEASON4: {
type: 'INTERACTS_4',
orientation: 'UNDIRECTED'}},
relationshipProperties: 'weight',
sourceNode: id(start),
targetNode: id(end),
relationshipWeightProperty: 'weight',
k: 3,
path: true}) YIELD totalCost, nodeIds, path
RETURN path,nodeIds,totalCost;
Link Prediction
Evolution of the relationship between Jon and Daenerys
CALL db.relationshipTypes()
YIELD relationshipType as book
WITH book WHERE book STARTS WITH 'INTERACTS_'
MATCH (n1:Person{name:'Daenerys Targaryen'})
MATCH (n2:Person{name:'Jon Snow'})
RETURN
gds.alpha.linkprediction.commonNeighbors(n1, n2, {relationshipQuery:book}) AS cn_score,
gds.alpha.linkprediction.preferentialAttachment(n1, n2, {relationshipQuery:book}) AS pa_score,
gds.alpha.linkprediction.adamicAdar(n1, n2, {relationshipQuery:book}) AS aa_score,
gds.alpha.linkprediction.resourceAllocation(n1, n2, {relationshipQuery:book}) AS ra_score,
gds.alpha.linkprediction.totalNeighbors(n1, n2, {relationshipQuery:book}) AS tn_score,
book