|
| 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 | +} |
0 commit comments