Reputation: 2362
I'm developing one application, that will have multiple Facebook users.
I'm trying to figure out the best way of knowing who (from a list of up to about 20), has most connections (friends) with others from that list.
So, let's say this situation:
list = [A, B, C, D, E, F...];
where, A
is friend of B
, C
and D
. And B
is friend of A
and C
.
I would then know, that A
has 3 connections, B
has 2, and C
has 2. And use A
as the one to post something.
I thought in some solutions:
1) For every person in the list, check for friend connection of all others (using Graph API).
2) Cache all friends of all users, in the DB (maybe Neo4j
or MongoDB
, SQL
...), and then transverse the search, looking for common friends.
Problem of 1: Might take time to do it every time. Problem of 2: DB will grow really big.
Question: Is 1 and/or 2 ok? Is there any other way of doing it? (I read the Facebook API, but didn't saw anything like this).
Upvotes: 0
Views: 125
Reputation: 471
You could use Neo4j as you stated. Save persons in neo4j including their relationships (Friendships) and then use query similar to this:
MATCH (a:Person)-[f:FRIENDS]-(b:Person)
WHERE Id(a) IN [1, 2, 3] AND Id(b) IN [1, 2, 3]
RETURN a.name AS name, count(f) AS numOfFriends
ORDER BY numOfFriends DESC
Upvotes: 3