【问题标题】:Displaying Depth-First Search Graph Traversal C++显示深度优先搜索图遍历 C++
【发布时间】:2012-12-05 21:39:44
【问题描述】:

我正在遍历我设置为一个类的图,使用向量来存储顶点和边。我在图表上使用深度优先搜索来显示路径,但我想以某种方式让我的代码在通过它们时按顺序显示顶点,格式如下:

<u, i1, i2, ... v>

其中 'u' 和 'v' 都是起始顶点(我希望它在同一个顶点开始和结束),而 'i' 值是它沿途经过的顶点。

这是我目前拥有的 DFS 功能,我已对其进行了简化,以便可以作为一般参考。我可以在这里修改什么以使其显示我想要的内容吗? (目前未设置为显示任何内容)。

vector<Vertex*> vertices;
vector<Edge*> edges;

class Vertex {
    public:
        Vertex () {};
        Vertex (int id, float safetyIndex, string name)
            : id(id), safetyIndex(safetyIndex), name(name), previous(NULL), distFromStart(INT_MAX), color("white")
        {
            vertices.push_back(this);
        }
    public:
        int id;
        float safetyIndex;
        string name;
        int distFromStart;
        Vertex* previous;
        string color;
};

class Edge {
    public:
        Edge () {};
        Edge (Vertex* intersection1, Vertex* intersection2, int distance)
            : intersection1(intersection1), intersection2(intersection2), distance(distance)
        {
            edges.push_back(this);
        }
        bool street_connection(Vertex* intersection1, Vertex* intersection2)
        {
            return (
                (intersection1 == this->intersection1 && intersection2 == this->intersection2) ||
                (intersection1 == this->intersection2 && intersection2 == this->intersection1));
        }
    public:
        Vertex* intersection1;
        Vertex* intersection2;
        int distance;
};

void pathFinder(Vertex* startVertex)
{
    DFS_visit(startVertex); 
}

void DFS_visit(Vertex* u)
{
    u->color = "gray";  //  Mark that we have visited intersection 'u'

    //  Create a vector containing all adjacent vertices to intersection 'u'
    vector<Vertex*>* adjVertex = AdjVertices(u);

    const int size = adjVertex->size();
    for( int i=0; i<size; ++i)
    {
        Vertex* v = adjVertex->at(i);
        if ( v->color == "white" )
        {
            DFS_visit(v);  // recursive function call
        }
    }

    //  Once all adjacent vertices have been located, we are done with this node
    u->color = "black";
}

vector <Vertex*>* AdjVertices(Vertex* vert)
{
    //  Creates a vector containing all of the adjacent vertices
    //  to the intersection in question (vert)
    vector<Vertex*>* adjVertex = new vector <Vertex*> ();
    const int size = edges.size();
    for(int i=0; i<size; ++i)
    {
        Edge* edge = edges.at(i);
        Vertex* adjacent = NULL;
        if (edge->intersection1 == vert)    // if edge's start vertex is the vertex in question
        {
            adjacent = edge->intersection2;
        }
        else if (edge->intersection2 == vert)   // if edge's end vertex is the vertex in question
        {
            adjacent = edge->intersection1;
        }
        if (adjacent && vertices_check(vertices, adjacent))
        {
            adjVertex->push_back(adjacent);
        }
    }
    return adjVertex;
} 

【问题讨论】:

  • 您介意发布VertexEdge 的定义吗?
  • 没问题,现在应该起来了

标签: c++ graph vector depth-first-search graph-traversal


【解决方案1】:

您可以使用向量(在调用 DFS_visit 的其他函数中构建)并将其传递给 DFS_visit。在 DFS_visit 中,在开始时添加节点,然后每次从被调查的子节点返回。这应该会给你一个完整的路径描述。

void DFS_visit(Vertex* u, Vector<Vertex*> path )
{
    u->color = "gray";  //  Mark that we have visited intersection 'u'
    path.push_back(u);
    //  Create a vector containing all adjacent vertices to intersection 'u'
    vector<Vertex*>* adjVertex = AdjVertices(u);

    const int size = adjVertex->size();
    for( int i=0; i<size; ++i)
    {
        Vertex* v = adjVertex->at(i);
        if ( v->color == "white" )
        {
            DFS_visit(v,path);  // recursive function call
            path.push_back(u);
        }
    }

    //  Once all adjacent vertices have been located, we are done with this node
    u->color = "black";
}

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多