-
June 5th, 2009, 10:28 AM
#1
Help with Dijkstra's Algorithm
Hi, I'm working with Dijkstra's Algorithm and i was successful to find the shortest path to every possible node (i used a mix of my own code and a sample found on codeproject) now the problem is that the program ouputs only the cost of the shortest path and i want it to output the path it took (e.g. A->C -> f) and i'm facing problems doing that any help would be appreciated.
<code>
public void Solve()
{
int count;
for (count =1 ; count< Nn; count++)
{
pathpoint = 0;
Calc();
}
pathpoint = 0;
iteration = 0;
for (int i = 0; i < Nn; i++)
{
for (int j = 0; j < Nn; j++)
{
if (Path[i,j] != -1)
textBox3.Text += (NodesNames[Path[i, j]] + " ");
}
textBox3.Text += Environment.NewLine;
}
textBox3.Text += Environment.NewLine;
for (int i = 0; i < Nn; i++)
textBox3.Text += (LeastCost[i] + " ");
}
public void Calc ()
{
int minValue = Int32.MaxValue;
int minNode = 0;
for (int i = 0; i < Nn; i++)
{
if (Nodes[i] == 0)
continue;
if (LeastCost[i] > 0 && LeastCost[i] < minValue)
{
minValue = LeastCost[i];
minNode = i;
}
}
Nodes[minNode] = 0;
pathpoint++;
Path[iteration,pathpoint] = minNode;
pathpoint++;
for (int i = 1; i < Nn; i++)
{
if (RoutingTable[minNode, i] < 1)
continue;
if (LeastCost[i] < 1)
{
LeastCost[i] = minValue + RoutingTable[minNode, i];
Path[iteration, pathpoint] = i;
pathpoint++;
continue;
}
if ((LeastCost[minNode] + RoutingTable[minNode, i]) < LeastCost[i])
{
Path[iteration, pathpoint] = i;
pathpoint++;
LeastCost[i] = minValue + RoutingTable[minNode, i];
}
}
iteration++;
pathpoint = 0;
} </code>
Tags for this Thread
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
|