// JavaScript implementation of the approach
// to delete an edge in an undirected graph
const addEdge = (adj, u, v) => {
// add the edge to the graph by adding v to the
// list of u and adding u to the list of v
adj[u].push(v);
adj[v].push(u);
};
const delEdge = (adj, u, v) => {
// find the index of v in the list of u and remove it
const indexOfVInU = adj[u].indexOf(v);
adj[u].splice(indexOfVInU, 1);
// find the index of u in the list of v and remove it
const indexOfUInV = adj[v].indexOf(u);
adj[v].splice(indexOfUInV, 1);
};
const printGraph = (adj, V) => {
for (let v = 0; v < V; v++) {
console.log(`vertex ${v} `, adj[v].join("-> "));
}
};
const main = () => {
const V = 5;
const adj = [];
for (let i = 0; i < V; i++) {
adj[i] = [];
}
// Adding edge as shown in the example figure
addEdge(adj, 0, 1);
addEdge(adj, 0, 4);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 3);
addEdge(adj, 3, 4);
// Printing adjacency matrix
printGraph(adj, V);
// Deleting edge (1, 4)
// as shown in the example figure
delEdge(adj, 1, 4);
// Printing adjacency matrix
printGraph(adj, V);
};
main();