Created
July 30, 2018 00:24
-
-
Save ziyoshams/43fa9a9ca4ca7f1443dca83ceb76febd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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