Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of friend groups in a set of friends connections in Python
Finding friend groups in a network is a classic graph connectivity problem. We need to count the number of connected components where each person is represented as a node and friendships as edges. This can be solved using Depth-First Search (DFS) to traverse connected friends.
Understanding the Problem
Given a friends list where friends[i] contains the indices of people that person i is friends with, we need to find how many separate friend groups exist. Two people belong to the same group if there's a path of mutual friendships connecting them.
Algorithm Steps
The solution uses DFS to find connected components ?
- Initialize: Create a visited array and counter for groups
- DFS Function: Mark current person as visited and recursively visit all unvisited friends
- Count Groups: For each unvisited person, perform DFS and increment group count
Implementation
class Solution:
def solve(self, friends):
nodes = len(friends)
visited = [False for _ in range(nodes)]
ans = 0
def dfs(vertex, visited):
visited[vertex] = True
for neighbor in friends[vertex]:
if not visited[neighbor]:
dfs(neighbor, visited)
for i in range(nodes):
if not visited[i]:
dfs(i, visited)
ans += 1
return ans
# Test the solution
solution = Solution()
friends = [[0, 1, 5], [1, 0], [2], [3, 4], [4, 3], [5, 0]]
result = solution.solve(friends)
print(f"Number of friend groups: {result}")
Number of friend groups: 3
How It Works
Let's trace through the example step by step ?
# Input: friends = [[0, 1, 5], [1, 0], [2], [3, 4], [4, 3], [5, 0]]
# Step 1: Start with person 0 (unvisited)
# DFS from 0: visits 0 ? 1 ? 5 (Group 1)
# Visited: [True, True, False, False, False, True]
# Step 2: Person 1 already visited, skip
# Step 3: Start with person 2 (unvisited)
# DFS from 2: visits only 2 (Group 2)
# Visited: [True, True, True, False, False, True]
# Step 4: Start with person 3 (unvisited)
# DFS from 3: visits 3 ? 4 (Group 3)
# Final visited: [True, True, True, True, True, True]
friends = [[0, 1, 5], [1, 0], [2], [3, 4], [4, 3], [5, 0]]
visited = [False] * len(friends)
def trace_dfs(vertex, group_name):
print(f"Visiting person {vertex} in {group_name}")
visited[vertex] = True
for neighbor in friends[vertex]:
if not visited[neighbor]:
trace_dfs(neighbor, group_name)
group_count = 0
for i in range(len(friends)):
if not visited[i]:
group_count += 1
trace_dfs(i, f"Group {group_count}")
print(f"--- End of Group {group_count} ---")
print(f"\nTotal friend groups: {group_count}")
Visiting person 0 in Group 1 Visiting person 1 in Group 1 Visiting person 5 in Group 1 --- End of Group 1 --- Visiting person 2 in Group 2 --- End of Group 2 --- Visiting person 3 in Group 3 Visiting person 4 in Group 3 --- End of Group 3 --- Total friend groups: 3
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(V + E) | Visit each person once + traverse all friendships |
| Space | O(V) | Visited array + recursion stack depth |
Where V = number of people and E = total friendships.
Conclusion
This problem demonstrates finding connected components in an undirected graph using DFS. Each DFS traversal from an unvisited node discovers one complete friend group, making it an efficient solution for social network analysis.
