2

I’m trying to implement a mutual friends feature using a graph data structure represented as an adjacency list in Python. Each node represents a person, and each edge represents a friendship (an undirected connection).

For example: John — A, B, C, D
David — B, C, E, F The mutual friends of John and David should be B and C.

What I Want to Improve ->I want to make this more efficient for larger networks (e.g., thousands of nodes). -> I’d like to know if there’s a better graph representation or built-in library that simplifies this task. ->How can I extend this to find mutual friends among more than two users (e.g., common friends of John, David, and Alex)?

2 Answers 2

1

You can use set intersection:

adj = {
    "John": set(["A", "B", "C", "D"]),
    "David": set(["B", "C", "E", "F"]),
    "Helen": set(["A", "C", "D", "F"]),
}

print(adj["John"].intersection(adj["David"]))  # {'C', 'B'}
print(adj["John"].intersection(adj["David"], adj["Helen"]))  # {'C'}

The & set operator can be used as well:

print(adj["John"] & adj["David"])  # {'C', 'B'}
print(adj["John"] & adj["David"] & adj["Helen"])  # {'C'}
Sign up to request clarification or add additional context in comments.

4 Comments

ok....tq.... is there any other solution for this
Yes, you can write your own implementation of intersection. But I see no good reason to do that when the language offers you a native intersection method.
actually i am not asking about the coding part....about the solution logic
But you want the intersection, right? So what more do you expect than doing an intersection? Can you clarify more precisely what you look for?
0

As trincot said : set intersection is the best way to do that.

If you want to use a graph, you could represents each person with a node and the friendship as edges connecting between the person and their friends.

To catch the mutual friends between X and Y , you could consider the outdegree of X and Y nodes, afterwards the intersection.

The full code will be:

class FriendshipGraph:
    def __init__(self):
        self.graph = defaultdict(set)

    def add_friendship(self, person1, person2):
        self.graph[person1].add(person2)
        self.graph[person2].add(person1)

    def get_mutual_friends(self, *people):
        if not people:
            return set()

        mutual_friends = self.graph[people[0]].copy()

        for person in people[1:]:
            if person not in self.graph:
                return set()
            mutual_friends &= self.graph[person]

        mutual_friends -= set(people)

        return mutual_friends


graph = FriendshipGraph()

Test the code with those examples:

graph = FriendshipGraph()

graph.add_friendship("Ahmed", "Mohammed")
graph.add_friendship("Ahmed", "Khaled")
graph.add_friendship("Ahmed", "Sara")
graph.add_friendship("Mohammed", "Khaled")
graph.add_friendship("Mohammed", "Fatima")
graph.add_friendship("Sara", "Fatima")
graph.add_friendship("Khaled", "Sara")
graph.add_friendship("Khaled", "Nour")
graph.add_friendship("Sara", "Nour")

print("Mutual friends between Ahmed and Mohammed:", graph.get_mutual_friends("Ahmed", "Mohammed"))

print("Mutual friends between Ahmed and Fatima:", graph.get_mutual_friends("Ahmed", "Fatima"))

print("Mutual friends between 3 people:", graph.get_mutual_friends("Ahmed", "Mohammed", "Sara"))

print("Mutual friends between 4 people:", graph.get_mutual_friends("Ahmed", "Mohammed", "Sara", "Fatima"))

print("Mutual friends between 5 people:", graph.get_mutual_friends("Ahmed", "Mohammed", "Sara", "Khaled", "Nour"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.