Queries to Count Connected Components after Removal of a Vertex from a Tree


The following queries can be used to determine how many connected components remain after a tree vertex is removed: Start by taking the tree structure into account. Then, by moving through the tree using breadth- or depth-first search algorithms, examine each connected component. Utilise the same traversal method to decide the number of connected components once the desired vertex has been expelled. The result will be decided by the variation between the counts before and after the expulsion. This method effectively monitors connectivity changes and aids in counting the connected components in the updated tree.

Methods Used

  • Depth-First Search (DFS) Approach

  • Union-Find Approach

Depth-First Search (DFS) Approach

In the DFS approach, we begin by performing a DFS traversal from any selected node in the original tree to count connected components after removing a vertex from the tree. We traverse every connected node during this traversal, marking each one as visited and keeping track of how many times DFS is used. We perform a new DFS traversal after removing the specified vertex, making sure to skip the removed vertex during the exploration phase. We can determine the number of connected components in the updated tree by comparing the number of times DFS is called before and after the removal. This approach effectively counts connected elements while adjusting for changes in the tree's structure.

Algorithm

  • Pick any vertex on the original tree as the starting point for DFS traversal.

  • Set up the variable "count" to start counting the connected components. At first, set it to 0.

  • From the selected starting vertex, traverse the tree using DFS.

  • Mark each vertex that was visited during the DFS traversal and increase the "count" by 1 for each fresh DFS invocation (i.e., for each connected component found).

  • The number of connected elements in the original tree will be represented by "count" after the DFS traversal is finished.

  • From the tree, remove the specified vertex.

  • Repeat the DFS traversal from the same starting vertex, making sure to avoid exploring the removed vertex.

  • Update the "count" variable similarly to before while running the DFS.

  • The number of associated components within the upgraded tree will be decided by subtracting the "count" after the evacuation from the beginning "count."

Example

#include <iostream>
#include <vector>

void dfs(const std::vector<std::vector<int>>& tree, int v, 
std::vector<bool>& visited, int& count) {
   visited[v] = true;
   count++;
   for (int u : tree[v]) {
      if (!visited[u]) {
         dfs(tree, u, visited, count);
      }
   }
}

int countConnectedComponents(const std::vector<std::vector<int>>& tree, int startVertex) {
   int n = tree.size();
   std::vector<bool> visited(n, false);
   int count = 0;

   dfs(tree, startVertex, visited, count);
   return count;
}

int main() {
   std::vector<std::vector<int>> tree = {
      {1, 2},
      {0, 3},
      {0, 4},
      {1},
      {2}
   };

   int startVertex = 0;
   std::cout << countConnectedComponents(tree, startVertex) << std::endl;
   return 0;
}

Output

5

Union-Find Approach

We start by initialising each vertex as a separate component in the Union-Find method for counting connected components after removing a vertex from a tree. To keep track of the parts and connections in the original tree, we employ the union-find data structure. We update the union-find data structure to reflect the changes in the connectivity of the tree after removing the designated vertex. The number of distinct sets in the union-find data structure is then counted. The resulting count is a representation of the connectedness of the tree's updated components. After a vertex is removed, the Union-Find method effectively counts the connected components and handles structural changes in the tree with efficiency.

Algorithm

  • Create an array or data structure from scratch to represent each vertex as a distinct part of the tree. Initially, every vertex is its own parent.

  • Use union-find operations in a pre-processing step to determine the original tree's connected component count.

  • Use the union-find data structure to combine the parts that contain vertices u and v for each edge (u, v) in the tree. The initial connectivity of the tree is established in this step.

  • From the tree, remove the specified vertex.

  • Apply the pre-processing step's union-find operations to the modified tree.

  • After the removal, count the number of distinct parent representatives in the union-find data structure.

  • The resulting count is a representation of the connectedness of the tree's updated components.

Example

#include <iostream>
#include <vector>

class UnionFind {
public:
   UnionFind(int n) {
      parent.resize(n);
      for (int i = 0; i < n; ++i) {
         parent[i] = i;
      }
   }

   int find(int u) {
      if (parent[u] != u) {
         parent[u] = find(parent[u]);
      }
      return parent[u];
   }

   void unite(int u, int v) {
      int rootU = find(u);
      int rootV = find(v);
      if (rootU != rootV) {
         parent[rootU] = rootV;
      }
   }

   int countDistinctParentRepresentatives() {
      int n = parent.size();
      std::vector<bool> distinct(n, false);
      for (int i = 0; i < n; ++i) {
         distinct[find(i)] = true;
      }
      int count = 0;
      for (bool isDistinct : distinct) {
         if (isDistinct) {
            count++;
         }
      }
      return count;
   }

private:
   std::vector<int> parent;
};

int main() {
   int n = 5;
   UnionFind uf(n);

   uf.unite(0, 1);
   uf.unite(0, 2);
   uf.unite(2, 3);
   uf.unite(2, 4);

   std::cout << uf.countDistinctParentRepresentatives() << 
std::endl;
   return 0;
}

Output

1

Conclusion

In conclusion, the methods offered effectively count the number of connected parts in a tree after a specific vertex has been removed. It is effective to handle connectivity changes in the tree's structure using both the Depth-First Search (DFS) approach and the Union-Find method.The DFS method begins with a traversal from a chosen vertex, marking each node that is visited, and tallying the connected components. The updated count is obtained by comparing the before and after traversal counts after the vertex has been removed, and a new traversal is performed without including the removed node.

The initial connected component count is determined using union operations by the Union-Find method, which initialises each vertex as a separate component. The same union operations are applied after the vertex has been removed, and the distinct parent representatives are counted to obtain the updated count.

After removing the vertex, both approaches offer useful insights into the connectedness of the tree and are appropriate for various scenarios.

Updated on: 04-Aug-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements