[英]How to compare edges in a graph to implement a triadic closure like in network graphs like facebook
我正在嘗試實現一個包含與Profile
類相關的頂點(節點)的圖形(如Facebook配置文件,但更平庸)。 每個頂點(Profiles)存儲在二進制搜索樹中,然后將其加載或存儲在Array-List中。
就像在Facebook Profile
一樣(並非所有人都不是所有人都有朋友),班級Profile
實例會有朋友,這是邊緣。
圖形的每個邊緣關系存儲在文本文件中,然后在圖形構造期間通過單獨的方法讀入該文本文件並將其添加到圖形中。
一旦完成,我試圖實現一個推薦的方法:
兩個不是朋友,但希望成為朋友的個人資料可能是一個三合一的關閉,但我到目前為止失敗了很多,我現在被卡住了。
Matrix
創建一個圖形,但我仍然試圖找到一個可能的解決方案來為每個頂點推薦可能的朋友,這個頂點有一個朋友j
,他的朋友x
不是頂點的朋友(當前輪廓)。 推薦的每個朋友都應該存儲在一個數組中並添加到二進制搜索樹中。
private List<Profile> getRecommendedFriends(){ Iterator profileIterator = this.vertexProfileMap.entrySet().iterator(); List<Profile> recommendedProfiles = new ArrayList<>(); Boolean isContained; while(profileIterator.hasNext()){ Map.Entry mapElement = (Map.Entry)profileIterator.next(); Profile user = (Profile) mapElement.getValue(); int sizeOfList = user.numOfFriends(); for(int i = 0; i < sizeOfList; i++){ recommendedProfiles.add(user.getFriend(i)); for(int x = 0; x < user.numOfFriends(); x++){ for(int j = 0; j < user.numOfFriends(); j++){ isContained = user.getFriend(x).getName().equals(user.getFriend(j).getName()); if(!isContained){ recommendedProfiles.add(user.getFriend(j)); } } } } } return recommendedProfiles; }
基於給定的算法,並假設Profile
類將有一個名為containsProfile(Profile p)
的附加方法,它檢查給定的配置文件是否包含在用戶的朋友列表中,以下強力方法應該有效:
private Map<Profile,List<Profile>> getRecommendedFriends(){
// under the asssumption that vertexProfileMap maps all Profiles/Users
Iterator profileIterator = this.vertexProfileMap.entrySet().iterator();
Map<Profile,List<Profile>> recommendedProfiles = new HashMap<>();
while(profileIterator.hasNext()){
Map.Entry mapElement = (Map.Entry)profileIterator.next();
Profile currentUser = (Profile) mapElement.getValue();
List<Profile> recommendedPerProfile = new ArrayList<>();
// iterate over all of the friends of the current user
for(int i = 0, endI = currentUser.numOfFriends(); i < endI; i++){
Profile checkedFriend = currentUser.getFriend(i);
// iterate over all of the friends of the currently checked friend
for(int j = 0; endJ < checkedFriend.numOfFriends(); j < endJ; ++j) {
Profile possibleRecommendation = checkedFriend.getFriend(j);
// add a recommended profile if it belongs to a friend of a friend, but is not a friend of the current user
if(!currentUser.containsProfile(possibleRecommendation)) {
recommendedPerProfile.add(possibleRecommendation);
}
}
}
// remove possible duplicate recommendations
recommendedProfiles = recommendedProfiles.stream()
.distinct()
.collect(Collectors.toList());
// map the current user to a list of friend recommendations
recommendedProfiles.put(currentUser, recommendedPerProfile);
}
return recommendedProfiles;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.