构造三角形DAG

时间:2014-06-11 23:56:34

标签: directed-acyclic-graphs

受项目欧拉问题67(https://projecteuler.net/problem=67)的启发,我想构建一个有向无环的数据图。我已经使用2D数组解决了这个问题,但我对使用输入文本构建DAG的算法感到好奇。

除了节点可以共享子节点之外,它看起来与二叉树非常相似。我曾想过通过使用BFS插入节点并在前两个开放点中插入节点,但这不适用于三角形的边缘。

这是我想要构建的DAG的简单可视化,所有边缘都指向下方。显示从文件中读取值的顺序的数字。

                             1
                            / \
                           2   3
                          / \ / \
                         4   5   6

赞赏任何意见。

1 个答案:

答案 0 :(得分:0)

我使用DAG解决了它:

public class Problem067 {

    private static BinaryTreeNode<Long> root = null;
    private static LinkedList<LinkedList<BinaryTreeNode<Long>>> data = new LinkedList<LinkedList<BinaryTreeNode<Long>>>();

    private static void readFile() {
        try {
            BufferedReader in = new BufferedReader(new FileReader(
                    "inputs//Problem067.txt"));
            String str;
            while ((str = in.readLine()) != null) {
                LinkedList<BinaryTreeNode<Long>> list = new LinkedList<BinaryTreeNode<Long>>();
                for (String num : str.split(" ")) {
                    list.add(new BinaryTreeNode<Long>(Long.parseLong(num)));
                }
                data.add(list);
            }
            in.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    private static void buildTree() {
        root = data.get(0).get(0);

        for (int i = 0; i < data.size() - 1; i++) {
            for (int j = 0; j < data.get(i).size(); j++) {
                data.get(i).get(j).setLeft(data.get(i + 1).get(j));
                data.get(i).get(j).setRight(data.get(i + 1).get(j + 1));
            }
        }
    }

    private static void findMaxRoute(BinaryTreeNode<Long> node) {
        node.setVisited(true);

        if (node.getLeft() == null) {
            return;
        }

        if (!node.getLeft().getVisited()) {
            findMaxRoute(node.getLeft());
        }

        if (!node.getRight().getVisited()) {
            findMaxRoute(node.getRight());
        }

        node.setValue(node.getValue()
                + Math.max(node.getLeft().getValue(), node.getRight()
                        .getValue()));
    }

    public static void main(String[] args) {
        readFile();
        buildTree();
        findMaxRoute(root);
        System.out.println(root.getValue());
    }
}

这是树节点:

    public class BinaryTreeNode<T> {
    private BinaryTreeNode<T> father = null;
    private BinaryTreeNode<T> left = null;
    private BinaryTreeNode<T> right = null;
    private T value = null;
    private Boolean visited = false;

    public BinaryTreeNode(T val) {
        value = val;
    }

    public BinaryTreeNode<T> getLeft() {
        return left;
    }

    public void setLeft(BinaryTreeNode<T> left) {
        this.left = left;
    }

    public BinaryTreeNode<T> getRight() {
        return right;
    }

    public void setRight(BinaryTreeNode<T> right) {
        this.right = right;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public BinaryTreeNode<T> getFather() {
        return father;
    }

    public Boolean getVisited() {
        return visited;
    }

    public void setVisited(Boolean visited) {
        this.visited = visited;
    }

}