Skip to content

Commit dd0c8b1

Browse files
author
Mark Needham
committed
moar notebooks
1 parent 47ab43e commit dd0c8b1

9 files changed

+688
-68
lines changed

generate_notebooks.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ python generate_notebook.py \
3333
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/scripts/connected-components.cypher" \
3434
"weighted-stream-sample-graph"
3535

36+
python generate_notebook.py \
37+
"Louvain" \
38+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/louvain.adoc" \
39+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/scripts/louvain.cypher"
40+
41+
python generate_notebook.py \
42+
"Single Source Shortest Path" \
43+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/single-shortest-path.adoc" \
44+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/scripts/single-shortest-path.cypher" \
45+
"single-pair-stream-sample-graph"
46+
47+
python generate_notebook.py \
48+
"All Pairs Shortest Path" \
49+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/all-pairs-shortest-path.adoc" \
50+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/scripts/single-shortest-path.cypher" \
51+
"all-pairs-sample-graph"
52+
53+
python generate_notebook.py \
54+
"Triangle Counting" \
55+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/blob/3.2/doc/asciidoc/triangleCount.adoc" \
56+
"https://github.com/neo4j-contrib/neo4j-graph-algorithms/raw/3.2/doc/asciidoc/scripts/triangle-count.cypher" \
57+
"stream-triples"
58+
3659

3760
python empty.py
3861
jupyter nbconvert --execute --inplace notebooks/PageRank.ipynb
@@ -51,3 +74,15 @@ jupyter nbconvert --execute --inplace notebooks/UnweightedConnectedComponents.ip
5174

