^{th}largest weight in a Graph

Given a weighted graph consisting of **N** nodes and **M** edges, a **source** vertex, a **destination** vertex, and an integer **K**, the task is to find the path with **K ^{th} **largest weight from

**source**to

**destination**in the graph.

**Examples:**

Input:N = 7, M = 8, source = 0, destination = 6, K = 3, Edges[][] = {{0, 1, 10}, {1, 2, 10}, {2, 3, 10}, {0, 3, 40}, {3, 4, 2}, {4, 5, 3}, {5, 6, 3}, {4, 6, 8}, {2, 5, 5}}Output:0 1 2 3 4 6Explanation:A total of 4 paths exists from the source to the destination:

Path: 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6. Weight = 38.

Path: 0 ->1 -> 2 -> 3 -> 6. Weight = 40.

Path: 0 -> 3 -> 4 -> 5 -> 6. Weight = 48.

Path: 0 -> 3 -> 4 -> 6. Weight = 50.

The 3^{rd}largest weighted path is the path with weight 40. Hence, the path having weight 40 is the output.

Input:N = 2, M = 1, source = 0, destination = 1, K = 1, Edges[][] = {{0 1 25}},Output:0 1

**Approach:** The given problem can be solved by finding all the paths from a given source to a destination and using a Priority Queue to find the K^{th} largest weight. Follow the steps below to solve the problem:

- Initialize an adjacency list to create a graph from the given set of edges
**Edges[][]**. - Initialize a Max-Heap using a
**priority queue**, say**PQ**as a Min-Heap of size**K**, to store**path weight**and**path**as a string from source to destination. - Initialize an array
**visited[]**, to mark if a vertex is visited or not. - Traverse the graph to find all paths from source to destination.
- Create a utility class Pair as
**psf**and**wsf**, for maintaining path and weight obtained so far respectively. - In the priority queue, perform the following operations:
- If the queue has less than
**K**elements, add the pair to the queue. - Otherwise, if the current weight of the path is greater than the weight of the pair at the front of the queue, then remove the front element, and add the new pair to the queue.

- If the queue has less than
- After completing the above steps, the element at the top of the priority queue gives the pair containing the
**K**largest weight path. Therefore, print that path.^{th}

Below is the implementation of the above approach:

## Java

