-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b77c84
commit 7b403fa
Showing
4 changed files
with
199 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
|
||
var dijkstra_t = function(){ | ||
|
||
var dijkstra = function(g, order, s){ | ||
var i, j, k, len, m, min; | ||
|
||
i = order; | ||
|
||
var dist = new Array(i); | ||
var prev = new Array(i); | ||
var used = new Array(i); | ||
var busy = new Array(i); | ||
|
||
while(i--){ | ||
dist[i] = Infinity; | ||
prev[i] = s[0]; | ||
used[i] = false; | ||
busy[i] = false; | ||
} | ||
|
||
var left = []; | ||
used[s[0]] = true; | ||
busy[s[0]] = true; | ||
|
||
g.eitr(s, function(e){ | ||
dist[e[0][0]] = e[1]; | ||
busy[e[0][0]] = true; | ||
left.push(e[0]); | ||
}); | ||
|
||
while(left.length){ | ||
|
||
m = left[0]; | ||
min = dist[m[0]]; | ||
j = 0; | ||
|
||
for(i = 1, len = left.length; i < len; ++i){ | ||
if(dist[left[i][0]] < min){ | ||
m = left[i]; | ||
min = dist[m[0]]; | ||
j = i; | ||
} | ||
} | ||
|
||
left.splice(j, 1); | ||
used[m[0]] = true; | ||
|
||
g.eitr(m, function(e){ | ||
var y = e[0]; | ||
|
||
if(!used[y[0]]){ | ||
|
||
var v = dist[m[0]] + e[1]; | ||
|
||
if(v < dist[y[0]]){ | ||
dist[y[0]] = v; | ||
prev[y[0]] = m[0]; | ||
} | ||
|
||
if(!busy[y[0]]){ | ||
left.push(y); | ||
busy[y[0]] = true; | ||
} | ||
|
||
} | ||
}); | ||
} | ||
|
||
return [prev, dist]; | ||
}; | ||
|
||
return dijkstra; | ||
|
||
}; | ||
|
||
|
||
exports.dijkstra_t = dijkstra_t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
|
||
var testrunner = require('qunit'); | ||
|
||
var argv = require('optimist').argv; | ||
|
||
var testrunner = require('qunit'); | ||
testrunner.options.coverage = true; | ||
|
||
testrunner.run( | ||
{ | ||
code : {path :[__dirname, '..', '..', 'js', 'index.js'].join('/'), namespace: 'gn'}, | ||
tests : [__dirname, 'index.js'].join('/') | ||
}, | ||
|
||
function(err, report) { | ||
console.dir(report); | ||
} | ||
); | ||
var cb = function(err, report) { | ||
// console.dir(report); | ||
}; | ||
|
||
var run = function(item, ns) { | ||
testrunner.run( | ||
{ | ||
code : { | ||
path :[__dirname, '..', '..', 'js', 'index.js'].join('/'), | ||
namespace: ns | ||
}, | ||
tests : [__dirname, item].join('/') | ||
}, cb | ||
); | ||
}; | ||
|
||
|
||
for(var i = 0; i < argv._.length; ++i){ | ||
argv._[i] = ['src', argv._[i]+'.js'].join('/'); | ||
} | ||
|
||
if(argv._.length === 0) argv._.push('index.js'); | ||
|
||
for(var i = 0; i < argv._.length; ++i){ | ||
run(argv._[i], 'gn'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
|
||
var check = function(label, n, s, edges, prev, dist){ | ||
|
||
test('dijkstra #' + label, function(assert){ | ||
|
||
var Graph = gn.dense_graph_t(); | ||
var dijkstra = gn.dijkstra_t(); | ||
|
||
var g = new Graph(); | ||
var i = n; | ||
|
||
var v = new Array(i); | ||
|
||
while(i--) v[n-i-1] = g.vadd(); | ||
|
||
for(var j = 0; j < edges.length; ++j){ | ||
var e = edges[j]; | ||
g.eadd(v[e[0]], v[e[1]], e[2]); | ||
} | ||
|
||
var sp = dijkstra(g, n, v[s]); | ||
|
||
deepEqual(prev, sp[0], 'prev'); | ||
deepEqual(dist, sp[1], 'dist'); | ||
|
||
}); | ||
|
||
}; | ||
|
||
|
||
var I = [ | ||
[ | ||
'1', | ||
10, | ||
9, | ||
[ | ||
[0, 1, 1], | ||
[3, 1, 2], | ||
[5, 4, 3], | ||
[3, 4, 4], | ||
[6, 1, 5], | ||
[2, 3, 1], | ||
[9, 2, 6], | ||
[4, 7, 6] | ||
], | ||
[1, 3, 9, 2, 3, 4, 1, 4, 9, 9], | ||
[10, 9, 6, 7, 11, 14, 14, 17, Infinity, Infinity] | ||
], | ||
|
||
[ | ||
'http://stackoverflow.com/questions/14159424/dijkstras-algorithm-why-is-it-needed-to-find-minimum-distance-element-in-the-q#1', | ||
4, | ||
0, | ||
[ | ||
[0, 1, 6], | ||
[1, 2, 7], | ||
[2, 3, 2], | ||
[0, 3, 7] | ||
], | ||
[0, 0, 3, 0], | ||
[Infinity, 6, 9, 7] | ||
], | ||
|
||
[ | ||
'http://stackoverflow.com/questions/14159424/dijkstras-algorithm-why-is-it-needed-to-find-minimum-distance-element-in-the-q#2', | ||
9, | ||
2, | ||
[ | ||
[1, 5, 6], | ||
[5, 3, 2], | ||
[1, 2, 7], | ||
[2, 3, 2], | ||
[1, 4, 7], | ||
[4, 3, 1], | ||
[1, 7, 3], | ||
[7, 8, 2], | ||
[8, 3, 2] | ||
], | ||
[2, 2, 2, 2, 3, 3, 2, 8, 3], | ||
[Infinity, 7, Infinity, 2, 3, 4, Infinity, 6, 4] | ||
] | ||
|
||
|
||
|
||
] | ||
|
||
|
||
for(var i = 0; i < I.length; ++i){ | ||
check.apply(undefined, I[i]); | ||
} |