Training GDS Pathfinding - tomgeudens/practical-neo4j GitHub Wiki

Context: Cut-and-paste commands for the Graph Data Science - Pathfinding algorithms session.

Prerequisite: This document will assume you have a Neo4j instance running and are connected to it with the Neo4j Browser. You also need to have the Game of Thrones database loaded.

Setup

001

// Make sure you're in the correct database
:use gameofthrones

Dijkstra

002

// It's a long long winding road
MATCH (start:Person{name:'Gormon Tyrell'}),(end:Person{name:'Manfrey Martell'})
CALL gds.shortestPath.dijkstra.stream({
  nodeProjection: 'Person',
  relationshipProjection: {
    INTERACTS_SEASON4: {type: 'INTERACTS_4', orientation: 'UNDIRECTED'}},
  sourceNode: id(start),
  targetNode: id(end)}) YIELD totalCost, nodeIds, path
RETURN path,nodeIds,totalCost;

003

// Some paths are more equal than others
MATCH (start:Person{name:'Gormon Tyrell'}),(end:Person{name:'Manfrey Martell'})
CALL gds.shortestPath.dijkstra.stream({
  nodeProjection: 'Person',
  relationshipProjection: { INTERACTS_SEASON4: { type: 'INTERACTS_4', orientation: 'UNDIRECTED'}}, relationshipProperties: 'weight',
  sourceNode: id(start), 
  targetNode: id(end),
  relationshipWeightProperty: 'weight'}) 
YIELD totalCost, nodeIds, path
RETURN path,nodeIds,totalCost;

Yen's

004

// Show me the way to go home
MATCH (start:Person{name:'Gormon Tyrell'}),(end:Person{name:'Manfrey Martell'})
CALL gds.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}) YIELD totalCost, nodeIds, path
RETURN path,nodeIds,totalCost;