Skip to content

Commit 8b5ab74

Browse files
committed
improve bellman_ford
1 parent 528b0c0 commit 8b5ab74

File tree

2 files changed

+44
-52
lines changed

2 files changed

+44
-52
lines changed
Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,55 @@
11
function BELLMAN_FORD(src, dest) {
2-
var weights = new Array(G.length);
2+
var weights = new Array(G.length), i, j;
33

4-
for (var i = 0; i < G.length; i++) {
5-
weights [i] = MAX_VALUE;
6-
tracer._weight(i, weights[i]);
7-
}
8-
weights [src] = 0;
9-
tracer._weight(src, 0);
10-
11-
logger._print('Initializing weights to: [' + weights + ']');
12-
logger._print('');
4+
for (i = 0; i < G.length; i++) {
5+
weights[i] = MAX_VALUE;
6+
tracer._weight(i, weights[i]);
7+
}
8+
weights[src] = 0;
9+
tracer._weight(src, 0);
1310

14-
//begin BF algorithm execution
15-
i = G.length - 1;
16-
while (i--) {
17-
logger._print('Iteration: ' + (G.length - i - 1));
18-
logger._print('------------------------------------------------------------------');
11+
logger._print('Initializing weights to: [' + weights + ']');
12+
logger._print('');
1913

20-
for (var currentNode = 0; currentNode < G.length; currentNode++) {
21-
for (var currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
22-
if (G [currentNode] [currentNodeNeighbor]) { //proceed to relax Edges only if a particular weight != 0 (0 represents no edge)
23-
logger._print('Exploring edge from ' + currentNode + ' to ' + currentNodeNeighbor + ', weight = ' + G [currentNode] [currentNodeNeighbor]);
14+
//begin BF algorithm execution
15+
var k = G.length;
16+
while (k--) {
17+
logger._print('Iteration: ' + (G.length - k));
18+
logger._print('------------------------------------------------------------------');
2419

25-
if (weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor])) {
26-
weights [currentNodeNeighbor] = weights [currentNode] + G [currentNode] [currentNodeNeighbor];
27-
logger._print('weights [' + currentNodeNeighbor + '] = weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
28-
}
29-
tracer._visit(currentNodeNeighbor, currentNode, weights [currentNodeNeighbor])._wait();
30-
tracer._leave(currentNodeNeighbor, currentNode)._wait();
31-
}
32-
}
20+
for (i = 0; i < G.length; i++) {
21+
for (j = 0; j < G.length; j++) {
22+
if (G[i][j]) { //proceed to relax Edges only if a particular weight != 0 (0 represents no edge)
23+
if (weights[j] > (weights[i] + G[i][j])) {
24+
weights[j] = weights[i] + G[i][j];
25+
logger._print('weights[' + j + '] = weights[' + i + '] + ' + G[i][j]);
26+
}
27+
tracer._visit(j, i, weights[j])._wait();
28+
tracer._leave(j, i)._wait();
3329
}
34-
35-
logger._print('updated weights: [' + weights + ']');
36-
logger._print('');
30+
}
3731
}
3832

39-
//check for cycle
40-
logger._print('checking for cycle');
41-
for (currentNode = 0; currentNode < G.length; currentNode++) {
42-
for (currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
43-
if (G [currentNode] [currentNodeNeighbor]) {
44-
if (weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor])) {
45-
logger._print('A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
46-
return (MAX_VALUE);
47-
}
48-
}
33+
logger._print('updated weights: [' + weights.join(', ') + ']');
34+
logger._print('');
35+
}
36+
37+
//check for cycle
38+
logger._print('checking for cycle');
39+
for (i = 0; i < G.length; i++) {
40+
for (j = 0; j < G.length; j++) {
41+
if (G[i][j]) {
42+
if (weights[j] > (weights[i] + G[i][j])) {
43+
logger._print('A cycle was detected: weights[' + j + '] > weights[' + i + '] + ' + G[i][j]);
44+
return (MAX_VALUE);
4945
}
46+
}
5047
}
48+
}
5149

52-
logger._print('No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']');
50+
logger._print('No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']');
5351

54-
return weights [dest];
52+
return weights[dest];
5553
}
5654

5755
var src = Math.random() * G.length | 0, dest;
@@ -64,7 +62,7 @@ var minWeight;
6462
*/
6563

6664
do {
67-
dest = Math.random() * G.length | 0;
65+
dest = Math.random() * G.length | 0;
6866
}
6967
while (src === dest);
7068

@@ -73,7 +71,7 @@ logger._print('finding the shortest path from ' + src + ' to ' + dest);
7371
minWeight = BELLMAN_FORD(src, dest);
7472

7573
if (minWeight === MAX_VALUE) {
76-
logger._print('there is no path from ' + src + ' to ' + dest);
74+
logger._print('there is no path from ' + src + ' to ' + dest);
7775
} else {
78-
logger._print('the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight);
76+
logger._print('the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight);
7977
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
var tracer = new WeightedDirectedGraphTracer();
22
var logger = new LogTracer();
33
tracer.attach(logger);
4-
var G = [
5-
[0, -1, 4, 0, 0],
6-
[0, 0, 3, 2, 2],
7-
[0, 0, 0, 0, 0],
8-
[0, 1, 5, 0, 0],
9-
[0, 0, 0, -3, 0]
10-
];
4+
var G = WeightedDirectedGraph.random(5, .5, -2, 5);
115
tracer._setData(G);

0 commit comments

Comments
 (0)