August 2024
Intermediate to advanced
516 pages
11h 47m
English
I demonstrated how a hash table can be used to implement a graph, but going forward, we’ll work with an object-oriented approach.
Here’s the beginning of an object-oriented graph implementation, using JavaScript:
| | class Vertex { |
| | constructor(value) { |
| | this.value = value; |
| | this.adjacentVertices = []; |
| | } |
| | |
| | addAdjacentVertex(vertex) { |
| | this.adjacentVertices.push(vertex); |
| | } |
| | } |
| | |
| | export default Vertex; |
The Vertex class has two primary attributes, the value and an array of adjacentVertices. In our social network example, each vertex represents a person, and the value might be a string containing the person’s name. With a more complex application, we’d probably want to store multiple pieces ...
Read now
Unlock full access