Find K vertices in the graph which are connected to at least one of remaining vertices


Finding K vertices in the network that are connected to at least one of the remaining vertices may be done using DFS (Depth-First Search). Your beginning point should be one of the remaining vertices, and you should then perform a DFS on that vertex. Each vertex you come across while conducting the search will be noted, and it will be added to the group of similar vertices. Once K vertices have been located or all remaining vertices have been searched, keep repeating this. DFS aids in completing the assignment by carefully exploring the graph to find the K vertices that are still linked to at least one of the remaining vertices.

Methods Used

  • DFS

  • BFS

DFS

DFS (Depth-First Look) may be used in the situation of locating K vertices in the chart that are connected to at least one of the remaining vertices. Choose one of the remaining vertices, and then launch a DFS from that vertex. Check each vertex that has passed while exploring the chart and add it to the group of linked vertices. Continue examining the chart until K vertices have been constructed, the criteria have been identified, or all vertices have been examined. DFS enables the differentiating proof of K vertices that maintain links with at least one of the remaining vertices via the depth-first traversal, hence achieving the necessary aim.

Algorithm

  • Initialise a purge set "connectedVertices" to store the K vertices.

  • Select a vertex, startVertex," from the remaining vertices.

  • Create a purge stack "stack" for DFS traversal.

  • Push the "startVertex" onto the "stack".

  • While the "stack" isn't empty,

  • Pop a vertex "currentVertex" from the best of the "stack".

  • Mark "current vertex" as visited.

  • Add "currentVertex" to the "connectedVertices" set.

  • Iterate through the adjoining vertices of "currentVertex":

  • If the adjacent vertex is within the remaining vertices and not visited,

  • Push the adjoining vertex onto the "stack".

  • Repeat steps 2–5 until all K vertices have been distinguished or all remaining vertices have been explored.

Example

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

vector<int> findConnectedVertices(int startVertex, vector<vector<int>>& adjacencyList) {
   int N = adjacencyList.size(); // Total number of vertices
   vector<int> connectedVertices; // Set of connected vertices
   vector<bool> visited(N, false); // Track visited vertices
   stack<int> stack; // Stack for DFS traversal

   stack.push(startVertex); // Push the startVertex onto the stack

   while (!stack.empty()) {
      int currentVertex = stack.top();
      stack.pop();

      if (!visited[currentVertex]) {
         visited[currentVertex] = true;
         connectedVertices.push_back(currentVertex);

         for (int adjVertex : adjacencyList[currentVertex]) {
            if (!visited[adjVertex]) {
               stack.push(adjVertex);
            }
         }
      }
   }

   return connectedVertices;
}

int main() {
   int N = 7; // Total number of vertices
   vector<vector<int>> adjacencyList(N);

   // Add adjacency list for each vertex
   adjacencyList[0] = {1, 2};
   adjacencyList[1] = {0, 2};
   adjacencyList[2] = {0, 1, 3};
   adjacencyList[3] = {2, 4};
   adjacencyList[4] = {3};
   adjacencyList[5] = {6};
   adjacencyList[6] = {5};

   int startVertex = 0;
   vector<int> connectedVertices = findConnectedVertices(startVertex, adjacencyList);

   cout << "Connected Vertices: ";
   for (int vertex : connectedVertices) {
      cout << vertex << " ";
   }
   cout << endl;

   return 0;
}

Output

Connected Vertices: 0 2 3 4 1 

BSF

Within the setting of finding K vertices within the chart associated to at slightest one of the remaining vertices, BFS (Breadth-First Look) can be utilised. Begin by selecting a vertex from the remaining vertices and perform a BFS from that vertex. Amid the look, check each gone-by vertex and include it in the set of associated vertices. Investigate the chart level by level, guaranteeing that vertices at a closer distance are gone by some time since moving to another level. Rehash this preparation until K vertices are recognised or all remaining vertices are investigated. By navigating the chart in a breadth-first way, BFS helps in recognising the K vertices that keep up an association with at least one of the remaining vertices, finishing the assignment at hand.

Algorithm

  • Make an purge set S to store the associated vertices.

  • While K > and R isn't purge, do the following:

  • a. Select a vertex v from R.

  • b. Perform a breadth-first look (BFS) beginning from v.

  • c. Amid the BFS, stamp each gone by vertex and include it to S.

  • d. Decrement K by 1.

  • e. Evacuate V from R.

  • Return the set S containing the K-associated vertices.

  • The calculation chooses a vertex from the remaining vertices and performs a BFS on that vertex. It marks each gone-by vertex and incorporates it within the set of associated vertices. The strategy continues until either K vertices have been perceived or no remaining vertices have been cleared out.

Example

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>

std::unordered_set<int> purgeVertices(int K, const std::vector<std::vector<int>>& graph) {
   std::unordered_set<int> S;
   std::vector<bool> visited(graph.size(), false);
    
   auto bfs = [&](int start) {
      std::queue<int> q;
      q.push(start);
        
      while (!q.empty()) {
         int v = q.front();
         q.pop();
            
         if (!visited[v]) {
            visited[v] = true;
            S.insert(v);
            K--;
                
            if (K == 0) {
               return;
            }
                
            for (int neighbor : graph[v]) {
               if (!visited[neighbor]) {
                  q.push(neighbor);
               }
            }
         }
      }
   };
    
   for (int i = 0; i < graph.size() && K > 0; i++) {
      bfs(i);
   }
    
   return S;
}

int main() {
   // Example usage
   int K = 3;
   std::vector<std::vector<int>> graph = {
      {1, 2},
      {0, 2, 3},
      {0, 1, 3},
      {1, 2}
   };
    
   std::unordered_set<int> result = purgeVertices(K, graph);
    
   // Print the resulting set
   std::cout << "Purged vertices: ";
   for (int v : result) {
      std::cout << v << " ";
   }
   std::cout << std::endl;
    
   return 0;
}

Output

Purged vertices: 2 1 0 

Conclusion

This article gives an algorithmic approach to finding K vertices in a chart that are associated to at slightest one of the remaining vertices. The calculation utilises either Depth-First Look (DFS) or Breadth-First Look (BFS) to navigate the chart and recognise the specified vertices. It incorporates code scraps in C that execute the calculation utilising diverse capacities for the search operation. The article points to assist software engineers get it and execute the method of distinguishing associated vertices in a chart, subsequently encouraging different applications and examinations that depend on chart network.

Updated on: 19-Jul-2023

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements