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;
}
}