gpt4 book ai didi

c++ - 如何增加 C++ 中相邻列表的最大大小?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:25 24 4
gpt4 key购买 nike

我在此 webpage 中使用 Dijkstra 算法.最近发现,如果图中的顶点数超过60000,在将新的边信息作为节点添加到相邻列表时,系统会响应“core dumped”。

这里是原程序中相邻列表的摘录:

// A structure to represent a node in adjacency list
struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
};

// A structure to represent an adjacency liat
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};

// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest, int weight)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
}

这里是图的代码和添加新边

// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};

// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;

// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));

// Initialize each adjacency list as empty by making head as NULL
for (int i = 0; i < V; ++i)
graph->array[i].head = NULL;

return graph;
}

// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest, int weight)
{
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest, weight);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;

// Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src, weight);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}

为了您的引用,这是我测试的主要功能

int main()
{
int V = 100000;
struct Graph* graph = createGraph(V);
for(int i=0;i<V/2;i++)
for(int j=i+1;j<V;j++)
addEdge(graph, i, j, i+j);

return 0;
}

最佳答案

因此,您正尝试向图中添加几乎 4*10^9 条边。在 64 位机器上,每条边(AdjListNode - 对象)都需要 16 个字节。我们也在谈论至少 64GB,这很多。

但是,每次调用 malloc(sizeof(struct AdjListNode)) 的成本不仅仅是 16 字节:管理堆上的元素有一些开销,系统承认超过 16 字节每个内存请求。在我的系统上,我需要 2GB 用于 4*10^7 边,即 ca。每边 50 字节。

无论如何,您将在程序执行的某个时刻内存不足,并且 malloc 将在您的这部分代码中返回 0:

struct AdjListNode* newAdjListNode(int dest, int weight)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
//newNode is NULL if there is no memory!
newNode->dest = dest; //BOOM! segmentation error due to newNode==NULL
....

如您所见,程序将因 NULL 指针解除引用而崩溃。

我想对于每个实现,它所适用的问题规模都有一个限制。并且此实现的限制远低于 4*10^9 边。

至于你的问题:如果你想使用更少的内存,你应该避免分配许多不同的对象 - 最好将它们一个接一个地放入一个连续的内存中。对于此 std::vector,如果您使用 C++(但您的代码是纯 C),这是一个不错的选择。

关于c++ - 如何增加 C++ 中相邻列表的最大大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38932996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com