为什么我的代码不起作用?

时间:2013-04-09 00:17:33

标签: c++

我正在为有向图实现一个简单的DFS遍历:

#include <iostream>
#include <vector>
#include <climits>
#include <utility>
#include <deque>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <list>

using namespace std;

enum class color_type {
    BLACK,
    WHITE,
    GRAY
};

struct vertex {
    char label;
    color_type color;
    int start;
    int finish;
    vertex *parent;
    vector<vertex> adjacents;

    vertex(char label)
        :label(label), start(0), finish(0), color(color_type::WHITE) {
    }

    void add_neighbor(const vertex &v) {
        adjacents.push_back(v);
    }
};

class digraph {
private:
    vector<vertex> vertices;    
    int count;

public:
    digraph() 
        :count(0) {
        vertex a('a');
        vertex b('b');
        vertex c('c');
        add_edge(a, b);
        add_edge(b, c);
        add_edge(c, a);
        vertices.push_back(a);
        vertices.push_back(b);
        vertices.push_back(c);
        for (int i = 0; i < vertices.size(); ++i) {
            vertices[i].color = color_type::WHITE;
            vertices[i].parent = NULL;
        }
    }

    void add_edge(vertex &u, vertex &v) {
        u.add_neighbor(v);
    }

    void dfs() {
        dfs_visit(vertices[0]);
    }

    void dfs_visit(vertex &u) {
        count++;
        u.start = count;
        u.color = color_type::GRAY;
        cout << "??? visit = " << u.label << endl;
        cout << "# neighbors: " << u.adjacents.size() << '\n';
        for (int i = 0; i < u.adjacents.size(); ++i) {
            if (u.adjacents[i].color == color_type::WHITE) {
                cout << "visit neighbor of [" << u.label << "] is: " << u.adjacents[i].label << endl;
                u.adjacents[i].parent = &u;
                dfs_visit(u.adjacents[i]);
            } 
        }
        u.color = color_type::BLACK;
        count++;
        u.finish = count;
    }

public:
    friend ostream& operator <<(ostream& o, const digraph &dg) {
        for (int i = 0; i < dg.vertices.size(); ++i) {
            o << dg.vertices[i].label << ":\n";
            o << "\t start  = " << dg.vertices[i].start << endl;
            o << "\t finish = " << dg.vertices[i].finish << endl;
        }
        return o;
    }
};

int main() {
    digraph dg;
    dg.dfs();
    cout << dg << endl;
    return 0;
}

问题是在访问第二个顶点后调用dfs_visit()。我通过引用传递顶点u作为dfs_visit()函数的参数,但不知何故,顶点b的邻居突然变为0,这非常奇怪。
在我看来,存储在向量vertices中的顶点与传递给dfs_visit的顶点不同,但我真的不知道这是怎么回事。我已经使用Java一段时间了,现在我对C ++真的很生气。那么有人可以就这个问题向我提出一些看法吗?

修改 enter image description here

1 个答案:

答案 0 :(得分:1)

这可能更接近你正在寻找的东西,使用指针作为邻居。希望这可以帮助。最终区别在于主顶点容器内邻居的按指针寻址,而不是代码中所有这些副本。

注意:add-construction只是设置一个节点,使顶点集合中的“next”节点作为其邻居,最后一个节点获得邻居的第一个节点。这似乎是你的代码试图完成的。

#include <iostream>
#include <vector>
#include <climits>
#include <utility>
#include <deque>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <list>

using namespace std;

enum class color_type {
    BLACK,
    WHITE,
    GRAY
};

struct vertex {
    char label;
    color_type color;
    int start;
    int finish;
    vertex *parent;
    vector<vertex*> adjacents;

    vertex(char label)
    :label(label), start(0), finish(0), color(color_type::WHITE) {
    }

    void add_neighbor(vertex &v) {
        adjacents.push_back(std::addressof(v));
    }
};

class digraph {
private:
    vector<vertex> vertices;
    int count;

public:
    digraph()
    :count(0) {
        vertices.push_back(vertex('a'));
        vertices.push_back(vertex('b'));
        vertices.push_back(vertex('c'));
        for (size_t i=0; i<vertices.size(); ++i)
        {
            vertices[i].color = color_type::WHITE;
            vertices[i].parent = NULL;
            vertices[i].add_neighbor(vertices[(i+1)%vertices.size()]);
        }
    }

    void dfs() {
        dfs_visit(vertices[0]);
    }

    void dfs_visit(vertex &u) {
        count++;
        u.start = count;
        u.color = color_type::GRAY;
        cout << "??? visit = " << u.label << endl;
        cout << "# neighbors: " << u.adjacents.size() << '\n';
        for (int i = 0; i < u.adjacents.size(); ++i) {
            if (u.adjacents[i]->color == color_type::WHITE) {
                cout << "visit neighbor of [" << u.label << "] is: " << u.adjacents[i]->label << endl;
                u.adjacents[i]->parent = &u;
                dfs_visit(*(u.adjacents[i]));
            }
        }
        u.color = color_type::BLACK;
        count++;
        u.finish = count;
    }

public:
    friend ostream& operator <<(ostream& o, const digraph &dg) {
        for (int i = 0; i < dg.vertices.size(); ++i) {
            o << dg.vertices[i].label << ":\n";
            o << "\t start  = " << dg.vertices[i].start << endl;
            o << "\t finish = " << dg.vertices[i].finish << endl;
        }
        return o;
    }
};

int main() {
    digraph dg;
    dg.dfs();
    cout << dg << endl;
    return 0;
}

<强>输出

??? visit = a
# neighbors: 1
visit neighbor of [a] is: b
??? visit = b
# neighbors: 1
visit neighbor of [b] is: c
??? visit = c
# neighbors: 1
a:
     start  = 1
     finish = 6
b:
     start  = 2
     finish = 5
c:
     start  = 3
     finish = 4