0

I have been struggling to improve the memory performance of this implementation of Dijkstra's algorithm. Do you have any tips on how to improve this while still using the same data structures and loops?

I basically used an implementation from a Cornell Univ lecture:https://www.cs.cornell.edu/~wdtseng/icpc/notes/graph_part2.pdf

#include <iostream>
#include <stdio.h>
#include <vector>
#include <limits.h>
#include <utility> 
#include <list>
#include <set>
#include<bits/stdc++.h>

using namespace std;

int graph[10000][10000];

void dijkstra(int src_node, int num_nodes, int dist[], bool done[]) ;

int main()
{
int num_nodes, num_edges;
cin >> num_nodes >> num_edges;

for(int i = 1; i <= num_nodes; i++)
{
    int src, dest, weight;
    cin >> src >> dest >> weight;

    graph[src][dest] = weight;
}

int src_node = 1;
int dist[num_nodes];
bool done[num_nodes];

dijkstra(src_node, num_nodes, dist,done);

for(int p = 2; p <= num_nodes; p++)
{
    cout << dist[p] << " " ;
}

return 0;
}

void dijkstra(int src_node, int num_nodes, int dist[], bool done[]) 
{
// set all distances to infinity and set all visits to false
for( int i = 1; i <= num_nodes; i++ ) 
{
    dist[i] = INT_MAX;
    done[i] = false;
}

dist[src_node] = 0;

while(true) 
{
    // find the vertex with the smallest dist[] value
    int cur_node = -1;
    int bestDist = INT_MAX;

    //
    for( int i = 1; i <= num_nodes; i++ ) 
    {
        if( !done[i] && dist[i] < bestDist ) 
        {
            cur_node = i;
            bestDist = dist[i];
        }
    }

    // if the best distance is infinity, break out of the loop
    if( bestDist == INT_MAX )
    {
        break;
    }

    // iterate through all the neighbors
    for( int adj_node = 1; adj_node <= num_nodes; adj_node++ )
    {
        // if the adj node has not been visited and has a weight
        if( !done[adj_node] && graph[cur_node][adj_node] != NULL ) 
        {
            // if the distance of the adj node is greater
            if( dist[adj_node] > dist[cur_node] + graph[cur_node][adj_node])
            {
                // make the dist = to curr node distance  + weight
                dist[adj_node] = dist[cur_node] + graph[cur_node][adj_node];
            }
        }
    }
    // mark current node as done
    done[cur_node] = true;
}
}
  • 1
    Profile. Where is most of the time spent? Please edit your post with the answer. – Thomas Matthews Apr 15 '17 at 22:39
  • Unrelated, If your intent here: `graph[cur_node][adj_node] != NULL` - was to compare to zero, maybe just do that rather than bringing `NULL` into the picture. – WhozCraig Apr 15 '17 at 22:39
  • 3
    [Code Review](https://codereview.stackexchange.com) – Andria Apr 15 '17 at 22:39
  • You are going to have a hard time improving the memory usage, if you're not open to altering the data structures. A big chunk is the 10000 x 10000 array (400 mb on a 32 bit system). Strategic use of a std::vector could let you size that to the number of nodes. As a side note, you need to remember arrays in C++ are indexed from 0 .. n-1, not 1 ..n – Dave S Apr 18 '17 at 11:08
  • Thanks folks, I eventually solved the issue by using a vector instead of an array as @DaveS mentioned. I appreciate the help. – Kgotso Koete Apr 22 '17 at 22:49

0 Answers0