DSA Python Project – 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 new user 
#   2. Create Friendship 
#   3. Display Friendship on Social Network
#    4. Friend Recommendation

class FriendRecommendationSystem:
    def __init__(self):
        self.max_users = 20
        self.users = []
        self.graph = [[0 for _ in range(self.max_users)] for _ in range(self.max_users)]

    def add_user(self, name):
        if len(self.users) >= self.max_users:
            print(" Maximum user limit reached.")
            return
        if name in self.users:
            print(" User already exists.")
            return
        self.users.append(name)
        print(f" User added: {name}")

    def find_index(self, name):
        try:
            return self.users.index(name)
        except ValueError:
            return -1

    def add_friendship(self, name1, name2):
        i = self.find_index(name1)
        j = self.find_index(name2)
        if i == -1 or j == -1:
            print(" One or both users not found.")
            return
        self.graph[i][j] = 1
        self.graph[j][i] = 1
        print(f" Friendship added between {name1} and {name2}")

    def show_friends(self):
        print("\n-------- Friend Lists ----------")
        for i in range(len(self.users)):
            print(f"{self.users[i]} -> ", end='')
            for j in range(len(self.users)):
                if self.graph[i][j] == 1:
                    print(self.users[j], end='  ')
            print()

    def recommend_friends(self, name):
        idx = self.find_index(name)
        if idx == -1:
            print(" User not found.")
            return

        recommended = [0] * self.max_users

        for i in range(len(self.users)):
            if self.graph[idx][i] == 1:
                for j in range(len(self.users)):
                    if self.graph[i][j] == 1 and j != idx and self.graph[idx][j] == 0:
                        recommended[j] += 1

        print(f"\n Friend Recommendations for {name}:")
        found = False
        for i in range(len(self.users)):
            if recommended[i] > 0:
                print(f" - {self.users[i]} (Mutual Friends: {recommended[i]})")
                found = True

        if not found:
            print("No recommendations at this time.")

    def menu(self):
        while True:
            print("\n-----------Friend Recommendation System ---------------")
            print("1. Add User")
            print("2. Add Friendship")
            print("3. Show Friends")
            print("4. Recommend Friends")
            print("5. Exit")
            print("-------------------------------------------------------")
            choice = input("Enter Your Choice: ")

            if choice == '1':
                name = input("Enter user name: ")
                self.add_user(name)

            elif choice == '2':
                name1 = input("Enter first user name: ")
                name2 = input("Enter second user name: ")
                self.add_friendship(name1, name2)

            elif choice == '3':
                self.show_friends()

            elif choice == '4':
                name = input("Enter your name: ")
                self.recommend_friends(name)

            elif choice == '5':
                print("Exiting...")
                break

            else:
                print(" Invalid choice. Try again.")

# Run the program
if __name__ == "__main__":
    system = FriendRecommendationSystem()
    system.menu()

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *