Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f90525

Browse files
Removed unnecessary comments + improved variable names + added new comments
1 parent 1782eea commit 0f90525

File tree

1 file changed

+91
-153
lines changed

1 file changed

+91
-153
lines changed

‎src/Algorithms/biDirectionalBfs.js

Lines changed: 91 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -2,168 +2,107 @@ import React from 'react'
22
import bfs from './bfs'
33
import { getAllNodes } from './Dijkstra';
44

5-
export function biDirectionalBfs(grid,start,finish) {
6-
let queueSt = [];
7-
let queueBk = [];
8-
const allnodes = getAllNodes(grid);
9-
// const visitedNodes = [];
10-
11-
queueSt.push(start);
12-
queueBk.push(finish);
13-
start.isVisited=true;
14-
finish.isVisited=true;
15-
let neighbors=[];
16-
let short=[]
17-
let shortBk=[]
18-
let st=[]
19-
let bk = []
5+
export function biDirectionalBfs(grid, start, finish) {
6+
let queueForward = []; /* queue that stores nodes to be visited next in forward dir */
7+
let queueBackward = []; /* queue that stores nodes to be visited next in backword dir */
8+
const allnodes = getAllNodes(grid); /* get all nodes coordinates from gird */
9+
10+
11+
/* push the start node in forward queue & finish node in backward queue */
12+
queueForward.push(start);
13+
queueBackward.push(finish);
14+
15+
/* mark the start node & finish node as visited */
16+
start.isVisited = true;
17+
finish.isVisited = true;
18+
19+
20+
let neighbors = []; // to store neighbours of current node
21+
22+
let curForwardLevelNodes = [];
23+
let curBackwardLevelNodes = [];
24+
25+
let visitedForward = [];
26+
let visitedBackward = [];
27+
2028
let s1;
2129
let s2;
30+
2231
neighbors.push(start);
2332
neighbors.push(finish);
24-
let neighborsUnvis=[];
25-
26-
// short.push(start);
27-
// shortBk.push(finish);
28-
29-
while(queueSt !==[] && queueBk!==[]){
30-
31-
const currentSt = queueSt.shift();
32-
const currentBk = queueBk.shift();
33-
const neighSt = getUnvisitedNeighboursBbfs(currentSt,grid);
34-
const neighBk = getUnvisitedNeighboursBbfs(currentBk,grid);
35-
36-
short=[]
37-
shortBk=[]
38-
39-
// if(neighSt!= null){
40-
updateUnvisitedNeighbors(currentSt,grid);
41-
// }
42-
// if( neighBk!=null){
43-
updateUnvisitedNeighbors(currentBk,grid);
44-
// }
45-
46-
console.log(neighSt);
47-
console.log(neighBk);
48-
49-
50-
51-
for(let i=0;i<neighSt.length || i<neighBk.length;i++){
52-
53-
console.log(neighBk[i],neighSt[i]);
54-
55-
if(i<neighSt.length ){
56-
// for(let j=0;j<neighbors.length;j++){
57-
// if(neighSt[i].row === neighbors[j].row && neighSt[i].col === neighbors[j].col){
58-
let sur = getSurroundings(neighSt[i],grid);
59-
for (let j=0;j<sur.length;j++){
60-
if(bk.includes(sur[j])){
61-
let r2;
62-
console.log("end ",sur[j]);
63-
neighSt[i].isVisited=true
64-
// let val = neighSt[i]
65-
// // while(!grid[val.row][val.col-1].isBbfs || !grid[val.row+1][val.col].isBbfs || !grid[val.row-1][val.col].isBbfs){
66-
// // val= val.previousNode
67-
// // }
68-
// if(start.row===val.row){
69-
// while(!grid[val.row][val.col-1].isBbfs){
70-
// val = val.previousNode
71-
// }
72-
// r2 = grid[val.row][val.col-1]
73-
// }
74-
// else if(start.col>val.col){
75-
// while(!grid[val.row+1][val.col].isBbfs){
76-
// val = val.previousNode
77-
// }
78-
// r2 = grid[val.row+1][val.col]
79-
// }
80-
// else if(start.col<val.col){
81-
// while(!grid[val.row-1][val.col].isBbfs){
82-
// val = val.previousNode
83-
// }
84-
// r2 = grid[val.row-1][val.col]
85-
// }
86-
// console.log(neighSt[i].row);
87-
88-
return [neighbors,neighSt[i],sur[j]];
89-
}
33+
34+
let neighborsUnvis = [];
35+
36+
while (queueForward !== [] && queueBackward !== []) {
37+
38+
const currentForwardNode = queueForward.shift(); /* shift() -> pops & returns front node in queue*/
39+
const currentBackwardNode = queueBackward.shift();
40+
41+
const neighsForward = getUnvisitedNeighboursBbfs(currentForwardNode, grid);
42+
const neighsBackward = getUnvisitedNeighboursBbfs(currentBackwardNode, grid);
43+
44+
curForwardLevelNodes = [];
45+
curBackwardLevelNodes = [];
46+
47+
updateUnvisitedNeighbors(currentForwardNode, grid);
48+
updateUnvisitedNeighbors(currentBackwardNode, grid);
49+
50+
for (let i = 0; i < neighsForward.length || i < neighsBackward.length; i++) {
51+
52+
// visits all the currentForwardNodes neighbours, if any
53+
if (i < neighsForward.length) {
54+
55+
let sur = getSurroundings(neighsForward[i], grid);
56+
for (let j = 0; j < sur.length; j++) {
57+
58+
// if forward nodes meet backward nodes, the we found a path & return it
59+
if (visitedBackward.includes(sur[j])) {
60+
neighsForward[i].isVisited = true;
61+
return [neighbors, neighsForward[i], sur[j]];
9062
}
91-
92-
93-
console.log(neighSt[i])
94-
neighSt[i].isVisited = true;
95-
neighSt[i].isBbfs = true;
96-
queueSt.push(neighSt[i]);
97-
st.push(neighSt[i]);
98-
short.push(neighSt[i]);
9963
}
10064

101-
if(i<neighBk.length){
102-
// for(let j=0;j<neighbors.length;j++){
103-
// if(neighSt[i].row === neighbors[j].row && neighSt[i].col === neighbors[j].col){
104-
let sur = getSurroundings(neighBk[i],grid);
105-
for (let j=0;j<sur.length;j++){
106-
if(st.includes(sur[j])){
107-
let r2;
108-
console.log("end--",sur[j]);
109-
neighBk[i].isVisited=true
110-
// let val = neighBk[i]
111-
// while(!grid[val.row][val.col-1].isBbfs || !grid[val.row+1][val.col].isBbfs || !grid[val.row-1][val.col].isBbfs){
112-
// val= val.previousNode
113-
// }
114-
// if(start.row===val.row){
115-
// while(!grid[val.row][val.col-1].isBbfs){
116-
// val = val.previousNode
117-
// }
118-
// r2 = grid[val.row][val.col-1]
119-
// }
120-
// else if(start.col>val.col){
121-
// while(!grid[val.row+1][val.col].isBbfs){
122-
// val = val.previousNode
123-
// }
124-
// r2 = grid[val.row+1][val.col]
125-
// }
126-
// else if(start.col<val.col){
127-
// while(!grid[val.row-1][val.col].isBbfs){
128-
// val = val.previousNode
129-
// }
130-
// r2 = grid[val.row-1][val.col]
131-
// }
132-
// console.log(neighBk[i]);
133-
return [neighbors,neighBk[i],sur[j]];
134-
}
65+
neighsForward[i].isVisited = true;
66+
neighsForward[i].isBbfs = true;
67+
68+
queueForward.push(neighsForward[i]);
69+
70+
visitedForward.push(neighsForward[i]);
71+
curForwardLevelNodes.push(neighsForward[i]);
13572
}
136-
137-
console.log(neighBk[i])
138-
neighBk[i].isVisited = true;
139-
neighBk[i].isBbfs = true;
140-
queueBk.push(neighBk[i]);
141-
bk.push(neighBk[i])
142-
shortBk.push(neighBk[i]);
143-
144-
145-
73+
74+
// visits all the currentBackwardNodes neighbours, if any
75+
if (i < neighsBackward.length) {
76+
let sur = getSurroundings(neighsBackward[i], grid);
77+
for (let j = 0; j < sur.length; j++) {
78+
79+
// if forward nodes meet backward nodes, the we found a path & return it
80+
if (visitedForward.includes(sur[j])) {
81+
neighsBackward[i].isVisited = true
82+
return [neighbors, neighsBackward[i], sur[j]];
83+
}
14684
}
14785

148-
// console.log(neighSt[i]);
149-
// if(neighBk[i] in neighbors){
150-
// return [neighbors,neighBk[i]]
151-
// }
86+
neighsBackward[i].isVisited = true;
87+
neighsBackward[i].isBbfs = true;
88+
89+
queueBackward.push(neighsBackward[i]);
90+
91+
visitedBackward.push(neighsBackward[i])
92+
curBackwardLevelNodes.push(neighsBackward[i]);
93+
94+
}
15295
}
153-
neighbors = [...neighbors,...short,...shortBk]
96+
neighbors = [...neighbors,...curForwardLevelNodes,...curBackwardLevelNodes]
15497
}
15598
}
15699

157100
function updateUnvisitedNeighbors(node, grid) {
158101
/** Updates the distances of unvisited nodes */
159102
const unvisitedNeighbors = getUnvisitedNeighboursBbfs(node, grid);
160-
console.log("unvis")
161-
console.log(unvisitedNeighbors);
162-
// if(unvisitedNeighbors !== undefined){
103+
163104
for (const neighbor of unvisitedNeighbors) {
164-
165-
neighbor.previousNode = node;
166-
105+
neighbor.previousNode = node;
167106
}
168107

169108
}
@@ -178,24 +117,23 @@ function getUnvisitedNeighboursBbfs(node, grid) {
178117
if (col > 0) neighbors.push(grid[row][col - 1]);
179118
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
180119
return neighbors.filter(neighbor => !neighbor.isVisited);
181-
182-
}
120+
121+
}
183122

184123

185124

186125
export function getNodesInShortestPathOrderBbfs(edNode) {
187126
/** Backtracking alogirthm to get the path from Finish node to Start node (or vice versa) */
188-
const nodesInShortestPathOrder = [];
127+
const nodesInShortestPathOrderBbfs = [];
189128
let currentNode = edNode;
190129
while (currentNode != null) {
191-
nodesInShortestPathOrder.unshift(currentNode);
192-
currentNode = currentNode.previousNode;
193-
console.log(currentNode)
130+
nodesInShortestPathOrderBbfs.unshift(currentNode);
131+
currentNode = currentNode.previousNode;
194132
}
195-
return nodesInShortestPathOrder;
133+
return nodesInShortestPathOrderBbfs;
196134
}
197135

198-
function getSurroundings(node,grid){
136+
function getSurroundings(node,grid){
199137
const neighbors = [];
200138
const col = node.col;
201139
const row = node.row;
@@ -205,4 +143,4 @@ function getSurroundings(node,grid){
205143
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
206144

207145
return neighbors;
208-
}
146+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /