Skip to content

Instantly share code, notes, and snippets.

@ziyoshams
Created July 30, 2018 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziyoshams/43fa9a9ca4ca7f1443dca83ceb76febd to your computer and use it in GitHub Desktop.
Save ziyoshams/43fa9a9ca4ca7f1443dca83ceb76febd to your computer and use it in GitHub Desktop.
class Graph {
constructor() {
this.AdjList = new Map();
}
addVertex(vertex) {
if (!this.AdjList.has(vertex)) {
this.AdjList.set(vertex, []);
} else {
throw 'Already Exist!!!';
}
}
addEdge(vertex, node) {
if (this.AdjList.has(vertex)) {
if (this.AdjList.has(node)){
let arr = this.AdjList.get(vertex);
if(!arr.includes(node)){
arr.push(node);
}
}else {
throw `Can't add non-existing vertex ->'${node}'`;
}
} else {
throw `You should add '${vertex}' first`;
}
}
print() {
for (let [key, value] of this.AdjList) {
console.log(key, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment