1- ( function ( global ) {
1+ /**
2+ * Bellman–Ford algorithm computes shortest paths from a single source
3+ * vertex to all of the other vertices in a weighted digraph (negative weights allowed).<br><br>
4+ * Time complexity: O(|V||E|) where V and E are the number of vertices and edges respectively.
5+ *
6+ * @example
7+ *
8+ * var BellmanFord = require('../src/graphs/shortest-path/bellman-ford');
9+ * var Edge = BellmanFord.Edge;
10+ * var bellmanFord = BellmanFord.bellmanFord;
11+ * var edges = [];
12+ * var vertexes = [0, 1, 2, 3, 4];
13+ *
14+ * edges.push(new Edge(0, 1, -1));
15+ * edges.push(new Edge(0, 2, 4));
16+ * edges.push(new Edge(1, 2, 3));
17+ * edges.push(new Edge(1, 3, 2));
18+ * edges.push(new Edge(3, 1, 1));
19+ * edges.push(new Edge(4, 3, -3));
20+ * edges.push(new Edge(1, 4, 2));
21+ * edges.push(new Edge(3, 2, 5));
22+ *
23+ * // {
24+ * // parents: { '0': null, '1': 0, '2': 1, '3': 4, '4': 1 },
25+ * // distances: { '0': 0, '1': -1, '2': 2, '3': -2, '4': 1 }
26+ * // }
27+ * var pathInfo = bellmanFord(vertexes, edges, 0);
28+ *
29+ * @module graphs/shortest-path/bellman-ford
30+ */
31+ ( function ( exports ) {
32+
233 'use strict' ;
334
4- function Edge ( u , v , weight ) {
35+ /**
36+ * Graph edge.
37+ *
38+ * @constructor
39+ * @public
40+ * @param {Vertex } u Start vertex.
41+ * @param {Vertex } v End vertex.
42+ * @param {Number } weight Weight of the edge.
43+ */
44+ exports . Edge = function ( u , v , weight ) {
545 this . from = u ;
646 this . to = v ;
747 this . weight = weight ;
848 }
949
1050 /**
11- * Bellman–Ford algorithm computes shortest paths from a single source
12- * vertex to all of the other vertices in a weighted digraph (negative weights allowed).<br><br>
13- * Time complexity: O(|V||E|) where V and E are the number of vertices and edges respectively.
51+ * Computes shortest paths from a single source vertex to all of the other vertices.
1452 *
1553 * @public
16- * @module graphs/shortest-path/bellman-ford
1754 * @param {Array } vertexes Vertices of the graph.
1855 * @param {Array } edges Edges of the graph.
1956 * @param {Number } source Start vertex.
2057 * @returns {Object } Object with two arrays (parents and distances) with shortest-path information.
2158 *
22- * @example
23- * require('path-to-algorithms/src/graphs/shortest-path/bellman-ford');
24- *
25- * var glob = (typeof window === 'undefined') ? global : window;
26- * var Edge = glob.Edge;
27- * var bellmanFord = glob.bellmanFord;
28- * var edges = [];
29- * var vertexes = [0, 1, 2, 3, 4];
30- *
31- * edges.push(new Edge(0, 1, -1));
32- * edges.push(new Edge(0, 2, 4));
33- * edges.push(new Edge(1, 2, 3));
34- * edges.push(new Edge(1, 3, 2));
35- * edges.push(new Edge(3, 1, 1));
36- * edges.push(new Edge(4, 3, -3));
37- * edges.push(new Edge(1, 4, 2));
38- * edges.push(new Edge(3, 2, 5));
39- *
40- * // {
41- * // parents: { '0': null, '1': 0, '2': 1, '3': 4, '4': 1 },
42- * // distances: { '0': 0, '1': -1, '2': 2, '3': -2, '4': 1 }
43- * // }
44- * var pathInfo = bellmanFord(vertexes, edges, 0);
4559 */
46- function bellmanFord ( vertexes , edges , source ) {
60+ exports . bellmanFord = function ( vertexes , edges , source ) {
4761 var distances = { } , parents = { } , c ;
4862 for ( var i = 0 ; i < vertexes . length ; i += 1 ) {
4963 distances [ vertexes [ i ] ] = Infinity ;
7084 return { parents : parents , distances : distances } ;
7185 }
7286
73- global . Edge = Edge ;
74- global . bellmanFord = bellmanFord ;
75-
76- } ( ( typeof window === 'undefined' ) ? global : window ) ) ;
87+ } ) ( typeof window === 'undefined' ? module . exports : window ) ;
0 commit comments