Friendship Recommendation System Using Graph Theory
A visual website explaining how social media platforms suggest new friends by representing users as vertices, friendships as edges, and candidate suggestions as friends-of-friends.
Problem & Business Opportunity
Social platforms need a smart way to suggest people who are relevant, close enough, and not already connected.
The Problem
- Users cannot manually search through thousands or millions of profiles.
- Random friend suggestions often feel irrelevant.
- Already-friends must be removed from the recommendation list.
- The system needs a simple rule to measure connection strength.
The Opportunity
Graph theory gives a clear mathematical solution. By looking at mutual friends, distance, degree, and interaction strength, a platform can rank suggested users in a logical and explainable way.
Graph Theory Basics
A social network can be modeled as a graph G = (V, E).
| Graph Term | Meaning in Friendship System | Example |
|---|---|---|
| Vertex / Node | A user or person in the system. | Ahmed, Sara, Ali |
| Edge | A friendship between two users. | Ahmed — Ali |
| Path | A sequence of friendships connecting two users. | Ahmed → Ali → Sara |
| Degree | Number of friends a user has. | Degree(Ahmed) = 2 |
| Weight | Strength of a relation or recommendation score. | 2 mutual friends |
Undirected Graph
Friendship goes both ways. If Ahmed is friends with Ali, Ali is also friends with Ahmed.
Weighted Graph
Edges or suggestions have values such as mutual friend count, interactions, or shared groups.
Unweighted Graph
Only shows whether a friendship exists or not, without measuring strength.
Mutual Friends Graph
Sara is not directly connected to Ahmed, but she shares Ali and Mohamed as mutual friends.
Recommendation Algorithm
The system checks friends of friends, removes invalid users, counts mutual friends, then ranks candidates.
Ranking Friend Suggestions
Candidates with more mutual friends are ranked first.
| Candidate | Why Suggested? | Score | Decision |
|---|---|---|---|
| Sara | Connected to Ali and Mona. | 2 | Strong suggestion |
| Nada | Connected to Mona and Youssef. | 2 | Strong suggestion |
| Kareem | Connected to Ali only. | 1 | Possible but weaker |
Breadth First Search & Shortest Path
BFS explores the graph level by level and helps find users close to the target user.
BFS Levels
- Level 0: Target user.
- Level 1: Direct friends.
- Level 2: Friends of friends, usually the best recommendation candidates.
- Level 3+: Distant users with weaker relevance.
Shortest Path
The shortest path is the minimum number of edges needed to connect two users. If Ahmed → Ali → Sara, the distance between Ahmed and Sara is 2. This makes Sara a second-degree connection.
Graph Representation in Code
A graph can be stored as an adjacency list or adjacency matrix.
Adjacency List
Each user has a list of direct friends. This is memory-efficient when the graph is sparse.
Ali: Ahmed, Sara, Omar
Mohamed: Ahmed, Sara
Sara: Ali, Mohamed
Omar: Ali
Adjacency Matrix
A table where 1 means friendship exists and 0 means no friendship. For an undirected graph, the matrix is symmetric.
Ahmed 0 1 1 0 0
Ali 1 0 0 1 1
Mohamed 1 0 0 1 0
Sara 0 1 1 0 0
Omar 0 1 0 0 0
Improved Scoring Model
To make the topic stronger, we can improve the basic mutual-friends score using weights.
Note: In the example below, the distance penalty is assumed to be 0 because all candidates are second-degree connections.
More Accurate
A user with shared groups and interests may be more relevant than a user with only one mutual friend.
More Realistic
Modern platforms use multiple signals, not just one simple rule.
More Flexible
The weights can be adjusted depending on the platform's goal.
| Candidate | Mutual Friends | Common Groups | Common Interests | Final Score |
|---|---|---|---|---|
| Sara | 2 × 3 = 6 | 1 × 2 = 2 | 2 × 2 = 4 | 12 |
| Nada | 2 × 3 = 6 | 0 × 2 = 0 | 1 × 2 = 2 | 8 |
| Kareem | 1 × 3 = 3 | 1 × 2 = 2 | 0 × 2 = 0 | 5 |
C++ Implementation
The code below stores friendships using an adjacency list and ranks candidates by mutual friend count.
Sara (2 mutual friends)
Omar (1 mutual friends)
Real-Life Applications, Advantages & Limitations
Applications
- Social media friend suggestions.
- LinkedIn-like professional connections.
- Gaming team recommendations.
- Community detection.
- Shared-interest recommendation systems.
Advantages
- Easy to understand and implement.
- Works well for small and medium networks.
- Uses clear discrete mathematics concepts.
- Can be improved using weights or machine learning.
Limitations
- Large networks require faster algorithms.
- Mutual friends alone may not guarantee relevance.
- Privacy must be protected.
- Fake or inactive accounts reduce quality.
Extra Worked Examples
These examples make the project easier to explain in discussion because they show different cases, not only one graph.
Situation
Ahmed has two candidates with the same mutual-friends score. Sara and Nada both have a score of 2. The system needs another rule to decide who appears first.
Nada: 2 mutual friends + 1 common interest
Result
Sara should be ranked before Nada because both have the same number of mutual friends, but Sara has more shared interests.
Candidate Filtering
Ahmed is already friends with Ali and Mona. Even if Ali and Mona appear again while searching through friends-of-friends, the system must not suggest them.
- Target user: Ahmed
- Already friends: Ali, Mona
- Possible candidates: Sara, Nada, Kareem
Why This Matters
Without this filtering step, the recommendation system would suggest people who are already connected. That would make the system look wrong and useless.
if candidate is direct friend → remove
| Candidate | Mutual Friends | Interaction Strength | Shared Group | Final Recommendation |
|---|---|---|---|---|
| Sara | 2 | High | Yes | Very Strong |
| Nada | 2 | Low | No | Strong |
| Kareem | 1 | Medium | Yes | Medium |
Too Far
A user at Level 4 or Level 5 may be too distant, so the suggestion becomes weak.
Blocked User
If Ahmed blocked someone, the system must remove that person from all recommendations.
Inactive Account
If the candidate has not used the platform for a long time, the recommendation quality decreases.
Mini Case Study: Small Social Network
This part shows a complete mini scenario from graph input to final recommendation output.
| Step | Action | Result |
|---|---|---|
| 1 | Select target user | Ahmed |
| 2 | Find direct friends | Ali, Mona, Youssef |
| 3 | Find friends of direct friends | Sara, Nada, Kareem |
| 4 | Count repeated candidates | Sara appears twice, Nada appears twice, Kareem appears once |
| 5 | Rank by score | Sara and Nada first, then Kareem |
Conclusion
Books
- Kenneth H. Rosen, Discrete Mathematics and Its Applications.
- Douglas B. West, Introduction to Graph Theory.
- Thomas H. Cormen, Introduction to Algorithms.
Online Source
- GeeksforGeeks, Graph Data Structure and Algorithms.