-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphHasEdge.js
48 lines (39 loc) · 983 Bytes
/
graphHasEdge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class graph{
constructor(){
this.adjacenyList={}
}
addVertex(vertex){
if(!this.adjacenyList[vertex]){
this.adjacenyList[vertex]= new Set()
}
}
addEdge(vertex1,vertex2){
if(!this.addVertex[vertex1]){
this.addVertex(vertex1)
}
if(!this.addVertex[vertex2]){
this.addVertex(vertex2)
}
this.adjacenyList[vertex1].add(vertex2)
this.adjacenyList[vertex2].add(vertex1)
}
display(){
for(let vertex in this.adjacenyList){
console.log(vertex + "->"+ [...this.adjacenyList[vertex]]);
}
}
hasEdge(vertex1,vertex2){
return(
this.adjacenyList[vertex1].has(vertex2)&&
this.adjacenyList[vertex2].has(vertex1)
)
}
}
let gr=new graph()
gr.addVertex("A")
gr.addVertex("B")
gr.addVertex("C")
gr.addEdge("A","B")
gr.addEdge("B","C")
gr.display()
console.log(gr.hasEdge("A","B"));