amejiarosario/dsa.js-data-structures-algorithms-javascript

Graph nodes store adjacents in HashSet object which does not exist

plasmak opened this issue · 3 comments

Hi
Great content.
I am experimenting with it.
In : dsa.js-data-structures-algorithms-javascript/src/data-structures/graphs/node.js
the constructor uses HashSet where it should be HashMapSet if I am not mistaken.

Hi Aït-Kaci!

Glad to hear you like the content and thanks for taking the time to report a possible error.

Could you please paste the code here where you find the error?

or even better could you please create a pull request and write a test case either in graph.spec.js or node.spec.js that captures the bug?

Hi,
unfortunately, I really don't have the time so here's the paste

const HashSet = require('../sets/hash-set');

// tag::constructor[]
/**
 * Graph node/vertex that hold adjacencies nodes
 * For performance, uses a HashSet instead of array for adjacents.
 */
class Node {
  constructor(value) {
    this.value = value;
    this.adjacents = new HashSet(); // adjacency list
  }
  // end::constructor[]

  // tag::addAdjacent[]
  /**
     * Add node to adjacency list
     * Runtime: O(1)
     * @param {Node} node
     */
  addAdjacent(node) {
    this.adjacents.add(node);
  }
  // end::addAdjacent[]

  // tag::removeAdjacent[]
  /**
     * Remove node from adjacency list
     * Runtime: O(1)
     * @param {Node} node
     * @returns removed node or `false` if node was not found
     */
  removeAdjacent(node) {
    return this.adjacents.delete(node);
  }
  // end::removeAdjacent[]

  // tag::isAdjacent[]
  /**
     * Check if a Node is adjacent to other
     * Runtime: O(1)
     * @param {Node} node
     */
  isAdjacent(node) {
    return this.adjacents.has(node);
  }
  // end::isAdjacent[]

  /**
     * Get all adjacent nodes
     */
  getAdjacents() {
    return Array.from(this.adjacents);
  }
  // tag::constructor[]
}
// end::constructor[]

module.exports = Node;

Oh, that's the code from node.js. Sorry, I thought you found an issue with some experimental code using it.

So about your question,

the constructor uses HashSet where it should be HashMapSet, if I am not mistaken.

In dsa.js, HashSet or HashMapSet is the same. The only different set is the TreeSet.

Let me know if you have more questions about it.