【问题标题】:BFS Shortest Path: Edge weight either 1 or 2BFS 最短路径:边权重为 1 或 2
【发布时间】:2012-11-19 22:16:55
【问题描述】:

我正在尝试使用 BFS 实现最短路径算法。那就是我试图找到从指定顶点到每个其他顶点的最短路径。但是,它是一种特殊情况,所有边权重都是 1 或 2。 我知道可以使用 Dijkstra 算法完成,但我必须使用广度优先搜索。

到目前为止,我有一个 BFS 的工作版本,它首先搜索与权重为 1 的边相连的顶点。如果找不到,则返回与权重为 2 的边相连的顶点。经过考虑,这不是找到最短路径的正确方法。 问题是我想不出为什么 BFS 会使用权重 1 或 2,而不是任何权重。

代码如下:

public void addEdge(int start, int end, int weight)
  {
  adjMat[start][end] = 1;
  adjMat[end][start] = 1;
  edge_weight[start][end] = weight; 
  edge_weight[end][start] = weight; 
  }

// -------------------------------------------------------------
public void bfs()                   // breadth-first search
  {                                // begin at vertex 0
  vertexList[0].wasVisited = true; // mark it
  displayVertex(0);                // display it
  theQueue.insert(0);              // insert at tail
  int v2;

  while( !theQueue.isEmpty() )     // until queue empty,
     {
     int v1 = theQueue.remove();   // remove vertex at head
     // until it has no unvisited neighbors
     while( (v2=getAdjUnvisitedVertex(v1)) != -1 ){// get one,
        vertexList[v2].wasVisited = true;  // mark it
        displayVertex(v2);                 // display it
        theQueue.insert(v2);               // insert it
        }
     }  // end while(queue not empty)

  // queue is empty, so we're done
  for(int j=0; j<nVerts; j++)             // reset flags
     vertexList[j].wasVisited = false;
  }  // end bfs()
// -------------------------------------------------------------
// returns an unvisited vertex adj to v -- ****WITH WEIGHT 1****
public int getAdjUnvisitedVertex(int v) {
    for (int j = 0; j < nVerts; j++)
        if (adjMat[v][j] == 1 && vertexList[j].wasVisited == false && edge_weight[v][j] == 1){
            //System.out.println("Vertex found with 1:"+ vertexList[j].label);
            return j;
        }
    for (int k = 0; k < nVerts; k++)
        if (adjMat[v][k] == 1 && vertexList[k].wasVisited == false && edge_weight[v][k] == 2){
            //System.out.println("Vertex found with 2:"+vertexList[k].label);
            return k;
        }
    return -1;
}  // end getAdjUnvisitedVertex()
   // -------------------------------------------------------------
}  
////////////////////////////////////////////////////////////////
public class BFS{
public static void main(String[] args)
  {
  Graph theGraph = new Graph();
  theGraph.addVertex('A');    // 0  (start for bfs)
  theGraph.addVertex('B');    // 1
  theGraph.addVertex('C');    // 2

  theGraph.addEdge(0, 1,2);     // AB
  theGraph.addEdge(1, 2,1);     // BC
  theGraph.addEdge(2, 0,1);     // AD


  System.out.print("Visits: ");
  theGraph.bfs();             // breadth-first search
  System.out.println();
  }  // end main()
   }

那么问题是,我不知道为什么 BFS 可以解决权重为 1 或 2 的最短路径问题,而不是任何权重的任何边。

感谢任何帮助。 谢谢!

编辑:问题是:你希望找到从 s 到其余顶点的最短路径。这可以使用 Dijkstra 算法完成,但您需要使用广度优先搜索策略。您应该能够做到这一点,因为任何边权重都是 1 或 2。描述 BFS 的更改将解决您的问题

【问题讨论】:

    标签: java graph-algorithm shortest-path breadth-first-search


    【解决方案1】:

    边权重限制对BFS的意义在于,它保证如果从A到B有边,那么从A到B没有更短的路径。AB边的最大权重为2。最小总和从 A 到 C 到 B 的路径的权重也是 2。

    【讨论】:

    • 感谢您的帮助!关于如何在 BFS 算法中实现这一点的任何帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多