Dynamic connectivity is a data structure that dynamically maintains the information about thee connected components of graph. In simple words suppose there is a graph G(V, E) in which no. of vertices V is constant but no. of edges E is variable. There are three ways in which we can change the number of edges
- Incremental Connectivity : Edges are only added to the graph.
- Decremental Connectivity : Edges are only deleted from the graph.
- Fully Dynamic Connectivity : Edges can both be deleted and added to the graph.
In this article only Incremental connectivity is discussed. There are mainly two operations that need to be handled.
- An edge is added to the graph.
- Information about two nodes x and y whether they are in the same connected components or not.
Input : V = 7 Number of operations = 11 1 0 1 2 0 1 2 1 2 1 0 2 2 0 2 2 2 3 2 3 4 1 0 5 2 4 5 2 5 6 1 2 6 Note: 7 represents number of nodes, 11 represents number of queries. There are two types of queries Type 1 : 1 x y in this if the node x and y are connected print Yes else No Type 2 : 2 x y in this add an edge between node x and y Output : No Yes No Yes Explanation : Initially there are no edges so node 0 and 1 will be disconnected so answer will be No Node 0 and 2 will be connected through node 1 so answer will be Yes similarly for other queries we can find whether two nodes are connected or not
To solve the problems of incremental connectivity disjoint data structure is used. Here each connected component represents a set and if the two nodes belong to the same set it means that they are connected.
Implementation is given below here we are using union by rank and path compression
No Yes No Yes
The amortized time complexity is O(alpha(n)) per operation where alpha is inverse ackermann function which is nearly constant.
This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Dynamic Programming on Trees | Set 2
- Dynamic Programming on Trees | Set-1
- Sum over Subsets | Dynamic Programming
- Extendible Hashing (Dynamic approach to DBMS)
- Static and Dynamic data structures in Java with Examples
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Dynamic Disjoint Set Data Structure for large range values
- Longest path in a directed Acyclic graph | Dynamic Programming
- Dynamic Segment Trees : Online Queries for Range Sum with Point Updates
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Finding shortest path between any two nodes using Floyd Warshall Algorithm
- Proof that Clique Decision problem is NP-Complete | Set 2
- Count of all cycles without any inner cycle in a given Graph
- Isomorphism in N-ary Trees