5275
python empty.py
5376
jupyter nbconvert --execute --inplace notebooks/WeightedConnectedComponents.ipynb
77+
78+
python empty.py
79+
jupyter nbconvert --execute --inplace notebooks/Louvain.ipynb
80+
81+
python empty.py
82+
jupyter nbconvert --execute --inplace notebooks/SingleSourceShortestPath.ipynb
83+
84+
python empty.py
85+
jupyter nbconvert --execute --inplace notebooks/AllPairsShortestPath.ipynb
86+
87+
python empty.py
88+
jupyter nbconvert --execute --inplace notebooks/TriangleCounting.ipynb
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# All Pairs Shortest Path\n",
8+
"_All Pairs Shortest Path_ calculates the shortest weight path between all pairs of nodes.\n",
9+
"An algorithm to solve this is Floyd Warshall or Parallel Johnson's algorithm.\n"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 1,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from neo4j.v1 import GraphDatabase, basic_auth\n",
19+
"import pandas as pd\n",
20+
"import os"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 2,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"host = os.environ.get(\"NEO4J_HOST\", \"bolt://localhost\") \n",
30+
"user = os.environ.get(\"NEO4J_USER\", \"neo4j\")\n",
31+
"password = os.environ.get(\"NEO4J_PASSWORD\", \"neo\")\n",
32+
"driver = GraphDatabase.driver(host, auth=basic_auth(user, password))"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 3,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"name": "stdout",
42+
"output_type": "stream",
43+
"text": [
44+
"Stats: {'labels-added': 6, 'relationships-created': 11, 'nodes-created': 6, 'properties-set': 17}\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"create_graph_query = '''\n",
50+
"CREATE (a:Loc{name:'A'}), (b:Loc{name:'B'}), (c:Loc{name:'C'}), \n",
51+
" (d:Loc{name:'D'}), (e:Loc{name:'E'}), (f:Loc{name:'F'}),\n",
52+
" (a)-[:ROAD {cost:50}]->(b),\n",
53+
" (a)-[:ROAD {cost:50}]->(c),\n",
54+
" (a)-[:ROAD {cost:100}]->(d),\n",
55+
" (a)-[:RAIL {cost:50}]->(d),\n",
56+
" (b)-[:ROAD {cost:40}]->(d),\n",
57+
" (c)-[:ROAD {cost:40}]->(d),\n",
58+
" (c)-[:ROAD {cost:80}]->(e),\n",
59+
" (d)-[:ROAD {cost:30}]->(e),\n",
60+
" (d)-[:ROAD {cost:80}]->(f),\n",
61+
" (e)-[:ROAD {cost:40}]->(f),\n",
62+
" (e)-[:RAIL {cost:20}]->(f);\n",
63+
"'''\n",
64+
"\n",
65+
"with driver.session() as session:\n",
66+
" result = session.write_transaction(lambda tx: tx.run(create_graph_query))\n",
67+
" print(\"Stats: \" + str(result.consume().metadata.get(\"stats\", {})))"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 4,
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"data": {
77+
"text/html": [
78+
"<div>\n",
79+
"<style scoped>\n",
80+
" .dataframe tbody tr th:only-of-type {\n",
81+
" vertical-align: middle;\n",
82+
" }\n",
83+
"\n",
84+
" .dataframe tbody tr th {\n",
85+
" vertical-align: top;\n",
86+
" }\n",
87+
"\n",
88+
" .dataframe thead th {\n",
89+
" text-align: right;\n",
90+
" }\n",
91+
"</style>\n",
92+
"<table border=\"1\" class=\"dataframe\">\n",
93+
" <thead>\n",
94+
" <tr style=\"text-align: right;\">\n",
95+
" <th></th>\n",
96+
" <th>sourceNodeId</th>\n",
97+
" <th>targetNodeId</th>\n",
98+
" <th>distance</th>\n",
99+
" </tr>\n",
100+
" </thead>\n",
101+
" <tbody>\n",
102+
" <tr>\n",
103+
" <th>0</th>\n",
104+
" <td>134</td>\n",
105+
" <td>134</td>\n",
106+
" <td>0.0</td>\n",
107+
" </tr>\n",
108+
" <tr>\n",
109+
" <th>1</th>\n",
110+
" <td>16</td>\n",
111+
" <td>16</td>\n",
112+
" <td>0.0</td>\n",
113+
" </tr>\n",
114+
" <tr>\n",
115+
" <th>2</th>\n",
116+
" <td>177</td>\n",
117+
" <td>134</td>\n",
118+
" <td>110.0</td>\n",
119+
" </tr>\n",
120+
" <tr>\n",
121+
" <th>3</th>\n",
122+
" <td>177</td>\n",
123+
" <td>177</td>\n",
124+
" <td>0.0</td>\n",
125+
" </tr>\n",
126+
" <tr>\n",
127+
" <th>4</th>\n",
128+
" <td>177</td>\n",
129+
" <td>178</td>\n",
130+
" <td>40.0</td>\n",
131+
" </tr>\n",
132+
" <tr>\n",
133+
" <th>5</th>\n",
134+
" <td>177</td>\n",
135+
" <td>179</td>\n",
136+
" <td>70.0</td>\n",
137+
" </tr>\n",
138+
" <tr>\n",
139+
" <th>6</th>\n",
140+
" <td>16</td>\n",
141+
" <td>134</td>\n",
142+
" <td>110.0</td>\n",
143+
" </tr>\n",
144+
" <tr>\n",
145+
" <th>7</th>\n",
146+
" <td>16</td>\n",
147+
" <td>178</td>\n",
148+
" <td>40.0</td>\n",
149+
" </tr>\n",
150+
" <tr>\n",
151+
" <th>8</th>\n",
152+
" <td>16</td>\n",
153+
" <td>179</td>\n",
154+
" <td>70.0</td>\n",
155+
" </tr>\n",
156+
" <tr>\n",
157+
" <th>9</th>\n",
158+
" <td>15</td>\n",
159+
" <td>15</td>\n",
160+
" <td>0.0</td>\n",
161+
" </tr>\n",
162+
" <tr>\n",
163+
" <th>10</th>\n",
164+
" <td>15</td>\n",
165+
" <td>16</td>\n",
166+
" <td>50.0</td>\n",
167+
" </tr>\n",
168+
" <tr>\n",
169+
" <th>11</th>\n",
170+
" <td>179</td>\n",
171+
" <td>134</td>\n",
172+
" <td>40.0</td>\n",
173+
" </tr>\n",
174+
" <tr>\n",
175+
" <th>12</th>\n",
176+
" <td>15</td>\n",
177+
" <td>134</td>\n",
178+
" <td>120.0</td>\n",
179+
" </tr>\n",
180+
" <tr>\n",
181+
" <th>13</th>\n",
182+
" <td>15</td>\n",
183+
" <td>177</td>\n",
184+
" <td>50.0</td>\n",
185+
" </tr>\n",
186+
" <tr>\n",
187+
" <th>14</th>\n",
188+
" <td>15</td>\n",
189+
" <td>178</td>\n",
190+
" <td>50.0</td>\n",
191+
" </tr>\n",
192+
" <tr>\n",
193+
" <th>15</th>\n",
194+
" <td>179</td>\n",
195+
" <td>179</td>\n",
196+
" <td>0.0</td>\n",
197+
" </tr>\n",
198+
" <tr>\n",
199+
" <th>16</th>\n",
200+
" <td>15</td>\n",
201+
" <td>179</td>\n",
202+
" <td>80.0</td>\n",
203+
" </tr>\n",
204+
" <tr>\n",
205+
" <th>17</th>\n",
206+
" <td>178</td>\n",
207+
" <td>134</td>\n",
208+
" <td>70.0</td>\n",
209+
" </tr>\n",
210+
" <tr>\n",
211+
" <th>18</th>\n",
212+
" <td>178</td>\n",
213+
" <td>178</td>\n",
214+
" <td>0.0</td>\n",
215+
" </tr>\n",
216+
" <tr>\n",
217+
" <th>19</th>\n",
218+
" <td>178</td>\n",
219+
" <td>179</td>\n",
220+
" <td>30.0</td>\n",
221+
" </tr>\n",
222+
" </tbody>\n",
223+
"</table>\n",
224+
"</div>"
225+
],
226+
"text/plain": [
227+
" sourceNodeId targetNodeId distance\n",
228+
"0 134 134 0.0\n",
229+
"1 16 16 0.0\n",
230+
"2 177 134 110.0\n",
231+
"3 177 177 0.0\n",
232+
"4 177 178 40.0\n",
233+
"5 177 179 70.0\n",
234+
"6 16 134 110.0\n",
235+
"7 16 178 40.0\n",
236+
"8 16 179 70.0\n",
237+
"9 15 15 0.0\n",
238+
"10 15 16 50.0\n",
239+
"11 179 134 40.0\n",
240+
"12 15 134 120.0\n",
241+
"13 15 177 50.0\n",
242+
"14 15 178 50.0\n",
243+
"15 179 179 0.0\n",
244+
"16 15 179 80.0\n",
245+
"17 178 134 70.0\n",
246+
"18 178 178 0.0\n",
247+
"19 178 179 30.0"
248+
]
249+
},
250+
"execution_count": 4,
251+
"metadata": {},
252+
"output_type": "execute_result"
253+
}
254+
],
255+
"source": [
256+
"streaming_query = \"\"\"\n",
257+
"CALL algo.allShortestPaths.stream('cost',{nodeQuery:'Loc',defaultValue:1.0})\n",
258+
"YIELD sourceNodeId, targetNodeId, distance\n",
259+
"WITH sourceNodeId, targetNodeId, distance \n",
260+
"WHERE algo.isFinite(distance) = true\n",
261+
"RETURN sourceNodeId, targetNodeId, distance LIMIT 20\n",
262+
"\"\"\"\n",
263+
"\n",
264+
"with driver.session() as session:\n",
265+
" result = session.read_transaction(lambda tx: tx.run(streaming_query)) \n",
266+
" df = pd.DataFrame([r.values() for r in result], columns=result.keys())\n",
267+
"\n",
268+
"df"
269+
]
270+
}
271+
],
272+
"metadata": {},
273+
"nbformat": 4,
274+
"nbformat_minor": 2
275+
}

