图中的顶点4度连接-Java Hashmap

时间:2017-03-28 03:49:42

标签: java graph hashmap connection

我的Java代码是做以下事情: (1)建立彼此连接的朋友的图(地图)。 (2)检查在4度连接内是否有任何两个人连接(少于3个边缘),例如A-> B(A& B 1度连接),A-> B-> C(A& C,2度连接),A-> B-> C-> D(A& D,3度连接),A-> B-> CD-> E(A& E,4度连接)。

现在我的Java代码使用Hashmap来构建连接地图的朋友。然后搜索Hashmap,如果两个人(例如,X和Y)在4度连接内连接。 它适用于少数人的地图。当涉及到330万人的朋友地图时,它对于第一,第二和第三连接非常有效。第三次连接搜索在我的电脑上测试了大约20分钟。但对于4度连接,它在50小时后无法完成。

问题:我想知道当涉及300万人的大尺寸图(地图)时,Hashmap是不是正确的方法。如果没有,那么可能的数据结构是什么?

构建好友连接地图:     public HashMap> buildFriendMap(String inputFile){         String paymentRecord = inputFile;

    try{
        FileInputStream prStream = new FileInputStream(paymentRecord);
        Scanner prScanner = new Scanner(prStream);
        while(prScanner.hasNextLine()){

            String line = prScanner.nextLine();
            System.out.println(line);
            String[] columns = line.split(",");
               if(columns.length<3){
                  continue;
               }
            String sender = columns[1];
            String receiver = columns[2];

            if(!(sender.equals(" id1")) && !(receiver.equals(" id2"))){
                if(friendMap.containsKey(sender)==false){
                    friendMap.put(sender,new HashSet<String>());
                }
                friendMap.get(sender).add(receiver);

                if(friendMap.containsKey(receiver) ==false){
                    friendMap.put(receiver,new HashSet<String>());
                }
                friendMap.get(receiver).add(sender);

            }
        }
        prScanner.close();
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return friendMap;

检查两个人是否在4度连接范围内:     public String is4thDegreeFriend(String id1,String id2){

    String sender = id1;
    String receiver = id2;
    String fraudFlag = "Unverified";

    if(!(sender.equals(" id1")) && !(receiver.equals(" id2"))){
        if(friendMap.containsKey(sender)==true){
            if(friendMap.get(sender).contains(receiver)==true){
                fraudFlag = "Trusted";
            }else if(friendMap.get(sender).contains(receiver)==false){
                Iterator index2nd = friendMap.get(sender).iterator();
                while (index2nd.hasNext()){
                    Object idx2nd = index2nd.next();
                    if(friendMap.get(idx2nd).contains(receiver)==true){
                        fraudFlag = "Trusted";
                    }else if(friendMap.get(idx2nd).contains(receiver)==false){
                        Iterator index3rd = friendMap.get(idx2nd).iterator();
                        while (index3rd.hasNext()){
                            Object idx3rd = index3rd.next();
                            if(friendMap.get(idx3rd).contains(receiver)==true){
                                fraudFlag = "Trusted";
                            }else if(friendMap.get(idx3rd).contains(receiver)==false){
                                Iterator index4th = friendMap.get(idx3rd).iterator();
                                while (index4th.hasNext()){
                                    Object idx4th = index4th.next();
                                    if(friendMap.get(idx4th).contains(receiver)==true){
                                        fraudFlag = "Trusted";
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
    }
    return fraudFlag;
}   

谢谢,

1 个答案:

答案 0 :(得分:0)

查看Dijkstra的算法。 https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

您可以将每个朋友的连接视为权重为1的图表边缘。