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