G面经prepare: Friends Recommendation

时间:2022-07-04 15:23:59
想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友

一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁给A,我说D,因为他是BC的共同好友,而E只是B的好友,到这我才明白干啥,就是给一个图和里面的一个节点A,用bfs从A出发,找出第二层中indegree度数最大节点

用HashMap<Character, HashSet<Character>>来建图

用HashMap<Character, Integer> SndIndegree来表示第二层indegree数目

用maxIndegree记录第二层Indegree最大值

用res记录第二层Indegree最大者

BFS

 package FriendRecommendation;
import java.util.*; public class Solution {
public char recommend(char tar, char[][] arr) {
HashMap<Character, HashSet<Character>> graph = new HashMap<Character, HashSet<Character>>();
HashMap<Character, Integer> SndIndegree = new HashMap<Character, Integer>(); //build graph
for (char[] edge : arr) {
if (!graph.containsKey(edge[0])) graph.put(edge[0], new HashSet<Character>());
if (!graph.containsKey(edge[1])) graph.put(edge[1], new HashSet<Character>());
graph.get(edge[0]).add(edge[1]);
if (!SndIndegree.containsKey(edge[0])) SndIndegree.put(edge[0], 0);
if (!SndIndegree.containsKey(edge[1])) SndIndegree.put(edge[1], 0);
} Queue<Character> queue = new LinkedList<Character>();
HashSet<Character> visited = new HashSet<Character>();
int level = 0;
queue.offer(tar);
visited.add(tar);
int PNum = 1;
int CNum = 0;
int maxIndegree = 0;
char res = '\0';
while (!queue.isEmpty()) {
char cur = queue.poll();
PNum--;
for (Character neigh : graph.get(cur)) {
if (level+1 == 2) {
if (neigh == tar) continue;
int curIndegree = SndIndegree.get(neigh)+1;
if (curIndegree > maxIndegree) res = neigh.charValue();
SndIndegree.put(neigh, curIndegree);
}
else { //not second level
if (!visited.contains(neigh)) {
queue.offer(neigh);
CNum++;
visited.add(neigh);
}
}
}
if (PNum == 0) {
PNum = CNum;
CNum = 0;
level++;
}
}
return res;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
char res = sol.recommend('A', new char[][]{{'A','B'},{'A','C'},{'B','D'},{'B','E'},{'C','D'},{'B','A'},{'C','A'}});
System.out.println(res);
} }