INPUT: weighted undirected graph in the form of adjacency list
OUTPUT: adjacency list without the edge e
Naive approach is:
for(int i = 0; i < adj.get(e.s).size(); i++) { // loop through adjacent of s
if(adj.get(e.s).get(i).v == e.t) { // find t and remove it
adj.get(e.s).remove(i);
break;
}
}
for(int i = 0; i < adj.get(e.t).size(); i++) { // loop through adjacent of t
if(adj.get(e.t).get(i).v == e.s) { // find s and remove it
adj.get(e.t).remove(i);
break;
}
}
O(k) - k is number of adjacent vertices
A faster approach is to use adjacencyMatrix but my algorithm has alot of traversals involved and looping through each dimension in the adjMat costs more despite a O(1) edge removal.
How can I speed up removing an edge? (used in generating a MST (kruskal))