Skip to content

Instantly share code, notes, and snippets.

@ehorodyski
Created April 5, 2019 21:54
Show Gist options
  • Save ehorodyski/c08626e400465a8cfbd70e1d7a0246e3 to your computer and use it in GitHub Desktop.
Save ehorodyski/c08626e400465a8cfbd70e1d7a0246e3 to your computer and use it in GitHub Desktop.
JavaScript Graph Implementation
class Graph {
constructor(numberOfVertices) {
this.numberOfVertices = numberOfVertices;
this.adjacencyList = new Map();
}
addVertex(v) {
this.adjacencyList.set(v, []);
}
addEdge(source, desination) {
this.adjacencyList.get(source).push(desination);
this.adjacencyList.get(desination).push(source);
}
printGraph() {
let keys = this.adjacencyList.keys();
for (let key of keys) {
let values = this.adjacencyList.get(key);
let str = '';
for (let value of values)
str += `${value} `;
console.log(`${key} -> ${str}`);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment