# S.O.S. Mathematics CyberBoard

Your Resource for mathematics help on the web!
 It is currently Sun, 19 May 2013 20:13:56 UTC

 All times are UTC [ DST ]

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Regarding Dijikstra's algorithm (programming problem)Posted: Thu, 8 Dec 2011 03:15:43 UTC

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

Tarektarek

Top

 Post subject: Re: Regarding Dijikstra's algorithm (programming problem)Posted: Thu, 8 Dec 2011 07:10:47 UTC
 Moderator

Joined: Mon, 29 Dec 2008 17:49:32 UTC
Posts: 6005
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

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.

_________________

Top

 Post subject: Re: Regarding Dijikstra's algorithm (programming problem)Posted: Thu, 8 Dec 2011 17:31:24 UTC

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?

Tarektarek

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC [ DST ]

#### Who is online

Users browsing this forum: No registered users

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

Search for:
 Jump to:  Select a forum ------------------ High School and College Mathematics    Algebra    Geometry and Trigonometry    Calculus    Matrix Algebra    Differential Equations    Probability and Statistics    Proposed Problems Applications    Physics, Chemistry, Engineering, etc.    Computer Science    Math for Business and Economics Advanced Mathematics    Foundations    Algebra and Number Theory    Analysis and Topology    Applied Mathematics    Other Topics in Advanced Mathematics Other Topics    Administrator Announcements    Comments and Suggestions for S.O.S. Math    Posting Math Formulas with LaTeX    Miscellaneous