The function and the method DPS do the same thing, they take a vertex (and the graph for the function DPS) and do a depth first search and return all the nodes in the graph. Whenever I call the method tree.DPS() I get the error
VM1267 graphs:37 Uncaught TypeError: Cannot read property 'adjacencyList' of undefined
This is line 37
let l = this.adjacencyList[0];
But when I run the function DPS() it works fine. Can any one see why? adjacencyList is a attribute of the graph that contains vertices as keys , and the value as the vertex's corresponding edges as a list.
DPS(vertex) {
visited = []
helper(vertex)
function helper(vertex) {
visited.push(vertex)
let l = this.adjacencyList[0];
console.log(l)
return l
for (let i =0; i < l.length; i++ ) {
if (!visited.includes(l[i])) {
helper(l[i])
}
}
}
return visited
}
}
g = new Graph
g.addVertex('Tokyo')
g.addVertex('NYC')
g.addVertex('San Francisco')
g.addEdge('NYC','Tokyo')
g.addEdge('San Francisco','Tokyo')
g.addEdge('NYC','San Francisco')
function DPS(graph,vertex) {
visited = []
helper(vertex)
function helper(vertex) {
visited.push(vertex)
let l = graph.adjacencyList[vertex];
for (let i =0; i < l.length; i++ ) {
if (!visited.includes(l[i])) {
helper(l[i])
}
}
}
return visited
}
//DPS(g,'NYC') This works
g.DPS('NYC')