java - 如何比较图中的边以实现像 facebook 这样的网络图中的三元闭包

标签 java graph undirected-graph

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

一旦完成,我将尝试实现一个推荐的方法:
两个不是 friend 但可能希望基于关系成为 friend 的配置文件是一个三元闭包,但到目前为止我失败得很厉害,我现在被困住了。

  • 目前,我能够使用 Matrix 创建图形,但我被卡住了 试图找到一个可能的解决方案来为每个顶点推荐可能的 friend ,这些顶点有一个 friend j 有一个 friend x 不是 a 顶点的 friend (当前个人资料)。
  • 推荐的每一个 friends 应该被存储到一个数组中并添加到一个 二叉搜索树。

    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;
    }
    

    I get a range of errors that I cannot explain:(

最佳答案

基于给定的算法,并假设 Profile 类将有一个名为 containsProfile(Profile p) 的附加方法,它检查 friend 中是否包含给定的配置文件用户列表,以下暴力方法应该有效:

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;

关于java - 如何比较图中的边以实现像 facebook 这样的网络图中的三元闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55779578/

相关文章:

java - 使用Java在android中加载DXT纹理文件

java - 通过stmt.executeUpdate更新access数据库中的整数

使用 Neo4j 图形数据库的图形分区算法

ruby - 寻找许多连接的交集(图形算法?)

algorithm - 具有某些公共(public)边的两个图上的最小生成树

algorithm - 哪种技术将用于在计算机程序中表示一个非常大的无向图以进行操作?

java - 集合未充满 OneToMany 关系 - EclipseLink

所有线程的 Java 未捕获全局异常处理程序

javascript - 根据它们之间的距离绘制 4 个点?

algorithm - 与 3 人握手引理?