我正试图找到一个使用BFS alghoritm的最短路。例如 我添加一个点来映射
add("berlin",london")
add("budapest","london")
add("london","glassgow")
add("budapest","moscow")
find("berlin",moscow") // should return berlin , london , budapest,moscow
我已经定义了一个队列
struct node {
string info;
node *next;
};
/*----------------------------------------------------*/
class Queue {
private:
node *front;
node *rear;
public:
Queue();
~Queue();
bool isEmpty();
void enqueue(string );
string dequeue();
void display();
bool find(string);
};
/*----------------------------------------------------*/
void Queue::display(){
node *p = new node;
p = front;
if(front == NULL){
cout<<"Nothing to Display" << endl;
}else{
while(p!=NULL){
cout<<p->info << endl;
p = p->next;
}
}
}
/*----------------------------------------------------*/
Queue::Queue() {
front = NULL;
rear = NULL;
}
/*----------------------------------------------------*/
Queue::~Queue() {
delete front;
}
/*----------------------------------------------------*/
void Queue::enqueue(string data) {
node *temp = new node();
temp->info = data;
temp->next = NULL;
if(front == NULL){
front = temp;
}else{
rear->next = temp;
}
rear = temp;
}
/*----------------------------------------------------*/
string Queue::dequeue() {
node *temp = new node();
string value;
if(front == NULL){
cout<<"Queue is Emtpty" << endl;
}else{
temp = front;
value = temp->info;
front = front->next;
delete temp;
}
return value;
}
/*----------------------------------------------------*/
bool Queue::isEmpty() {
return (front == NULL);
}
bool Queue::find( string name){
node *temp = rear;
while( temp != nullptr ){
if( temp -> info == name){
return true;
}
temp = temp -> next;
}
return false;
}
并尝试实施bfs
class Graph {
private:
map< string , map<string, int >> graf;
public:
Graph(){};
~Graph(){};
bool isConnected(string , string );
void addEdge (string one , string two);
void BFS(string ,string);
};
/*----------------------------------------------------*/
bool Graph::isConnected(string one , string two){
return (graf[one][two] == 1 || graf[two][one] == 1 );
}
/*----------------------------------------------------*/
void Graph::addEdge( string one , string two){
graf [one][two] = graf[two][one] = 1;
}
/*----------------------------------------------------*/
void Graph::BFS(string s , string m){
Queue one;
map<string , bool> check;
vector<string> path;
for( auto &x : graf){
check[x.first] = false;
}
check[s] = true;
one.enqueue(s);
path.push_back(s);
string tmp = one.dequeue();
for( auto &x: graf){
if( isConnected(tmp , x.first) && !check[x.first] ){
one.enqueue(x.first);
check[x.first] = true;
path.push_back(x.first);
if(x.first == m){
break;
}
}
}
for( auto &x: path )
cout << x << " ";
}
找到正确的城镇&#34;并存储它们用于打印。问题是,这确实打印了所有可能性而不仅仅是正确的方法。例如,如果前面提到它也发现&#34;布达佩斯 - 伦敦&#34;然后打印出来。我知道问题在于我将每个城镇排队等等。在路上但未能找到如何检查其正确性的方法。
我不确定我怎么能找到唯一(最高的)方式。我最近发现了这个alghoritm并且无法让他工作。我怎样才能改进这种以实现这种方式行事的方式呢?
答案 0 :(得分:1)
您可以保留每个节点的“父级”,而不是将节点放在“路径”中。我已经更改了代码并使用check
变量作为父数据结构。
如果未设置父级,则不会选中,因此if语句也会检查父级是否已设置。
最后,您只需要通过父母,直到您到达目的地。
请注意,我将BFS更改为从目的地开始。我之所以这样做是因为否则从最后一个节点向第一个节点迭代将返回你需要的路径的反向。
以下是代码:
#include <iostream>
#include <map>
using namespace std;
struct node {
string info;
node *next;
};
/*----------------------------------------------------*/
class Queue {
private:
node *front;
node *rear;
public:
Queue();
~Queue();
bool isEmpty();
void enqueue(string );
string dequeue();
void display();
bool find(string);
};
/*----------------------------------------------------*/
void Queue::display(){
node *p = new node;
p = front;
if(front == NULL){
cout<<"Nothing to Display" << endl;
}else{
while(p!=NULL){
cout<<p->info << endl;
p = p->next;
}
}
}
/*----------------------------------------------------*/
Queue::Queue() {
front = NULL;
rear = NULL;
}
/*----------------------------------------------------*/
Queue::~Queue() {
delete front;
}
/*----------------------------------------------------*/
void Queue::enqueue(string data) {
node *temp = new node();
temp->info = data;
temp->next = NULL;
if(front == NULL){
front = temp;
}else{
rear->next = temp;
}
rear = temp;
}
/*----------------------------------------------------*/
string Queue::dequeue() {
node *temp = new node();
string value;
if(front == NULL){
cout<<"Queue is Emtpty" << endl;
}else{
temp = front;
value = temp->info;
front = front->next;
delete temp;
}
return value;
}
/*----------------------------------------------------*/
bool Queue::isEmpty() {
return (front == NULL);
}
bool Queue::find( string name){
node *temp = rear;
while( temp != nullptr ){
if( temp -> info == name){
return true;
}
temp = temp -> next;
}
return false;
}
class Graph {
private:
map< string , map<string, int >> graf;
public:
Graph(){};
~Graph(){};
bool isConnected(string , string );
void addEdge (string one , string two);
void BFS(string ,string);
};
/*----------------------------------------------------*/
bool Graph::isConnected(string one , string two){
return (graf[one][two] == 1 || graf[two][one] == 1 );
}
/*----------------------------------------------------*/
void Graph::addEdge( string one , string two){
graf [one][two] = graf[two][one] = 1;
}
/*----------------------------------------------------*/
void Graph::BFS(string s , string m){
Queue one;
map<string , string> check;
for( auto &x : graf){
check[x.first] = "-";
}
check[m] = "";
one.enqueue(m);
while (!one.isEmpty())
{
string tmp = one.dequeue();
if(tmp == s){
string c = tmp;
while (c != m) {
cout << c << " ";
c = check[c];
}
cout << c << endl;
return;
}
for( auto &x: graf){
if( isConnected(tmp , x.first) && check[x.first] == "-" ){
one.enqueue(x.first);
check[x.first] = tmp;
}
}
}
}
int main()
{
Graph g;
g.addEdge("berlin","london");
g.addEdge("budapest","london");
g.addEdge("london","glassgow");
g.addEdge("budapest","moscow");
g.BFS("berlin","moscow");
g.addEdge("london", "moscow");
g.BFS("berlin","moscow");
return 0;
}
这是输出。第一个是没有“伦敦” - &gt;“莫斯科”边缘,第二个是将该边缘添加到图表中。
berlin london budapest moscow
berlin london moscow