@@ -10,59 +10,69 @@ var graph = [[1,0,1,0,0,0],
10
10
[0,0,0,0,1,1]];
11
11
* * * * * * * * * * * * * * * * * */
12
12
13
+ 'use strict' ;
14
+
13
15
/**
14
16
* Breadth-first search algorithm for matrix representation of graph.
15
17
* The algorithm finds whether there's a path between two given nodes.
16
18
*/
17
- var breadthFirstSearch = function ( ) {
19
+ var breadthFirstSearch = ( function ( ) {
20
+
21
+ var visited = [ ] ,
22
+ queue = [ ] ,
23
+ target ,
24
+ graph ;
18
25
19
- var visted = [ ] ,
20
- queue = [ ] ,
21
- target ,
22
- graph ;
26
+ /**
27
+ * Initializes the algorithm
28
+ *
29
+ * @private
30
+ * @param {array } inputGraph The input matrix of the graph
31
+ * @param {number } destination The destination
32
+ */
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
+ }
23
42
24
- /**
25
- * Initializes the algorithm
26
- *
27
- * @private
28
- * @param {array } inputGraph The input matrix of the graph
29
- * @param {number } destination The destination
30
- */
31
- function init ( inputGraph , destination ) {
32
- graph = inputGraph ;
33
- target = destination ;
34
- visited = [ ] ;
35
- queue = [ ] ;
36
- for ( var i = 0 ; i < graph . length ; i += 1 )
37
- visited [ i ] = false ;
43
+ function processNode ( destination , current , node ) {
44
+ if ( graph [ current ] [ node ] ) {
45
+ if ( node === destination ) {
46
+ return true ;
47
+ }
48
+ if ( ! visited [ node ] ) {
49
+ queue . push ( node ) ;
50
+ }
38
51
}
52
+ }
39
53
40
- /**
41
- * Finds whether there's a path between a given start node
42
- * to given destination
43
- *
44
- * @public
45
- * @param {array } graph A matrix representation of the graph
46
- * @param {number } source The source node
47
- * @param {number } destination The destination node
48
- * @returns {boolean } true/false depending whether there's a path between the nodes
49
- */
50
- return function ( graph , source , destination ) {
51
- init ( graph , destination ) ;
52
- var current ;
53
- queue . push ( source ) ;
54
- while ( queue . length > 0 ) {
55
- current = queue . shift ( ) ;
56
- visited [ current ] = true ;
57
- for ( var i = 0 ; i < graph . length ; i += 1 ) {
58
- if ( graph [ current ] [ i ] ) {
59
- if ( i === destination )
60
- return true ;
61
- if ( ! visited [ i ] )
62
- queue . push ( i ) ;
63
- }
64
- }
65
- }
66
- return false ;
67
- } ;
68
- } ( ) ;
54
+ /**
55
+ * Finds whether there's a path between a given start node
56
+ * to given destination
57
+ *
58
+ * @public
59
+ * @param {array } graph A matrix representation of the graph
60
+ * @param {number } source The source node
61
+ * @param {number } destination The destination node
62
+ * @returns {boolean } true/false depending whether there's
63
+ * a path between the nodes
64
+ */
65
+ return function ( graph , source , destination ) {
66
+ init ( graph , destination ) ;
67
+ var current ;
68
+ queue . push ( source ) ;
69
+ while ( queue . length > 0 ) {
70
+ current = queue . shift ( ) ;
71
+ visited [ current ] = true ;
72
+ for ( var i = 0 ; i < graph . length ; i += 1 ) {
73
+ processNode ( destination , current , i ) ;
74
+ }
75
+ }
76
+ return false ;
77
+ } ;
78
+ } ( ) ) ;
0 commit comments