使用邻接列表在c ++中实现DFS(使用<vector>和链表)

时间:2016-09-26 16:53:06

标签: c++ algorithm depth-first-search adjacency-list

我使用了链接列表制作了邻接列表。在节点的结构内部,我有数据,下一个和访问过。当我尝试在DFS函数中将visited设置为true时,算法无法正常工作。它只适用于我创建一个存储布尔值的新数组并将该数组用于dfs算法。我需要帮助让访问顶点结构的成员工作。我不确定为什么它不起作用。

Graph.h

#ifndef GRAPH_H
#define GRAPH_H
#include <vector>

class Graph{
private:
    struct vertex{
        int data;
        bool visited;
        struct vertex* next;
    };

    struct adjList
    {
        struct vertex *head;
    };

    int V;
    bool visited[100];
    std::vector<adjList> G;
public:
    Graph(int vertices);
    vertex* addVertex(int data);
    void addEdge(int index, int data);
    void dfs(int vertex);
    void printGraph();
};

#endif

Graph.cpp

#include "Graph.h"
#include <iostream>
#include <cstdlib>
using namespace std;

Graph:: Graph(int vertices){
    this->V=vertices;
    for(int i=0; i<V; i++){
        //allocating space in G for V amount and using constructor of struct
        G.push_back(adjList());
        visited[i]=false;
    }
}
//create a node
Graph:: vertex* Graph::addVertex(int data){
    struct vertex* newNode= new vertex;
    newNode->data= data;
    newNode->next= NULL;
    newNode->visited=false;
    return newNode;
}
//add an Edge to the list
void Graph:: addEdge(int index, int data){
    struct vertex* cursor= G[index].head;
    while(cursor){
      if(cursor->data==data)
        return;
      cursor= cursor->next;
    }
    struct vertex* newVertex= addVertex(data);
    newVertex->next = G[index].head;
    G[index].head= newVertex;
    // this is undirected graph, so we are adding an edge from data to index;
    newVertex = addVertex(index);
    newVertex->next= G[data].head;
    G[data].head= newVertex;
}
// dfs algorithm
void Graph::dfs(int vertex){
    cout<<vertex<<", ";
    G[vertex].head->visited = true;
    visited[vertex]=true;
    struct vertex* cursor = G[vertex].head;
    while(cursor!=NULL){
      vertex=cursor->data;
      if(visited[vertex]==false)
          dfs(vertex);
      cursor= cursor->next;
    }
}

void Graph::printGraph(){
    for(int i=0; i<V; i++){
        struct vertex* cursor= G[i].head;
        cout<<"vertex: "<<i<<endl;
        while(cursor!=NULL){
            cout<<"->"<<cursor->data;
            cursor=cursor->next;
        }
        cout<<endl;
    }
}

