Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Add and Remove vertex in Adjacency List representation of Graph

  • Difficulty Level : Hard
  • Last Updated : 07 Feb, 2020

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:

The graph can be represented in the Adjacency List representation as:

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 representated in the adjacency list(or Linked List) representation.


    The idea is to represent the graph as a list of linked lists where the head of the linked list is the vertex and all the connected linked lists are the vertices to which it is connected.

  • Adding a Vertex in the Graph: 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:
  • Removing a Vertex in the Graph: 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:

    Below is the implementation of the above approach:

    # Python implementation of the above approach
    # Implementing Linked List representation
    class AdjNode(object):
        def __init__(self, data):
            self.vertex = data
   = None
    # Adjacency List representation
    class AdjList(object):
        def __init__(self, vertices):
            self.v = vertices
            self.graph =[None]*self.v
    # Function to add an edge from a source vertex 
    # to a destination vertex
        def addedge(self, source, destination):
            node = AdjNode(destination)
   = self.graph
            self.graph= node
    # Function to call the above function.
        def addvertex(self, vk, source, destination):
            graph.addedge(source, vk) 
            graph.addedge(vk, destination)
    # Function to print the graph
        def print_graph(self):
            for i in range(self.v):
                print(i, end ="")
                temp = self.graph[i]
                while temp:
                   print("->", temp.vertex, end ="")
                   temp =
    # Function to delete a vertex
        def delvertex(self, k):
    # Iterating through all the vertices of the graph
            for i in range(self.v):
                temp = self.graph[i]
                if i == k:
                    while temp:
                        temp = self.graph[i]
                # Delete the vertex 
                # using linked list concept        
                if temp:
                    if temp.vertex == k:
                        temp = None
                while temp:
                    if temp.vertex == k:
                    prev = temp
                    temp =
                if temp == None:
                temp = None
    # Driver code
    if __name__ == "__main__":
        V = 6
        graph = AdjList(V) 
        graph.addedge(0, 1)
        graph.addedge(0, 3)
        graph.addedge(0, 4)
        graph.addedge(1, 2)
        graph.addedge(3, 2)
        graph.addedge(4, 3)
    print("Initial adjacency list")
    # Add vertex
    graph.addvertex(5, 3, 2)
    print("Adjacency list after adding vertex")
    # Delete vertex
    print("Adjacency list after deleting vertex")
    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-> 1
    1-> 2
    3-> 5-> 2
    5-> 2

    My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!