Skip to content

Instantly share code, notes, and snippets.

@jexp
Last active March 14, 2022 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jexp/e4fa7765c9836d44f54628253b119226 to your computer and use it in GitHub Desktop.
Save jexp/e4fa7765c9836d44f54628253b119226 to your computer and use it in GitHub Desktop.
pi-graph Represent the digits of π as a graph
create constraint on (p:Pair) assert p.digits is unique;
// pi digits from https://github.com/eneko/Pi (we use 10k here) in pairs of 2
load csv from "https://raw.githubusercontent.com/eneko/Pi/master/Pi10KDP.txt" as row
with row[0] as digits
unwind range(0,size(digits)-4,2) as idx
merge (from:Pair {digits:substring(digits,idx,2)})
merge (to:Pair {digits:substring(digits,idx+2,2)})
merge (from)-[:NEXT {idx:idx}]->(to);
// connect starting digits
match (to:Pair {digits:'14'})
create (from:Pair:Start {digits:'3.'})
create (from)-[:NEXT {idx:-2}]->(to);
MATCH p=(:Pair:Start {digits:'3.'})-[:NEXT]->(:Pair {digits:'14'})-[:NEXT]->(:Pair {digits:'15'})-[:NEXT]->(:Pair {digits:'92'})-[:NEXT]->(:Pair {digits:'65'})
RETURN p LIMIT 25;
// find path automatically
MATCH path = (n:Start:Pair)-[:NEXT*5]->()
WHERE all(idx in range(0,length(path)-2) where relationships(path)[idx].idx = relationships(path)[idx+1].idx-2)
RETURN path LIMIT 1;
// optimization test for path expansion
create constraint on (t:Triple2) assert t.digits is unique;
load csv from "https://raw.githubusercontent.com/eneko/Pi/master/Pi10KDP.txt" as row
with row[0] as digits
unwind range(0,size(digits)-6,3) as idx
merge (from:Triple2 {digits:substring(digits,idx,3)})
merge (to:Triple2 {digits:substring(digits,idx+3,3)})
with *
call apoc.create.relationship(from,toString(idx), {idx:idx},to) yield rel return count(*);
match (to:Triple2 {digits:'141'})
create (from:Triple2:Start {digits:'3.'})
create (from)-[:START]->(to);
with 50 as length
match (:Start:Triple2)-[:START]->(first)
call apoc.path.expandConfig(first, {minLevel:length, limit:1, relationshipFilter:apoc.text.join([x in range(0,length*3,3)|toString(x)],','), bfs:false, uniqueness:'RELATIONSHIP_GLOBAL'}) yield path
return path limit 1;
create constraint on (t:Triple) assert t.digits is unique;
load csv from "https://raw.githubusercontent.com/eneko/Pi/master/Pi10KDP.txt" as row
with row[0] as digits
unwind range(0,size(digits)-6,3) as idx
merge (from:Triple {digits:substring(digits,idx,3)})
merge (to:Triple {digits:substring(digits,idx+3,3)})
merge (from)-[:NEXT {idx:idx}]->(to);
match (to:Triple {digits:'141'})
create (from:Triple:Start {digits:'3.'})
create (from)-[:NEXT {idx:-3}]->(to);
// find concrete path
MATCH path = (n:Start:Triple)-[:NEXT*10]->()
WHERE all(idx in range(0,length(path)-2) where relationships(path)[idx].idx = relationships(path)[idx+1].idx-3)
RETURN path LIMIT 1;
// find any shortest path to a given triple
match path = shortestPath((n:Start:Triple)-[:NEXT*]->(end:Triple {digits:'999'}))
return path, length(path);
// all shortest paths to 123
match path = allShortestPaths((n:Start:Triple)-[:NEXT*]->(end:Triple {digits:'123'}))
return path, length(path) limit 20
@jexp
Copy link
Author

jexp commented Mar 14, 2022

pi-graph-chain-expanded
pi-graph-chain

@jexp
Copy link
Author

jexp commented Mar 14, 2022

pi-graph-bloom

@jexp
Copy link
Author

jexp commented Mar 14, 2022

pi-graph-bloom-triples

@jexp
Copy link
Author

jexp commented Mar 14, 2022

all-shortest-paths-to-123

@jexp
Copy link
Author

jexp commented Mar 14, 2022

Uploading pi-graph-chain-50.png…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment