Skip to content

Commit 76dcfaf

Browse files
committed
Refactored the BFS code
1 parent ef61525 commit 76dcfaf

File tree

1 file changed

+69
-38
lines changed

1 file changed

+69
-38
lines changed
Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,90 @@
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 = [],
419
currentNode,
5-
childNode,
6-
parentNodeOf = Object.create(null),
7-
shortestPath;
20+
childNode;
21+
822

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;
1230

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;
1634

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;
2036

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+
}
2446
}
2547
}
2648

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];
3157
}
3258
}
3359

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;
3664

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+
}
4171

42-
return {
43-
distance: shortestPath.length - 1,
44-
shortestPath: shortestPath
72+
return shortestPath;
4573
}
4674
}
4775

4876

49-
/* TESTING */
77+
/************ Testing BFS ***************/
5078
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],
5382
3: [1, 2, 4],
54-
4: [2, 3, 5],
55-
5: [2, 4],
83+
4: [1, 3],
5684
}
57-
var srcNode = 1, destNode = 5;
85+
var srcNode = 1, dstNode = 4;
86+
87+
var bfs = new BFS(graph, srcNode);
5888

59-
console.log(BFSFindShortestPath(graph, srcNode, destNode));
89+
console.log(bfs.getShortestDistance(dstNode));
90+
console.log(bfs.getShortestPath(dstNode));

0 commit comments

Comments
 (0)