C++ 在C+中使用BFS的最短路径+;

C++ 在C+中使用BFS的最短路径+;,c++,algorithm,graph,C++,Algorithm,Graph,我必须写一个算法,在无权图中寻找最短路径。我发现,在未加权图中找到最短路径的最佳方法是简单地执行BFS并在到达目的地后停止。问题是我不知道如何追踪通往那个目的地的路径 到目前为止,我一直在考虑为发现的每个新节点创建一个新的路径列表,但我不知道如何实现它 这段代码似乎在访问每个节点时都能正常工作(很抱歉,有点凌乱): void shortestPath(字符串开始、字符串结束、列表adjList){ 队列我的队列; listlistVertices=initializeVertices(开始,调整

我必须写一个算法,在无权图中寻找最短路径。我发现,在未加权图中找到最短路径的最佳方法是简单地执行BFS并在到达目的地后停止。问题是我不知道如何追踪通往那个目的地的路径

到目前为止,我一直在考虑为发现的每个新节点创建一个新的路径列表,但我不知道如何实现它

这段代码似乎在访问每个节点时都能正常工作(很抱歉,有点凌乱):

void shortestPath(字符串开始、字符串结束、列表adjList){
队列<列表<列表>::迭代器>我的队列;
listlistVertices=initializeVertices(开始,调整列表);
list::迭代器aux=extractList(开始,列表顶点);
myQueue.push(aux);
while(myQueue.size()>0){
list::迭代器vert=myQueue.front();
对于(列表::迭代器it=vert->begin();it!=vert->end();it++){
如果((*it)->颜色==“白色”){
(*it)->color=“gray”;
push(extractList((*it)->name,listVertexts));
}
}
列表::迭代器vertax=vert->begin();
(*Vertax)->color=“黑色”;
myQueue.pop();
}
}

有什么想法吗?

您可以通过在最短路径树中为每个顶点
v
保留
v
父节点的名称来存储最短路径树。然后,您可以按照这些父指针重建所需的最短路径,直到到达源顶点。

您可以通过在最短路径树中为每个顶点保留
v
的父节点的名称来存储最短路径树。然后,您可以按照这些父指针重建所需的最短路径,直到到达源顶点。

顶点*父项添加到顶点类中,不管是什么,然后向
推送
函数中再添加一个输入
*顶点
,并更改此行:

push(extractList((*it)->name,listVertexts))

为此:

push(extractList((*it)->name,listVertexts),*vert)


在您
myQueue.pop()之后
检查
poped
节点是否是您的目的地如果是,从while循环中断,从您的目的地开始,每个
node->parent
进行循环打印(或执行任何操作),然后
node=node->parent
,直到到达源。

vertex*parent
添加到vertex类中,无论是什么,并将另一个输入
*顶点
添加到
推送
功能中,并更改此行:

//Breadth first Search Shortest Path
//It is implemented using Queue Linked list representation
//It is used to pind the shortest path from destination to the source

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

typedef struct queue1{
    int info;
    struct queue1 *next;
}node;

void createqueue(node **front1,node **rear1){
    *front1=*rear1=NULL;
}

void pushqueue(node **front1, node **rear1,int item){
    node *ptr;
    ptr=(node *)malloc(sizeof(node));
    ptr->info=item;
    ptr->next=NULL;

    if((*front1)==NULL)
        *front1=*rear1=ptr;
    else{
        (*rear1)->next=ptr;
        *rear1=ptr;
    }
}

int popqueue(node **front1){

int item;
    node *ptr;
    ptr=*front1;
    item=ptr->info;
    *front1=ptr->next;

return item;
}

int peekqueue(node *front1){
    return front1->info;
}

bool isEmpty(node *front1){
    if(front1==NULL)
        return true;
return false;
}

int main(){

    int graph[7][7]={
                {0,1,1,0,0,0,0},
                {1,0,0,1,1,0,0},
                {1,0,0,0,0,1,1},
                {0,1,0,0,0,0,0},
                {0,1,0,0,0,0,0},
                {0,0,1,0,0,0,0},
                {0,0,1,0,0,0,0},
             };

    int src;
    cout << "Following is Breadth First Traversal (starting from vertex 0) \n";
    cin>>src;

    int parent[10];
    parent[src]=-1; //Source is the root node

    cout<<"Breadth First Search\n";

node *front1;
node *rear1;
int choice,element,temp;

createqueue(&front1,&rear1);
pushqueue(&front1,&rear1,src);

bool visited[7];

for(int i=0;i<7;i++) //Mark all nodes as visited as false
    visited[i]=false;


    while(!isEmpty(front1)){
        src=popqueue(&front1);
        cout<<src<<"\t";
        if(!visited[src]){
            for(int i=0;i<7;i++){
                if(graph[src][i] && !visited[i]){
                    parent[i]=src;        //TO get the predessecor of the node which will help in backtracking
                    pushqueue(&front1,&rear1,i);
                }
            }
        visited[src]=true;
        }
    }

int destination;
cout<<"\nEnter destination = \n";
cin>>destination;

int j=destination;
cout<<"Path from "<<j<<" to root node is as follows\n";

//Backtrack from destination to root node
do{
    cout<<""<<j<<"\t";
    j=parent[j];
}while(j!=-1);

cout<<"Thank You\n";

return 0;
}
push(extractList((*it)->name,listVertexts))

为此:

push(extractList((*it)->name,listVertexts),*vert)

在您
myQueue.pop()之后
检查
poped
节点是否是您的目的地如果是,从while循环中断,从您的目的地开始,每个
节点->父节点-/code>进行循环打印(或执行任何操作),然后
节点=节点->父节点-/code>,直到您到达源节点。

//广度优先搜索最短路径
//Breadth first Search Shortest Path
//It is implemented using Queue Linked list representation
//It is used to pind the shortest path from destination to the source

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

typedef struct queue1{
    int info;
    struct queue1 *next;
}node;

void createqueue(node **front1,node **rear1){
    *front1=*rear1=NULL;
}

void pushqueue(node **front1, node **rear1,int item){
    node *ptr;
    ptr=(node *)malloc(sizeof(node));
    ptr->info=item;
    ptr->next=NULL;

    if((*front1)==NULL)
        *front1=*rear1=ptr;
    else{
        (*rear1)->next=ptr;
        *rear1=ptr;
    }
}

int popqueue(node **front1){

int item;
    node *ptr;
    ptr=*front1;
    item=ptr->info;
    *front1=ptr->next;

return item;
}

int peekqueue(node *front1){
    return front1->info;
}

bool isEmpty(node *front1){
    if(front1==NULL)
        return true;
return false;
}

int main(){

    int graph[7][7]={
                {0,1,1,0,0,0,0},
                {1,0,0,1,1,0,0},
                {1,0,0,0,0,1,1},
                {0,1,0,0,0,0,0},
                {0,1,0,0,0,0,0},
                {0,0,1,0,0,0,0},
                {0,0,1,0,0,0,0},
             };

    int src;
    cout << "Following is Breadth First Traversal (starting from vertex 0) \n";
    cin>>src;

    int parent[10];
    parent[src]=-1; //Source is the root node

    cout<<"Breadth First Search\n";

node *front1;
node *rear1;
int choice,element,temp;

createqueue(&front1,&rear1);
pushqueue(&front1,&rear1,src);

bool visited[7];

for(int i=0;i<7;i++) //Mark all nodes as visited as false
    visited[i]=false;


    while(!isEmpty(front1)){
        src=popqueue(&front1);
        cout<<src<<"\t";
        if(!visited[src]){
            for(int i=0;i<7;i++){
                if(graph[src][i] && !visited[i]){
                    parent[i]=src;        //TO get the predessecor of the node which will help in backtracking
                    pushqueue(&front1,&rear1,i);
                }
            }
        visited[src]=true;
        }
    }

int destination;
cout<<"\nEnter destination = \n";
cin>>destination;

int j=destination;
cout<<"Path from "<<j<<" to root node is as follows\n";

//Backtrack from destination to root node
do{
    cout<<""<<j<<"\t";
    j=parent[j];
}while(j!=-1);

cout<<"Thank You\n";

return 0;
}
//它是使用队列链表表示实现的 //它用于确定从目标到源的最短路径 #包括 #包括 #包括 使用名称空间std; typedef结构队列1{ 国际信息; 结构队列1*next; }节点; void createqueue(节点**front1,节点**real1){ *front1=*rear1=NULL; } 无效推送队列(节点**前1,节点**后1,整数项){ 节点*ptr; ptr=(node*)malloc(sizeof(node)); ptr->info=物料; ptr->next=NULL; if((*front1)==NULL) *前1=*后1=ptr; 否则{ (*rear1)->next=ptr; *rear1=ptr; } } int popqueue(节点**front1){ 国际项目; 节点*ptr; ptr=*front1; 项目=ptr->info; *front1=ptr->next; 退货项目; } int peekqueue(节点*front1){ 返回front1->info; } bool isEmpty(节点*front1){ if(front1==NULL) 返回true; 返回false; } int main(){ 整数图[7][7]={ {0,1,1,0,0,0,0}, {1,0,0,1,1,0,0}, {1,0,0,0,0,1,1}, {0,1,0,0,0,0,0}, {0,1,0,0,0,0,0}, {0,0,1,0,0,0,0}, {0,0,1,0,0,0,0}, }; int-src; cout>src; int-parent[10]; 父[src]=-1;//源是根节点 cout
//宽度优先搜索最短路径
//它是使用队列链表表示实现的
//它用于确定从目标到源的最短路径
#包括
#包括
#包括
使用名称空间std;
typedef结构队列1{
国际信息;
结构队列1*next;
}节点;
void createqueue(节点**front1,节点**real1){
*front1=*rear1=NULL;
}
无效推送队列(节点**前1,节点**后1,整数项){
节点*ptr;
ptr=(node*)malloc(sizeof(node));
ptr->info=物料;
ptr->next=NULL;
if((*front1)==NULL)
*前1=*后1=ptr;
否则{
(*rear1)->next=ptr;
*rear1=ptr;
}
}
int popqueue(节点**front1){
国际项目;
节点*ptr;
ptr=*front1;
项目=ptr->info;
*front1=ptr->next;
退货项目;
}
int peekqueue(节点*front1){
返回front1->info;
}
bool isEmpty(节点*front1){
if(front1==NULL)
返回true;
返回false;
}
int main(){
整数图[7][7]={
{0,1,1,0,0,0,0},
{1,0,0,1,1,0,0},
{1,0,0,0,0,1,1},
{0,1,0,0,0,0,0},
{0,1,0,0,0,0,0},
{0,0,1,0,0,0,0},
{0,0,1,0,0,0,0},
};
int-src;
cout>src;
int-parent[10];
父[src]=-1;//源是根节点
库特