# k’th heaviest adjacent node in a graph where each vertex has weight

Given a positive number **k** and an undirected graph of **N** nodes, numbered from 0 to N-1, each having a weight associated with it. Note that this is different from a normal weighted graph where every edge has a weight.

For each node, if we sort the nodes (according to their weights), which are directly connected to it, in decreasing order, then what will be the number of the node at the **kth** position. Print kth node number(not weight) for each node and if it does not exist, print -1.

**Examples:**

Input :N = 3, k = 2, wt[] = { 2, 4, 3 }. edge 1: 0 2 edge 2: 0 1 edge 3: 1 2Output :2 0 0 Graph: 0 (weight 2) / \ / \ 1-----2 (weight 4) (weight 3) For node0, sorted (decreasing order) nodes according to their weights are node 1(weight 4), node 2(weight 3). The node at 2nd position for node 0 is node 2. For node1, sorted (decreasing order) nodes according to their weight are node 2(weight 3), node 0(weight 2). The node at 2nd position for node 1 is node 0. For node2, sorted (decreasing order) nodes according to their weight are node 1(weight 4), node 0(weight 2). The node at 2nd position for node 2 is node 0.

The idea is to sort Adjacency List of each node on the basis of adjacent node weights.

First, create Adjacency List for all the nodes. Now for each node, all the nodes which are directly connected to it stored in a list. In adjacency list, store the nodes along with their weights.

Now, for each node sort the weights of all nodes which are directly connected to it in reverse order, and then print the node number which is at kth position in the list of each node.

Below is implementation of this approach:

## C++

`// C++ program to find Kth node weight after s ` `// orting of nodes directly connected to a node. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Print Kth node number for each node after sorting ` `// connected node according to their weight. ` `void` `printkthnode(vector< pair<` `int` `, ` `int` `> > adj[], ` ` ` `int` `wt[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Sort Adjacency List of all node on the basis ` ` ` `// of its weight. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sort(adj[i].begin(), adj[i].end()); ` ` ` ` ` `// Printing Kth node for each node. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(adj[i].size() >= k) ` ` ` `cout << adj[i][adj[i].size() - k].second; ` ` ` `else` ` ` `cout << ` `"-1"` `; ` ` ` `} ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 3, k = 2; ` ` ` `int` `wt[] = { 2, 4, 3 }; ` ` ` ` ` `// Making adjacency list, storing the nodes ` ` ` `// along with their weight. ` ` ` `vector< pair<` `int` `, ` `int` `> > adj[n+1]; ` ` ` ` ` `adj[0].push_back(make_pair(wt[2], 2)); ` ` ` `adj[2].push_back(make_pair(wt[0], 0)); ` ` ` ` ` `adj[0].push_back(make_pair(wt[1], 1)); ` ` ` `adj[1].push_back(make_pair(wt[0], 0)); ` ` ` ` ` `adj[1].push_back(make_pair(wt[2], 2)); ` ` ` `adj[2].push_back(make_pair(wt[1], 1)); ` ` ` ` ` `printkthnode(adj, wt, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find Kth node weight after s ` `// orting of nodes directly connected to a node. ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// pair class ` ` ` `static` `class` `pair ` ` ` `{ ` ` ` `int` `first, second; ` ` ` ` ` `pair(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `first = a; ` ` ` `second = b; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print Kth node number for each node after sorting ` ` ` `// connected node according to their weight. ` ` ` `static` `void` `printkthnode(Vector<pair> adj[], ` `int` `wt[], ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// Sort Adjacency List of all node on the basis ` ` ` `// of its weight. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `Collections.sort(adj[i], ` `new` `Comparator<pair>() ` ` ` `{ ` ` ` `public` `int` `compare(pair p1, pair p2) ` ` ` `{ ` ` ` `return` `p1.first - p2.first; ` ` ` `} ` ` ` `}); ` ` ` ` ` `// Printing Kth node for each node. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(adj[i].size() >= k) ` ` ` `System.out.print(adj[i].get(adj[i].size() - ` ` ` `k).second + ` `" "` `); ` ` ` `else` ` ` `System.out.print(` `"-1"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `, k = ` `2` `; ` ` ` `int` `wt[] = { ` `2` `, ` `4` `, ` `3` `}; ` ` ` ` ` `// Making adjacency list, storing the nodes ` ` ` `// along with their weight. ` ` ` `Vector<pair>[] adj = ` `new` `Vector[n + ` `1` `]; ` ` ` `for` `(` `int` `i = ` `0` `; i < n + ` `1` `; i++) ` ` ` `adj[i] = ` `new` `Vector<pair>(); ` ` ` ` ` `adj[` `0` `].add(` `new` `pair(wt[` `2` `], ` `2` `)); ` ` ` `adj[` `2` `].add(` `new` `pair(wt[` `0` `], ` `0` `)); ` ` ` ` ` `adj[` `0` `].add(` `new` `pair(wt[` `1` `], ` `1` `)); ` ` ` `adj[` `1` `].add(` `new` `pair(wt[` `0` `], ` `0` `)); ` ` ` ` ` `adj[` `1` `].add(` `new` `pair(wt[` `2` `], ` `2` `)); ` ` ` `adj[` `2` `].add(` `new` `pair(wt[` `1` `], ` `1` `)); ` ` ` ` ` `printkthnode(adj, wt, n, k); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find Kth node ` `# weight after sorting of nodes ` `# directly connected to a node. ` ` ` `# PrKth node number for each node ` `# after sorting connected node ` `# according to their weight. ` `def` `printkthnode(adj, wt, n, k): ` ` ` ` ` `# Sort Adjacency List of all ` ` ` `# node on the basis of its weight. ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `adj[i].sort() ` ` ` ` ` `# Printing Kth node for each node. ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(` `len` `(adj[i]) >` `=` `k): ` ` ` `print` `(adj[i][` `len` `(adj[i]) ` `-` ` ` `k][` `1` `], end ` `=` `" "` `) ` ` ` `else` `: ` ` ` `print` `(` `"-1"` `, end ` `=` `" "` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `3` ` ` `k ` `=` `2` ` ` `wt ` `=` `[` `2` `, ` `4` `, ` `3` `] ` ` ` ` ` `# Making adjacency list, storing ` ` ` `# the nodes along with their weight. ` ` ` `adj ` `=` `[[] ` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `adj[` `0` `].append([wt[` `2` `], ` `2` `]) ` ` ` `adj[` `2` `].append([wt[` `0` `], ` `0` `]) ` ` ` ` ` `adj[` `0` `].append([wt[` `1` `], ` `1` `]) ` ` ` `adj[` `1` `].append([wt[` `0` `], ` `0` `]) ` ` ` ` ` `adj[` `1` `].append([wt[` `2` `], ` `2` `]) ` ` ` `adj[` `2` `].append([wt[` `1` `], ` `1` `]) ` ` ` ` ` `printkthnode(adj, wt, n, k) ` ` ` `# This code is contributed by PranchalK ` |

*chevron_right*

*filter_none*

**Output:**

2 0 0

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Find the Degree of a Particular vertex in a Graph
- Find a Mother Vertex in a Graph
- Find dependencies of each Vertex in a Directed Graph
- All vertex pairs connected with exactly k edges in a graph
- Find a Mother vertex in a Graph using Bit Masking
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Add and Remove vertex in Adjacency List representation of Graph
- Topological Sort of a graph using departure time of vertex
- Finding minimum vertex cover size of a graph using binary search
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Check if there is a cycle with odd weight sum in an undirected graph
- Find minimum weight cycle in an undirected graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Find weight of MST in a complete graph with edge-weights either 0 or 1