Main Content

Find Friends of Friends in Social Neighborhood

This example shows how to search a social neighborhood to find the second-degree friends of a person, 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 a value ranging from User1 through User7. Each relationship has type knows.

To find the second-degree friends of User1, use the MATLAB interface to Neo4j and the digraph object. For details about the MATLAB interface to Neo4j, see Graph Database Workflow for Neo4j Database Interfaces.

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.

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. The blank Message property indicates a successful connection.

ans =


Search for One Person in Database

Find the node that has the node label Person with the property name User1.

user1 = searchNode(neo4jconn,'Person','PropertyKey','name', ...
user1 = 
  Neo4jNode with properties:

        NodeID: 0
      NodeData: [1×1 struct]
    NodeLabels: 'Person'

Search for All Second-Degree Friends of Person

Find outgoing relationships for User1. To limit the search to relationships with a distance of two or less, specify 2 as the value of the name-value pair argument 'Distance'.

user1_relation = searchRelation(neo4jconn,user1,'out','Distance',2)
user1_relation = struct with fields:
       Origin: 0
        Nodes: [4×3 table]
    Relations: [4×5 table]

Convert Graph Data to Directed Graph

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

Assign the table user1_relation.Nodes to nodestable.

nodestable = user1_relation.Nodes
nodestable=4×3 table
         NodeLabels      NodeData                  NodeObject             
         __________    ____________    ___________________________________

    0     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    1     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    2     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    3     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]

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

rownames = nodestable.Properties.RowNames
rownames = 4×1 cell array

Access the NodeData variable from nodestable for each row. nodedata contains an array of structures.

nodedata = [nodestable.NodeData{rownames}]
nodedata = 1×4 struct array with fields:

To retrieve the name field from each structure, index into the array. nodenames is a cell array of character vectors that contains node names.

nodenames = {nodedata(:).name}
nodenames = 1×4 cell array
    {'User1'}    {'User3'}    {'User2'}    {'User4'}

Create the digraph object user1_graph using the neo4jStruct2Digraph function with the relationship data stored in user1_relation and the node names stored in nodenames.

user1_graph = neo4jStruct2Digraph(user1_relation,'NodeNames',nodenames)
user1_graph = 
  digraph with properties:

    Edges: [4×3 table]
    Nodes: [4×3 table]

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


Find Friends of Person

Retrieve a list of all the first-degree friends of User1. The user1_friend variable is a cell array of character vectors that contains the names of first-degree friends.

disp('Friends of User1 are:')
Friends of User1 are:
user1_friend = successors(user1_graph,'User1')
user1_friend = 2×1 cell array

Find Second-Degree Friends

To find the second-degree friends of User1, run successors again by looping through the list of the first-degree friends. user1_friends_friend is a cell array of character vectors that contains the names of second-degree friends.

user1_friends_friend = {};
for i = 1:length(user1_friend)
    user1_friends_friend = [user1_friends_friend; ...
disp('Friends of User1''s friends are:')
Friends of User1's friends are:
user1_friends_friend = unique(user1_friends_friend)
user1_friends_friend = 2×1 cell array

Remove Duplicate Friends

Remove duplicates from the second-degree friends list that are already in the first-degree friends list using setdiff.

finalResult = setdiff(user1_friends_friend,user1_friend);

disp('User1''s second-degree friends are:')
User1's second-degree friends are:
for i = 1:length(finalResult)

finalResult is a cell array of character vectors that contains the names of second-degree friends. This list removes the names of the first-degree friends.

Close Database Connection


See Also

| | | | |

Related Examples

More About