Skip to content

Instantly share code, notes, and snippets.

@vladbatushkov
Last active July 31, 2019 15:37
Show Gist options
  • Save vladbatushkov/4587d3680f208f83ebb26e7ae6a4a56b to your computer and use it in GitHub Desktop.
Save vladbatushkov/4587d3680f208f83ebb26e7ae6a4a56b to your computer and use it in GitHub Desktop.
Apply algorithms to sub-graph of people
// Dijkstra
MATCH (p1:Person { name:'Omar' }), (p2:Person { name:'Ronin' })
CALL algo.shortestPath.stream(p1, p2, 'duration') YIELD nodeId, cost
RETURN algo.asNode(nodeId).name AS name, cost
// Dijkstra Projection
MATCH (p1:Person { name: 'Omar' }), (p2:Person { name: 'Ronin' })
CALL algo.shortestPath(p1, p2, 'cost',{
nodeQuery:'MATCH (p:Person) RETURN id(p) as id',
relationshipQuery:'MATCH (p1:Person)-[c:COMMUNICATE_WITH]->(p2:Person) RETURN id(p1) as source, id(p2) as target, c.duration as weight',
graph: 'cypher'}) YIELD writeMillis, loadMillis, nodeCount, totalCost
RETURN writeMillis, loadMillis, nodeCount as hops, totalCost as cost
// Check for isolations
MATCH (p1:Person)
//WHERE (p1)-[]->(:Call)<-[]-(:Person)
WHERE NOT (p1)-[]->(:Call)<-[]-(:Person)
RETURN COUNT(p1) as total
// Connected Components
CALL algo.unionFind.stream('Person', 'COMMUNICATE_WITH', {}) YIELD nodeId, setId
RETURN algo.asNode(nodeId).name AS person, setId
// Closeness Centrality
CALL algo.closeness.stream('Person', 'COMMUNICATE_WITH') YIELD nodeId, centrality
RETURN algo.asNode(nodeId).name AS name, centrality
ORDER BY centrality DESC
LIMIT 10
// Betweenness Centrality
CALL algo.betweenness.stream('Person', 'COMMUNICATE_WITH', { direction: 'both' }) YIELD nodeId, centrality
RETURN algo.asNode(nodeId).name AS name, centrality
ORDER BY centrality DESC
LIMIT 10
// PageRank
CALL algo.pageRank.stream('Person', 'COMMUNICATE_WITH', {
iterations: 50, dampingFactor: 0.85
}) YIELD nodeId, score
RETURN algo.asNode(nodeId).name AS name, score
ORDER BY score DESC
LIMIT 10
// PageRank Weighted
CALL algo.pageRank.stream('Person', 'COMMUNICATE_WITH', {
iterations:20, dampingFactor:0.85, weightProperty: "weight"
}) YIELD nodeId, score
RETURN algo.asNode(nodeId).name AS name, score
ORDER BY score DESC
LIMIT 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment