Discrete Mathematics Project Report

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.

Graph Theory Social Networks BFS Shortest Path Adjacency Matrix C++
Main Data Unit
V
Vertex = User
Relationship
E
Edge = Friendship
Best Candidates
L2
Friends of friends
Ranking Factor
Score
Mutual friends + weights
Team Members
Eslam Mohamed202511723
Loay Wael Nabil202505192
Marwan Salah Attia202500548
Yassin Ahmed202505974
Section 02

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.

Project Goal
Build a friendship recommendation model that suggests users who are not direct friends, but have strong indirect connections through mutual friends and short paths in the graph.
Section 03

Graph Theory Basics

A social network can be modeled as a graph G = (V, E).

Graph TermMeaning in Friendship SystemExample
Vertex / NodeA user or person in the system.Ahmed, Sara, Ali
EdgeA friendship between two users.Ahmed — Ali
PathA sequence of friendships connecting two users.Ahmed → Ali → Sara
DegreeNumber of friends a user has.Degree(Ahmed) = 2
WeightStrength of a relation or recommendation score.2 mutual friends
Types of Graphs Used

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.

Section 04

Mutual Friends Graph

Sara is not directly connected to Ahmed, but she shares Ali and Mohamed as mutual friends.

Example 1 — Mutual Friends
friend friend mutual path mutual path Ahmed Ali Mohamed Sara Omar Recommendation Sara score = 2 Omar score = 1
Target user Direct friends Strong suggestion Weaker suggestion
Section 05

Recommendation Algorithm

The system checks friends of friends, removes invalid users, counts mutual friends, then ranks candidates.

01
Choose the target userExample: Ahmed.
02
Find direct friendsAhmed's Level 1 friends are Ali and Mohamed.
03
Find friends of direct friendsThese are Level 2 nodes and become candidate suggestions.
04
Remove invalid candidatesRemove Ahmed himself and remove users already directly connected to Ahmed.
05
Calculate scoreEach mutual friend adds +1 to the candidate score.
06
Rank suggestionsSort candidates from highest score to lowest score.
Section 06

Ranking Friend Suggestions

Candidates with more mutual friends are ranked first.

Example 2 — Larger Ranking Graph
Ahmed Ali Mona Youssef Sara Nada Kareem Ranked Output 1. Sara Ali + Mona = score 2 2. Nada Mona + Youssef = score 2 3. Kareem Ali only = score 1
Candidate Ranking Table
CandidateWhy Suggested?ScoreDecision
SaraConnected to Ali and Mona.2Strong suggestion
NadaConnected to Mona and Youssef.2Strong suggestion
KareemConnected to Ali only.1Possible but weaker
Section 07

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.

BFS Visual Layers
LEVEL 0 LEVEL 1 LEVEL 2 Ahmed Ali Mona Sara Nada
Section 08

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.

Ahmed: Ali, Mohamed
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.

        A Al M S O
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
Section 09

Improved Scoring Model

To make the topic stronger, we can improve the basic mutual-friends score using weights.

Final Score = (Mutual Friends × 3) + (Common Groups × 2) + (Common Interests × 2) + (Recent Interactions × 1) - (Distance Penalty)

Note: In the example below, the distance penalty is assumed to be 0 because all candidates are second-degree connections.
Why this is better

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.

Example
CandidateMutual FriendsCommon GroupsCommon InterestsFinal Score
Sara2 × 3 = 61 × 2 = 22 × 2 = 412
Nada2 × 3 = 60 × 2 = 01 × 2 = 28
Kareem1 × 3 = 31 × 2 = 20 × 2 = 05
Section 10

C++ Implementation

The code below stores friendships using an adjacency list and ranks candidates by mutual friend count.

#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; class SocialGraph { private: map<string, vector<string>> friendsList; public: void addFriendship(string a, string b) { friendsList[a].push_back(b); friendsList[b].push_back(a); } bool isAlreadyFriend(string person, string other) { for (string friendName : friendsList[person]) { if (friendName == other) return true; } return false; } void suggestFriends(string person) { map<string, int> score; for (string friendName : friendsList[person]) { for (string candidate : friendsList[friendName]) { if (candidate != person && !isAlreadyFriend(person, candidate)) { score[candidate]++; } } } vector<pair<string, int>> suggestions(score.begin(), score.end()); sort(suggestions.begin(), suggestions.end(), [](pair<string, int> a, pair<string, int> b) { return a.second > b.second; }); cout << "Friend Suggestions for " << person << ":" << endl; for (auto suggestion : suggestions) { cout << suggestion.first << " (" << suggestion.second << " mutual friends)" << endl; } } }; int main() { SocialGraph g; g.addFriendship("Ahmed", "Ali"); g.addFriendship("Ahmed", "Mohamed"); g.addFriendship("Ali", "Sara"); g.addFriendship("Mohamed", "Sara"); g.addFriendship("Ali", "Omar"); g.suggestFriends("Ahmed"); return 0; }
Expected Output
Friend Suggestions for Ahmed:
Sara (2 mutual friends)
Omar (1 mutual friends)
Small Accuracy Note
The code sorts users by the number of mutual friends. If two candidates have the same score, it uses alphabetical order as a simple tie-breaker. A real platform could replace this with common interests, common groups, or interaction strength.
Section 11

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.
Section 12

Extra Worked Examples

These examples make the project easier to explain in discussion because they show different cases, not only one graph.

Example 3 — Tie Breaking

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.

Sara: 2 mutual friends + 3 common interests
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.

Final order: Sara → Nada
Example 4 — Removing Already Existing Friends

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 == target user → remove
if candidate is direct friend → remove
Example 5 — Weighted Friendships
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
Example 6 — When Not to Suggest Someone

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.

Section 13

Mini Case Study: Small Social Network

This part shows a complete mini scenario from graph input to final recommendation output.

Case Study Graph — University Students Network
Ahmed Ali Mona Youssef Sara Nada Kareem Omar Output 1. Sara: score 2 2. Nada: score 2 3. Kareem: score 1 Omar is farther, so weaker.
Step-by-Step Calculation
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
Presentation Talking Point
This case study proves that the system is not random. It follows a clear mathematical process: direct friends are ignored, friends-of-friends become candidates, repeated appearances increase the score, and the final list is sorted from strongest to weakest suggestion.
Section 14

Conclusion

Graph theory is a powerful tool for building friendship recommendation systems. By representing users as vertices and friendships as edges, the system can detect second-degree connections, count mutual friends, use BFS to explore nearby users, and rank suggestions in a clear mathematical way.
References

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.