Sopan Maiti
Sopan Maiti

Reputation: 189

How to calculate mutual friend using cypher query in Neo4j as per facebook representation?

I am describing a small part of facebook that is mutual friend calculation in Neo4j with graphical representaion. I want to share it with all of you who are beginners and interested with graph database specially neo4j with my tiny knowledge.

Upvotes: 0

Views: 262

Answers (1)

Sopan Maiti
Sopan Maiti

Reputation: 189

Example of Facebook Representation to Calculate Mutual Friend Between the Two:

  • Cypher Query To Represent Friends Database:

    CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
    CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
    CREATE (Laurence:Person {name:'Laurence Fishburne', born:1961})
    CREATE (JoelS:Person {name:'Joel Silver', born:1952})
    CREATE (DemiM:Person {name:'Demi Moore', born:1962})
    CREATE (KevinB:Person {name:'Kevin Bacon', born:1958})
    CREATE (KieferS:Person {name:'Kiefer Sutherland', born:1966})
    CREATE (NoahW:Person {name:'Noah Wyle', born:1971})
    CREATE
       (Keanu)-[:FRIENDS_WITH ]->(Carrie),
       (Keanu)-[:FRIENDS_WITH ]->(Laurence),
       (Keanu)-[:FRIENDS_WITH ]->(JoelS),
       (Keanu)-[:FRIENDS_WITH ]->(DemiM),
       (KevinB)-[:FRIENDS_WITH ]->(KieferS),
       (KevinB)-[:FRIENDS_WITH ]->(NoahW),
       (KevinB)-[:FRIENDS_WITH ]->(DemiM),
       (KevinB)-[:FRIENDS_WITH ]->(JoelS);
    
  • Equivalent Graph Representation: Mutual Friend Representation

  • Cypher Query to Retrieve Mutual Friend's Name between KevinB and Keanu:

     MATCH (Keanu:`Person` {name: "Keanu Reeves"})-[r]->(mutualFriend)<-[r1]-(Kevin:`Person` {name: "Kevin Bacon"}) RETURN mutualFriend.name
    
  • Cypher Query to Return what Number of Mutual Friend with whom one of Keanu:

     MATCH (Keanu:`Person` {name: "Keanu Reeves"})-[r]->(mutualFriend)<-[r1]-(unknown) RETURN COUNT (mutualFriend) as NumberOfMutualFriend,unknown.name
    

Upvotes: 1

Related Questions