Count nodes with maximum reachable neighbours at a d distance
Given a graph with n nodes and m edges, each edges[i] = [u, v, weight] and d as the maximum distance to reach the neighbor nodes, the task is to find the total number of nodes with maximum reachable neighbors.
Input: n = 4, edges = [[0, 1, 3], [1, 2, 1], [1, 3, 4], [2, 3, 1]], d = 4
Output: 2
Explanation: node 0 -> [node 1, node 2]
node 1 -> [node 0, node 2, node 3]
node 2 -> [node 0, node 1, node 3]
node 3 -> [node 1, node 2]These are the nodes that are reachable with a maximum distance of d. Thereby out of these node1 and node2 can reach maximum neighbors (3 neighbors). So the answer is node1 and node2, i.e. 2. As we need to return the count of the total number of such nodes.
Approach: To solve the problem follow the below observations:
Here in the above graph, We can observe that Node 1 and 2 can reach to maximum no. of neighbors (here it is all the 3 other nodes). So total there 2 number of nodes that can reach the maximum no. of neighbors (here it is 3) with a limited distance of d (here it is 4).
Follow the below steps to solve the above approach:
- Create an adjacency list for the given graph.
- Run the Floyd warshall algorithm to find all sources shortest distance.
- Then iterate for every node against every adjacent node.
- Count the number of adjacent nodes that are reachable with less than or equal to distance d.
- Then, have a separate count of answers and maxCount (Maximum neighbors), comparing to it increment the answer counter.
Below is the Implementation of the above approach:
C++
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std; int findTheCity(int n, vector<vector<int> >& edges, int d) { // 2D matrix n*n vector<vector<int> > dist(n, vector<int>(n, 1e9)); for (auto e : edges) { // nodes int u = e[0]; int v = e[1]; int d = e[2]; // edge wt // bidirectional edges u-v and v-u dist[u][v] = d; dist[v][u] = d; } for (int i = 0; i < n; i++) { // Self loops dist[i][i] = 0; } // Floyd Warshall Algo for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } // To store the maximum neighbors // possible int maxCtr = INT_MIN; // Stores the count of total such nodes // having max neighbours with // atmax d distance int ansctr = 0; for (int node = 0; node < n; node++) { // How many neighbours within limit // for that node int ctr = 0; for (int adjNode = 0; adjNode < n; adjNode++) { if (dist[node][adjNode] <= d) { ctr++; } } if (ctr > maxCtr) { // Update as we got a node that // has higher neighbours maxCtr = ctr; ansctr = 1; } // Equal to max, so increment counter // as of now this is the max else if (ctr == maxCtr) ansctr++; } return ansctr; } // Driver Code int main() { // edges [u, v, wt] vector<vector<int> > edges = { { 0, 1, 3 }, { 1, 2, 1 }, { 1, 3, 4 }, { 2, 3, 1 } }; // Maximum distance to reach the neighbour int d = 4; // Number of nodes in graph int n = 4; int ans = findTheCity(n, edges, d); cout << ans << "\n"; return 0; }
Time Complexity: O(M) + O(N^3) + O(N^2) = O(N^3) Dominant factor
- O(M) = O(N^2) as at max edges can be N^2
- O(N^3) Floyd warshall algorithm takes
- O(N^2) To find out the count of such nodes.
Auxiliary Space Complexity: O(N+M) + O(N^2)
- O(N+M) the adjacency list takes as such
- O(N^2) the distance 2D Vector / Matrix
Related Articles:
Given a graph with n nodes and m edges, each edges[i] = [u, v, weight] and d as the maximum distance to reach the neighbor nodes, the task is to find the total number of nodes with maximum reachable neighbors.
Input: n = 4, edges = [[0, 1, 3], [1, 2, 1], [1, 3, 4], [2, 3, 1]], d = 4
Output: 2
Explanation: node 0 -> [node 1, node 2]
node 1 -> [node 0, node 2, node 3]
node 2 -> [node 0, node 1, node 3]
node 3 -> [node 1, node 2]These are the nodes that are reachable with a maximum distance of d. Thereby out of these node1 and node2 can reach maximum neighbors (3 neighbors). So the answer is node1 and node2, i.e. 2. As we need to return the count of the total number of such nodes.
Approach: To solve the problem follow the below observations:
Here in the above graph, We can observe that Node 1 and 2 can reach to maximum no. of neighbors (here it is all the 3 other nodes). So total there 2 number of nodes that can reach the maximum no. of neighbors (here it is 3) with a limited distance of d (here it is 4).
Follow the below steps to solve the above approach:
- Create an adjacency list for the given graph.
- Run the Floyd warshall algorithm to find all sources shortest distance.
- Then iterate for every node against every adjacent node.
- Count the number of adjacent nodes that are reachable with less than or equal to distance d.
- Then, have a separate count of answers and maxCount (Maximum neighbors), comparing to it increment the answer counter.
Below is the Implementation of the above approach:
C++
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std; int findTheCity(int n, vector<vector<int> >& edges, int d) { // 2D matrix n*n vector<vector<int> > dist(n, vector<int>(n, 1e9)); for (auto e : edges) { // nodes int u = e[0]; int v = e[1]; int d = e[2]; // edge wt // bidirectional edges u-v and v-u dist[u][v] = d; dist[v][u] = d; } for (int i = 0; i < n; i++) { // Self loops dist[i][i] = 0; } // Floyd Warshall Algo for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } // To store the maximum neighbors // possible int maxCtr = INT_MIN; // Stores the count of total such nodes // having max neighbours with // atmax d distance int ansctr = 0; for (int node = 0; node < n; node++) { // How many neighbours within limit // for that node int ctr = 0; for (int adjNode = 0; adjNode < n; adjNode++) { if (dist[node][adjNode] <= d) { ctr++; } } if (ctr > maxCtr) { // Update as we got a node that // has higher neighbours maxCtr = ctr; ansctr = 1; } // Equal to max, so increment counter // as of now this is the max else if (ctr == maxCtr) ansctr++; } return ansctr; } // Driver Code int main() { // edges [u, v, wt] vector<vector<int> > edges = { { 0, 1, 3 }, { 1, 2, 1 }, { 1, 3, 4 }, { 2, 3, 1 } }; // Maximum distance to reach the neighbour int d = 4; // Number of nodes in graph int n = 4; int ans = findTheCity(n, edges, d); cout << ans << "\n"; return 0; }
Time Complexity: O(M) + O(N^3) + O(N^2) = O(N^3) Dominant factor
- O(M) = O(N^2) as at max edges can be N^2
- O(N^3) Floyd warshall algorithm takes
- O(N^2) To find out the count of such nodes.
Auxiliary Space Complexity: O(N+M) + O(N^2)
- O(N+M) the adjacency list takes as such
- O(N^2) the distance 2D Vector / Matrix
Related Articles: