问题描述:

I've hit a wall in attempting to understand Dijkstra's algorithm. The algorithm, in short, finds the shortest distances between A and B given distances between the two.

I will post my version of the algorithm (I haven't had much success looking online thus far), followed by the distances between nodes.

`void GraphM::findShortestPath()`

{

for (int i = 1; i <= nodeCount; i++) // From Node i...

{

for (int v = 1; v <= nodeCount; v++) // ...through Node v...

{

for (int w = 1; w <= nodeCount; w++) // ...to Node w

{

if (!T[i][w].visited || !T[i][v].visited)

{

T[i][w].dist = min(T[i][w].dist, T[i][v].dist + C[v][w]);

T[i][v].visited = true;

T[i][w].visited = true;

}

}

}

}

cout << "1 to 2 is " << T[1][2].dist << endl;

}

This outputs the following:

`1 to 2 is 48`

...when it should be

`1 to 2 is 40`

The values I'm working with are as follows:

`1 2 50`

1 3 20

1 5 30

2 4 10

3 2 20

3 4 40

5 2 20

5 4 25

...where, in each line, the first token is the first node, the second token is the second node, and the third token is the distance between those nodes (in the algorithm's case, these tokens would be i, v, and T[i][v].dist). In the algorithm, nodeCount is the number of nodes in the grid (5), and w is a node that we are looking for the distance to, from i. C[v][w] returns the original distance between v and w. So, if v was 5 and w was 2, C[v][w] would return 20. This is constant, whereas T[v][w].dist (for instance) can be changed.

Any nonexistant node relationships such as C[5][3] or T[1][4].dist (at least at the outset) return INT_MAX, which is equivalent to infinity.

Also, for anyone wondering; yes, this is a homework assignment. Unfortunately, my professor has necessitated some specific details (such as using a struct T), and she never went into much detail on how to write Dijkstra's algorithm into code, other than a somewhat vague outline. I am simply asking if someone can tell me what I am doing wrong and how to fix it, if possible.

Any help is very much appreciated and would save me a lot of time from banging my head against a wall.

This is not Dijkstra's algorithm. What you are trying to implement is Floyd-Warshall algorithm. This would find the minimum distance for all pair of vertices.

http://en.wikipedia.org/wiki/Floyd–Warshall_algorithm

Note that the first loop loops through the transfer node. With this implementation you don't need to remember which edge you already visited.

```
void GraphM::findShortestPath()
{
// Initialize T[i][j] to C[i][j] or MAX_INT here
for (int k = 1; k <= nodeCount; k++) // Through Node k...
{
for (int u = 1; u <= nodeCount; u++) // ...From Node u...
{
for (int v = 1; v <= nodeCount; u++) // ...to Node v
{
// if going through k provides a cheaper path, update T[u][v]
T[u][v] = min(T[u][v], T[u][k] + T[k][v]);
}
}
}
cout << "1 to 2 is " << T[1][2]<< endl;
}
```

您可能感兴趣的文章：

- java - hyperlinking the table header and making it open in new Window
- javascript - Delay startup of routing logic in angularjs
- php - Set the path for mysql logging relative to DOCUMENT_ROOT
- digital logic - Strange component in quartus RTL viewer using verilog
- webserver - What is the best way to start thin in a rails application?
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial