Graph Analytics with Neo4j

Neo4j is free and very powerful graph analytics tool. Let’s learn to work with data sets, which will load into Neo4j and can then be analyzed. Neo4j is capable of processing and analyzing extremely complex graph networks consisting of millions of nodes and relationships.  Cypher is the query language used in Neo4j.

Downloading, Installing, and Running Neo4j

Download Neo4j at: http://www.neo4j.com/

Useful Links

Getting Started With Neo4j

neo4j1

Create a Toy Network with five nodes

  1. N1 = Tom
  2. N2 = Harry
  3. N3 = Julian
  4. N4 = Michele
  5. N5 = Josephine

and five edges:

  1. e1 = Harry ‘is known by’ Tom
  2. e2 = Julian ‘is co-worker of’ Harry
  3. e3 = Michele ‘is wife of’ Harry
  4. e4 = Josephine ‘is wife of’ Tom
  5. e5 = Josephine ‘is friend of’ Michele

View the resulting graph

neo4j2

Delete all nodes and edges

Delete all nodes which have no edges

Delete only ToyNode nodes which have no edges

Delete all edges

Delete only ToyRelation edges

Selecting an existing single ToyNode node

Counting the number of nodes

Counting the number of edges

Finding leaf nodes

Finding root nodes

Finding triangles:

Finding 2nd neighbors of D

Finding the types of a node

Finding the label of an edge:

Finding all properties of a node:

Finding loops:

Finding multigraphs:

Finding the induced subgraph given a set of nodes:

Importing Data Into Neo4j

One way to “clean the slate” in Neo4j before importing (run both lines):

Script to Import Data Set: test.csv (simple road network)

For Windows use something like the following
[NOTE: replace any spaces in your path with %20, “percent twenty” ]

Adding to and Modifying a Graph

Adding a Node Correctly

Adding a Node Incorrectly

Correct your mistake by deleting the bad nodes and edge

Modify a Node’s Information

Path Analytics with Neo4j

Viewing the graph

Finding paths between specific nodes:

Shortest path between nodes H and P

Finding the length between specific nodes

Finding a shortest path between specific nodes:

All Shortest Paths

All Shortest Paths with Path Conditions

Diameter of the graph

Extracting and computing with node and properties

Dijkstra’s algorithm for a specific target node

Dijkstra’s algorithm SSSP

Graph not containing a selected node

Shortest path over a Graph not containing a selected node

Graph not containing the immediate neighborhood of a specified node

Graph not containing a selected neighborhood

Connectivity Analytics

Viewing the graph

Find the outdegree of all nodes

Find the indegree of all nodes

Find the degree of all nodes

Find degree histogram of the graph

 

 

Save the degree of the node as a new node property

Construct the Adjacency Matrix of the graph

Construct the Normalized Laplacian Matrix of the graph

 

3 Comments

  1. Hi Ranjan,
    Am looking for possible query to carry out betweenness centrality on a selected subgraph with Cypher.

    I found an example for PageRank, still looking for betweenness…

    CALL apoc.algo.pageRankWithCypher(
    {
    iterations: 100,
    write: true,
    property: ‘pagerank_number_1′,
    node_cypher:’none’,
    rel_cypher: ‘MATCH (n)-[r]->(m) where //some criteria on the edge // return id(n) as source, id(m) as target, 1 as weight’
    }
    );

Leave a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Fork me on GitHub