Last active
November 28, 2016 19:52
-
-
Save jmsevold/d462592b5184b8fc08cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nodeNotVisited = (list,item) => { | |
if (list.indexOf(item) === -1) { | |
return true; | |
} | |
return false; | |
}; | |
A | |
/ \ | |
F B | |
/ / \ | |
G D C | |
/ | \ | | |
J I H E | |
var graph = { | |
"A": ["F","B"], | |
"B": ["D","C"], | |
"C": [], | |
"D": ["E"], | |
"E": [], | |
"F": ["G"], | |
"G": ["J","I","H"], | |
"H": [], | |
"I": [], | |
"J": [] | |
}; | |
var dfs = (graph,node) => { | |
let visited = []; | |
let stack = []; | |
stack.push(node); | |
while (stack.length > 0) { | |
let node = stack.pop(); | |
let neighbors = graph[node]; | |
visited.push(node); | |
if(neighbors.length > 0){ | |
neighbors.forEach((neighbor) => { | |
stack.push(neighbor); | |
}); | |
} | |
} | |
return visited; | |
}; | |
dfs(graph,"A"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment