- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add and Remove Edge in Adjacency List Representation of Graph
Adjacency lists effectively store graph relationships. Graph algorithms and operations use it. Adding and deleting edges can dynamically change the connections between vertices. Graph modification, connection analysis, and evolution need this procedure.Adding and deleting edges link and detach vertices, respectively. The adjacency list representation commonly performs these actions by altering the vertices' adjacency lists. Using a vector of vectors, sets, or maps of sets may change the implementation.New edges create pathways and linkages in the graph. However, removing edges breaks connections, changing graph structure and dynamics. These procedures are essential for graph adjacency list integrity and evolution.
Methods Used
Vector of vectors
Vector of sets
Map of Sets
Vector of Vectors Approach
The vector of vectors technique implements the adjacency list representation of a graph. The outer vector represents vertices, and the inner vector represents their neighbours. This method stores graph structure easily. Adjacency lists are updated by modifying vectors. Small graphs may be easily accessed and modified using this way.
Algorithm
Vectorize an empty graph.
Add v to u's adjacency list to create an edge.
Add u to v's adjacency list.
Eliminate v from the adjacency list of u to eliminate an edge.
Example
#include <iostream> #include <vector> #include <algorithm> // Function to add an edge between two vertices void addEdge(std::vector<std::vector<int>>& graph, int u, int v) { graph[u].push_back(v); graph[v].push_back(u); } // Function to remove an edge between two vertices void removeEdge(std::vector<std::vector<int>>& graph, int u, int v) { graph[u].erase(std::remove(graph[u].begin(), graph[u].end(), v), graph[u].end()); graph[v].erase(std::remove(graph[v].begin(), graph[v].end(), u), graph[v].end()); } int main() { int numVertices = 4; std::vector<std::vector<int>> graph(numVertices); // Add edges to the graph addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 2); addEdge(graph, 2, 3); // Remove edge between vertices 0 and 2 removeEdge(graph, 0, 2); // Display the updated graph for (int i = 0; i < graph.size(); ++i) { std::cout << "Adjacent vertices of vertex " << i << ": "; for (int j = 0; j < graph[i].size(); ++j) { std::cout << graph[i][j] << " "; } std::cout << std::endl; } return 0; }
Output
Adjacent vertices of vertex 0: 1 Adjacent vertices of vertex 1: 0 2 Adjacent vertices of vertex 2: 1 3 Adjacent vertices of vertex 3: 2
Vector of Sets Approach
The vector of sets technique uses unordered sets to express a graph's adjacency list. The unordered set maintains nearby vertices for each vector element. This method allows quick edge insertion, deletion, and lookup. It prevents adjacency list duplicates. This approach is handy for efficiently checking edge existence.
Algorithm
Create an empty graph using unordered sets.
To connect u and v:
v into u's unordered set.
u into v's unordered set.
Remove u−v edge:
Delete v from u.
Remove u from v.
Example
#include <iostream> #include <vector> #include <unordered_set> // Function to add an edge between two vertices void addEdge(std::vector<std::unordered_set<int>>& graph, int u, int v) { graph[u].insert(v); graph[v].insert(u); } // Function to remove an edge between two vertices void removeEdge(std::vector<std::unordered_set<int>>& graph, int u, int v) { graph[u].erase(v); graph[v].erase(u); } int main() { int numVertices = 4; std::vector<std::unordered_set<int>> graph(numVertices); // Add edges to the graph addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 2); addEdge(graph, 2, 3); // Remove edge between vertices 0 and 2 removeEdge(graph, 0, 2); // Display the updated graph for (int i = 0; i < graph.size(); ++i) { std::cout << "Adjacent vertices of vertex " << i << ": "; for (int adjVertex : graph[i]) { std::cout << adjVertex << " "; } std::cout << std::endl; } return 0; }
Output
Adjacent vertices of vertex 0: 1 Adjacent vertices of vertex 1: 2 0 Adjacent vertices of vertex 2: 3 1 Adjacent vertices of vertex 3: 2
Map of Sets Approach
Map data structures represent graph adjacency lists in the map of sets technique. The map's key−value pairs represent vertices and their neighbouring unordered sets. This method simplifies edge addition and removal and vertex adjacency lists. A map can handle graphs with non−numeric vertex IDs. Certain graph algorithms benefit from adjacency lists ordered by vertex IDs.
Algorithm
Map unordered sets to create an empty graph.
To connect u and v:
Insert v into the unordered map set of u.
Insert u into the map's unordered v set.
Remove u−v edge:
Delete v from the map's unordered u set.
Delete u from the map's unordered v set.
Example
#include <iostream> #include <map> #include <unordered_set> // Function to add an edge between two vertices void addEdge(std::map<int, std::unordered_set<int>>& graph, int u, int v) { graph[u].insert(v); graph[v].insert(u); } // Function to remove an edge between two vertices void removeEdge(std::map<int, std::unordered_set<int>>& graph, int u, int v) { graph[u].erase(v); graph[v].erase(u); } int main() { std::map<int, std::unordered_set<int>> graph; // Add edges to the graph addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 2); addEdge(graph, 2, 3); // Remove edge between vertices 0 and 2 removeEdge(graph, 0, 2); // Display the updated graph for (const auto& entry : graph) { int vertex = entry.first; std::cout << "Adjacent vertices of vertex " << vertex << ": "; for (int adjVertex : entry.second) { std::cout << adjVertex << " "; } std::cout << std::endl; } return 0; }
Output
Adjacent vertices of vertex 0: 1 Adjacent vertices of vertex 1: 2 0 Adjacent vertices of vertex 2: 3 1 Adjacent vertices of vertex 3: 2
Conclusion
In conclusion, altering, analysing, and growing graph structures requires adding and removing edges in the adjacency list form. Using a vector of vectors, sets, or maps of sets trades simplicity, efficiency, and usefulness.These methods let you update the graph's structure by adding or removing adjacency lists. Many graph−related applications require graph extension, edge elimination, and graph evolution, which this flexibility allows.Performance, ease of implementation, and edge presence checks or sorting determine your graph−based application's technique. To add and remove edges efficiently, use the right procedure.