|
-
March 8th, 2011, 10:42 PM
#1
Help with impletementing dijkstra's Algorithm
I have two files; one with places then the distance between them.
Example:
Moffat Carlisle 65
Doncaster Hull 76
Northampton Birmingham 90
Leicester Lincoln 82
Sheffield Birmingham 122
I was wanting to use dijkstra's method, but i seem to have a problem because looking pseudo codes to understand it they all ask for a node, then all the connecting nodes. The problem is that the file is unordered so it will keep changing nodes. I thought i could search if it's already in the array/vector. If not then add it, but the problem with this is i don't know how many connecting nodes there is, so i would have to include a expanding varible.
The second file, contains a source city and dest city, which i have to work out the shortest path.
hope you can help!
-
March 9th, 2011, 04:50 AM
#2
Re: Help with impletementing dijkstra's Algorithm
so your first part of the problem is to create the graph / nodes.
Think about the properties of the nodes and how you would represent them in code.
-
March 9th, 2011, 05:38 AM
#3
Re: Help with impletementing dijkstra's Algorithm
The problem is though that say, the first line has York and Hull, So the first node would be York, with a edge of Hull, then the next Node would be Leeds with a edge of Manchester. The if York comes up again then it will be a seperate node unless i have a method of adding unlimited number of edges for each node.
-
March 9th, 2011, 06:56 AM
#4
Re: Help with impletementing dijkstra's Algorithm
ok, so a node has to be able to contain 'unlimited' edges.
Code:
class Node
{
public:
string name;
list<string> edges;
};
if nodes has a Node with name == "whatever"
then just add an edge.
else add a new node.
Of course, if you add a node York with edge Hull, you may want to also add a node Hull with edge York.
-
March 9th, 2011, 08:51 AM
#5
Re: Help with impletementing dijkstra's Algorithm
Hello, Thanks for the reply. I've worked out it will probably be best to create a adjacency graph for my problem first then impletement dijkstra's
-
March 9th, 2011, 07:48 PM
#6
Re: Help with impletementing dijkstra's Algorithm
Hello, Can you help. I used this class that you gave but i've struggling to add anything.
I tried
nodes.name.push_back(str);
but no use, and also i tried
nodes::name.push_back(str);
i also tried to compare sections by trying
nodes[i].name == temp;
but no use, Please help.
-
March 10th, 2011, 04:39 AM
#7
Re: Help with impletementing dijkstra's Algorithm
 Originally Posted by imhiya
Hello, Can you help. I used this class that you gave but i've struggling to add anything.
I tried
nodes.name.push_back(str);
but no use, and also i tried
nodes::name.push_back(str);
i also tried to compare sections by trying
nodes[i].name == temp;
but no use, Please help.
How do I know what 'temp' is?
name is a string - it has no push_back member.
Code:
string astring = "abc";
Node node;
node.name = astring;
You should also realise that the Node class I gave above is far from complete - it has no edge distances in. You should think about how the distances should be associated with an edge.
Last edited by Amleto; March 10th, 2011 at 04:42 AM.
-
March 10th, 2011, 04:44 PM
#8
Re: Help with impletementing dijkstra's Algorithm
If it is not some learning assignment, use boost::graph library, namely boost::adjacent_list.
Set the number of vertises, set edges with properties (the lib has functions to do it) , and use embedded Dijkstra algorithm function.
-
March 12th, 2011, 09:43 AM
#9
Re: Help with impletementing dijkstra's Algorithm
 Originally Posted by geoyar
If it is not some learning assignment, use boost::graph library, namely boost::adjacent_list.
Set the number of vertises, set edges with properties (the lib has functions to do it) , and use embedded Dijkstra algorithm function.
haha, Thank you. Atleast i know i have a last resort
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
|