%%bash rm -rf plugins mkdir plugins cd plugins echo "Downloading APOC plugin" wget https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/3.3.0.1/apoc-3.3.0.1-all.jar echo "Downloading algorithms plugin" wget https://github.com/neo4j-contrib/neo4j-graph-algorithms/releases/download/3.3.2.0/graph-algorithms-algo-3.3.2.0.jar cd .. %%bash docker run \ -d --name myneo4j \ --publish=7474:7474 \ --publish=7687:7687 \ --volume=$(pwd)/plugins:/plugins \ --env NEO4J_AUTH=neo4j/class \ --env=NEO4J_dbms_security_procedures_unrestricted=apoc.\\\*,algo.\\\* \ neo4j CREATE (a:Loc{name:'A'}), (b:Loc{name:'B'}), (c:Loc{name:'C'}), (d:Loc{name:'D'}), (e:Loc{name:'E'}), (f:Loc{name:'F'}), (a)-[:ROAD {cost:50}]->(b), (a)-[:ROAD {cost:50}]->(c), (a)-[:ROAD {cost:100}]->(d), (a)-[:RAIL {cost:50}]->(d), (b)-[:ROAD {cost:40}]->(d), (c)-[:ROAD {cost:40}]->(d), (c)-[:ROAD {cost:80}]->(e), (d)-[:ROAD {cost:30}]->(e), (d)-[:ROAD {cost:80}]->(f), (e)-[:ROAD {cost:40}]->(f), (e)-[:RAIL {cost:20}]->(f); MATCH (n) RETURN labels(n) AS labels, COUNT(*) AS count ORDER BY count DESC MATCH (n:Loc)-[r]->(m:Loc) RETURN n,r,m; CALL algo.allShortestPaths.stream('cost',{nodeQuery:'Loc',defaultValue:1.0}) YIELD sourceNodeId, targetNodeId, distance WITH sourceNodeId, targetNodeId, distance WHERE algo.isFinite(distance) = true MATCH (source:Loc) WHERE id(source) = sourceNodeId MATCH (target:Loc) WHERE id(target) = targetNodeId WITH source, target, distance WHERE source <> target RETURN source.name AS source, target.name AS target, distance ORDER BY distance DESC LIMIT 10 %%python df MATCH (start:Loc{name:'A'}), (end:Loc{name:'F'}) CALL algo.shortestPath.stream(start, end, 'cost') YIELD nodeId, cost MATCH (other:Loc) WHERE id(other) = nodeId RETURN other.name AS name, cost MERGE (nAlice:User {id:'Alice'}) MERGE (nBridget:User {id:'Bridget'}) MERGE (nCharles:User {id:'Charles'}) MERGE (nDoug:User {id:'Doug'}) MERGE (nMark:User {id:'Mark'}) MERGE (nMichael:User {id:'Michael'}) MERGE (nAlice)-[:FOLLOWS]->(nDoug) MERGE (nAlice)-[:FOLLOWS]->(nBridget) MERGE (nAlice)-[:FOLLOWS]->(nCharles) MERGE (nMark)-[:FOLLOWS]->(nDoug) MERGE (nMark)-[:FOLLOWS]->(nMichael) MERGE (nBridget)-[:FOLLOWS]->(nDoug) MERGE (nCharles)-[:FOLLOWS]->(nDoug) MERGE (nMichael)-[:FOLLOWS]->(nDoug) MATCH (n:User)-[f:FOLLOWS]->(m:User) RETURN n, f, m MATCH (u:User) RETURN u.id AS name, size((u)-[:FOLLOWS]->()) AS follows, size((u)<-[:FOLLOWS]-()) AS followers MATCH (u:User) set u.followers = size((u)<-[:FOLLOWS]-()) MATCH (n:User)-[f:FOLLOWS]->(m:User) RETURN n, f, m MERGE (a:Node{id:"A"}) MERGE (b:Node{id:"B"}) MERGE (c:Node{id:"C"}) MERGE (d:Node{id:"D"}) MERGE (e:Node{id:"E"}) MERGE (a)-[:LINK]->(b) MERGE (b)-[:LINK]->(a) MERGE (b)-[:LINK]->(c) MERGE (c)-[:LINK]->(b) MERGE (c)-[:LINK]->(d) MERGE (d)-[:LINK]->(c) MERGE (d)-[:LINK]->(e) MERGE (e)-[:LINK]->(d) MATCH (n:Node)-[f:LINK]->(m:Node) RETURN n, f, m CALL algo.closeness.stream('Node', 'LINK') YIELD nodeId, centrality MATCH (n:Node) WHERE id(n) = nodeId RETURN n.id AS node, centrality ORDER BY centrality DESC limit 20; MERGE (nAlice:User {id:'Alice'}) MERGE (nBridget:User {id:'Bridget'}) MERGE (nCharles:User {id:'Charles'}) MERGE (nDoug:User {id:'Doug'}) MERGE (nMark:User {id:'Mark'}) MERGE (nMichael:User {id:'Michael'}) MERGE (nAlice)-[:MANAGE]->(nBridget) MERGE (nAlice)-[:MANAGE]->(nCharles) MERGE (nAlice)-[:MANAGE]->(nDoug) MERGE (nMark)-[:MANAGE]->(nAlice) MERGE (nCharles)-[:MANAGE]->(nMichael) MATCH (n:User)-[f:MANAGE]->(m:User) RETURN n, f, m CALL algo.betweenness.stream('User','MANAGE',{direction:'out'}) YIELD nodeId, centrality MATCH (user:User) WHERE id(user) = nodeId RETURN user.id AS user,centrality ORDER BY centrality DESC LIMIT 20; MERGE (nAlice:User {id:'Alice'}) MERGE (nBridget:User {id:'Bridget'}) MERGE (nCharles:User {id:'Charles'}) MERGE (nDoug:User {id:'Doug'}) MERGE (nMark:User {id:'Mark'}) MERGE (nMichael:User {id:'Michael'}) MERGE (nAlice)-[:FOLLOW]->(nBridget) MERGE (nAlice)-[:FOLLOW]->(nCharles) MERGE (nMark)-[:FOLLOW]->(nDoug) MERGE (nMark)-[:FOLLOW]->(nMichael) MERGE (nBridget)-[:FOLLOW]->(nMichael) MERGE (nDoug)-[:FOLLOW]->(nMark) MERGE (nMichael)-[:FOLLOW]->(nAlice) MERGE (nAlice)-[:FOLLOW]->(nMichael) MERGE (nBridget)-[:FOLLOW]->(nAlice) MERGE (nMichael)-[:FOLLOW]->(nBridget) MATCH (n:User)-[f:FOLLOW]->(m:User) RETURN n, f, m CALL algo.scc.stream('User','FOLLOW') YIELD nodeId, partition MATCH (u:User) WHERE id(u) = nodeId RETURN u.id AS name, partition %%python df CALL algo.scc('User','FOLLOW', {write:true,partitionProperty:'partition'}) YIELD loadMillis, computeMillis, writeMillis, setCount, maxSetSize, minSetSize; MERGE (nAlice:User {id:'Alice'}) MERGE (nBridget:User {id:'Bridget'}) MERGE (nCharles:User {id:'Charles'}) MERGE (nDoug:User {id:'Doug'}) MERGE (nMark:User {id:'Mark'}) MERGE (nMichael:User {id:'Michael'}) MERGE (nAlice)-[:FRIEND {weight:0.5}]->(nBridget) MERGE (nAlice)-[:FRIEND {weight:4}]->(nCharles) MERGE (nMark)-[:FRIEND {weight:1}]->(nDoug) MERGE (nMark)-[:FRIEND {weight:2}]->(nMichael); MATCH (n:User)-[f:FRIEND]->(m:User) RETURN n, f, m CALL algo.unionFind.stream('User', 'FRIEND', {weightProperty:'weight', defaultValue:0.0, threshold:1.0, concurrency: 1}) YIELD nodeId,setId MATCH (u:User) WHERE id(u) = nodeId RETURN u.id AS user, setId CALL algo.unionFind('User', 'FRIEND', {write:true, partitionProperty:"partition",weightProperty:'weight', defaultValue:0.0, threshold:1.0, concurrency: 1}) YIELD nodes, setCount, loadMillis, computeMillis, writeMillis; MERGE (nAlice:User {id:'Alice'}) MERGE (nBridget:User {id:'Bridget'}) MERGE (nCharles:User {id:'Charles'}) MERGE (nDoug:User {id:'Doug'}) MERGE (nMark:User {id:'Mark'}) MERGE (nMichael:User {id:'Michael'}) MERGE (nAlice)-[:FRIEND {weight:0.5}]->(nBridget) MERGE (nAlice)-[:FRIEND {weight:4}]->(nCharles) MERGE (nMark)-[:FRIEND {weight:1}]->(nDoug) MERGE (nMark)-[:FRIEND {weight:2}]->(nMichael); MATCH (n:User)-[f:FRIEND]->(m:User) RETURN n, f, m CALL algo.unionFind.stream('User', 'FRIEND', {}) YIELD nodeId,setId MATCH (u:User) WHERE id(u) = nodeId RETURN u.id AS user, setId CALL algo.unionFind('User', 'FRIEND', {write:true, partitionProperty:"partition"}) YIELD nodes, setCount, loadMillis, computeMillis, writeMillis; MERGE (nAlice:User {id:'Alice'}) SET nAlice.predefined_label=52 MERGE (nBridget:User {id:'Bridget'}) SET nBridget.predefined_label=21 MERGE (nCharles:User {id:'Charles'}) SET nCharles.predefined_label=43 MERGE (nDoug:User {id:'Doug'}) SET nDoug.predefined_label=21 MERGE (nMark:User {id:'Mark'}) SET nMark.predefined_label=19 MERGE (nMichael:User {id:'Michael'}) SET nMichael.predefined_label=52 MERGE (nAlice)-[:FOLLOW]->(nBridget) MERGE (nAlice)-[:FOLLOW]->(nCharles) MERGE (nMark)-[:FOLLOW]->(nDoug) MERGE (nBridget)-[:FOLLOW]->(nMichael) MERGE (nDoug)-[:FOLLOW]->(nMark) MERGE (nMichael)-[:FOLLOW]->(nAlice) MERGE (nAlice)-[:FOLLOW]->(nMichael) MERGE (nBridget)-[:FOLLOW]->(nAlice) MERGE (nMichael)-[:FOLLOW]->(nBridget) MERGE (nCharles)-[:FOLLOW]->(nDoug) MATCH (n:User)-[f:FOLLOW]->(m:User) RETURN n, f, m CALL algo.labelPropagation.stream("User", "FOLLOW", {direction: "OUTGOING", iterations: 10}) CALL algo.labelPropagation('User', 'FOLLOW','OUTGOING', {iterations:10,partitionProperty:'partition', write:true}) YIELD nodes, iterations, loadMillis, computeMillis, writeMillis, write, partitionProperty; MERGE (nAlice:User {id:'Alice'}) MERGE (nBridget:User {id:'Bridget'}) MERGE (nCharles:User {id:'Charles'}) MERGE (nDoug:User {id:'Doug'}) MERGE (nMark:User {id:'Mark'}) MERGE (nMichael:User {id:'Michael'}) MERGE (nAlice)-[:FRIEND]->(nBridget) MERGE (nAlice)-[:FRIEND]->(nCharles) MERGE (nMark)-[:FRIEND]->(nDoug) MERGE (nBridget)-[:FRIEND]->(nMichael) MERGE (nCharles)-[:FRIEND]->(nMark) MERGE (nAlice)-[:FRIEND]->(nMichael) MERGE (nCharles)-[:FRIEND]->(nDoug) MATCH (n:User)-[f:FRIEND]->(m:User) RETURN n, f, m CALL algo.louvain.stream('User', 'FRIEND', {}) YIELD nodeId, community MATCH (user:User) WHERE id(user) = nodeId RETURN user.id AS user, community ORDER BY community; CALL algo.louvain('User', 'FRIEND', {write:true, writeProperty:'community'}) YIELD nodes, communityCount, iterations, loadMillis, computeMillis, writeMillis CREATE (alice:Person{id:"Alice"}), (michael:Person{id:"Michael"}), (karin:Person{id:"Karin"}), (chris:Person{id:"Chris"}), (will:Person{id:"Will"}), (mark:Person{id:"Mark"}) CREATE (michael)-[:KNOWS]->(karin), (michael)-[:KNOWS]->(chris), (will)-[:KNOWS]->(michael), (mark)-[:KNOWS]->(michael), (mark)-[:KNOWS]->(will), (alice)-[:KNOWS]->(michael), (will)-[:KNOWS]->(chris), (chris)-[:KNOWS]->(karin); MATCH (n:Person)-[f:KNOWS]->(m:Person) RETURN n, f, m CALL algo.triangle.stream('Person','KNOWS') yield nodeA,nodeB,nodeC MATCH (a:Person) WHERE id(a) = nodeA MATCH (b:Person) WHERE id(b) = nodeB MATCH (c:Person) WHERE id(c) = nodeC RETURN a.id AS nodeA, b.id AS nodeB, c.id AS nodeC MATCH (n) DETACH DELETE n %%bash docker stop myneo4j docker rm myneo4j