Prerequisites: Graph and Its Representation
In this article, adding and removing edge is discussed in a given adjacency list representation.
A vector has been used to implement the graph using adjacency list representation. It is used to store the adjacency lists of all the vertices. The vertex number is used as the index in this vector.
Below is a graph and its adjacency list representation:
If the edge between 1 and 4 has to be removed, then the above graph and the adjacency list transforms to:
Approach: The idea is to represent the graph as an array of vectors such that every vector represents adjacency list of the vertex.
- Adding an edge: Adding an edge is done by inserting both of the vertices connected by that edge in each others list. For example, if an edge between (u, v) has to be added, then u is stored in v’s vector list and v is stored in u’s vector list. (push_back)
- Deleting an edge: To delete edge between (u, v), u’s adjacency list is traversed until v is found and it is removed from it. The same operation is performed for v.(erase)
Below is the implementation of the approach:
vertex 0 -> 1-> 4 vertex 1 -> 0-> 2-> 3-> 4 vertex 2 -> 1-> 3 vertex 3 -> 1-> 2-> 4 vertex 4 -> 0-> 1-> 3 vertex 0 -> 1-> 4 vertex 1 -> 0-> 2-> 3 vertex 2 -> 1-> 3 vertex 3 -> 1-> 2-> 4 vertex 4 -> 0-> 3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Add and Remove Edge in Adjacency Matrix representation of a Graph
- Comparison between Adjacency List and Adjacency Matrix representation of Graph
- Convert Adjacency Matrix to Adjacency List representation of Graph
- Convert Adjacency List to Adjacency Matrix representation of a Graph
- Add and Remove vertex in Adjacency List representation of Graph
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- C program to implement Adjacency Matrix of a given Graph
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Check if removing a given edge disconnects a graph
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Program to Calculate the Edge Cover of a Graph
- Maximize number of nodes which are not part of any edge in a Graph
- Edge Coloring of a Graph
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Find the maximum component size after addition of each edge to the graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.