-1

I have a csv file and its format:

user1;user2;0.3
user1;user7;0.8
user2;user7;0.3
.....
user78;user36;0.1

Each line has three elements : user_1_ID, user_2_ID, and similar value. I want to write an algorithm to find the mutual relationships among a certain number of users. For example, as we can see, the user1 has the relationship with the user2(user1;user2;0.3), with the user7 (user1;user7;0.8), and the user2 has the relationship with the user7(user2;user7;0.3). So if we want to find which THREE users have the mutual relationships, we should find user1-user2-user7. THREE is just one example, and it may be FIVE, EIGHT as long as this relationship exists. May someone gives me some ideas about how to write this algorithm. Any data structure is acceptable and prefer to implement it in java.

user3369592
  • 1,367
  • 5
  • 21
  • 43
  • I'd suggest looking into graphing libraries: http://stackoverflow.com/questions/51574/good-java-graph-algorithm-library – deyur Apr 14 '15 at 02:42

1 Answers1

1
class Node {
    public static void main(String[] args) {
        HashMap<String, Node> nodes = new HashMap<String, Node>();
        try {
            while (readLine()) {
                String user1 = "user1";
                String user2 = "user2" ;
                int value = 1;
                Node node1 = nodes.get(user1);
                if (node1 == null) {
                    node1 = new Node("user1");
                }
                Node node2 = new Node("user2");
                node1.getNodes().put(node2, value);
                nodes.put("user1", node1);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public HashMap getNodes() {
        return nodes;
    }

    public void setNodes(HashMap nodes) {
        this.nodes = nodes;
    }

    public Node(String key) {
        this.key = key;
    }
    private String key;
    private HashMap nodes = new HashMap<Node, Integer>();
}
chengpohi
  • 14,064
  • 1
  • 24
  • 42