1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
| public class SocialNetworkAnalyzer {
private Graph socialNetwork;
private Map<Integer, String> userNames;
public SocialNetworkAnalyzer() {
this.socialNetwork = new Graph(1000); // 支持1000个用户
this.userNames = new HashMap<>();
}
/**
* 添加用户
*/
public void addUser(int userId, String userName) {
userNames.put(userId, userName);
}
/**
* 添加好友关系
*/
public void addFriendship(int user1, int user2) {
socialNetwork.addEdge(user1, user2);
}
/**
* 查找共同好友
*/
public List<String> findMutualFriends(int user1, int user2) {
Set<Integer> friends1 = new HashSet<>(socialNetwork.getNeighbors(user1));
Set<Integer> friends2 = new HashSet<>(socialNetwork.getNeighbors(user2));
friends1.retainAll(friends2); // 求交集
return friends1.stream()
.map(userNames::get)
.collect(Collectors.toList());
}
/**
* 计算社交距离(六度分隔理论)
*/
public int calculateSocialDistance(int user1, int user2) {
BFSTraversal bfs = new BFSTraversal();
Map<Integer, Integer> distances = bfs.shortestDistances(socialNetwork, user1);
return distances.getOrDefault(user2, -1);
}
/**
* 推荐可能认识的人(朋友的朋友)
*/
public List<String> recommendFriends(int userId, int maxRecommendations) {
BFSTraversal bfs = new BFSTraversal();
List<List<Integer>> levels = bfs.levelOrderTraversal(socialNetwork, userId);
List<String> recommendations = new ArrayList<>();
// 推荐距离为2的用户(朋友的朋友)
if (levels.size() > 2) {
List<Integer> friendsOfFriends = levels.get(2);
for (int i = 0; i < Math.min(friendsOfFriends.size(), maxRecommendations); i++) {
int recommendedUserId = friendsOfFriends.get(i);
recommendations.add(userNames.get(recommendedUserId));
}
}
return recommendations;
}
/**
* 检测社交圈子(连通组件)
*/
public List<List<String>> findSocialCircles() {
ConnectedComponents cc = new ConnectedComponents();
List<List<Integer>> components = cc.findConnectedComponents(socialNetwork);
return components.stream()
.map(component -> component.stream()
.map(userNames::get)
.collect(Collectors.toList()))
.collect(Collectors.toList());
}
/**
* 查找影响力最大的用户
*/
public String findMostInfluentialUser() {
int maxConnections = 0;
int mostInfluentialUser = -1;
for (int userId : userNames.keySet()) {
int connections = socialNetwork.getNeighbors(userId).size();
if (connections > maxConnections) {
maxConnections = connections;
mostInfluentialUser = userId;
}
}
return userNames.get(mostInfluentialUser);
}
/**
* 分析信息传播路径
*/
public Map<String, Integer> analyzeInformationSpread(int sourceUser, int maxLevels) {
BFSTraversal bfs = new BFSTraversal();
List<List<Integer>> levels = bfs.levelOrderTraversal(socialNetwork, sourceUser);
Map<String, Integer> spreadAnalysis = new HashMap<>();
for (int level = 0; level < Math.min(levels.size(), maxLevels); level++) {
for (int userId : levels.get(level)) {
spreadAnalysis.put(userNames.get(userId), level);
}
}
return spreadAnalysis;
}
/**
* 完整的演示方法
*/
public void demonstrateFeatures() {
// 添加示例用户
addUser(1, "Alice");
addUser(2, "Bob");
addUser(3, "Charlie");
addUser(4, "David");
addUser(5, "Eve");
addUser(6, "Frank");
// 添加好友关系
addFriendship(1, 2); // Alice - Bob
addFriendship(1, 3); // Alice - Charlie
addFriendship(2, 4); // Bob - David
addFriendship(3, 5); // Charlie - Eve
addFriendship(4, 6); // David - Frank
System.out.println("=== 社交网络分析演示 ===");
// 查找共同好友
List<String> mutualFriends = findMutualFriends(1, 2);
System.out.println("Alice和Bob的共同好友: " + mutualFriends);
// 计算社交距离
int distance = calculateSocialDistance(1, 6);
System.out.println("Alice到Frank的社交距离: " + distance);
// 推荐朋友
List<String> recommendations = recommendFriends(1, 3);
System.out.println("为Alice推荐的朋友: " + recommendations);
// 查找影响力最大的用户
String influencer = findMostInfluentialUser();
System.out.println("影响力最大的用户: " + influencer);
// 分析信息传播
Map<String, Integer> spreadAnalysis = analyzeInformationSpread(1, 3);
System.out.println("从Alice开始的信息传播分析: " + spreadAnalysis);
}
}
|