@@ -10,79 +10,85 @@ var graph = [[1,0,1,0,0,0],
1010 [0,0,0,0,1,1]];
1111* * * * * * * * * * * * * * * * * */
1212
13- 'use strict' ;
13+ ( function ( exports ) {
1414
15- /**
16- * Breadth-first search algorithm for matrix representation of graph.
17- * The algorithm finds whether there's a path between two given nodes.
18- */
19- var breadthFirstSearch = ( function ( ) {
20-
21- var visited = [ ] ,
22- queue = [ ] ,
23- target ,
24- graph ;
15+ 'use strict' ;
2516
2617 /**
27- * Initializes the algorithm
28- *
29- * @private
30- * @param {array } inputGraph The input matrix of the graph
31- * @param {number } destination The destination
18+ * Breadth-first search algorithm for matrix representation of graph.
19+ * The algorithm finds whether there's a path between two given nodes.
3220 */
33- function init ( inputGraph , destination ) {
34- graph = inputGraph ;
35- target = destination ;
36- visited = [ ] ;
37- queue = [ ] ;
38- for ( var i = 0 ; i < graph . length ; i += 1 ) {
39- visited [ i ] = false ;
40- }
41- }
21+ var breadthFirstSearch = ( function ( ) {
4222
43- /**
44- * Process given node
45- *
46- * @param {number } destination The destionation, which should be reached
47- * @param {number } current The current node
48- * @param {number } node Neighbour node
49- */
50- function processNode ( destination , current , node ) {
51- if ( graph [ current ] [ node ] ) {
52- if ( node === destination ) {
53- return true ;
54- }
55- if ( ! visited [ node ] ) {
56- queue . push ( node ) ;
23+ var visited = [ ] ,
24+ queue = [ ] ,
25+ target ,
26+ graph ;
27+
28+ /**
29+ * Initializes the algorithm
30+ *
31+ * @private
32+ * @param {array } inputGraph The input matrix of the graph
33+ * @param {number } destination The destination
34+ */
35+ function init ( inputGraph , destination ) {
36+ graph = inputGraph ;
37+ target = destination ;
38+ visited = [ ] ;
39+ queue = [ ] ;
40+ for ( var i = 0 ; i < graph . length ; i += 1 ) {
41+ visited [ i ] = false ;
5742 }
5843 }
59- }
6044
61- /**
62- * Finds whether there's a path between a given start node
63- * to given destination
64- *
65- * @public
66- * @param {array } graph A matrix representation of the graph
67- * @param {number } source The source node
68- * @param {number } destination The destination node
69- * @returns {boolean } true/false depending whether there's
70- * a path between the nodes
71- */
72- return function ( graph , source , destination ) {
73- init ( graph , destination ) ;
74- var current ;
75- queue . push ( source ) ;
76- while ( queue . length > 0 ) {
77- current = queue . shift ( ) ;
78- visited [ current ] = true ;
79- for ( var i = 0 ; i < graph . length ; i += 1 ) {
80- var result = processNode ( destination , current , i ) ;
81- if ( result ) {
45+ /**
46+ * Process given node
47+ *
48+ * @param {number } destination The destionation, which should be reached
49+ * @param {number } current The current node
50+ * @param {number } node Neighbour node
51+ */
52+ function processNode ( destination , current , node ) {
53+ if ( graph [ current ] [ node ] ) {
54+ if ( node === destination ) {
8255 return true ;
8356 }
57+ if ( ! visited [ node ] ) {
58+ queue . push ( node ) ;
59+ }
8460 }
8561 }
86- return false ;
87- } ;
88- } ( ) ) ;
62+
63+ /**
64+ * Finds whether there's a path between a given start node
65+ * to given destination
66+ *
67+ * @public
68+ * @param {array } graph A matrix representation of the graph
69+ * @param {number } source The source node
70+ * @param {number } destination The destination node
71+ * @returns {boolean } true/false depending whether there's
72+ * a path between the nodes
73+ */
74+ return function ( graph , source , destination ) {
75+ init ( graph , destination ) ;
76+ var current ;
77+ queue . push ( source ) ;
78+ while ( queue . length > 0 ) {
79+ current = queue . shift ( ) ;
80+ visited [ current ] = true ;
81+ for ( var i = 0 ; i < graph . length ; i += 1 ) {
82+ var result = processNode ( destination , current , i ) ;
83+ if ( result ) {
84+ return true ;
85+ }
86+ }
87+ }
88+ return false ;
89+ } ;
90+ } ( ) ) ;
91+
92+ exports . breadthFirstSearch = breadthFirstSearch ;
93+
94+ } ( typeof exports === 'undefined' ? window : exports ) ) ;
0 commit comments