Java 如何比较图中的边来实现三元闭包,就像facebook这样的网络图一样

Java 如何比较图中的边来实现三元闭包,就像facebook这样的网络图一样,java,graph,undirected-graph,Java,Graph,Undirected Graph,我试图实现一个包含与概要文件类相关的顶点(节点)的图(类似于Facebook概要文件,但更平庸)。每个顶点(配置文件)都存储在一个二进制搜索树中,然后加载或存储在一个数组列表中。就像在Facebook配置文件中一样,有些(不是全部,因为不是每个人都有朋友)类Profile的实例将有朋友,朋友是边。图形的每个边关系都存储在一个文本文件中,然后在图形构建过程中通过单独的方法读入并添加到图形中。完成此操作后,我将尝试实现一个推荐的方法:两个不存在的配置文件朋友,但我希望成为朋友,因为这段关系可能是三合

我试图实现一个包含与
概要文件
类相关的顶点(节点)的图(类似于Facebook概要文件,但更平庸)。每个顶点(配置文件)都存储在一个二进制搜索树中,然后加载或存储在一个数组列表中。
就像在Facebook
配置文件中一样,有些(不是全部,因为不是每个人都有朋友)类
Profile
的实例将有朋友,朋友是边。
图形的每个边关系都存储在一个文本文件中,然后在图形构建过程中通过单独的方法读入并添加到图形中。

完成此操作后,我将尝试实现一个推荐的方法:
两个不存在的配置文件朋友,但我希望成为朋友,因为这段关系可能是三合一的结束,但到目前为止我失败得很惨,我现在陷入困境

  • 目前,我能够使用
    矩阵创建一个图形,但我被卡住了
    尝试找到一个可能的解决方案,为每个顶点推荐可能的朋友,这些顶点有一个朋友
    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;
    }
    
    private List getRecommendedFriends(){
    迭代器配置文件迭代器=this.vertexProfileMap.entrySet().Iterator();
    List recommendedProfiles=new ArrayList();
    布尔不包含;
    while(profileIterator.hasNext()){
    Map.Entry mapElement=(Map.Entry)profileIterator.next();
    Profile user=(Profile)mapElement.getValue();
    int-sizeOfList=user.numofriends();
    对于(int i=0;i


基于给定的算法,并假设
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;
private Map getRecommendedFriends(){
//假设vertexProfileMap映射所有配置文件/用户
迭代器配置文件迭代器=this.vertexProfileMap.entrySet().Iterator();
Map recommendedProfiles=新HashMap();
while(profileIterator.hasNext()){
Map.Entry mapElement=(Map.Entry)profileIterator.next();
Profile currentUser=(Profile)mapElement.getValue();
List recommendedPerProfile=新建ArrayList();
//迭代当前用户的所有好友
对于(int i=0,endI=currentUser.numofriends();i

}

对于特定的给定用户,您的算法是否应该查找所有可能相关的朋友,即用户朋友的朋友中没有出现在用户朋友地图中的朋友?算法应该取一个顶点(用户[x])查找用户朋友[y]通过确认他们在矩阵中有边缘关系,并查看用户的朋友[y]是否有与当前用户[x]不是朋友的朋友[z](边缘关系),并将其放入该用户[x]的ArrayOfReconDations中。感谢您提供当前解决方案,我将研究它,看看它是否可以应用。:)Rann LifshitzI在graph类而不是profile类中提出了类似的方法,从配置文件的ArrayList(每个配置文件都是从二进制搜索树添加的)中获取配置文件的名称(字符串)标签(顶点)。我将尝试使用字符串作为输入而不是配置文件对象来实现您的解决方案,看看它是否有效。@BrevynKurt:很乐意提供帮助。注意上面实现中的假设-
vertexProfileMap
是所有现有配置文件的映射。哦,是的,我的错。关于变量endI和endJ,我还有一个问题,它们到底是如何在条件中实现的?它们是在其他地方声明的还是?@BrevynKurt:endI和endJ是我多年来一直使用的迭代约定。对for循环进行了轻微的优化,通过与迭代器(i和j)一起声明,它们允许在for循环期间只对for循环的结束条件求值一次,并提高了可读性IMHO。对,这是有意义的。因此,我的实现必须将for循环的翻译转换为普通标准。提前感谢:)