
Graphs: breadth-first search - Beau teaches JavaScript
video description
Date: 2022-03-14
Related videos
Comments and reviews: 7
Of
Thanks for explanation. The nested while and for loops, indexOfs, and neighborIdx array however add confusion. indexOf is O(n) so not sure why you wouldn't just use a loop over curConnected to locate 1's and enqueue/push the corresponding indexes and set the distances.
for (var j = 0; j < curConnected.length; j++) -
if (curConnected[j] === 1 && nodesLen[j] === Infinity) -
queue.push(j);
nodesLen[j] = nodesLen[current] + 1;
-
-
reply
Thanks for explanation. The nested while and for loops, indexOfs, and neighborIdx array however add confusion. indexOf is O(n) so not sure why you wouldn't just use a loop over curConnected to locate 1's and enqueue/push the corresponding indexes and set the distances.
for (var j = 0; j < curConnected.length; j++) -
if (curConnected[j] === 1 && nodesLen[j] === Infinity) -
queue.push(j);
nodesLen[j] = nodesLen[current] + 1;
-
-
reply
dnavas77
For Loop within the while loop explained:
For every neighbor -j- of node 1:
1. if the distance from 1 to neighbor -j- is infinity, assign distance to -distance to current node + 1-
(a) For example if current is node 2, its distance is 1 because it was assigned in the previous loop, thus the distance to 0 will be 1 + 1.
2. push neighbor -j- to queue.
reply
For Loop within the while loop explained:
For every neighbor -j- of node 1:
1. if the distance from 1 to neighbor -j- is infinity, assign distance to -distance to current node + 1-
(a) For example if current is node 2, its distance is 1 because it was assigned in the previous loop, thus the distance to 0 will be 1 + 1.
2. push neighbor -j- to queue.
reply
Divyanshu
you are not maintaining a visited array, won't it run into an infinite loop? and keep pushing and popping the same node again and again? in this case: 1 -> 2 -> 1 -> 2....
reply
you are not maintaining a visited array, won't it run into an infinite loop? and keep pushing and popping the same node again and again? in this case: 1 -> 2 -> 1 -> 2....
reply
Tiki
It's a bit confusing when you go into the for loops and neighbors...it would help if you had some graphical representation to refer to which cells you are referring to...
reply
It's a bit confusing when you go into the for loops and neighbors...it would help if you had some graphical representation to refer to which cells you are referring to...
reply
Deepali
Best code and best explanation I have found so far on breadth first search using Javascript. Thank You for making this excellent video.
reply
Best code and best explanation I have found so far on breadth first search using Javascript. Thank You for making this excellent video.
reply
Mohsin
WOrst Explanation Ever just Read code lines nothing explain.
please dont make videos if you dont know how to explain dont just read.
reply
WOrst Explanation Ever just Read code lines nothing explain.
please dont make videos if you dont know how to explain dont just read.
reply
Jbdoster
I searched -depth first algorithm javascript- into YouTube and took a huge sigh of relief seeing -Beau teaches JavaScript-
reply
I searched -depth first algorithm javascript- into YouTube and took a huge sigh of relief seeing -Beau teaches JavaScript-
reply
Add a review, comment
Other channel videos















