使用adj矩阵的广度优先遍历

时间:2011-11-11 22:26:52

标签: java breadth-first-search

我正在编写宽度优先,深度优先,深度优先递归遍历以下图表:

enter image description here

根据我的理解,遍历应该是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);
 }

3 个答案:

答案 0 :(得分:2)

关于BFS中的以下片段:

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

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

答案 1 :(得分:0)

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;
             }
     }
}

答案 2 :(得分:0)

使用队列的非递归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;
}