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
Got what you were looking for? Learn more and become self sufficient. Start learning Data Sructures & Algorithms with the help of the most trusted DSA Self Paced course, and that too at the most student-friendly price.
Recommended Posts:
- Hexadecimal equivalents in Binary Valued Graph
- Find any one of the multiple repeating elements in read only array | Set 2
- Proof that Hamiltonian Cycle is NP-Complete
- How to set up fixed width for
? - JavaScript | Super keyword
- Difference between lodash and Underscore
- How to navigate to a parent route from a child route?
- CSS | Value Integer
- Difference between Directive and Component in AngularJS
- HTTP headers | cache-control
- Difference between Direct and Implied Addressing Modes
- Find if there is a path between two vertices in an undirected graph
- Binary Search Tree | Set 3 (Iterative Delete)
- Double Threaded Binary Search Tree
- Shortest path in a directed graph by Dijkstra’s algorithm
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.



