Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 find_all_paths(origin, destination) {
- if(!origin) return []
- // Write your code here
- let output = []
- const helper = (currentNode, currentPath) => {
- if(currentPath.includes(currentNode.id)) return
- if(currentNode.id === destination){
- currentPath.push(currentNode.id)
- output.push(currentPath.join(""))
- return
- }
- currentPath.push(currentNode.id)
- let otherNodes = currentNode.edges
- otherNodes.forEach(node => {
- helper(node, currentPath)
- })
- }
- helper(origin, [])
- return output
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement