Problem Description
You are developing an email service that analyzes friendship relationships. Each user has a friend list, and friends form a close relationship with each other. Your task is to determine whether two users are friends based on the given friendship data and to compute the indirect closeness between them, including friends of friends.
The input consists of a friend list and the IDs of the users to check. The friend list is in the form of a list, where each element consists of a pair of user IDs that are friends with each other. Based on this information, write a program to determine if two users are indirectly friends or not.
Input Example
Friend List: [(1, 2), (2, 3), (3, 4), (1, 5)] User ID1: 1 User ID2: 4
Output Example
1 and 4 are friends or indirect friends.
Solution Method
To solve this problem, you need to use the structure of a graph. Model the friendship relationships as vertices and edges, and explore the relationship between the two users using the BFS (Breadth-First Search) or DFS (Depth-First Search) algorithm. This process will allow you to confirm both direct and indirect friendships.
1. Graph Modeling
Create an undirected graph based on the friend list. Each user is represented by their user ID, and the friendship relationship is represented as an edge between two users.
2. Implementing BFS or DFS
Now that the graph is created, find the indirect friendship relationship between the given two users using BFS or DFS. During this process, keep track of visited nodes and stop the search if a node is already visited.
3. Code Example
using System;
using System.Collections.Generic;
class FriendNetwork
{
private Dictionary> graph = new Dictionary>();
public void AddFriendship(int user1, int user2)
{
if (!graph.ContainsKey(user1))
graph[user1] = new List();
if (!graph.ContainsKey(user2))
graph[user2] = new List();
graph[user1].Add(user2);
graph[user2].Add(user1);
}
public bool AreFriends(int user1, int user2)
{
if (user1 == user2) return true;
HashSet visited = new HashSet();
Queue queue = new Queue();
queue.Enqueue(user1);
visited.Add(user1);
while (queue.Count > 0)
{
int current = queue.Dequeue();
foreach (var friend in graph[current])
{
if (friend == user2)
return true;
if (!visited.Contains(friend))
{
visited.Add(friend);
queue.Enqueue(friend);
}
}
}
return false;
}
}
class Program
{
static void Main(string[] args)
{
FriendNetwork network = new FriendNetwork();
// Add friendships
network.AddFriendship(1, 2);
network.AddFriendship(2, 3);
network.AddFriendship(3, 4);
network.AddFriendship(1, 5);
// Check the relationship between two users
int user1 = 1;
int user2 = 4;
if (network.AreFriends(user1, user2))
Console.WriteLine($"{user1} and {user2} are friends or indirect friends.");
else
Console.WriteLine($"{user1} and {user2} are not friends.");
}
}
4. Code Explanation
The code above demonstrates how to manage friendship relationships and verify if two users are friends. The FriendNetwork
class provides a graph to store the friend list and methods for adding friendships. The AreFriends
method uses BFS to check the connectivity between the two users.
5. Complexity Analysis
The time complexity of this implementation, which uses an adjacency list for the graph, is O(V + E), where V is the number of vertices and E is the number of edges. The space complexity is also O(V + E).
Conclusion
In this way, graph and search algorithms can be appropriately utilized to solve friendship relationship problems. In C#, various data structures and algorithms can elegantly address such issues, showcasing strong problem-solving skills in job interviews and coding tests.