广度优先搜索:有向图

时间:2015-02-02 12:20:02

标签: c++ breadth-first-search

我正在尝试在有向图上实现BFS。 我完全相信我的算法是正确的,但是,在一段代码返回错误之后:

Graph.CPP文件:

#include<iostream>
#include <list>
using namespace std;

class node{
    int value;
    int color;


    public:
        list <node> adj;
        node(int val){
            value = val;
            color = 0;
        }

        void add_edge(node x){
            adj.push_back(x);
        }

        void change_color(int c){
            color = c;
        }

        int get_color(){
            return color;
        }

        int get_value(){
            return value;
        }
};

实际BFS实施IN:

#include "graph.cpp"
node n0(0),n1(1),n2(2),n3(3), n4(4);

//PrintQ: Print the list
void printq( list <node> A){
    while (!A.empty())
    {
        cout << A.front().get_value() <<" ";
        A.pop_front();
    }
}

void bfs(node s){
    list <node> q;
    q.push_back(s);
    s.change_color(1);

    while(!q.empty()){
        node s = q.front();

        cout<<s.get_value()<<'\t';
        printq(s.adj);      
        cout<<endl;

        list<node>::iterator i;
        for(i = s.adj.begin(); i != s.adj.end(); ++i){

            if((*i).get_color() == 0){
                q.push_back(*i);
                (*i).change_color(1);
            }
        }

        q.pop_front();
    }

}
int main(){

    n0.add_edge(n1);
    n0.add_edge(n2);
    n1.add_edge(n2);
    n2.add_edge(n0);
    n2.add_edge(n3);
    n0.add_edge(n3);
    n1.add_edge(n4);
    n4.add_edge(n3);
/*  
    printq(n0.adj);cout<<endl;
    printq(n1.adj);cout<<endl;
    printq(n2.adj);cout<<endl;
*/
    bfs(n1);

    return 0;

因此,出现错误,除源节点外,队列中的每个其他节点都会给出错误的邻接列表。虽然队列顺序运行良好,但队列中的节点会给出错误的邻接关系。

我不确定为什么会发生这种情况,但我怀疑它是按值而不是引用来复制节点。

经过很长一段时间后,这是我的CPP计划,所以如果我遗漏了什么,请赐教。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你确切地说过了这个问题。当你添加'n1.add_edge(n2);'时,n2没有任何边,而n1将保留n2的副本。稍后,当您向n2添加更多边时,n1的副本不会更改。我修改了你的代码以使用指针,我认为它现在可以正常工作。

#include<iostream>
#include <list>
using namespace std;
class node{
    int value;
    int color;


    public:
        list <node * > adj;
        node(int val){
            value = val;
            color = 0;
        }

        void add_edge(node * x){
            adj.push_back(x);
        }

        void change_color(int c){
            color = c;
        }

        int get_color(){
            return color;
        }

        int get_value(){
            return value;
        }
};

和您的主文件

#include "graph.cpp"

//PrintQ: Print the list
void printq( list <node *> A){
    while (!A.empty())
    {
        cout << A.front()->get_value() <<" ";
        A.pop_front();
    }
}

void bfs(node * s){
    list <node *> q;
    q.push_back(s);
    s->change_color(1);

    while(!q.empty()){
        node * s = q.front();

        cout<<s->get_value()<<'\t';
        printq(s->adj);      
        cout<<endl;

        list<node * >::iterator i;
        for(i = s->adj.begin(); i != s->adj.end(); ++i){

            if((*i)->get_color() == 0){
                q.push_back(*i);
                (*i)->change_color(1);
            }
        }

        q.pop_front();
    }

}
int main(){

    node* n0 = new node(0);
    node* n1 = new node(1);
    node* n2 = new node(2);
    node* n3 = new node(3);
    node* n4 = new node(4);

    n0->add_edge(n1);
    n0->add_edge(n2);
    n1->add_edge(n2);
    n2->add_edge(n0);
    n2->add_edge(n3);
    n0->add_edge(n3);
    n1->add_edge(n4);
    n4->add_edge(n3);
/*  
    printq(n0.adj);cout<<endl;
    printq(n1.adj);cout<<endl;
    printq(n2.adj);cout<<endl;
*/
    bfs(n1);
    delete n0, n1, n2, n3, n4;
}