Skip to content

Instantly share code, notes, and snippets.

@jmsevold
Last active November 28, 2016 19:52
Show Gist options
  • Save jmsevold/d462592b5184b8fc08cb to your computer and use it in GitHub Desktop.
Save jmsevold/d462592b5184b8fc08cb to your computer and use it in GitHub Desktop.
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