代码之家  ›  专栏  ›  技术社区  ›  kushj

广度优先搜索:有向图

  •  0
  • kushj  · 技术社区  · 10 年前

    我正在尝试在有向图上实现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 回复  |  直到 10 年前
        1
  •  0
  •   MehrZ    10 年前

    你准确地说明了问题。添加“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;
    }