Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph nodes store adjacents in HashSet object which does not exist #102

Closed
plasmak opened this issue Aug 29, 2021 · 3 comments
Closed

Graph nodes store adjacents in HashSet object which does not exist #102

plasmak opened this issue Aug 29, 2021 · 3 comments

Comments

@plasmak
Copy link

plasmak commented Aug 29, 2021

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.

@amejiarosario
Copy link
Owner

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?

@plasmak
Copy link
Author

plasmak commented Aug 31, 2021

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;

@amejiarosario
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants