|
12 | 12 | * var Edge = BellmanFord.Edge;
|
13 | 13 | * var bellmanFord = BellmanFord.bellmanFord;
|
14 | 14 | * var edges = [];
|
15 |
| - * var vertexes = [0, 1, 2, 3, 4]; |
| 15 | + * var vertexes = [ |
| 16 | + * new Vertex(0), |
| 17 | + * new Vertex(1), |
| 18 | + * new Vertex(2), |
| 19 | + * new Vertex(3), |
| 20 | + * new Vertex(4) |
| 21 | + * ]; |
16 | 22 | *
|
17 | 23 | * edges.push(new Edge(0, 1, -1));
|
18 | 24 | * edges.push(new Edge(0, 2, 4));
|
|
55 | 61 | varparents={};
|
56 | 62 | varc;
|
57 | 63 | for(vari=0;i<vertexes.length;i+=1){
|
58 |
| -distances[vertexes[i]]=Infinity; |
59 |
| -parents[vertexes[i]]=null; |
| 64 | +distances[vertexes[i].id]=Infinity; |
| 65 | +parents[vertexes[i].id]=null; |
60 | 66 | }
|
61 | 67 | distances[source]=0;
|
62 | 68 | for(i=0;i<vertexes.length-1;i+=1){
|
63 | 69 | for(varj=0;j<edges.length;j+=1){
|
64 | 70 | c=edges[j];
|
65 |
| -if(distances[c.from]+c.weight<distances[c.to]){ |
66 |
| -distances[c.to]=distances[c.from]+c.weight; |
67 |
| -parents[c.to]=c.from; |
| 71 | +if(distances[c.from.id]+c.distance<distances[c.to.id]){ |
| 72 | +distances[c.to.id]=distances[c.from.id]+c.distance; |
| 73 | +parents[c.to.id]=c.from.id; |
68 | 74 | }
|
69 | 75 | }
|
70 | 76 | }
|
71 | 77 |
|
72 | 78 | for(i=0;i<edges.length;i+=1){
|
73 | 79 | c=edges[i];
|
74 |
| -if(distances[c.from]+c.weight<distances[c.to]){ |
| 80 | +if(distances[c.from.id]+c.distance<distances[c.to.id]){ |
75 | 81 | returnundefined;
|
76 | 82 | }
|
77 | 83 | }
|
|