Maximum number of nodes which can be reached from each node in a graph


In a graph, the most extreme number of hubs that can be reached from each hub depends on the structure and network of the chart. This value is determined by the number of active edges on each hub. In an undirected chart, each hub can reach all the hubs associated with it specifically, with the most extreme number of reachable hubs rising to the number of adjoining hubs. In a coordinated chart, the most extreme number of reachable hubs may shift for each hub, depending on the outdegree of each hub. The most noteworthy conceivable number of reachable hubs from a single hub happens when it has active edges to all other hubs within the chart.

Methods Used

  • DSF

  • BSF

DSF

Constraint satisfaction refers to the method of deciding whether it is conceivable to allot values to factors in a way that fulfils a set of given imperatives. Within the setting of checking on the off chance that it is conceivable to dole out values such that all the given relations are fulfilled, we analyse the limitations that characterise the connections between factors. By efficiently investigating the space of conceivable values for each variable and checking in the event that the assigned values meet all the required relations, we are able to decide if a substantial task exists. Limitation fulfilment includes finding an arrangement that fulfils all imperatives or deciding that no such arrangement exists.

Algorithm

  • Initialise a gone-by array or set to keep track of gone-by nodes.

  • For each hub within the graph.

  • Initialise a counter variable to store the most extreme number of reachable hubs from the current node.

  • If the current hub has not been visited, Call a DFS, passing the current hub and the counter variable as parameters.

    DFS Function.

  • Mark the current hub as visited.

  • Increment the counter variable.

  • For each neighbour of the current node:

  • If the neighbour has not been visited: Recursively call the DFS work on the neighbour and pass the counter variable. Return the counter variable. After iterating through all hubs within the chart, the most extreme number of reachable hubs from each hub would have been decided.

Example

#include <iostream>
#include <vector>

using namespace std;

vector<bool> visited;
vector<vector<int>> graph;

void DFS(int hub, int& counter) {
   visited[hub] = true;
   counter++;

   // Iterate through neighbors of the current hub
   for (int neighbor : graph[hub]) {
      if (!visited[neighbor]) {
         DFS(neighbor, counter);
      }
   }
}

int main() {
   int numHubs = 5; // Number of hubs in the graph

   visited.assign(numHubs, false);
   graph.resize(numHubs);

   // Define the graph connections (edges)
   graph[0] = {1, 2};
   graph[1] = {2};
   graph[2] = {3};
   graph[3] = {4};
   graph[4] = {};

   // Process each hub
   for (int hub = 0; hub < numHubs; hub++) {
      visited.assign(numHubs, false);
      int counter = 0; // Initialize the counter for each hub
      if (!visited[hub]) {
         DFS(hub, counter);
      }
      cout << "Hub " << hub << ": " << counter << " reachable hubs" << endl;
   }

   return 0;
}

Output

Hub 0: 5 reachable hubs
Hub 1: 4 reachable hubs
Hub 2: 3 reachable hubs
Hub 3: 2 reachable hubs
Hub 4: 1 reachable hubs

BSF

Breadth-First Search (BFS) is a calculation utilised to discover the maximum number of hubs reachable from each hub in a chart. Beginning with a hub, BFS investigates its neighbours level by level, guaranteeing that all hubs at the current level have been gone by some time since recently moving to another level. By keeping up a list of hubs to be investigated, BFS methodically visits hubs in a breadth-first way. Amid the traversal, ready to keep track of the tally of gone-by hubs for each beginning hub, which eventually gives the maximum number of reachable hubs from each hub within the chart,

Algorithm

  • Initialise a purge line and a purge set to track gone nodes.

  • For each hub within the chart, do the following:

  • Enqueue the hub into the queue.

  • Add the hub to the set.

  • Initialise a tally variable to 0.

  • While the line isn't purge, rehash the steps after:

  • Dequeue a hub from the front of the queue.

  • Increment the number variable by 1.

  • Explore all unvisited neighbours of the queued node.

  • Enqueue each unvisited neighbour into the queue.

  • Add each unvisited neighbour to the gone by set.

  • Once the BFS traversal is totaled for all hubs, the tally variable for each hub speaks to the greatest number of reachable hubs from that node.

  • Return the checks for each hub as the result.

Example

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

// Structure to represent a hub
struct Hub {
   int id;
   std::vector<int> neighbors;
};

// Function to initialize a purge set
std::unordered_set<int> initializePurgeSet() {
   return std::unordered_set<int>();
}

// Function to perform BFS traversal and return the tally variable for each hub
std::vector<int> performBFS(const std::vector<Hub>& chart) {
   std::vector<int> tally(chart.size(), 0); // Initialize tally variable for each hub
   std::queue<int> queue;
   std::unordered_set<int> goneSet = initializePurgeSet();

   // Process each hub in the chart
   for (const Hub& hub : chart) {
      queue.push(hub.id);
      goneSet.insert(hub.id);

      while (!queue.empty()) {
         int currentHub = queue.front();
         queue.pop();
         tally[currentHub]++;

         for (int neighbor : chart[currentHub].neighbors) {
            if (goneSet.find(neighbor) == goneSet.end()) {
               queue.push(neighbor);
               goneSet.insert(neighbor);
            }
         }
      }
   }

   return tally;
}

int main() {
   // Create a sample chart with hubs and their neighbors
   std::vector<Hub> chart{
      {0, {1, 2}},
      {1, {2}},
      {2, {0, 3}},
      {3, {3}}
   };

   // Perform BFS traversal and get the tally for each hub
   std::vector<int> hubTally = performBFS(chart);

   // Print the result
   for (int i = 0; i < hubTally.size(); ++i) {
      std::cout << "Hub " << i << ": " << hubTally[i] << std::endl;
   }

   return 0;
}

Output

Hub 0: 1
Hub 1: 2
Hub 2: 2
Hub 3: 2

Conclusion

This article clarifies two calculations, Depth-First Look (DFS) and Breadth-First Look (BFS), to discover the greatest number of hubs reachable from each hub in a chart. It gives step-by-step clarifications of both calculations beside the test code in C. The DFS calculation employs recursion to investigate the chart, whereas the BFS calculation employs a line to navigate the chart in a breadth-first way. The yield of each algorithm is the tally of reachable hubs from each hub within the chart.

Updated on: 19-Jul-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements