UNPKG

lite-pathfindings

Version:

Simple, intuitive and lightweight pathfinding algorithms

35 lines (30 loc) 1.54 kB
"use strict"; var litepathfindings = require('./lite-pathfindings'); var FloydWarshall = litepathfindings.FloydWarshall; var Helpers = litepathfindings.Helpers; // Matrix which contains edge and weight var matrixEdges = [[0, 12, 20, 9, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY], [12, 0, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, 13], [20, Number.POSITIVE_INFINITY, 0, 8, Number.POSITIVE_INFINITY, 11, 2], [9, Number.POSITIVE_INFINITY, 8, 0, Number.POSITIVE_INFINITY, 21, Number.POSITIVE_INFINITY], [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, 0, 3, 9], [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, 11, 21, 3, 0, 5], [Number.POSITIVE_INFINITY, 13, 20, Number.POSITIVE_INFINITY, 9, 5, 0]]; var edgeMap = { a: { b: 12, c: 20, d: 9 }, b: { a: 12, g: 13 }, c: { a: 20, d: 8, f: 11, g: 2 }, d: { a: 9, c: 8, f: 21 }, e: { g: 9, f: 3 }, f: { c: 11, d: 21, e: 3, g: 5 }, g: { b: 13, c: 2, f: 5, e: 9 } }; edgeMap["a"]["b"] = -2000; console.log(Helpers.edgeMapContainNegativeValue(edgeMap)); console.log('a'); var matrix = matrixEdges; console.log('b'); var next = FloydWarshall.init(matrix); console.log('c'); if (!FloydWarshall.containNegativeCycle(matrix)) { var path = FloydWarshall.getPath(0, 5, next); console.log('d'); var weight = FloydWarshall.getWeight(0, 5, matrix); console.log('e'); console.log(path); console.log("(" + weight + ")"); }