Site icon DataFlair

DSA C++ Project – Social Media Friend Recommendation System

Program 1

//Friend Recommendation System in Social Media using graphs 

//System that recommends new friends to users in a social network by analyzing their mutual friends 
/* Features
  1. Add user 
  2. Create Friendship 
  3. Display Friendship on Social Network
   4. Friend Recommendation
*/


#include <iostream>
#include <cstring>
using namespace std;

const int MAX_USERS = 30;   // Max user size

class SocialGraph 
{
    string users[MAX_USERS];
    int adjMatrix[MAX_USERS][MAX_USERS]; 
    int userCount;

public:
    SocialGraph()  //constructor 
    {
        userCount = 0;
        for (int i = 0; i < MAX_USERS; i++)
            for (int j = 0; j < MAX_USERS; j++)
                adjMatrix[i][j] = 0;
    }

    int findUserIndex(string name) 
  {
        for (int i = 0; i < userCount; i++) 
        {
            if (users[i] == name)
                return i;
        }
        return -1;
   }

    void addUser(string name)    
     {
        if (userCount >= MAX_USERS) 
        {
            cout << " Maximum users reached!" << endl;
            return;
        }
        if (findUserIndex(name) != -1) 
        {
            cout << " User already exists!" << endl;
            return;
        }
        users[userCount] = name;
        userCount++;
        cout << " User added: " << name << endl;
    }

    void addFriendship(string u1, string u2)   // dipesh rajesh
    {
        int idx1 = findUserIndex(u1);   //dipesh    1     
        int idx2 = findUserIndex(u2);  // rajesh     2 

        if (idx1 == -1 || idx2 == -1) 
        {
            cout << " One or both users not found!" << endl;
            return;
        }

        adjMatrix[idx1][idx2] = 1;
        adjMatrix[idx2][idx1] = 1;
        cout << " Friendship created between " << u1 << " and " << u2 << endl;
    }

    void displayNetwork()
    {
        cout << "\n Social Network:" << endl;
        for (int i = 0; i < userCount; i++)    //row
         {
            cout << users[i] << " --> ";
            for (int j = 0; j < userCount; j++)   //column
            {
                if (adjMatrix[i][j]==1)
                    cout << users[j] << " ";
            }
            cout << endl;
        }
    }

    void recommendFriends(string name)    // vivek 
    {
        int index = findUserIndex(name);  // vivek 0
        if (index == -1) 
        {
            cout << " User not found!" << endl;
            return;
        }

        int recommended[MAX_USERS] = {0};    

        for (int i = 0; i < userCount; i++)    // i=1
        {
            if (adjMatrix[index][i]==1)   //[0][1]
            { 
                for (int j = 0; j < userCount; j++) // j=2
                {
                    if (adjMatrix[i][j]==1 && j != index && adjMatrix[index][j] == 0) 
                    {
                        recommended[j]++;
                    }
                }
            }
        }

        cout << "\nFriend Recommendations for " << name << ":" << endl;
        bool found = false;
        for (int i = 0; i < userCount; i++) 
        {
            if (recommended[i] > 0) 
            {
                cout << "- " << users[i] << " (Mutual friends: " << recommended[i] << ")" << endl;
                found = true;
            }
        }

        if (found==false)
            cout << "No recommendations available." << endl;
    }
};

// --------------MAIN FUNCTION---------------
int main() 
{
    SocialGraph sg;
    int choice;
    string u1, u2;

    do 
    {
        cout << "\n------------Friend Recommendation System ------------" << endl;
        cout << "1. Add User\n2. Add Friendship\n3. Show Network\n4. Recommend Friends\n5. Exit" << endl;
        cout<<"\n----------------------------------------------------";
        cout << "\nEnter your choice: ";
        cin >> choice;
        cin.ignore(); 

        switch (choice) 
        {
            case 1:
                cout << "Enter user name: ";
                getline(cin, u1);
                sg.addUser(u1);   
                break;
            case 2:
                cout << "Enter first user name: ";
                getline(cin, u1);
                cout << "Enter second user name: ";
                getline(cin, u2);
                sg.addFriendship(u1, u2); 
                break;
            case 3:
                sg.displayNetwork();
                break;
            case 4:
                cout << "Enter user name to get recommendations: ";
                getline(cin, u1);
                sg.recommendFriends(u1);
                break;
            case 5:
                cout << " Exiting. Thank you!" << endl;
                break;
            default:
                cout << " Invalid choice. Try again." << endl;
        }
    } while (choice != 5);

    return 0;
}

 

Exit mobile version