简体   繁体   English

广度优先搜索:需要多少个顶点状态?

[英]Breadth First Search: How many states of a vertex are needed?

I am working on Breadth First Search and I tried to write BFS to print all the edges. 我正在开发广度优先搜索,我尝试编写BFS来打印所有边缘。 The first version is adapted from Algorithm book in which a vertex has 3 states: NOT_VISIT (initial state), VISIT and PROCESSED. 第一个版本改编自Algorithm book,其中顶点有3种状态:NOT_VISIT(初始状态),VISIT和PROCESSED。 A vertex is 'VISIT' when you first see it. 当你第一次看到它时,顶点是'VISIT'。 A vertex is 'PROCESSED' when all of its neighbors are visited. 当访问所有邻居时,顶点是“已处理”。 The second version is the one that I wrote, use only 2 states: Initial state and VISITED. 第二个版本是我写的,只使用2个状态:初始状态和VISITED。 Both work: 两者都有效:

public static void BFS(Graph g, int start) {
    boolean[] visit = new boolean[g.size()];
    boolean[] process = new boolean[g.size()];
    List<Integer> queue = new ArrayList<Integer>();
    queue.add(start);
    visit[start] = true;
    while (!queue.isEmpty()) {
        int currentVertex = queue.remove(0);
        process[currentVertex] = true;
        List<Integer> adj = g.getAdjacents(currentVertex);
        if (adj != null) {
            for (Integer i : adj) {
                if (visit[i] == false) {
                    visit[i] = true;
                    queue.add(i);
                }
                if (process[i] == false) {
                    System.out.println(currentVertex + " --- "
                            + i);

                }
            }
        }
    }
}




public static int BFS2(Graph g, int start) {
    List<Integer> queue = new ArrayList<Integer>();
            boolean[] visited = new boolean[g.size()];
    queue.add(start);
    while (!queue.isEmpty()) {
        int v = queue.remove(0);
        visited[v] = true;// only mark visited[v] = true when processing all
                            // its neighbors
        List<Integer> adj = g.getAdjacents(v);
        if (adj != null) {
            for (Integer i : adj) {
                if (!visited[i]) {
                    queue.add(i);
                    System.out.println(v + " --- "
                            + i);
                }
            }
        }
      }
  }

My question is: When is it necessary to have 3 states for a vertex in BFS ? 我的问题是:什么时候需要在BFS中有一个顶点的3个状态? Can you give an example when we need 3 states ? 当我们需要3个州时,你能举个例子吗?

Usually you add the middle state ("Visit" in your case, commonly "Gray" when using colors to mark nodes) just to visualise how BFS is working. 通常你添加中间状态(在你的情况下为“访问”,当使用颜色标记节点时通常为“灰色”)只是为了可视化BFS的工作方式。 In standard implementation it is not necessary (you may switch to "Visited" without going through the middle state.) 在标准实现中,没有必要(您可以在不经过中间状态的情况下切换到“已访问”。)

You can see it yourself, try to follow BFS (even with paper and pencil you can do it). 你可以自己看看,尝试跟随BFS(即使用纸和铅笔也可以做到)。 You will see that nodes in state "Visit" are equally distanced from source (with maximum difference of 1, to be specific). 您将看到处于“访问”状态的节点与源距离相等(最大差异为1,具体而言)。 For educational purposes it is good to do the same with DFS then (so you can observe the difference between Breadth-First and Depth-First searching). 出于教育目的,最好对DFS进行相同的操作(因此您可以观察广度优先和深度优先搜索之间的区别)。

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

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