1
- function BFSFindShortestPath ( graph , srcNode , dstNode ) {
2
- var isPushed = Object . create ( null ) ,
3
- bfsQueue ,
1
+ /*
2
+ BFS(Breadth First Search) implementation in JavaScript
3
+ ------------------------------------------------------
4
+ */
5
+
6
+ /**
7
+ * Processing BFS for the given srcNode in the Graph
8
+ * @param {Array } graph (will be an adjacency list)
9
+ * @param {Number or String } srcNode
10
+ * @param {Number or String } dstNode (Optional)
11
+ * If the 'dstNode' is not given, then it will process shortest path for all of the nodes from 'srcNode'
12
+ */
13
+ function BFS ( graph , srcNode , dstNode ) {
14
+ var isProcessed = { } ,
15
+ bfsQueue = [ ] ,
16
+ parentNodeOf = { } ,
17
+ distance = { } ,
4
18
currentNode ,
5
- childNode ,
6
- parentNodeOf = Object . create ( null ) ,
7
- shortestPath ;
8
-
9
- bfsQueue = [ srcNode ] ;
10
- parentNodeOf [ srcNode ] = null ;
11
- isPushed [ srcNode ] = true ;
12
-
13
- while ( bfsQueue . length > 0 ) {
14
- currentNode = bfsQueue . shift ( ) ;
15
- if ( currentNode === dstNode ) break ;
16
-
17
- if ( ! graph [ currentNode ] ) continue ;
18
- for ( var i = 0 ; i < graph [ currentNode ] . length ; i ++ ) {
19
- childNode = graph [ currentNode ] [ i ] ;
20
- if ( isPushed [ childNode ] ) continue ;
21
-
22
- parentNodeOf [ childNode ] = currentNode ;
23
- bfsQueue . push ( childNode ) ;
24
- isPushed [ childNode ] = true ;
19
+ childNode ;
20
+
21
+
22
+ //Processing the distance and path from the given graph
23
+ this . init = function ( ) {
24
+ //Initializing for the 'srcNode'
25
+ bfsQueue . push ( srcNode ) ;
26
+ parentNodeOf [ srcNode ] = null ;
27
+ isProcessed [ srcNode ] = true ;
28
+ distance [ srcNode ] = 0 ;
29
+
30
+ while ( bfsQueue . length > 0 ) {
31
+ currentNode = bfsQueue . shift ( ) ;
32
+ if ( currentNode === dstNode ) break ;
33
+
34
+ var listLength = graph [ currentNode ] . length ;
35
+
36
+ for ( var i = 0 ; i < listLength ; i ++ ) {
37
+ childNode = graph [ currentNode ] [ i ] ;
38
+ if ( isProcessed [ childNode ] ) continue ; //already entered in the queue, so don't need to add again
39
+
40
+ parentNodeOf [ childNode ] = currentNode ;
41
+ distance [ childNode ] = distance [ currentNode ] + 1 ;
42
+ bfsQueue . push ( childNode ) ;
43
+ isProcessed [ childNode ] = true ;
44
+ }
25
45
}
26
46
}
27
47
28
- if ( ! isPushed [ dstNode ] ) {
29
- return {
30
- distance : Infinity ,
31
- shortestPath : [ ]
48
+ this . init ( ) ;
49
+
50
+ //Get the shortest distance from the processed 'distance' Array
51
+ this . getShortestDistance = function ( dstNode ) {
52
+ if ( ! isProcessed [ dstNode ] ) {
53
+ return Infinity ;
54
+ } else {
55
+ return distance [ dstNode ] ;
32
56
}
33
57
}
34
58
35
- shortestPath = [ dstNode ] ;
36
- currentNode = dstNode ;
59
+ //Get the Shortest Path from the breadcrumb of the 'parentNodeOf'
60
+ this . getShortestPath = function ( dstNode ) {
61
+ var shortestPath = [ dstNode ] ;
62
+ var currentNode = dstNode ;
37
63
38
- while ( parentNodeOf [ currentNode ] ) {
39
- currentNode = parentNodeOf [ currentNode ] ;
40
- shortestPath . unshift ( currentNode ) ;
41
- }
64
+ if ( ! isProcessed [ dstNode ] ) return [ ] ;
42
65
43
- return {
44
- distance : shortestPath . length - 1 ,
45
- shortestPath : shortestPath
66
+ while ( parentNodeOf [ currentNode ] ) {
67
+ currentNode = parentNodeOf [ currentNode ] ;
68
+ shortestPath . push ( currentNode ) ;
69
+ }
70
+
71
+ return shortestPath . reverse ( ) ;
46
72
}
47
73
}
48
74
49
75
50
- /* TESTING */
76
+ /************ Testing BFS ************** */
51
77
var graph = {
52
- 1 : [ 2 , 3 ] ,
53
- 2 : [ 1 , 3 , 4 , 5 ] ,
78
+ 0 : [ 1 , 2 ] ,
79
+ 1 : [ 0 , 2 , 3 , 4 ] ,
80
+ 2 : [ 0 , 1 , 3 ] ,
54
81
3 : [ 1 , 2 , 4 ] ,
55
- 4 : [ 2 , 3 , 5 ] ,
56
- 5 : [ 2 , 4 ] ,
82
+ 4 : [ 1 , 3 ] ,
57
83
}
58
- var srcNode = 1 , destNode = 5 ;
84
+ var srcNode = 1 , dstNode = 4 ;
85
+
86
+ var bfs = new BFS ( graph , srcNode ) ;
59
87
60
- console . log ( BFSFindShortestPath ( graph , srcNode , destNode ) ) ;
88
+ console . log ( bfs . getShortestDistance ( dstNode ) ) ;
89
+ console . log ( bfs . getShortestPath ( dstNode ) . join ( ' -> ' ) ) ;
0 commit comments