Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Neo4j Graph Algorithms Jupyter Notebooks
= Neo4j Graph Data Science Jupyter Notebooks

This repository contains Jupyter Notebooks for each of the https://neo4j-contrib.github.io/neo4j-graph-algorithms/[Neo4j graph algorithms^].
This repository contains Jupyter Notebooks for each of the https://neo4j.com/docs/graph-data-science/current/[Neo4j graph algorithms^].


== Path finding
Expand Down Expand Up @@ -40,10 +40,10 @@ pip install -r requirements.txt

=== Neo4j

We'll also need to have a Neo4j server, with the Graph Algorithms library installed, running locally.
We'll also need to have a Neo4j server, with the Graph Data Science library installed, running locally.
The easiest way to do this is to download the Neo4j Desktop from http://neo4j.com/download[neo4j.com/download^].

Once we've done that we can create a project and then install Graph Algorithms from the Plugins section.
Once we've done that we can create a project and then install Graph Data Science from the Plugins section.

image::images/installation.png[]

Expand Down
4 changes: 2 additions & 2 deletions empty.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os

from neo4j.v1 import GraphDatabase, basic_auth
from neo4j import GraphDatabase

host = os.environ.get("NEO4J_HOST", "bolt://localhost")
user = os.environ.get("NEO4J_USER", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "neo")
driver = GraphDatabase.driver(host, auth=basic_auth(user, password))
driver = GraphDatabase.driver(host, auth=(user, password))


def clear_db():
Expand Down
Binary file modified images/installation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 72 additions & 38 deletions notebooks/AllPairsShortestPath.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"metadata": {},
"outputs": [],
"source": [
"from neo4j.v1 import GraphDatabase, basic_auth\n",
"from neo4j import GraphDatabase\n",
"import pandas as pd\n",
"import os"
]
Expand All @@ -38,7 +38,7 @@
"host = os.environ.get(\"NEO4J_HOST\", \"bolt://localhost\") \n",
"user = os.environ.get(\"NEO4J_USER\", \"neo4j\")\n",
"password = os.environ.get(\"NEO4J_PASSWORD\", \"neo\")\n",
"driver = GraphDatabase.driver(host, auth=basic_auth(user, password))"
"driver = GraphDatabase.driver(host, auth=(user, password))"
]
},
{
Expand Down Expand Up @@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -126,54 +126,54 @@
" <th>0</th>\n",
" <td>A</td>\n",
" <td>F</td>\n",
" <td>160.0</td>\n",
" <td>100.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A</td>\n",
" <td>E</td>\n",
" <td>120.0</td>\n",
" <td>C</td>\n",
" <td>F</td>\n",
" <td>90.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>B</td>\n",
" <td>F</td>\n",
" <td>110.0</td>\n",
" <td>90.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>C</td>\n",
" <td>F</td>\n",
" <td>110.0</td>\n",
" <td>A</td>\n",
" <td>E</td>\n",
" <td>80.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>A</td>\n",
" <td>D</td>\n",
" <td>90.0</td>\n",
" <td>B</td>\n",
" <td>E</td>\n",
" <td>70.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>B</td>\n",
" <td>C</td>\n",
" <td>E</td>\n",
" <td>70.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>A</td>\n",
" <td>D</td>\n",
" <td>F</td>\n",
" <td>70.0</td>\n",
" <td>50.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>C</td>\n",
" <td>E</td>\n",
" <td>70.0</td>\n",
" <td>A</td>\n",
" <td>B</td>\n",
" <td>50.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>A</td>\n",
" <td>B</td>\n",
" <td>D</td>\n",
" <td>F</td>\n",
" <td>50.0</td>\n",
" </tr>\n",
" <tr>\n",
Expand All @@ -188,29 +188,38 @@
],
"text/plain": [
" source target distance\n",
"0 A F 160.0\n",
"1 A E 120.0\n",
"2 B F 110.0\n",
"3 C F 110.0\n",
"4 A D 90.0\n",
"5 B E 70.0\n",
"6 D F 70.0\n",
"7 C E 70.0\n",
"8 A B 50.0\n",
"0 A F 100.0\n",
"1 C F 90.0\n",
"2 B F 90.0\n",
"3 A E 80.0\n",
"4 B E 70.0\n",
"5 C E 70.0\n",
"6 A D 50.0\n",
"7 A B 50.0\n",
"8 D F 50.0\n",
"9 A C 50.0"
]
},
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"streaming_query = \"\"\"\n",
"CALL algo.allShortestPaths.stream('cost',{nodeQuery:'Loc',defaultValue:1.0})\n",
"CALL gds.alpha.allShortestPaths.stream({\n",
" nodeProjection:'Loc',\n",
" relationshipProjection:{\n",
" ALL:{\n",
" type:'*',\n",
" orientation:'NATURAL',\n",
" properties: {cost:{property:'cost', defaultValue:1.0}}\n",
" }\n",
" },\n",
" relationshipWeightProperty:'cost'})\n",
"YIELD sourceNodeId, targetNodeId, distance\n",
"WITH sourceNodeId, targetNodeId, distance \n",
"WHERE algo.isFinite(distance) = true\n",
"WHERE gds.util.isFinite(distance) = true\n",
"\n",
"MATCH (source:Loc) WHERE id(source) = sourceNodeId\n",
"MATCH (target:Loc) WHERE id(target) = targetNodeId\n",
Expand All @@ -222,8 +231,8 @@
"\"\"\"\n",
"\n",
"with driver.session() as session:\n",
" result = session.read_transaction(lambda tx: tx.run(streaming_query)) \n",
" df = pd.DataFrame([r.values() for r in result], columns=result.keys())\n",
" result = session.run(streaming_query)\n",
" df = pd.DataFrame([dict(row) for row in result])\n",
"\n",
"df"
]
Expand All @@ -233,11 +242,36 @@
"metadata": {},
"source": [
"This query returned the top 10 pairs of nodes that are the furthest away from each other.\n",
"\"F\" and \"E\" seem to be quite distant from the others."
"Node \"F\" seem to be quite distant from the others."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {},
"metadata": {
"kernelspec": {
"display_name": "scispacy",
"language": "python",
"name": "scispacy"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading