【问题标题】:Breadth First Traversal using adj Matrix使用 adj 矩阵的广度优先遍历
【发布时间】:2019-06-25 10:29:02
【问题描述】:

我正在为下图编写广度优先、深度优先和深度优先递归遍历:

据我了解,遍历应该是 0 1 3 6 4 5 2...但我只得到深度优先遍历,对于 dfs(递归)和 BFS,我得到 0 1 3 6 2 4 5. 我不知道哪个是正确的,我不知道我需要做什么来解决这个问题。

    public void depthFirst(int vFirst,int n, int[] isvisited)
   {       //vFirst = 0, n = 6
  int v,i;
  // st is a stack
  st.push(vFirst);

  while(!st.isEmpty())
  {
      v = st.pop();
      if(isvisited[v]==0)
      {
          System.out.print(v);
          isvisited[v]=1;
      }
      for ( i = 0; i <= n; i++)
      {
          if((adjMatrix[v][i] == 1) && (isvisited[i] == 0))
          {
              st.push(v);
              isvisited[i]=1;
              System.out.print(" " + i);
              v = i;
          }
      }
  }

}

public void depthFirstRecursive(int w) {
    int j;     //w = 0;

    visited[w] = 1;
    if (w == 0) {
        System.out.print(w + " ");
    }

    for (j = 0; j <= 6; j++) {
        if ((adjMatrix[w][j] == 1) && (visited[j] == 0)) {
            System.out.print(j + " ");

            depthFirstRecursive(j);
        } 

    } 
}

public void breadthFirst(int first, int p) {
    int e;     // first = 0; p = 6
    int[] nodeVisited = new int[7];
    que.add(first);


   while (!que.isEmpty()) {
       e = que.remove();
       if(nodeVisited[e]==0)
          {
              System.out.print(e);
              nodeVisited[e]=1;
          }
       for (int i = 0; i <= p; i++)
          { 

              if((adjMatrix[e][i] == 1) && (nodeVisited[i] == 0))
              {
                  que.add(e);
                  nodeVisited[i]=1;
                  System.out.print(" " + i);
                  e = i;
              } 
          }

   }



}


public static void main(String[] args) {



                        // 1  2  3  4  5  6  7
    int[][] adjMatrix = { {0, 1, 1, 0, 0, 0, 0},
                          {1, 0, 0, 1, 1, 1, 0},
                          {1, 0, 0, 0, 0, 0, 1},
                          {0, 1, 0, 0, 0, 0, 1},
                          {0, 1, 0, 0, 0, 0, 1},
                          {0, 1, 0, 0, 0, 0 ,0},
                          {0, 0, 1, 1, 1, 0, 0}  };


        new myGraphs(adjMatrix);
 }

【问题讨论】:

  • stque 是如何定义的?
  • 你的图是定向的吗?如果不是,则很难为无向图得到一个正确的结果。
  • @ThomasJungblut st 是堆栈,que 是队列 = linkedlist
  • @Ravi 我不认为它是定向的,如果两个数字相连,上面的邻接矩阵显示为 1。我应该如何或应该如何指导它?
  • @RaviBhatt 为什么需要有向图?

标签: java breadth-first-search


【解决方案1】:

关于在 BFS 中跟随 sn-p:

que.add(e);
nodeVisited[i]=1;
System.out.print(" " + i);
e = i;

他们为什么要更改e 并将e 添加到队列中?这对我来说似乎不正确。

【讨论】:

  • 这就是我能够搜索邻接矩阵的方式,如果我不更改 e 它只会搜索第 0 列。
  • @TMan 否。您在第一循环行中更改 ee = que.remove();,这就是 BFS 的工作方式。更深入地检查BFS
  • 好吧,但是所有的遍历方法都应该打印出来吗?
  • @TMan,没有。 BFS 应该首先访问顶点 0,然后访问距离 0 为 1 的所有顶点。这样的顶点是 1 和 2。接下来,它应该访问距离顶点 0 的距离等于 2 的顶点。这样的顶点是 3 4 5 6。你的 DFS 似乎给出正确答案。
  • 感谢您的回复,非常感谢。通过查看我的广度优先搜索,我会说我是接近还是远离正确?这给我带来了比应有的更多的麻烦。
【解决方案2】:

使用队列的非递归 BFS:

public int[] breadthFirstSearch(int[][] adjacencyMatrix, int start) {
    int totalNumberOfvertices = adjacencyMatrix.length;
    boolean[] visited = new boolean[totalNumberOfvertices];
    Queue<Integer> queue = new LinkedList<>();

    queue.add(start);
    visited[start] = true;

    List<Integer> list = new ArrayList<>();

    while (!queue.isEmpty()) {
        list.add(queue.peek());
        int currentFirstElementInQueue = queue.poll();

        for (int i = 0; i < adjacencyMatrix.length; i++) {
            if ((adjacencyMatrix[currentFirstElementInQueue][i] == 1) && (!visited[i])) {
                queue.add(i);
                visited[i] = true;
            }
        }
    }

    int[] result = new int[list.size()];
    int i = 0;
    Iterator<Integer> itr = list.iterator();
    while (itr.hasNext()) {
        result[i++] = itr.next();
    }
    return result;
}

【讨论】:

    【解决方案3】:
    public void BFS(int start)
    {
        int v=a.length;//a[][] is adj matrix declared globally
        boolean visited[]=new boolean[v];//indexing done from 1 to n
        LinkedList<Integer> queue=new LinkedList<Integer>();
        visited[start]=true;
        queue.add(start);
        while(queue.size()!=0)
        {
            int x=queue.remove();
            System.out.print(x+" ");
            for (int i=1; i < v; i++) 
                if((a[x][i] == 1) && (!visited[i]))
                {
                  queue.add(i);
                  visited[i]=true;
                 }
         }
    }
    

    【讨论】:

    • 请评论您的答案。描述解决方案,使其与其他可能的解决方案具有可比性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2016-02-15
    • 2011-07-12
    相关资源
    最近更新 更多