08-27-周三_17-09-29

This commit is contained in:
2025-08-27 17:10:05 +08:00
commit 86df397d8f
12735 changed files with 1145479 additions and 0 deletions

50
node_modules/graphlibrary/lib/alg/floyd-warshall.js generated vendored Normal file
View File

@@ -0,0 +1,50 @@
var _ = require('../lodash')
module.exports = floydWarshall
var DEFAULT_WEIGHT_FUNC = _.constant(1)
function floydWarshall (g, weightFn, edgeFn) {
return runFloydWarshall(g,
weightFn || DEFAULT_WEIGHT_FUNC,
edgeFn || function (v) { return g.outEdges(v) })
}
function runFloydWarshall (g, weightFn, edgeFn) {
const results = {}
const nodes = g.nodes()
nodes.forEach(function (v) {
results[v] = {}
results[v][v] = { distance: 0 }
nodes.forEach(function (w) {
if (v !== w) {
results[v][w] = { distance: Number.POSITIVE_INFINITY }
}
})
edgeFn(v).forEach(function (edge) {
const w = edge.v === v ? edge.w : edge.v
const d = weightFn(edge)
results[v][w] = { distance: d, predecessor: v }
})
})
nodes.forEach(function (k) {
var rowK = results[k]
nodes.forEach(function (i) {
var rowI = results[i]
nodes.forEach(function (j) {
var ik = rowI[k]
var kj = rowK[j]
var ij = rowI[j]
var altDistance = ik.distance + kj.distance
if (altDistance < ij.distance) {
ij.distance = altDistance
ij.predecessor = kj.predecessor
}
})
})
})
return results
}