社交关系中的共同好友数目计算(一度好友与二度好友)

 1 public class test1 {
 2     /**
 3      * 思路:如果A与某人有共同好友,则该人必出现在二度好友中
 4      *     不在二度好友中的,则与A的共同好友数必然为0
 5      *     故只需遍历所有的二度好友即可,但是不要将那些无共同好友的一度好友忘记!也就是初始化firstout 
 6      * @param args
 7      */
 8     
 9 
10     // directs为A的一度好友list,indirects为Map<String,List<String>代表一度好友的一度好友(不包括A)映射关系表
11     // firstout为A与其一度好友的共同好友数Map,secondout为A与其二度好友的共同好友数Map
12     public static void main(String[] args) {
13         
14         String directsString = "BDEH";
15         String bString = "CD";
16         String dString = "B,E,G,G";
17         String eString = "C,D";
18         String hString = "I";
19         List<String> directs = stringToList(directsString);
20         
21         
22         Map<String, List<String>> indirects = new HashMap<String, List<String>>();
23         indirects.put("B", stringToList(bString));
24         indirects.put("D", stringToList(dString));
25         indirects.put("E", stringToList(eString));
26         indirects.put("H", stringToList(hString));
27         
28         Map<String, Integer> firstout = new HashMap<String, Integer>();//用于存放A与一度好友的共同好友数目
29         Map<String, Integer> secondout = new HashMap<String, Integer>();//用于存放A与二度好友的共同好友数目
30         
31         //由于遍历所有二度好友,先去考虑在不在fistout中,如果不在则必为二度好友,然后才去考虑在不在secondout中做最后处理,
32         //故firstout需要进行初始化,保证fistout+secondout包含所有A的一度好友与二度好友
33         for(String str : directs) {
34             firstout.put(str, 0);
35         }
36         
37         for(Map.Entry<String, List<String>> entry : indirects.entrySet()) {
38             for(String id2 : entry.getValue()) {
39                 Integer value = firstout.get(id2);
40                 if(value != null) {
41                     firstout.put(id2, value+1);
42                 }else {
43                     if(secondout.get(id2) == null) {
44                         secondout.put(id2, 1);
45                     }else {
46                         secondout.put(id2, secondout.get(id2) + 1);
47                     }
48                 }
49             }
50         }
51         
52         System.out.println("A与一度好友B的共同好友数: " + firstout.get("B"));
53         System.out.println("A与一度好友D的共同好友数: " + firstout.get("D"));
54         System.out.println("A与二度好友C的共同好友数: " + secondout.get("C"));
55         System.out.println("A与二度好友I的共同好友数: " + secondout.get("I"));
56     }
57 
58     public static List<String> stringToList(String str) {
59         List<String> list = new ArrayList<String>();
60         for (int i = 0; i < str.length(); i++) {
61             list.add(str.charAt(i) + "");
62         }
63         return list;
64     }
65 }

输出结果:

A与一度好友B的共同好友数: 1
A与一度好友D的共同好友数: 2
A与二度好友C的共同好友数: 2
A与二度好友I的共同好友数: 1

转载于:https://www.cnblogs.com/leehfly/p/4957421.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种用于处理大规模数据集的编程模型和软件框架。它可以将大规模数据集分解成多个小的数据块,并在分布式计算集群上进行并行处理。二度好友推荐是一种基于社交网络的推荐算法,它可以根据用户的好友关系,推荐用户可能认识但尚未添加为好友的人。 在MapReduce实现二度好友推荐算法的一种常见方法是使用两个MapReduce作业。第一个作业用于计算用户的直接好友关系,第二个作业用于计算用户的二度好友关系。 以下是一个简单的示例,演示了如何使用MapReduce实现二度好友推荐算法: 1. 第一个作业(计算直接好友关系): ```python # Mapper函数 def mapper1(key, value): user, friends = value.split("\t") for friend in friends.split(","): yield (user, friend), 1 # Reducer函数 def reducer1(key, values): yield key, sum(values) # 执行第一个作业 input_data = # 输入数据,格式为:user1\tfriend1,friend2,friend3... output_data1 = map_reduce(input_data, mapper1, reducer1) ``` 2. 第二个作业(计算二度好友关系): ```python # Mapper函数 def mapper2(key, value): user1, user2 = key yield (user1, user2), value # Reducer函数 def reducer2(key, values): user1, user2 = key mutual_friends = [] for value in values: mutual_friends.append(value) yield (user1, user2), mutual_friends # 执行第二个作业 output_data2 = map_reduce(output_data1, mapper2, reducer2) ``` 请注意,上述示例的`map_reduce`函数是一个抽象的函数,用于执行MapReduce作业。具体的实现细节可以根据使用的编程语言和框架进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值