telsalti wrote:
Hi All,
I am dealing with 1000 nodes and I want to calculate the shortest path from one to all 999 nodes and keeping doing this with second node with 999 nodes and so on until each pair of nodes are explored as source and destination nodes. I am using Dijikstra's algorithm but seems the way it is implemented is inefficient. Each time I extract the minimum distance using priority queue based on a linked list. What I am using is a global array which stores a node and its neighbors as an array where I can think this is the inefficient part
Code:
for(int index4 = 0; index4 < sp.nodes[minimum].nbrcnt; index4++)
{
double temp_dis = sp.nodes[minimum].distance + pow(sp.dis(sp.nodes[minimum].nbrlist[index4],minimum),2);
if(temp_dis < sp.nodes[sp.nodes[minimum].nbrlist[index4]].distance)
{
sp.nodes[sp.nodes[minimum].nbrlist[index4]].distance = temp_dis;
sp.nodes[sp.nodes[minimum].nbrlist[index4]].predecessor_sub = minimum;
}
}
From my understanding and reading from references, I need to do the following change. I need to make each node has a linked list (its neighbors) as adjacency list and from there I can modify any value I want quickly. Correct me if I am wrong. Another concern is that if I change say the distance for one neighbor how come I will deal with the distances for other neighbors. for example
1---2---3---1 (This is my graph as a complete graph)
So node 1 has --- 3 ---> 2 ----> NULL (linked list) ..... (1)
node 2 has 1 ------> 3------> NULL ....... (2)
node 3 has 1------->2------->NULL ....... (3)
Say I changed the distance value for node 3 in linked list (1) I will also need to change it in linked list (2) and (3) unless I am missing something. Thank you so much
Thank you in advance
Tarektarek
What do you want to find exactly? d(i,j) for any pair of vertices?
If you change the weight of (undirected) edge ij, you need to change the value in the adjacency list of the vertices i and j
only. Of course, you should also re-sort the vertices in the linked list...
I'm not too convinced a linked list would be much good here, since the constant need to dereference the pointers (which may point quite arbitrarily in your RAM) could mean that you consistently missed the CPU's level 1/level 2 cache and end up needing more time. Maybe you should do some benchmarking.