Count nodes with maximum reachable neighbours at a d distance
Last Updated :
22 Feb, 2023
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
Example 1
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++
#include <bits/stdc++.h>
using namespace std;
int findTheCity( int n, vector<vector< int > >& edges, int d)
{
vector<vector< int > > dist(n, vector< int >(n, 1e9));
for ( auto e : edges) {
int u = e[0];
int v = e[1];
int d = e[2];
dist[u][v] = d;
dist[v][u] = d;
}
for ( int i = 0; i < n; i++) {
dist[i][i] = 0;
}
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]);
}
}
}
int maxCtr = INT_MIN;
int ansctr = 0;
for ( int node = 0; node < n; node++) {
int ctr = 0;
for ( int adjNode = 0; adjNode < n; adjNode++) {
if (dist[node][adjNode] <= d) {
ctr++;
}
}
if (ctr > maxCtr) {
maxCtr = ctr;
ansctr = 1;
}
else if (ctr == maxCtr)
ansctr++;
}
return ansctr;
}
int main()
{
vector<vector< int > > edges = {
{ 0, 1, 3 }, { 1, 2, 1 }, { 1, 3, 4 }, { 2, 3, 1 }
};
int d = 4;
int n = 4;
int ans = findTheCity(n, edges, d);
cout << ans << "\n" ;
return 0;
}
|
Java
import java.util.*;
public class Main {
static int findTheCity( int n, int [][] edges, int d)
{
int [][] dist = new int [n][n];
for ( int i = 0 ; i < n; i++) {
Arrays.fill(dist[i], Integer.MAX_VALUE / 2 );
dist[i][i] = 0 ;
}
for ( int [] e : edges) {
int u = e[ 0 ], v = e[ 1 ], w = e[ 2 ];
dist[u][v] = dist[v][u] = w;
}
for ( int k = 0 ; k < n; k++) {
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
dist[i][j]
= Math.min(dist[i][j],
dist[i][k] + dist[k][j]);
}
}
}
int maxCtr = Integer.MIN_VALUE;
int ansctr = 0 ;
for ( int node = 0 ; node < n; node++) {
int ctr = 0 ;
for ( int adjNode = 0 ; adjNode < n; adjNode++) {
if (dist[node][adjNode] <= d) {
ctr++;
}
}
if (ctr > maxCtr) {
maxCtr = ctr;
ansctr = 1 ;
}
else if (ctr == maxCtr) {
ansctr++;
}
}
return ansctr;
}
public static void main(String[] args)
{
int [][] edges = { { 0 , 1 , 3 },
{ 1 , 2 , 1 },
{ 1 , 3 , 4 },
{ 2 , 3 , 1 } };
int d = 4 ;
int n = 4 ;
int ans = findTheCity(n, edges, d);
System.out.println(ans);
}
}
|
Python3
from typing import List
def find_the_city(n: int , edges: List [ List [ int ]], d: int ) - > int :
dist = [[ 1e9 ] * n for _ in range (n)]
for u, v, wt in edges:
dist[u][v] = wt
dist[v][u] = wt
for i in range (n):
dist[i][i] = 0
for k in range (n):
for i in range (n):
for j in range (n):
dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j])
max_ctr = float ( '-inf' )
ans_ctr = 0
for node in range (n):
ctr = 0
for adj_node in range (n):
if dist[node][adj_node] < = d:
ctr + = 1
if ctr > max_ctr:
max_ctr = ctr
ans_ctr = 1
elif ctr = = max_ctr:
ans_ctr + = 1
return ans_ctr
d = 4
n = 4
edges = [[ 0 , 1 , 3 ], [ 1 , 2 , 1 ], [ 1 , 3 , 4 ], [ 2 , 3 , 1 ]]
print (find_the_city(d,edges,n))
|
Javascript
function findTheCity(n, edges, d) {
let dist = new Array(n).fill(0).map(() => new Array(n).fill(Number.MAX_VALUE));
for (let i = 0; i < edges.length; i++) {
let u = edges[i][0];
let v = edges[i][1];
let w = edges[i][2];
dist[u][v] = w;
dist[v][u] = w;
}
for (let i = 0; i < n; i++) {
dist[i][i] = 0;
}
for (let k = 0; k < n; k++) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
dist[i][j] = Math.min(dist[i][j],
dist[i][k] + dist[k][j]);
}
}
}
let maxCtr = Number.MIN_SAFE_INTEGER;
let ansctr = 0;
for (let node = 0; node < n; node++) {
let ctr = 0;
for (let adjNode = 0; adjNode < n; adjNode++) {
if (dist[node][adjNode] <= d) {
ctr++;
}
}
if (ctr > maxCtr) {
maxCtr = ctr;
ansctr = 1;
}
else if (ctr === maxCtr)
ansctr++;
}
return ansctr;
}
let edges = [
[0, 1, 3], [1, 2, 1], [1, 3, 4], [2, 3, 1]
];
let d = 4;
let n = 4;
let ans = findTheCity(n, edges, d);
console.log(ans);
|
C#
using System;
public class MainClass
{
static int FindTheCity( int n, int [][] edges, int d)
{
int [][] dist = new int [n][];
for ( int i = 0; i < n; i++) {
dist[i] = new int [n];
Array.Fill(dist[i], int .MaxValue / 2);
dist[i][i] = 0;
}
for ( int i = 0; i < edges.Length; i++) {
int u = edges[i][0], v = edges[i][1], w = edges[i][2];
dist[u][v] = dist[v][u] = w;
}
for ( int k = 0; k < n; k++) {
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
dist[i][j]
= Math.Min(dist[i][j],
dist[i][k] + dist[k][j]);
}
}
}
int maxCtr = int .MinValue;
int ansctr = 0;
for ( int node = 0; node < n; node++) {
int ctr = 0;
for ( int adjNode = 0; adjNode < n; adjNode++) {
if (dist[node][adjNode] <= d) {
ctr++;
}
}
if (ctr > maxCtr) {
maxCtr = ctr;
ansctr = 1;
}
else if (ctr == maxCtr) {
ansctr++;
}
}
return ansctr;
}
public static void Main()
{
int [][] edges = { new int [] { 0, 1, 3 },
new int [] { 1, 2, 1 },
new int [] { 1, 3, 4 },
new int [] { 2, 3, 1 } };
int d = 4;
int n = 4;
int ans = FindTheCity(n, edges, d);
Console.WriteLine(ans);
}
}
|
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...