Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 广度优先搜索无法找到确实存在的目标_C++_Algorithm_Breadth First Search_Adjacency Matrix - Fatal编程技术网

C++ 广度优先搜索无法找到确实存在的目标

C++ 广度优先搜索无法找到确实存在的目标,c++,algorithm,breadth-first-search,adjacency-matrix,C++,Algorithm,Breadth First Search,Adjacency Matrix,所以我一直在研究一个给定起始和结束节点的路径。然而,在某些情况下,它似乎失败了,无法获得路径,我知道这是可能的,因为深度优先搜索和目视检查表明它应该存在 我有一个邻接矩阵: 1 2 3 4 5 6 7 8 1 0 20 25 20 0 0 0 0 2 20 0 5 0

所以我一直在研究一个给定起始和结束节点的路径。然而,在某些情况下,它似乎失败了,无法获得路径,我知道这是可能的,因为深度优先搜索和目视检查表明它应该存在

我有一个邻接矩阵:

     1       2       3       4       5       6       7       8

 1   0       20      25      20      0       0       0       0
 2   20      0       5       0       30      0       0       0
 3   25      5       0       13      8       21      0       0
 4   20      0       13      0       0       17      0       0
 5   0       30      8       0       0       33      0       0
 6   0       0       21      17      33      0       0       0
 7   0       0       0       0       0       0       0       10
 8   0       0       0       0       0       0       10      0
其图表如下所示:

这是我的职责:

void Network::BFS(int src, int dest, vector<bool>& visited, vector<int>& path) {
    // The Queue is the core for the BFS.
    queue<int> Queue;
    // Mark current node as visited.
    visited[src] = true;
    Queue.push(src);
    // While queue is not empty.
    while (!Queue.empty()) {
        // Add node to path.
        // Check if we have found the destination yet or not, if we have we do one last push to path and we're done!
        if (Queue.front() == dest) {
            return;
        }
        int top = Queue.front();
        path.push_back(Queue.front());
        // Pop off front.
        Queue.pop();
        // Iterate and process all none visited nodes.
        for (int node = 0; node < amountOfNodes; node++) {
            // Check if it is not visited already.
            if (visited[node] == false && (adjMatrix[node * amountOfNodes + src] != 0)) {
                Queue.push(node); // Add to end.
                visited[node] = true;
            }
        }
    }
}
void Network::BFS(int src、int dest、vector和visted、vector和path){
//队列是BFS的核心。
排队;
//将当前节点标记为已访问。
访问[src]=真;
队列推送(src);
//而队列不是空的。
而(!Queue.empty()){
//将节点添加到路径。
//检查我们是否已经找到了目的地,如果我们已经完成了最后一次推送,我们就完成了!
if(Queue.front()==dest){
返回;
}
int top=Queue.front();
path.push_back(Queue.front());
//从前面跳下来。
Queue.pop();
//迭代并处理所有未访问的节点。
对于(int node=0;node
输入和输出示例:

(6,3)->路径为:6

(1,5)->路径是:1234


如您所见,它根本无法正确计算路径。我的算法哪里出了问题?如何修复它?

BFS涉及以FIFO方式访问相邻节点。一旦您到达一个节点,您就将其所有邻居放入队列中,除非它们已经被访问过

首先,有一个输入错误,您在相邻节点上迭代。您希望遍历
top
列,而不是
src
列:

adjMatrix[node * amountOfNodes + top] != 0
//                               ~~^
其次,当前的
path
实现存储节点的访问顺序,而不是从源到目标的路径。对于后者,您需要存储每个节点的父节点,以便通过从子节点(目的地)到其父节点、祖父母、曾祖父母等恢复最终路径

std::vector<int> parent(amountOfNodes, -1);

//...

if (visited[node] == false && (adjMatrix[node * amountOfNodes + top] != 0))
{
    Queue.push(node); // Add to end.
    visited[node] = true;
    parent[node] = top;
}
std::向量父节点(amountOfNodes,-1);
//...
如果(访问的[node]==false&(adjMatrix[node*amountOfNodes+top]!=0))
{
Queue.push(节点);//添加到末尾。
已访问[节点]=真;
父[节点]=顶部;
}
恢复路径非常简单:

int u = dest;            
do
{
    std::cout << u << " ";
    u = parent[u];
}
while (u != -1);
intu=dest;
做
{
标准::cout