`// Java program for the above approach` ` ` `import` `java.io.*;` `import` `java.util.*;` ` ` `// Edge class represents a source,` `// destination, weight of the edge` `class` `Edge {` ` ` ` ` `// Source` ` ` `int` `src;` ` ` ` ` `// Destination` ` ` `int` `nbr;` ` ` ` ` `// Weight` ` ` `int` `wt;` ` ` ` ` `// Constructor to create an Edge` ` ` `Edge(` `int` `src, ` `int` `nbr, ` `int` `wt)` ` ` `{` ` ` `this` `.src = src;` ` ` `this` `.nbr = nbr;` ` ` `this` `.wt = wt;` ` ` `}` `}` ` ` `// Pair class` `class` `Pair ` `implements` `Comparable<Pair> {` ` ` ` ` `// Weight so far` ` ` `int` `wsf;` ` ` ` ` `// Path so far` ` ` `String psf;` ` ` ` ` `// Constructor to create a Pair` ` ` `Pair(` `int` `wsf, String psf)` ` ` `{` ` ` `this` `.wsf = wsf;` ` ` `this` `.psf = psf;` ` ` `}` ` ` ` ` `// Function to sort in increasing` ` ` `// order of weights` ` ` `public` `int` `compareTo(Pair o)` ` ` `{` ` ` `return` `this` `.wsf - o.wsf;` ` ` `}` `}` ` ` `class` `GFG {` ` ` ` ` `// Intializing the priority queue` ` ` `static` `PriorityQueue<Pair> pq` ` ` `= ` `new` `PriorityQueue<>();` ` ` ` ` `// Function to find the path from src to` ` ` `// dest with Kth largest weight in the graph` ` ` `public` `static` `void` `kthLargest(` ` ` `ArrayList<Edge>[] graph,` ` ` `int` `src, ` `int` `dest,` ` ` `boolean` `[] visited, ` `int` `k,` ` ` `String psf, ` `int` `wsf)` ` ` `{` ` ` `// Base Case: When the` ` ` `// destination has been reached` ` ` `if` `(src == dest) {` ` ` ` ` `// If pq has at most K elements` ` ` `if` `(pq.size() < k) {` ` ` `pq.add(` `new` `Pair(wsf, psf));` ` ` `}` ` ` ` ` `else` `if` `(wsf > pq.peek().wsf) {` ` ` `pq.remove();` ` ` `pq.add(` `new` `Pair(wsf, psf));` ` ` `}` ` ` ` ` `return` `;` ` ` `}` ` ` ` ` `// Mark the source node as visited` ` ` `visited[src] = ` `true` `;` ` ` ` ` `// Iterating over all` ` ` `// the neighbours of src` ` ` `for` `(Edge e : graph[src]) {` ` ` ` ` `// If neighbour is not visited` ` ` `if` `(!visited[e.nbr]) {` ` ` ` ` `kthLargest(graph, e.nbr,` ` ` `dest, visited,` ` ` `k, psf + e.nbr,` ` ` `wsf + e.wt);` ` ` `}` ` ` `}` ` ` ` ` `// Mark src as unvisited` ` ` `visited[src] = ` `false` `;` ` ` `}` ` ` ` ` `// Function to add edges` ` ` `public` `static` `void` `addEdges(` ` ` `ArrayList<Edge>[] graph)` ` ` `{` ` ` `// Adding a bidirectional edge` ` ` `graph[` `0` `].add(` `new` `Edge(` `0` `, ` `1` `, ` `10` `));` ` ` `graph[` `1` `].add(` `new` `Edge(` `1` `, ` `0` `, ` `10` `));` ` ` ` ` `graph[` `1` `].add(` `new` `Edge(` `1` `, ` `2` `, ` `10` `));` ` ` `graph[` `2` `].add(` `new` `Edge(` `2` `, ` `1` `, ` `10` `));` ` ` ` ` `graph[` `2` `].add(` `new` `Edge(` `2` `, ` `3` `, ` `10` `));` ` ` `graph[` `3` `].add(` `new` `Edge(` `3` `, ` `2` `, ` `10` `));` ` ` ` ` `graph[` `0` `].add(` `new` `Edge(` `0` `, ` `3` `, ` `40` `));` ` ` `graph[` `3` `].add(` `new` `Edge(` `3` `, ` `0` `, ` `40` `));` ` ` ` ` `graph[` `3` `].add(` `new` `Edge(` `3` `, ` `4` `, ` `2` `));` ` ` `graph[` `4` `].add(` `new` `Edge(` `4` `, ` `3` `, ` `2` `));` ` ` ` ` `graph[` `4` `].add(` `new` `Edge(` `4` `, ` `5` `, ` `3` `));` ` ` `graph[` `5` `].add(` `new` `Edge(` `5` `, ` `4` `, ` `3` `));` ` ` ` ` `graph[` `5` `].add(` `new` `Edge(` `5` `, ` `6` `, ` `3` `));` ` ` `graph[` `6` `].add(` `new` `Edge(` `6` `, ` `5` `, ` `3` `));` ` ` ` ` `graph[` `4` `].add(` `new` `Edge(` `4` `, ` `6` `, ` `8` `));` ` ` `graph[` `6` `].add(` `new` `Edge(` `6` `, ` `4` `, ` `8` `));` ` ` `}` ` ` ` ` `// Utility function to find the` ` ` `// path with Kth largest weight` ` ` `public` `static` `void` `kthLargestPathUtil(` ` ` `int` `N, ` `int` `M, ` `int` `src,` ` ` `int` `dest, ` `int` `k)` ` ` `{` ` ` `@SuppressWarnings` `(` `"unchecked"` `)` ` ` ` ` `// Arraylist to store edges` ` ` `ArrayList<Edge>[] graph` ` ` `= ` `new` `ArrayList[` `2` `* N];` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `2` `* N; i++) {` ` ` `graph[i] = ` `new` `ArrayList<>();` ` ` `}` ` ` ` ` `// Function to add edges` ` ` `addEdges(graph);` ` ` ` ` `// Stores if a vertex is visited or not` ` ` `boolean` `[] visited = ` `new` `boolean` `[N];` ` ` ` ` `kthLargest(graph, src, dest,` ` ` `visited, k, src + ` `""` `,` ` ` `0` `);` ` ` ` ` `// Stores the kth largest weighted path` ` ` `String path = pq.peek().psf;` ` ` ` ` `// Traversing path string` ` ` `for` `(` `int` `i = ` `0` `;` ` ` `i < path.length(); i++) {` ` ` `System.out.print(` ` ` `path.charAt(i) + ` `" "` `);` ` ` `}` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `// No of vertices and Edges` ` ` `int` `N = ` `7` `, M = ` `8` `;` ` ` ` ` `// Source vertex` ` ` `int` `src = ` `0` `;` ` ` ` ` `// Destination vertex` ` ` `int` `dest = ` `6` `;` ` ` `int` `k = ` `3` `;` ` ` ` ` `kthLargestPathUtil(N, M, src,` ` ` `dest, k);` ` ` `}` `}` |

**Time Complexity:** O(V + E)**Auxiliary Space:** O(V)