-
April 25th, 2009, 12:25 AM
#1
Problems with vector manipulation
I'm implementing dijkstra's algorithm, and I'm implementing it using a priority queue so that the shortest distance is on top for fast access. The priority queue uses a vector for its operations, and contains objects of type Node.
Each node has the following properties:
int id
int dist
Node *path
Here is the issue: when I attempt to rearrange the priority queue (i.e. swap two elements during a percolate function to keep the lowest distance on the top), they overwrite eachother. So, let's take this example:
Vector:
[1] = node1
[2] = node2
[3] = node3
Node 3's path points to node 1.
Now when I attempt to swap indexes 3 and 1 using a temp variable and assignment statements, node 1 becomes node 3, and node 3's path member points to itself infinitely, destroying what I have of my path so far.
Summary: I need to be able to swap elements without them overwriting eachother.
Thank you much.
-
April 25th, 2009, 06:23 AM
#2
Re: Problems with vector manipulation
Originally Posted by IRvineKinneas509@gmail.com
Summary: I need to be able to swap elements without them overwriting each other.
Since you didn't show any code, let me start:
Code:
// Swap node1 and node2
void swap_nodes(Node* node1, Node *node2)
{
// fill this in with the code you described
}
Then we will know what exactly you're doing.
Regards,
Paul McKenzie
-
April 25th, 2009, 05:37 PM
#3
Re: Problems with vector manipulation
I have no code for that function because I don't know how to do it. I'm just doing the swap like this:
Node tmp;
tmp = array[i];
array[i] = array[j]
array[j] = tmp;
This overwrites the elements
-
April 25th, 2009, 11:49 PM
#4
Re: Problems with vector manipulation
Originally Posted by IRvineKinneas509@gmail.com
I have no code for that function because I don't know how to do it. I'm just doing the swap like this:
Node tmp;
tmp = array[i];
array[i] = array[j]
array[j] = tmp;-
This overwrites the elements
That, by definition, is exactly what swap is supposed to do, and I see no "overwriting". You are exchanging the exact contents of one Node with the other Node. If you desire the contents to not be exact after this operation, then what you want is not a swap.
Regards,
Paul McKenzie
-
April 26th, 2009, 08:52 AM
#5
Re: Problems with vector manipulation
Thanks for your help.
-
April 27th, 2009, 04:30 AM
#6
Re: Problems with vector manipulation
What is the declaration of Node
Do you have a user defined copy constructor and assignment operator?
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|