邻接列表c ++

时间:2015-04-04 18:06:53

标签: c++ graph stdvector adjacency-list

我试图创建一个邻接列表来存储图表。我创建它后,我在访问列表时遇到了一些麻烦。

class Weighted_graph {
private:
    std::vector <std::vector<std::pair<double, int>> > adjacencyList;
    ...
Weighted_graph::Weighted_graph(int n) {
    std::vector <std::vector<std::pair<double, int>> > adjacencyList(n);
    for (int i = 0; i < n; i++) {
        std::vector<std::pair<double, int>> row; // Create an empty row
        adjacencyList.push_back(row); 
    }
    ...
}

这是我创建列表的方式。每当我尝试访问列表中的任何内容时,我都会收到此错误:

Debug Assertion Failed!
Expression: vector subscript out of range

每当我尝试对lsit做任何事情时都会发生这种情况,例如,调用:

bool Weighted_graph::insert_edge(int i, int j, double d) {
if (!adjacencyList[i].empty()) {

bool Weighted_graph::insert_edge(int i, int j, double d) {
std::cout << adjacencyList[i].front().second

我创建列表错了吗?

2 个答案:

答案 0 :(得分:0)

一个明显的问题是&#39; adjacencyList,定义了两次。首先作为类的私有成员,第二个作为构造函数中的局部变量,其大小为&#39; n&#39;。因此,当您想要从其他成员函数作为类成员访问它时,它引用的第一个定义不是构造函数中初始化的定义。除非你有特殊的理由这样做,否则我建议只将它定义为一个类数据成员,并在构造函数中初始化它,以根据&#39; n&#39;的值动态设置向量的大小。这是一个方法论证。如果是这样,试试:

class Weighted_graph {
private:
    std::vector <std::vector<std::pair<double, int>> > adjacencyList;
    ...
    Weighted_graph::Weighted_graph(int n) {
    adjacencyList.resize(n);
    for (int i = 0; i < n; i++) {
        std::vector<std::pair<double, int>> row; // Create an empty row
        adjacencyList.push_back(row); 
    }
    ...
}

希望有所帮助!

答案 1 :(得分:0)

使用MAP代替向量更简单

splaylimit = 30m

};

#include<iostream>
#include<map>
#include<list>
#include<iterator>
#include<algorithm>


using namespace std;

class Graph
{
private:
map<int, list<int> > edges = {};

public:
// Read the Intialized Graph and Create a Adjacency list out of it 
// There could be cases where in the initialized graph <map> link issues are 
not maintained
// for example node 2 to 1 link 
// 2->1
//there needs to be a link then since undirected Graph
// 1->2
Graph(map<int,list<int> > edges)
{
    cout << "Graph is initialized"<<endl;
    //this->edges = edges;
    for (auto it : edges)
    {
        for (auto di = it.second.begin(); di != it.second.end(); di++)
        {
            addEdge(it.first, *di);
        }
    }

}
//Add a vertex to graph map
//structure is
// int => int list
void addVertex(int vertex)
{
    auto search = edges.find(vertex);
    if (search != edges.end()) {
        std::cout << "Already present  " << search->first <<endl;
    }
    else
    {
        list<int> A;
        edges[vertex] = A;
    }

}

//Add Edge from both vertex to each other
// Make sure the nodes are present 
void addEdge(int vertex1, int vertex2)
{
    addVertex(vertex1);
    addVertex(vertex2);
    if (find(edges[vertex1].begin(), edges[vertex1].end(), vertex2) == edges[vertex1].end())
    {
        edges[vertex1].push_back(vertex2);
    }
    if (find(edges[vertex2].begin(), edges[vertex2].end(), vertex1) == edges[vertex2].end())
    {
        edges[vertex2].push_back(vertex1);
    }

}

//Display the graph with vertex and edges 
void display()
{
    for (auto it:edges)
    {
        cout << it.first << "::";
        for (auto di = it.second.begin(); di != it.second.end(); di++)
        {
            cout << "=>" << *di;
        }
        cout << endl;
    }
}