如何遍历顶点并输出边缘?

时间:2019-05-08 00:27:47

标签: c++ graph breadth-first-search

我有一个顶点列表,这些顶点带有用于不同演员的标签,以及一个边缘列表,它们具有用于将演员连接起来的电影的标签。现在,我正在尝试创建一个函数,该函数在两个演员(顶点)之间创建路径,并输出介于它们之间的演员以及他们所在的电影。我执行此操作的功能是在某个部分无限循环无法找出问题所在。

我在代码中使用了这些不同的结构。

struct Edge {
    string label;
    string vertex;
    Edge(string v, string l)   
    {
        this->label = l;
        this->vertex = v;
    }
};

struct Vertex {
    string label;
    std::vector<Edge*> adjList;

    Vertex(string label) {
        this->label = label;
    }
};

struct pairs
{
    string vertex;
    string edge;
    pairs(string vertex, string edge)
    {
        this->vertex = vertex;
        this->edge = edge;
    }
};

struct triple
{
    string parent;
    string vertex;
    string edge;
    triple(string parent, string vertex, string edge)
    {
        this->parent = parent;
        this->vertex = vertex;
        this->edge = edge;
    }
};

这里也是输入的边和顶点

    imdb.vertexInsert("Al Pacino");
    imdb.vertexInsert("Robert De Niro");
    imdb.vertexInsert("Marlon Brando");
    imdb.vertexInsert("Mark Hamill");
    imdb.vertexInsert("Harrison Ford");
    imdb.vertexInsert("Sean Connery");
    imdb.vertexInsert("Kevin Costner");
    imdb.vertexInsert("John Cena");


    imdb.edgeInsert("The Godfather", "Al Pacino", "Marlon Brando");
    imdb.edgeInsert("Heat", "Al Pacino", "Robert De Niro");
    imdb.edgeInsert("Star Wars", "Mark Hamill", "Harrison Ford");
    imdb.edgeInsert("Indiana Jones", "Harrison Ford", "Sean Connery");
    imdb.edgeInsert("The Untouchables", "Sean Connery", "Kevin Costner");
    imdb.edgeInsert("The Untouchables", "Robert De Niro", "Kevin Costner");
    imdb.edgeInsert("The Untouchables", "Robert De Niro", "Sean Connery");

以下是输入/输出的示例

imdb.print();
    imdb.chain("Marlon Brando", "Robert De Niro");
    imdb.chain("Sean Connery", "Harrison Ford");
    imdb.chain("Kevin Costner", "Al Pacino");
    imdb.chain("Mark Hamill", "John Cena");

    //    Expected output:
    //
    //    Marlon Brando (The Godfather) Al Pacino (Heat) Robert De Niro
    //    Sean Connery (Indiana Jones) Harrison Ford
    //    Kevin Costner (The Untouchables) Robert De Niro (Heat) Al Pacino
    //    Mark Hamill is not connected to John Cena

然后这就是我要工作的链功能

bool chain(string start, string dest) {
        std::deque<string> q;
        q.push_front(start);

        std::vector<string> seen;
        std::vector<triple*> parent;
        std::deque<pairs*> path;
        parent.push_back(new triple(start, " ", "  "));

        while (!q.empty()) {
            string curr = q.at(0);
            q.pop_front();

            seen.push_back(curr);

            std::cout << "Now visiting " << curr << std::endl;

            if (curr == dest) {
                string temp = curr;
                int k = 0;
                for (k = 0; k < parent.size(); k++)
                {
                    if (parent[k]->vertex == temp)
                        break;
                }
                while (temp != start) {
                    path.push_front(new pairs(parent[k]->vertex, parent[k]->edge));
                    temp = parent[k]->parent;
                }
                cout << seen[0];

                for (int i = 0; i < path.size(); i++) {
                    std::cout << "(" << path[i]->edge << ") " << path[i]->vertex;
                }
                std::cout << std::endl;

                return true;
            }

            int n = 0;
            for (; n < vertices.size(); n++)
            {
                if (vertices[n]->label == curr) break;

            }

            Vertex* v = vertices[n];

            for (int i = 0; i < v->adjList.size(); i++) {
                if (!contains(seen, v->adjList[i]->vertex)) {
                    q.push_back(v->adjList[i]->vertex);

                    seen.push_back(v->adjList[i]->vertex);
                    parent.push_back(new triple(curr, v->adjList[i]->vertex, v->adjList[i]->label));
                }
            }
        }

        return false;
    }

现在这就是我运行它时的样子。

Al Pacino: The Godfather Heat
Robert De Niro: Heat The Untouchables The Untouchables
Marlon Brando: The Godfather
Mark Hamill: Star Wars
Harrison Ford: Star Wars Indiana Jones
Sean Connery: Indiana Jones The Untouchables The Untouchables
Kevin Costner: The Untouchables The Untouchables
John Cena:
Now visiting Marlon Brando
Now visiting Al Pacino
Now visiting Robert De Niro

0 个答案:

没有答案