class Graph {
constructor() {
this.adjacencyList = {};
}
// Add Value in Graph
addVertex(vertex) {
if(!this.adjacencyList[vertex]) {
this.adjacencyList[vertex] = [];
}
console.log(` -> ${vertex} -> Added successfully `)
return g;
}
// Add Edge-Array in Graph
// EXAMPLE.. -> g.addEdge("Rajkot", "Ahmedabad")
addEdge(v1,v2) {
this.adjacencyList[v1].push(v2);
this.adjacencyList[v2].push(v1);
console.log(` -> ${v1} or ${v2} -> Edge-Array Added successfully `)
return g;
}
// Remove Value in Graph
removeEdge(vertex1,vertex2) {
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter(
v => v !== vertex2
);
this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter(
v => v !== vertex1
);
console.log(`-> ${vertex1} or ${vertex2} -> Edge-Array Deleted successfully`)
return g;
}
// Remove a Vertex in Graph
removeVertex(vertex) {
while (this.adjacencyList[vertex].length) {
const adjacencyVertex = this.adjacencyList[vertex].pop()
this.removeEdge(vertex, adjacencyVertex);
}
delete this.adjacencyList[vertex]
console.log(`-> ${vertex} -> Deleted successfully `)
return g;
}
}
let g =new Graph();
g.addVertex("Rajkot");
g.addVertex("Ahmedabad");
g.addVertex("Morbi");
g.addEdge("Morbi", "Ahmedabad")
g.addEdge("Rajkot", "Morbi")
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)