Speeding up Dijkstra's Algorithm
Here, dist[][] is the adjacency matrix representing the graph and n is the number of nodes. d2[] is where the lengths of the shortest paths are saved.
For a full explanation, see this.
1 2 void dijkstra2(int s) { 3 int queue[GRAPHSIZE]; 4 char inQueue[GRAPHSIZE]; 5 int begq = 0, 6 endq = 0; 7 int i, mini; 8 int visited[GRAPHSIZE]; 9 10 for (i = 1; i <= n; ++i) { 11 d2[i] = INFINITY; 12 visited[i] = 0; /* the i-th element has not yet been visited */ 13 inQueue[i] = 0; 14 } 15 16 d2[s] = 0; 17 queue[endq] = s; 18 endq = (endq + 1) % GRAPHSIZE; 19 20 21 while (begq != endq) { 22 mini = queue[begq]; 23 begq = (begq + 1) % GRAPHSIZE; 24 inQueue[mini] = 0; 25 26 visited[mini] = 1; 27 28 for (i = 1; i <= n; ++i) 29 if (dist[mini][i]) 30 if (d2[mini] + dist[mini][i] < d2[i]) { 31 d2[i] = d2[mini] + dist[mini][i]; 32 if (!inQueue[i]) { 33 queue[endq] = i; 34 endq = (endq + 1) % GRAPHSIZE; 35 inQueue[i] = 1; 36 } 37 } 38 } 39 }