Skip to content

Commit 253d148

Browse files
author
minhaz.raufoon
committed
2 parents ef467b3 + 61b0e20 commit 253d148

File tree

3 files changed

+74
-45
lines changed

3 files changed

+74
-45
lines changed
Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,89 @@
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 = {},
418
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+
}
2545
}
2646
}
2747

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];
3256
}
3357
}
3458

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

38-
while (parentNodeOf[currentNode]) {
39-
currentNode = parentNodeOf[currentNode];
40-
shortestPath.unshift(currentNode);
41-
}
64+
if (!isProcessed[dstNode]) return [];
4265

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();
4672
}
4773
}
4874

4975

50-
/* TESTING */
76+
/************ Testing BFS ***************/
5177
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],
5481
3: [1, 2, 4],
55-
4: [2, 3, 5],
56-
5: [2, 4],
82+
4: [1, 3],
5783
}
58-
var srcNode = 1, destNode = 5;
84+
var srcNode = 1, dstNode = 4;
85+
86+
var bfs = new BFS(graph, srcNode);
5987

60-
console.log(BFSFindShortestPath(graph, srcNode, destNode));
88+
console.log(bfs.getShortestDistance(dstNode));
89+
console.log(bfs.getShortestPath(dstNode).join(' -> '));

Graph Theory/Graph Representation/adj-matrix-graph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function Graph(V) {
138138
}
139139

140140

141-
/************ Graph Representation ***************/
141+
/************ Testing Graph Representation ***************/
142142

143143
//Initializing a new Graph Object by giving it's size as a parameter
144144
var graph = new Graph(5);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
- [Radix Sort](./Sorting/Radix%20Sort/)
4040

4141

42-
- Graph Algorithms
42+
- [Graph Algorithms](./Graph%20Algorithms)
4343
- [Graph Representation](./Graph%20Theory/Graph%20Representation)
4444
- [Breadth First Search (BFS)](./Graph%20Theory/Breadth%20First%20Search)
4545
- [Depth First Search (DFS)](./Graph%20Theory/Depth%20First%20Search)

0 commit comments

Comments
 (0)