Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const fs = require('fs');
- process.stdin.resume();
- process.stdin.setEncoding('utf-8');
- let inputString = '';
- let currentLine = 0;
- process.stdin.on('data', function(inputStdin) {
- inputString += inputStdin;
- });
- process.stdin.on('end', function() {
- inputString = inputString.split('\n');
- main();
- });
- function readLine() {
- return inputString[currentLine++];
- }
- /*
- * Complete the 'bfs' function below.
- *
- * The function is expected to return a STRING.
- * The function accepts following parameters:
- * 1. Vertex origin
- */
- /*
- * For your reference:
- *
- * Vertex {
- * char id;
- * edges[Vertex];
- * }
- *
- */
- function bfs(origin) {
- // Write your code here
- if (!origin) return ""
- let queue = []
- queue.push(origin)
- let output = []
- while (queue.length) {
- // process queue
- // add nodes to queue if not seen
- let current = queue.shift()
- if(output.includes(current.id)) continue
- output.push(current.id)
- let otherNodes = current.edges
- otherNodes.forEach(node => {
- queue.push(node)
- })
- }
- return output.join("")
- }
- function main() {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement