VOOZH about

URL: https://www.geeksforgeeks.org/dsa/add-and-remove-vertex-in-adjacency-list-representation-of-graph/

⇱ Add and Remove vertex in Adjacency List representation of Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Add and Remove vertex in Adjacency List representation of Graph

Last Updated : 12 Jul, 2025

Prerequisites:Linked List, Graph Data Structure

In this article, adding and removing a vertex is discussed in a given adjacency list representation. Let the Directed Graph be: 👁 Image
The graph can be represented in the Adjacency List representation as: 👁 Image
It is a Linked List representation where the head of the linked list is a vertex in the graph and all the connected nodes are the vertices to which the first vertex is connected. For example, from the graph, it is clear that vertex 0 is connected to vertex 4, 3 and 1. The same is represented in the adjacency list(or Linked List) representation.

Adding a Vertex in the Adjacency List:

To add a vertex in the graph, the adjacency list can be iterated to the place where the insertion is required and the new node can be created using linked list implementation. For example, if 5 needs to be added between vertex 2 and vertex 3 such that vertex 3 points to vertex 5 and vertex 5 points to vertex 2, then a new edge is created between vertex 5 and vertex 3 and a new edge is created from vertex 5 and vertex 2. After adding the vertex, the adjacency list changes to: 

👁 Image

Removing a Vertex in Adjacency List:

To delete a vertex in the graph, iterate through the list of each vertex if an edge is present or not. If the edge is present, then delete the vertex in the same way as delete is performed in a linked list. For example, the adjacency list translates to the below list if vertex 4 is deleted from the list: 
👁 Image
Below is the implementation of the above approach: 


Output
Initial adjacency list
0 -> 4 -> 3 -> 1 
1 -> 2 
3 -> 2 
4 -> 3 
Adjacency list after adding vertex
0 -> 4 -> 3 -> 1 
1 -> 2 
3 -> 5 -> 2 
4 -> 3 
5 -> 2 
Adjacency list after deleting vertex
0 -> 3 -...
Comment