0

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')
Mustafa
  • 340
  • 1
  • 3
  • 9
  • 1
    First things first, have you checked to make sure `this.adjacencyList` is an array, and that is has at least one element? An empty array for instance will return `undefined` for `this.adjacencyList[0]`. – Muirik Jul 12 '20 at 21:03
  • Its definitely an array, I checked it, and also the function would not work otherwise. – Mustafa Jul 12 '20 at 21:15
  • 1
    Okay, and does it also contain at least one element? If not, it will return `undefined`. – Muirik Jul 12 '20 at 21:17
  • 1
    What's the definition of `Graph` and `Graph.DPS`? The question includes `DPS(vertex)` and `DPS(graph,vertex)` but does not show how `DPS` is associated with `Graph`'s prototype ... – Mzn Jul 12 '20 at 21:21
  • `this.adjacencyList[vertex]` contains a list of length 2 for all the verticies. I console.logged them to check. – Mustafa Jul 12 '20 at 21:24
  • the method does not need the extra graph parameter since its being called by the graph. For example `g.DPS('NYC') vs DPS(g,'NYC') ` The method uses `this.` to find the attribute `adjacencyList` – Mustafa Jul 12 '20 at 21:26
  • 1
    NOTE: Uncaught TypeError: Cannot read property of undefined. JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function. – Muirik Jul 12 '20 at 21:38
  • I think I found the problem. this.adjacencyList[vertex] is searching inside the function for the adjacencyList and not finding it. – Mustafa Jul 12 '20 at 21:41
  • Yup. I did `let parent = this` before the function, then used `parent.adjacencyList` and it worked. – Mustafa Jul 12 '20 at 21:44
  • Does this answer your question? [How does the "this" keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – Thomas Jul 12 '20 at 21:59

1 Answers1

0

Looks like the problem was a scope issue. The helper function could not access this.adjacencyList. To fix it you can do let parent = this before the helper function, then use parent.adjacencyList .

Mustafa
  • 340
  • 1
  • 3
  • 9