Kth largest node among all directly connected nodes to the given node in an undirected graph
Last Updated :
05 Aug, 2021
Given two arrays u and v, representing a graph such that there is an undirected edge from u[i] to v[i] (0 ? v[i], u[i] < N) and each node has some value val[i] (0 ? i < N). For each node, if the nodes connected directly to it are sorted in descending order according to their values (in case of equal values, sort it according to their indices in ascending order), print the number of the node at kth position. If total nodes are < k then print -1.
Examples:
Input: u[] = {0, 0, 1}, v[] = {2, 1, 2}, val[] = {2, 4, 3}, k = 2
Output:
2
0
0
For 0 node, the nodes directly connected to it are 1 and 2
having values 4 and 3 respectively, thus node with 2nd largest value is 2.
For 1 node, the nodes directly connected to it are 0 and 2
having values 2 and 3 respectively, thus node with 2nd largest value is 0.
For 2 node, the nodes directly connected to it are 0 and 1
having values 2 and 4 respectively, thus node with 2nd largest value is 0.
Input: u[] = {0, 2}, v[] = {2, 1}, val[] = {2, 4, 3}, k = 2
Output:
-1
-1
0
Approach: The idea is to store the nodes directly connected to a node along with their values in a vector and sort them in increasing order, and the kth largest value for a node, having n number of nodes directly connected to it, will be (n – k)th node from last.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findKthNode( int u[], int v[], int n, int val[], int V, int k)
{
vector<pair< int , int > > g[V];
for ( int i = 0; i < n; i++) {
g[u[i]].push_back(make_pair(val[v[i]], v[i]));
g[v[i]].push_back(make_pair(val[u[i]], u[i]));
}
for ( int i = 0; i < V; i++) {
if (g[i].size() > 0)
sort(g[i].begin(), g[i].end());
if (k <= g[i].size())
printf ( "%d\n" , g[i][g[i].size() - k].second);
else
printf ( "-1\n" );
}
return ;
}
int main()
{
int V = 3;
int val[] = { 2, 4, 3 };
int u[] = { 0, 0, 1 };
int v[] = { 2, 1, 2 };
int n = sizeof (u) / sizeof ( int );
int k = 2;
findKthNode(u, v, n, val, V, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first,second;
pair( int a, int b)
{
first = a;
second = b;
}
}
static void findKthNode( int u[], int v[], int n,
int val[], int V, int k)
{
Vector<Vector<pair >> g = new Vector<Vector<pair>>();
for ( int i = 0 ; i < V; i++)
g.add( new Vector<pair>());
for ( int i = 0 ; i < n; i++)
{
g.get(u[i]).add( new pair(val[v[i]], v[i]));
g.get(v[i]).add( new pair(val[u[i]], u[i]));
}
for ( int i = 0 ; i < V; i++)
{
if (g.get(i).size() > 0 )
Collections.sort(g.get(i), new Comparator<pair>()
{
public int compare(pair p1, pair p2)
{
return p1.first - p2.first;
}
});
if (k <= g.get(i).size())
System.out.printf( "%d\n" , g.get(i).get(g.get(i).size() - k).second);
else
System.out.printf( "-1\n" );
}
return ;
}
public static void main(String args[])
{
int V = 3 ;
int val[] = { 2 , 4 , 3 };
int u[] = { 0 , 0 , 1 };
int v[] = { 2 , 1 , 2 };
int n = u.length;
int k = 2 ;
findKthNode(u, v, n, val, V, k);
}
}
|
Python3
def findKthNode(u, v, n, val, V, k):
g = [[] for i in range (V)]
for i in range ( 0 , n):
g[u[i]].append((val[v[i]], v[i]))
g[v[i]].append((val[u[i]], u[i]))
for i in range ( 0 , V):
if len (g[i]) > 0 :
g[i].sort()
if k < = len (g[i]):
print (g[i][ - k][ 1 ])
else :
print ( "-1" )
return
if __name__ = = "__main__" :
V = 3
val = [ 2 , 4 , 3 ]
u = [ 0 , 0 , 1 ]
v = [ 2 , 1 , 2 ]
n, k = len (u), 2
findKthNode(u, v, n, val, V, k)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
class pair
{
public int first,second;
public pair( int a, int b)
{
first = a;
second = b;
}
}
class sortHelper : IComparer
{
int IComparer.Compare( object a, object b)
{
pair first = (pair)a;
pair second = (pair)b;
return first.first - second.first;
}
}
static void findKthNode( int []u, int []v, int n,
int []val, int V, int k)
{
ArrayList g = new ArrayList();
for ( int i = 0; i < V; i++)
g.Add( new ArrayList());
for ( int i = 0; i < n; i++)
{
((ArrayList)g[u[i]]).Add( new pair(val[v[i]], v[i]));
((ArrayList)g[v[i]]).Add( new pair(val[u[i]], u[i]));
}
for ( int i = 0; i < V; i++)
{
if (((ArrayList)g[i]).Count > 0)
{
ArrayList tmp = (ArrayList)g[i];
tmp.Sort( new sortHelper());
g[i] = tmp;
}
if (k <= ((ArrayList)g[i]).Count)
Console.Write(((pair)((ArrayList)g[i])[((ArrayList)g[i]).Count - k]).second+ "\n" );
else
Console.Write( "-1\n" );
}
return ;
}
public static void Main( string []args)
{
int V = 3;
int []val = { 2, 4, 3 };
int []u = { 0, 0, 1 };
int []v = { 2, 1, 2 };
int n = u.Length;
int k = 2;
findKthNode(u, v, n, val, V, k);
}
}
|
Javascript
<script>
function findKthNode(u, v, n, val, V, k)
{
var g = Array.from(Array(V), () => Array());
for ( var i = 0; i < n; i++)
{
g[u[i]].push([val[v[i]], v[i]]);
g[v[i]].push([val[u[i]], u[i]]);
}
for ( var i = 0; i < V; i++)
{
if (g[i].length > 0)
g[i].sort();
if (k <= g[i].length)
document.write(
g[i][g[i].length - k][1] + "<br>" );
else
document.write( "-1<br>" );
}
return ;
}
var V = 3;
var val = [ 2, 4, 3 ];
var u = [ 0, 0, 1 ];
var v = [ 2, 1, 2 ];
var n = u.length;
var k = 2;
findKthNode(u, v, n, val, V, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...