VehiclesFashionRecipesBlogsHuntTravelsSportFunHandmadeITEducation
Mini-Games
x

x
zakruti.com » IT - Software » freeCodeCamp.org
Graphs: breadth-first search - Beau teaches JavaScript

Graphs: breadth-first search - Beau teaches JavaScript

FBTwitterReddit

video description

Rating: 4.0; Vote: 1
Traversal algorithms are algorithms to traverse or visit nodes in a graph. In this video, I will be showing how to implement breadth-first search traversal algorithm in JavaScript. The algorithm starts at one node, first visits all its neighbors that are one edge away, then goes on to visiting each of their neighbors. The point is to determine how close nodes are to a root node. - Code: https://codepen.io/beaucarnes/pen/XgrXvw?editors=0012 - Beau Carnes on
Date: 2022-03-14

Comments and reviews: 7


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

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

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

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

Best code and best explanation I have found so far on breadth first search using Javascript. Thank You for making this excellent video.
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

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