簡體   English   中英

顯示深度優先搜索圖遍歷C ++

[英]Displaying Depth-First Search Graph Traversal C++

我正在遍歷已設置為類的圖,並使用矢量存儲頂點和邊。 我在圖形上使用深度優先搜索來顯示遍歷的路徑,但是我想以某種方式讓我的代碼在遍歷頂點時按順序顯示它們:

<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;
} 

您可以使用Vector(在另一個調用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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM