Daniel Pawłowski
Daniel Pawłowski

Reputation: 43

std::bad_alloc during dijkstra calculation for big dataset

I am trying to solve shortest path for big graph using dijkstra algorithm. Problem is when I am executing program in CLion I am getting std::bad alloc, always at node 491, however when I tried do the same on my Ubuntu VM, I am getting core dumped on the beggining.

I am new to c++ so it is hard for me to understand why does it happen.

Here is my code:

Utils:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <ctime>

#define INFINITY 9999999

int maxNode = 0;
using namespace std;

vector<int> loadFile(const string &path) {
    vector<int> graph;
    ifstream file;
    file.open(path);
    if (!file.fail()) {
        string line;
        while (getline(file, line)) {
            stringstream ss(line);
            for (int i; ss >> i;) {
                if (i + 1 > maxNode)
                    maxNode = i + 1;
                graph.push_back(i);
                if (ss.peek() == ';')
                    ss.ignore();
            }
        }
        file.close();
    }
    return graph;
}

int **formatGraph(vector<int> inData) {
    int **graph = 0;
    int currentIndex = 0;
    int srcNode = inData[0];
    int dstNode = inData[1];
    int cost = inData[2];
    graph = new int *[maxNode];
    for (int i = 0; i < maxNode; i++) {
        graph[i] = new int[maxNode];
        for (int j = 0; j < maxNode; j++) {
            if (srcNode == i && dstNode == j) {
                graph[i][j] = cost;
                currentIndex++;
                srcNode = inData[currentIndex * 3];
                dstNode = inData[currentIndex * 3 + 1];
                cost = inData[currentIndex * 3 + 2];
                //printf("%d %d\n", i, j);
            } else
                graph[i][j] = 0;
        }
    }
    for (int i = 0; i < maxNode; i++) {
        for (int j = 0; j < maxNode; j++) {
            graph[j][i] = graph[i][j];
        }
    }
    return graph;
}

Algorithm:

void dijkstra(int **G, int n, int startnode) {
    printf("%d\n", startnode);
    int **cost = new int *[maxNode];
    int distance[maxNode], pred[maxNode];
    int visited[maxNode], count, mindistance, nextnode, i, j;
    for (i = 0; i < n; i++) {
        cost[i] = new int[maxNode];
        for (j = 0; j < n; j++)
            cost[i][j] = 0;
    }
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            if (G[i][j] == 0)
                cost[i][j] = INFINITY;
            else
                cost[i][j] = G[i][j];
    for (i = 0; i < n; i++) {
        distance[i] = cost[startnode][i];
        pred[i] = startnode;
        visited[i] = 0;
    }
    distance[startnode] = 0;
    visited[startnode] = 1;
    count = 1;
    while (count < n - 1) {
        mindistance = INFINITY;
        for (i = 0; i < n; i++) {
            if (distance[i] < mindistance && !visited[i]) {
                mindistance = distance[i];
                nextnode = i;
            }
        }
        visited[nextnode] = 1;
        for (i = 0; i < n; i++) {
            if (!visited[i]) {
                if (mindistance + cost[nextnode][i] < distance[i]) {
                    distance[i] = mindistance + cost[nextnode][i];
                    pred[i] = nextnode;
                }
            }
        }
        count++;
    }
    delete[] cost;
    for (i = 0; i < n; i++)
        if (i != startnode) {
            j = i;
            do {
                j = pred[j];
            } while (j != startnode);
        }
}

And here is my main function:

int main() {
    vector<int> graph = loadFile("..\\data\\newFile2.csv");
    int **graphConverted = formatGraph(graph);
    //printMatrix(graphConverted);
    clock_t begin = clock();
    for (int i = 0; i < maxNode; i++)
        dijkstra(graphConverted, maxNode, i);
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    printf("\nTime: %f", elapsed_secs);
    return 0;
}

First the data is loaded into vector, and then it is converted to adjacency matrix. Data is stored in form:

src_node;dst_node;cost
1;2;3
1;3;30
1;66;20
etc.

Dataset consinsts of 1004 nodes and 25571 edges.

Could you please suggest me any solution how to fix this?

Upvotes: 0

Views: 105

Answers (1)

walnut
walnut

Reputation: 22152

In dijkstra you have dynamic memory allocations here:

int **cost = new int *[maxNode];

and here in a loop over i:

cost[i] = new int[maxNode];

You have only one call to delete[] in this function:

delete[] cost;

So all the allocations from the second new line are guaranteed to be leaked. After a while you will be out-of-memory, resulting in the std::bad_alloc.

You need to match each new[] call with exactly one delete[] call.


Don't use new/delete at all. Instead declare all your arrays as std::vector, which will take care of this automatically.

Also don't use variable-length arrays such as

int distance[maxNode], pred[maxNode];

They are a non-standard compiler extension. Make these std::vector as well.

Upvotes: 2

Related Questions