S.O.S. Mathematics CyberBoard

Your Resource for mathematics help on the web!
It is currently Thu, 20 Jun 2013 01:07:34 UTC

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Regarding Dijikstra's algorithm (programming problem)
PostPosted: Thu, 8 Dec 2011 03:15:43 UTC 
Offline
Math Cadet

Joined: Tue, 15 Feb 2011 04:55:41 UTC
Posts: 5
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


Top
 Profile  
 
 Post subject: Re: Regarding Dijikstra's algorithm (programming problem)
PostPosted: Thu, 8 Dec 2011 07:10:47 UTC 
Offline
Moderator
User avatar

Joined: Mon, 29 Dec 2008 17:49:32 UTC
Posts: 6068
Location: 127.0.0.1, ::1 (avatar courtesy of UDN)
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.

_________________
\begin{aligned}
Spin(1)&=O(1)=\mathbb{Z}/2&\quad&\text{and}\\
Spin(2)&=U(1)=SO(2)&&\text{are obvious}\\
Spin(3)&=Sp(1)=SU(2)&&\text{by }q\mapsto(\mathop{\mathrm{Im}}\mathbb{H}\ni p\mapsto qp\bar{q})\\
Spin(4)&=Sp(1)\times Sp(1)&&\text{by }(q_1,q_2)\mapsto(\mathbb{H}\ni p\mapsto q_1p\bar{q_2})\\
Spin(5)&=Sp(2)&&\text{by }\mathbb{HP}^1\cong S^4_{round}\hookrightarrow\mathbb{R}^5\\
Spin(6)&=SU(4)&&\text{by the irrep }\Lambda_+\mathbb{C}^4
\end{aligned}


Top
 Profile  
 
 Post subject: Re: Regarding Dijikstra's algorithm (programming problem)
PostPosted: Thu, 8 Dec 2011 17:31:24 UTC 
Offline
Math Cadet

Joined: Tue, 15 Feb 2011 04:55:41 UTC
Posts: 5
Thank you outermeasure for your response!
Suppose that I use the adjacancy lists as my solution rather than using arrays which are expensive in insertion and deletion. My main confusion is that if we go to the previous example:

1---2----3----1 (a complete graph)
so I will have two types of linked lists
one that is contained in the priority queue which has all the nodes in the graph and their distances
1----->2------>3----->NULL ..... (a)
another type is for each node there is an adjacency list
e.g., 1 has 2-----> 3----->NULL.... (1)
2 has 1----->3---->NULL...... (2)
3 has 2----->1--->NULL.... (3)
so my main question is that if I change something in the linked list (1) say the distance for 3, then I will have to change it in linked list (2) as well as in the main linked list (a), I think I am not thinking in the right way, correct me please.

You said something about benchmarking what do you mean by that please?

Thank you in advance
Tarektarek


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Contact Us | S.O.S. Mathematics Homepage
Privacy Statement | Search the "old" CyberBoard

users online during the last hour
Powered by phpBB © 2001, 2005-2011 phpBB Group.
Copyright © 1999-2013 MathMedics, LLC. All rights reserved.
Math Medics, LLC. - P.O. Box 12395 - El Paso TX 79913 - USA