简体   繁体   English

使用哈希图表示加权图

[英]representing a weighted graph using hashmap

My input text file: 我的输入文本文件:

  122334 
  45   
  67

Output should be: 输出应为:

0-{1=2,2=3,3=4} //node 0 is connected to 1 with weight 2,to 2 with weight 3,etc

1-{4=5}

2-{6=7}

Below is my program: 下面是我的程序:

public class BFS {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        HashMap<Integer, HashMap<Integer, Integer>>hash1=new HashMap<Integer,     HashMap<Integer,Integer>>();
        HashMap<Integer, Integer>hash2=new HashMap<Integer, Integer>();
        HashMap<Integer, Integer>temp1=new HashMap<Integer, Integer>();
    BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Translab\\workspace\\Algorithms\\inputfile.txt"));
        int remainder,remainder2,j=0;
        int line=1;
        String str;
        while ((str =in.readLine()) != null) {
            int foo = Integer.parseInt(str);
          while(foo>0){
                 remainder=foo % 10;
                   foo = foo / 10;
                 remainder2=foo%10;
                 foo=foo/10;
                 temp1.put(remainder2, remainder);
                 hash2.putAll(temp1);

        }
        temp1.clear();
        hash1.put(j, hash2);

        j++;


        }
        for (Entry<Integer, HashMap<Integer, Integer>> entry  : hash1.entrySet()) {
            System.out.println(entry.getKey()+"-"+entry.getValue() );

        }
        in.close();
    }
}

With this am getting: 随着这个越来越:

0-{1=2, 2=3, 3=4, 4=5, 6=7}

1-{1=2, 2=3, 3=4, 4=5, 6=7}

2-{1=2, 2=3, 3=4, 4=5, 6=7}

Can some one please help me-am new to hashmap. 有人可以帮我吗-是hashmap的新手。

You are unnecessarily overloading Map and Integer classes. 您不必要地重载了Map和Integer类。 Much better would be to encapsulate your graph nodes and edges in classes - this will make things much easier once you start having to perform operations on the graph: 更好的方法是将图形节点和边封装在类中-一旦开始对图形执行操作,这将使事情变得容易得多:

class Node {
    private int id;
    private List<Edge> edges;
    public boolean hasId(int id) {
        return id == this.id;
    }
    public addEdge(Node node, int weight) {
        edges.add(new Edge(node, weight));
    }
}

class Edge {
    private int weight;
    private Node destination;
}

class Graph {
    private List<Node> nodes;
    public Node getNodeById(int id) {
        return nodes.stream()
            .filter(node -> node.hasId(id))
            .findFirst().orElse(null);
    }
}

I would also suggest that you break your input lines into tokens before the conversion to id and weight. 我还建议您在将输入行转换为ID和权重之前将其分成令牌。 This could be done inside the Graph class: 这可以在Graph类中完成:

public void addEdge(int fromNodeID, String edgeData) {
    Node node = getNodeByID(fromNodeID);
    for (int pos = 0; pos < edgeData.length(); pos += 2) {
        int destNodeID = Integer.parseInt(edgeData.substring(pos, pos+1));
        int weight = Integer.parseInt(edgeData.substring(pos + 1, pos + 2);
        node.addEdge(getNodeByID(destNodeID), weight);
    }
}

Then processing input is fairly simple: 然后,处理输入非常简单:

int id = 0;
Map<Integer, String> edgeData = new HashMap<>();
while((line = in.readLine()) != null) {
    graph.addNode(new Node(id));
    put(id, line);
}
edgeData.entrySet().stream()
    .forEach(entry -> graph.addEdge(entry.getKey(), entry.getValue());

I've left out a lot of error checking, simple constructors etc. but hopefully you get the idea. 我遗漏了很多错误检查,简单的构造函数等,但是希望您能理解。

Here I am assuming your parsing logic is correct and that's what you want. 在这里,我假设您的解析逻辑正确,这就是您想要的。 You need to initialize the hash2 within the first while loop and you really don't need temp1 at all. 您需要在第一个while循环中初始化hash2while您实际上根本不需要temp1

int j=0;    
while ((str =in.readLine()) != null) {
            int foo = Integer.parseInt(str);
          HashMap<Integer, Integer> hash2=new HashMap<Integer, Integer>();
          while(foo>0){
                 remainder=foo % 10;
                   foo = foo / 10;
                 remainder2=foo%10;
                 foo=foo/10;
                 hash2.put(remainder2, remainder);
          }
          hash1.put(j, hash2);
          j++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM