简体   繁体   English

Java中的加权图实现

[英]Weighted Graph Implementation in Java

I just started taking the algorithms and data structures course.我刚开始学习算法和数据结构课程。 I've been trying to implement weighted graph for a few hours, but I haven't been able to add weights for edges.几个小时以来,我一直在尝试实现加权图,但我无法为边添加权重。 What would you suggest I do?你会建议我做什么?

Here is my code:这是我的代码:

public class Graph {
    int numberOfNodes = 0;
    Hashtable<Integer, ArrayList<Integer>> adjacentList = new Hashtable<>();

    public void addNode(int node) {
        adjacentList.put(node, new ArrayList<>());
        numberOfNodes++;
    }

    public void addEdge(int node1, int node2) {
        adjacentList.get(node1).add(node2);
        adjacentList.get(node2).add(node1);
    }

    public void showConnections() {
        Object[] keys = adjacentList.keySet().toArray();
        for (Object key : keys) {
            System.out.println(key + " ---> " + adjacentList.get(Integer.parseInt(key.toString())));
        }
    }

    public static void main(String[] args) {
        Graph graph = new Graph();
        graph.addNode(5);
        graph.addNode(54);
        graph.addNode(44);
        graph.addEdge(5,54);
        graph.addEdge(5,44);
        graph.showConnections();
    }
}
public final class Graph {

    private final Map<Integer, Set<Integer>> adjacentList = new HashMap<>();
    private final Map<Integer, Map<Integer, Double>> weights = new HashMap<>();

    public void addNode(int node) {
        adjacentList.computeIfAbsent(node, key -> new TreeSet<>());
    }

    public void addEdge(int one, int two, double weight) {
        addDirectedEdge(one, two, weight);
        addDirectedEdge(two, one, weight);
    }

    private void addDirectedEdge(int one, int two, double weight) {
        addNode(one);
        addNode(two);
        adjacentList.get(one).add(two);
        setWeight(one, two, weight);
    }

    private void setWeight(int one, int two, double weight) {
        weights.computeIfAbsent(one, key -> new HashMap<>());
        weights.get(one).put(two, weight);
    }

    public void showConnections() {
        for (Map.Entry<Integer, Set<Integer>> entry : adjacentList.entrySet()) {
            int one = entry.getKey();

            System.out.print(entry.getKey() + " ---> [");
            boolean comma = false;

            for (int two : entry.getValue()) {
                if (comma)
                    System.out.print(", ");

                comma = true;
                double weight = weights.get(one).get(two);
                System.out.format(Locale.ENGLISH, "%d (weight: %.2f)", two, weight);
            }

            System.out.println(']');
        }
    }

    public static void main(String... args) {
        Graph graph = new Graph();
        graph.addNode(5);
        graph.addNode(54);
        graph.addNode(44);
        graph.addEdge(5, 54, 1.1);
        graph.addEdge(5, 44, 2.2);
        graph.showConnections();
    }
}

Output: Output:

5 ---> [44 (weight: 2.20), 54 (weight: 1.10)]
54 ---> [5 (weight: 1.10)]
44 ---> [5 (weight: 2.20)]

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

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