图的遍历

图的深度遍历(BFS):

思路:

创建一个队列,用于存储点,创建一个HashSet来存储已经加入队列中的点,防止遍历的过程中重复遍历。遍历开始,选出当前队列的任意一个点,进入队列和set,然后弹出这个点并打印,然后把所有和这个点连通的点如果这些点在set里没有,就入队列,同一级别的连通点的入队列先后顺序不限定,不影响,比如点1连通了2,3,4;这三个点的入队列顺序无所谓

代码:

    public static void bfs(Node node) {
        if (node == null) {
            return;
        }
        Queue queue = new LinkedList<>();
        HashSet map = new HashSet<>();
        queue.add(node);
        map.add(node);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.println(cur.value);
            for (Node next : cur.nexts) {
                if (!map.contains(next)) {
                    map.add(next);
                    queue.add(next);
                }
            }
        }
    }

图的广度优先遍历(DFS):

思路:

一条路打印到底,然后看父节点有没有其它的分叉,有就继续打印到底。一个栈用来存节点,另一个set存该节点是否已经存在,首先头节点入栈,打印,然后当栈不为空的时候,栈顶出栈,然后找出栈顶的nexts们,即后继节点,看看set里是否存在,不存在说明还没遍历,就把当前结点和它的一个next入栈,并打印next,然后结束本次while,然后依次执行,因为,在打印next后直接break了就能实现一条路打印到底

代码:

public static void dfs(Node node) {
        if (node == null) {
            return;
        }
        Stack stack = new Stack<>();
        HashSet set = new HashSet<>();
        stack.add(node);
        set.add(node);
        System.out.println(node.value);
        while (!stack.isEmpty()) {
            Node cur = stack.pop();
            for (Node next : cur.nexts) {
                if (!set.contains(next)) {
                    stack.push(cur);
                    stack.push(next);
                    set.add(next);
                    System.out.println(next.value);
                    break;
                }
            }
        }
    }

你可能感兴趣的:(图的遍历)