Grokking Graph Algorithms for Coding Interviews
0% completed
Graph Traversal - Breadth First Search (BFS)
A
Anonymous
· 2 years ago
For Javascript it looks like you're calling a process.stdout.write
However, it looks like this is an old syntax used by Nodejs or low level, You could replace this line with a simple console.log
class Graph { constructor(vertices) { this.V = vertices; // Number of vertices this.adjList = new Array(vertices).fill(null).map(() => []); } addEdge(u, v) { this.adjList[u].push(v); this.adjList[v].push(u); // For undirected graph } BFS(startVertex) { const visited = new Array(this.V).fill(false); // To keep track of visited vertices const queue = []; visited[startVertex] = true; queue.push(startVertex); while (queue.length !== 0) { const currentVertex = queue.shif
A
Anonymous
· 3 years ago
In step 6 of the example, you shouldn't enqueue vertex F since it was enqueued in step 4 and vertex F was already marked as visited.
Show 1 reply
Reading Progress
0%