Count of Nodes Accessible from all Other Nodes of Graph


The number of nodes that may be reached from any particular node in a graph is called as the count of nodes accessible from all other nodes in the graph. It shows the degree of reachability and connectivity inside the graph. We start at each node and investigate all accessible routes to other nodes in order to get this count.The nodes we can access are recorded as we move across the graph. The count of reachable nodes in the graph includes all nodes that can be reached. This is vital for understanding network relationships and information flow efficiency.

Methods Used

  • Reachability Matrix

  • Connected Components

Reachability Matrix

The reachable nodes in a graph are counted using the reachability matrix, a square matrix. The matrix's [i][j] entries individually state whether or not node j can be reached from node i. The nodes that can be reached from each node in the network are revealed by examining the rows of the matrix. The number of '1's in each row represents the number of nodes that can be reached from a given node, providing information on the graph's general connectivity and reachability. This matrix helps to comprehend the connectedness and relationships between nodes in the graph by offering useful insights about how accessible the nodes are within it.

Algorithm

  • Consider n to be the total number of nodes in graph G.

  • Make a 2D array called ReachabilityMatrix that is n by n in size and has an initial value of zero.

  • From 0 through n−1, for each node i:

    Begin a Depth−First Search (DFS) or Breadth−First Search (BFS) from node i.

    Give the ReachabilityMatrix a value of 1 and mark all the nodes that are reachable from node i as visited.

  • In the ReachabilityMatrix, there are four rows.

    Count the number of "1"s in the row and add the total at the proper location in the count[] array.

  • Give me the array count[].

Example

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

void dfs(int node, vector<vector<int>>& graph, vector<bool>& visited) {
    visited[node] = true;
    for (int neighbor : graph[node]) {
        if (!visited[neighbor]) {
            dfs(neighbor, graph, visited);
        }
    }
}

int main() {
    int N = 5; 
    vector<vector<int>> graph(N);

    graph[0].push_back(1);
    graph[1].push_back(2);
    graph[0].push_back(2);
    graph[2].push_back(3);
    graph[3].push_back(4);
    graph[2].push_back(4);

    vector<vector<bool>> reachabilityMatrix(N, vector<bool> (N, false));
    for (int i = 0; i < N; ++i) {
        vector<bool> visited(N, false);
        dfs(i, graph, visited);
        reachabilityMatrix[i] = visited;
    }

    for (int i = 0; i < N; ++i) {
        int count = 0;
        for (int j = 0; j < N; ++j) {
            if (reachabilityMatrix[i][j]) {
                count++;
            }
        }
        cout << "Vertex " << i << " can reach " << count << " nodes." << endl;
    }

    return 0;
}

Output

Vertex 0 can reach 5 nodes.
Vertex 1 can reach 4 nodes.
Vertex 2 can reach 3 nodes.
Vertex 3 can reach 2 nodes.
Vertex 4 can reach 1 nodes.

Connected Components

In the context of "Count of nodes accessible from all other nodes of a Graph," connected components are collections of nodes that are connected by direct or indirect edges and permit communication amongst one another. We identify these related components in order to count the number of nodes that are reachable from every other node. Within the same component, nodes can talk to one another. Calculating the size of each component helps us better comprehend the connectivity and efficiency of the information flow in the graph as a whole by revealing the number of nodes reachable from all other nodes in a specific component.

Algorithm

  • Get the graph G as an adjacency list or matrix as an input.

  • Create an empty list or array to keep track of how many nodes are reachable from each node.

  • Create a set from scratch to record visited nodes.

  • The total number of nodes in the graph should be the initial value of the variable you create.

  • Go through every node in the graph iteratively:

    Initialise a variable to count the nodes reachable from the current node and set it to 1 (the node itself). a. If the current node is not visited: i.

    From the current node, conduct a Depth−First Search (DFS) or Breadth−First Search (BFS) traverse.

    Mark visited nodes and increase the count for each visited node while traversing.

  • For each node, increase the list or array with the number of available nodes.

  • Step 5 must be repeated until all nodes have been reached.

  • The list or array now contains the number of nodes that may be reached from each node in the graph.

  • Final step is to return the list with number of nodes available in the respective node that can be run as output.

Example

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

void dfs(int node, vector<vector<int>>& graph, vector<bool>& visited) {
    visited[node] = true;
    for (int neighbor : graph[node]) {
        if (!visited[neighbor]) {
            dfs(neighbor, graph, visited);
        }
    }
}

int countConnectedComponents(vector<vector<int>>& graph) {
    int N = graph.size();
    vector<bool> visited(N, false);
    int count = 0;
    for (int i = 0; i < N; ++i) {
        if (!visited[i]) {
            dfs(i, graph, visited);
            count++;
        }
    }
    return count;
}

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

    int connectedComponents = countConnectedComponents(graph);
    cout << "Number of connected components: " << connectedComponents << endl;

    return 0;
}

Output

Number of connected components: 3

Conclusion

In conclusion, a key indicator of connection and reachability within a network is the number of nodes in a graph that are reachable from every other node. To calculate this count effectively, the Floyd−Warshall Algorithm, Reachability Matrix, and Connected Components are crucial tools. To estimate how many nodes are accessible from each node, the Floyd−Warshall technique changes the traditional technique. The reachability information of the graph is represented by the reachability matrix, allowing us to determine which nodes are accessible from each node. Finally, recognising connected components makes it easier to count the nodes that are accessible within a single component. Understanding this count is essential for assessing the effectiveness of information flow in a variety of network systems, including computer networks, social networks, and transportation networks.

Updated on: 02-Aug-2023

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements