Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Edwin Torres
JavaScript syntax or Nodejs syntax

Edwin Torres

May 29, 2024

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.shift(); process.stdout.write(currentVertex + " "); // Explore adjacent vertices for (const neighbor of this.adjList[currentVertex]) { if (!visited[neighbor]) { visited[neighbor] = true; queue.push(neighbor); } } } } }

1

0

Comments
Comments

On this page

Step-by-Step Algorithm for BFS

Algorithm Walkthrough

Implementation of Breadth First Search

Complexity analysis

Final Words