2017-04-30 16 views
0

私はソーシャルネットワークをモデル化しようとしています。このJavaでWebデータ構造を効率的に作成するにはどうすればよいですか?

A Bと友人である、C、D
BはAと友達である、D
Cが
DはAと友達であると友達であると同様に、B

はどのようにして行くのですこれをJavaで実装します。私はこのように動作2つの入力取る機能記述する必要があり:

AreFriends(A、D)= 1
AreFriends(C、D)= 0

+1

読みます。 –

答えて

1

使用して、Adjacency listようなデータ構造はすべて格納します関係。しかし、そのためには、すべての文字列をStringを一意に識別できる同等の整数に変換する必要があります。そこにはmapを使用し、それに応じて整数をStringsに割り当てます。接続を保存するにはArrayList of HashSetを使用できます。

import java.util.*; 

class Main 
{ 
    static HashMap<String,Integer> map; 
    static ArrayList<HashSet<Integer>> list; 

    static boolean check(String id1,String id2) //To check if a connection exists or not 
    { 
     int index1=map.get(id1); 
     int index2=map.get(id2); 

     return list.get(index1).contains(index2); 
    } 

    public static void main(String args[]) 
    { 
     Scanner sc=new Scanner(System.in); 

     ArrayList<String> ids=new ArrayList<String>(); 
     ids.add("A");ids.add("B");ids.add("C");ids.add("D"); //Taking all possible IDs 

     map=new HashMap<String,Integer>(); 

     int given_id=0; 

     for(String id:ids) 
     { 
      if(!map.containsKey(id)) 
      { 
       map.put(id,given_id);   //Assigning each String a unique ID 
       given_id++; 
      } 
     } 

     list=new ArrayList<HashSet<Integer>>(); //ArrayList of HashSet is used to store the connections 

     for(int i=0;i<given_id;i++) 
     { 
      list.add(new HashSet<Integer>()); 
     } 

     //Now for example, we store the connections in HashSet 

     String connections="A B A C A D B A B D C A D A D B"; //You can change the following loop as per your need 
     String arr[]=connections.split(" "); 

     for(int i=0;i<arr.length;i+=2) 
     { 
      int index1=map.get(arr[i]); 
      int index2=map.get(arr[i+1]); 

      list.get(index1).add(index2);  //Adding connection in both IDs 
      list.get(index2).add(index1); 
     } 

     if(check("A","D")) 
      System.out.println("A and D are friends!"); 
     else 
      System.out.println("No, A and D are not friends!"); 

     if(check("C","D")) 
      System.out.println("C and D are friends"); 
     else 
      System.out.println("No, C and D are not friends!"); 
    } 
} 

出力:グラフデータ構造について

A and D are friends! 
No, C and D are not friends! 
+1

エレガントでシンプルです。ありがとう。 –