Add and Remove vertex in Adjacency List representation of Graph
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.
Approach:
- 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 representationclassAdjNode(object):def__init__(self, data):self.vertex=dataself.next=None# Adjacency List representationclassAdjList(object):def__init__(self, vertices):self.v=verticesself.graph=[None]*self.v# Function to add an edge from a source vertex# to a destination vertexdefaddedge(self, source, destination):node=AdjNode(destination)node.next=self.graphself.graph=node# Function to call the above function.defaddvertex(self, vk, source, destination):graph.addedge(source, vk)graph.addedge(vk, destination)# Function to print the graphdefprint_graph(self):foriinrange(self.v):print(i, end="")temp=self.graph[i]whiletemp:print("->", temp.vertex, end="")temp=temp.nextprint("\n")# Function to delete a vertexdefdelvertex(self, k):# Iterating through all the vertices of the graphforiinrange(self.v):temp=self.graph[i]ifi==k:whiletemp:self.graph[i]=temp.nexttemp=self.graph[i]# Delete the vertex# using linked list conceptiftemp:iftemp.vertex==k:self.graph[i]=temp.nexttemp=Nonewhiletemp:iftemp.vertex==k:breakprev=temptemp=temp.nextiftemp==None:continueprev.next=temp.nexttemp=None# Driver codeif__name__=="__main__":V=6graph=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")graph.print_graph()# Add vertexgraph.addvertex(5,3,2)print("Adjacency list after adding vertex")graph.print_graph()# Delete vertexgraph.delvertex(4)print("Adjacency list after deleting vertex")graph.print_graph()chevron_rightfilter_noneOutput:Initial adjacency list 0-> 4-> 3-> 1 1-> 2 2 3-> 2 4-> 3 5 Adjacency list after adding vertex 0-> 4-> 3-> 1 1-> 2 2 3-> 5-> 2 4-> 3 5-> 2 Adjacency list after deleting vertex 0-> 3-> 1 1-> 2 2 3-> 5-> 2 4 5-> 2
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Add and Remove Edge in Adjacency List representation of a Graph
- Convert Adjacency Matrix to Adjacency List representation of Graph
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- C program to implement Adjacency Matrix of a given Graph
- Find a Mother Vertex in a Graph
- Find the Degree of a Particular vertex in a Graph
- Find a Mother vertex in a Graph using Bit Masking
- All vertex pairs connected with exactly k edges in a graph
- Topological Sort of a graph using departure time of vertex
- k'th heaviest adjacent node in a graph where each vertex has weight
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



