老师布置的一个java的狄杰斯特拉算法问题。高手请进。
月神夜zz 2009-10-22 07:41:58 知道CSDN上的牛人比较多。所以发这儿来了,万能的CSDN请帮帮我吧。
Your task is to implement Dijkstra's algorithm for a graph structure. The idea is that you create a graph from an input file (as you did/will do for your homework in Week 11) and then find the length of the shortest paths from a given vertex to all the other vertices. Your program should then print out the path lengths. Here, the vertices will be integers (int), for simplicity's sake. You don't need to use a Node or Vertex class (these are just names for the same thing, remember).
1. Dijkstra(G, source)
2. for each v in V(G)
3. set dist[v] to ∞ (infinity)
4. set previous[v] to undefined
5. dist[source] ← 0
6. let Q ← V(G)
7. while Q is not empty
8. u ← vertex v in Q with smallest dist[] value
9. if dist[u] = ∞
10. break
11. remove u from Q
12. for each neighbour v of u
13. let alt ← dist[u] + edge length(u,v)
14. if alt < dist[v]
15. dist[v] ← alt
16. previous[v] = u
17. return dist[]
For the input graph attached in the file "weightedgraph.png", which could be expressed by the adjacency matrix (assuming 0 means " no edge")
a b c d e f g h
a 0 2 1 3 5 7 0 0
b 2 0 3 0 0 0 0 0
c 1 3 0 0 0 0 0 0
d 3 0 0 0 0 0 9 0
e 5 0 0 0 0 1 4 0
f 7 0 0 0 1 0 0 3
g 0 0 0 9 4 0 0 0
h 0 0 0 0 0 3 0 0
The output should be
dist[] = [ 0 2 1 3 5 6 9 9 ]