notebooks/BetweennessCentrality.ipynb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,32 @@
102102
" <tbody>\n",
103103
" <tr>\n",
104104
" <th>0</th>\n",
105-
" <td>19</td>\n",
105+
" <td>81</td>\n",
106106
" <td>4.0</td>\n",
107107
" </tr>\n",
108108
" <tr>\n",
109109
" <th>1</th>\n",
110-
" <td>109</td>\n",
110+
" <td>83</td>\n",
111111
" <td>2.0</td>\n",
112112
" </tr>\n",
113113
" <tr>\n",
114114
" <th>2</th>\n",
115-
" <td>108</td>\n",
115+
" <td>82</td>\n",
116116
" <td>0.0</td>\n",
117117
" </tr>\n",
118118
" <tr>\n",
119119
" <th>3</th>\n",
120-
" <td>128</td>\n",
120+
" <td>84</td>\n",
121121
" <td>0.0</td>\n",
122122
" </tr>\n",
123123
" <tr>\n",
124124
" <th>4</th>\n",
125-
" <td>129</td>\n",
125+
" <td>85</td>\n",
126126
" <td>0.0</td>\n",
127127
" </tr>\n",
128128
" <tr>\n",
129129
" <th>5</th>\n",
130-
" <td>130</td>\n",
130+
" <td>86</td>\n",
131131
" <td>0.0</td>\n",
132132
" </tr>\n",
133133
" </tbody>\n",
@@ -136,12 +136,12 @@
136136
],
137137
"text/plain": [
138138
" nodeId centrality\n",
139-
"0 19 4.0\n",
140-
"1 109 2.0\n",
141-
"2 108 0.0\n",
142-
"3 128 0.0\n",
143-
"4 129 0.0\n",
144-
"5 130 0.0"
139+
"0 81 4.0\n",
140+
"1 83 2.0\n",
141+
"2 82 0.0\n",
142+
"3 84 0.0\n",
143+
"4 85 0.0\n",
144+
"5 86 0.0"
145145
]
146146
},
147147
"execution_count": 4,

0 commit comments

Comments
 (0)