int main(){
    Graph Graph(5);
    Graph.addEdge(0,1);
    Graph.addEdge(0,4);
    Graph.addEdge(1,2);
    Graph.addEdge(1,3);
    Graph.addEdge(1,4);
    Graph.addEdge(2,3);
    Graph.addEdge(3,4);

    Graph.printGraph();
    Graph.dfs(0);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

首先清理你的数据结构,你过早地弯曲它们以支持你的算法,这会引起一些混乱。确保你有一些坚实的模型&#34;首先,在没有任何算法的情况下,然后检查算法需要什么,并将其添加为本地临时内部算法,或添加到模型的一些缓存/扩展数据。但要保留核心模型。

我的意思是,让我向您展示超低效,但简单实现您的DFS,希望可以考虑的东西是现代C ++&#34; (但我也不是专家):

现场演示:http://cpp.sh/9fyw

#include <iostream>
#include <vector>
#include <string>

/**
 * Super naive and inefficient (but simple) implementation of Depth-first-search of graph
 * Just to show basic usage of std::vector, and how it helps to avoid new/delete
 **/

struct Vertex {
    // nothing at the moment
};

struct Edge {   // One-way edge, to make things intentionally harder
    size_t fromIndex, toIndex;

    Edge(const size_t _fromIndex, const size_t _toIndex)
        : fromIndex(_fromIndex), toIndex(_toIndex) {}
};

class Graph {
    std::vector<Vertex> vertices;
    std::vector<Edge> edges;

public:
    Graph(const size_t expectedVerticesCount = 20, const size_t expectedEdgesCount = 50) {
        if (expectedVerticesCount) vertices.reserve(expectedVerticesCount);
        if (expectedEdgesCount) edges.reserve(expectedEdgesCount);
    }

    void clear() {
        vertices.clear();
        edges.clear();
    }

    void initVertices(const size_t newVertexCount) {
        // A bit pointless function to set vertices, as vertices have no data
        // Storing the count itself inside Graph would suffice,
        // but let's demonstrate vector usage a bit more with N empty vertices
        clear();    // removes old vertices + edges
        vertices.resize(newVertexCount);
    }

    void addEdgeBiDirectional(const size_t v1Index, const size_t v2Index) {
        if (vertices.size() <= v1Index || vertices.size() <= v1Index) {
            std::cout << "Ups, unexpected vertex in edge: "
                << v1Index << " <-> " << v2Index << "\n";
            return;
        }
        if (v1Index == v2Index) {
            std::cout << "Ups, loop at vertex: " << v1Index << " - ignored\n";
            return;
        }
        // Add two one-way edges, to make this edge work in both directions
        edges.push_back(Edge(v1Index, v2Index));
        edges.push_back(Edge(v2Index, v1Index));
    }

    void printGraph() {
        for (size_t i = 0; i < vertices.size(); ++i) {
            std::cout << "Vertex " << i << " has edges to:";
            for (const auto & edge : edges) {
                if (edge.fromIndex == i) std::cout << " " << edge.toIndex;
            }
            std::cout << "\n";
        }
    }

private:
    void dfs(std::vector<size_t> & result, std::vector<bool> & visited, size_t v) {
        // It's important to pass vectors as references here (that "&")
        // so you don't fill stack space too quickly, and the modifications
        // done to them inside are propagated up into final result.
        // Without "&" a local copy of vector would be created.
        if (visited[v]) return;
        result.push_back(v);
        visited[v] = true;
        for (const auto edge : edges) {
            if (edge.fromIndex != v) continue;
            dfs(result, visited, edge.toIndex);
        }
    }

public:
    // Returns vector with vertex indices found
    std::vector<size_t> dfs(const size_t vertexIndex) {
        if (vertices.size() <= vertexIndex) {
            std::cout << "DSF: Ups, invalid starting vertex: "
                << vertexIndex << "\n";
            return std::vector<size_t>();
        }

        std::vector<bool> visited(vertices.size());
        std::vector<size_t> result;
        result.reserve(vertices.size());
        dfs(result, visited, vertexIndex);
        return result;
    }
};

int main()
{
    Graph g;
    // fill up graph data
    g.initVertices(5);
    g.addEdgeBiDirectional(0,1);
    g.addEdgeBiDirectional(0,4);
    g.addEdgeBiDirectional(1,2);
    g.addEdgeBiDirectional(1,3);
    g.addEdgeBiDirectional(1,4);
    g.addEdgeBiDirectional(2,3);
    g.addEdgeBiDirectional(3,4);
    // Show the validation works
    g.addEdgeBiDirectional(1,1);
    g.addEdgeBiDirectional(5,4);

    g.printGraph();

    auto dfsResult = g.dfs(2);
    std::cout << "DFS for 2 result:";
    for (const auto v : dfsResult) std::cout << " " << v;
    std::cout << "\n";
}

(现在我意识到,我的&#34; addEdge&#34;不会阻止重复边缘添加,就像你的一样,考虑它的bug或功能)

如果你检查它,你会发现表现不好,因为它每次都在搜索所有边缘。怎么帮忙呢?已经为每个顶点准备了邻居数据。

struct Vertex {
    std::vector<size_t> adjacency;
};

然后在Graph中,您可以为每个添加的边设置相邻的顶点:

void addAdjacency(const size_t v1Index, const size_t v2Index) {
    auto & adjacency = vertices[v1Index].adjacency;
    if (adjacency.end() != std::find(adjacency.begin(), adjacency.end(), v2Index)) return;
    adjacency.push_back(v2Index);
}

void addEdgeBiDirectional(const size_t v1Index, const size_t v2Index) {
    ...
    addAdjacency(v1Index, v2Index);
    addAdjacency(v2Index, v1Index);
}

现场演示:http://cpp.sh/4saoq

现在效率更高(就深度优先而言效率更高,广度优先搜索会更容易编写而不使用大量堆栈空间进行递归)。

但是如果DFS和printGraph是你唯一的目标,那么可以通过完全删除edges并仅保留vertices并在其中adjacency来重构。您可以自己尝试,您将看到它只需要很少的更改。

visited字段仍属dfs临时拥有,IMO是最适合使用它的方式。

这已经很久了,花了很长时间,以至于我没心情向你展示指针,新品和删除品。向您展示如何避免它们可能仍然有更多好处,至少在您自己生成类似或更好的代码之前。

学习裸指针/新/删除内容也很重要,但是......查看一些教程?

至少有一个提示&#34;何时到delete&#34;:在现代C ++中,您可以在范围中思考。就像一切都属于某个地方(在某种范围内),然后在退出范围时它被释放。通过这种方式思考,您只需在类中实现构造函数+析构函数,就可以完成清理。

就像我的示例中的Graph g;main的局部变量一样,因此在它的范围内。当退出main时,调用g的析构函数(我没有写,因为默认的析构函数是由编译器创建的,用于调用vertices和{{1析构函数,edges析构函数由Vertex析构函数调用,它隐式调用vector析构函数。所以一切都被释放,没有内存泄漏。

如果我在adjacency中通过它的生命周期(在构造函数或某个函数中)使用new中的某些Graph,我可以将指针放在某个Graph成员变量中,并编写显式析构函数,检查是否为非nullptr值,并将其删除,或在某个函数中尽快删除它(并将存储设置为nullptr以避免在同一指针上双重删除)。

因此,如果您确保您的类设计有意义,并且所有内容都属于某个合理的范围,那么您可以使用new/delete与构造函数/析构函数配对,并且您知道在退出范围时进行清理,它确实拥有(负责)该作品。

还有其他技巧,如何将指针(或任何实际资源)从原始所有者传递到其他类...通常我会尽力避免这种情况,但如果你真的坚持这样的应用程序结构,你可以使用std::unique_ptr周围的东西。但是这种设计有点难以保持清洁,并且追踪特定内存或资源的责任/所有权。请注意this video示例,了解如何以一种优雅的方式处理它。

关于new以及指针和链接列表的最后一些注释。您可以从Java使用链接列表和哈希映射之类的东西,并且它在VM下很有意义,因为您几乎无法控制内存管理,而且实例通常是&#34;并且#34;大量使用对象元数据。

在C ++中,它的开销通常接近于零,因此std::vector<uint32_t> millionIntegers(1e6);是一个长度为四百万字节的连续内存块,另外还有几个字节的向量元数据那个。

这意味着,我的第一个O(n * n)示例几乎每次都会通过所有边缘,对于具有100 + k边缘性能的图形来说,非常接近带有指针的O(n),因为每个new可能以内存的不同部分结束,破坏已处理数据的局部性。当您尝试访问缓存内存页面之外的内存时,这会产生巨大的性能损失。虽然以连续的方式通过100k整数,但是CPU可以半睡半醒,最大限度地提高缓存吞吐量(不幸的是,深度优先也使它变得混乱)。

这就是为什么在编写第一版时,通常你不应该对容器类型感到困扰。如果vector适合,请使用它。然后,在您运行,测试代码之后,您可以对其进行分析,并尝试一些更智能的内存结构,最终甚至用某种链接列表替换vector。但是你不能仅仅依靠算法理论或者#34;逻辑&#34;。

当涉及到x86性能时,从真实生产数据中分析真实生产代码的硬数据至关重要,现代硬件可以在很多方面让人类逻辑感到惊讶,并以意想不到的方式挑战理论。为了获得最佳性能,您需要相当复杂(简单)的算法,以及具有可预测的常规访问模式的整齐排列的数据。只有两个中的一个可能还不够。

相关问题