Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Model for Social Network?

If I wanted to create a site that allowed users to have 0 or more "friends", how would I model such a relationship in a database? Would something this simple work:

Table Friends
- Id (PK)
- UserId (FK)
- FriendId (FK)

???

Would this allow me to later on do things like Facebook does (e.g. "3 of your friends knows this user, maybe you do too")? Or something like 6-degrees-to-Kevin-Bacon?

EDIT 1:

Table Friends
- UserId (FK)
- FriendId (FK)
- Status ('Pending', 'Approved', 'Rejected', 'Blocked'?)
like image 952
StackOverflowNewbie Avatar asked Nov 14 '10 10:11

StackOverflowNewbie


People also ask

What are social networking models?

Abstract: Three models are often invoked to capture the structure of social networks: (i) Erdos-Renyi random graphs (which are tractable for theoretical analysis), (ii) small-world constructions, and (iii) multi-scale (or scale-free) graphs constructed with preferential attachments of new nodes.

Which data structure is used in social media?

Graphs are awesome data structures that you use every day through Google Search, Google Maps, GPS, and social media. They are used to represent elements that share connections. The elements in the graph are called Nodes and the connections between them are called Edges.

How do social networks collect data?

The most common data collection methods used in SNA are surveys and interviews. A survey should include questions regarding the background of the respondent and a way for them to provide information on connections .

What is social network data analysis?

Social network analysis (SNA) is the process of investigating social structures through the use of networks and graph theory. It characterizes networked structures in terms of nodes (individual actors, people, or things within the network) and the ties, edges, or links (relationships or interactions) that connect them.


1 Answers

This will work. Following are points to be noted:

  • Do you have something like friend confirmation. If yes, you will have to think on how to store 'pending'
  • Indexing both UserId and FriendId. These are the values on which you will be joining the tables.
  • The unordered pair (UserId, FriendId) is contender for Primary key.
  • Suppose Uid_1 and Fid_1 are friends where Uid_1 != Fid_1 then does your Friends Table store (Fid_1, Uid_1) as well as (Uid_1, Fid_1).
  • How far in degrees of relationship are you going to search.

Everytime you have to query for DOR(Degree of relationship) you will have to initialize a graph and run Shortest Path Algo (This is the least optimization I can think of). If your member-count rises to some kilos then how are you going to handle this?

like image 60
Ashwini Dhekane Avatar answered Nov 26 '22 13:11

Ashwini Dhekane