MATLAB Examples

Find Shortest Path Between People in Social Neighborhood

This example shows how to search a social neighborhood to find the shortest path between people, using the MATLAB® interface to Neo4j®. Assume that you have graph data that is stored on a Neo4j® database which represents a social neighborhood. This database has seven nodes and eight relationships. Each node has only one unique property key name with values User1 through User7. Each relationship has type knows.

To find the shortest path between User1 and User7, use the MATLAB® interface to Neo4j® and the docid:matlab_ref.bun70tf object. For details about the MATLAB® interface to Neo4j®, see docid:database_ug.bveys16-1.

The local machine hosts the Neo4j® database with port number 7474, user name neo4j, and password matlab. For a visual representation of the data in the database, see this figure.

Contents

Connect to Neo4j® Database

Create a Neo4j® connection object neo4jconn using the URL http://localhost:7474/db/data, user name neo4j, and password matlab.

url = 'http://localhost:7474/db/data';
username = 'neo4j';
password = 'matlab';

neo4jconn = neo4j(url,username,password);

Check the Message property of the Neo4j® connection object neo4jconn.

neo4jconn.Message
ans =

     []

The blank Message property indicates a successful connection.

Search Entire Graph

Find all the Person nodes and all the relationships associated with each Person node using searchGraph.

social_graphdata = searchGraph(neo4jconn,{'Person'})
social_graphdata = 

  struct with fields:

        Nodes: [7×3 table]
    Relations: [8×4 table]

Convert Graph Data to Directed Graph

Using the table social_graphdata.Nodes, access the name property for each node that appears in the NodeData variable of the table.

Assign the table social_graphdata.Nodes to nodestable.

nodestable = social_graphdata.Nodes
nodestable = 

         NodeLabels      NodeData                NodeObject          
         __________    ____________    ______________________________

    0    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    1    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    2    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    3    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    4    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    5    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    6    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]

Assign the row names for each row in the table nodestable to rownames.

rownames = nodestable.Properties.RowNames
rownames =

  7×1 cell array

    '0'
    '1'
    '2'
    '3'
    '4'
    '5'
    '6'

Access the NodeData variable from nodestable for each row.

nodedata = [nodestable.NodeData{rownames}]
nodedata = 

  1×7 struct array with fields:

    name

nodedata contains an array of structures.

To retrieve the name field from each structure, index into the array.

nodenames = {nodedata(:).name}
nodenames =

  1×7 cell array

    'User1'    'User3'    'User2'    'User4'    'User5'    'User6'    'User7'

nodenames is a cell array of character vectors that contains node names.

Create the digraph object social_graph using the neo4jStruct2Digraph function with the graph data stored in social_graphdata and the node names stored in nodenames.

social_graph = neo4jStruct2Digraph(social_graphdata,'NodeNames',nodenames)
social_graph = 

  digraph with properties:

    Edges: [8×3 table]
    Nodes: [7×3 table]

To see a visual representation of the graph, create a figure that displays social_graph.

plot(social_graph,'EdgeLabel',social_graph.Edges.RelationType)

Find Shortest Path

Find the shortest path between User1 and User7 using shortestpath.

[user1_to_user7,distance] = shortestpath(social_graph,'User1','User7')
user1_to_user7 =

  1×5 cell array

    'User1'    'User3'    'User4'    'User6'    'User7'


distance =

     4