Given a positive integer K and a weighted undirected connected graph of N nodes and E edges as an array Edges[] of the type {u, v, W} representing the edges between Node u and Node v having weight W, the task is to find the shortest distance between the two given nodes S and D after reducing the cost of at most K edges to 0.
Examples:
Input: N = 5, K = 1, Edges[][] = {{0, 1, 1}, {0, 4, 1}, {1, 2, 2}, {2, 3, 4}, {4, 3, 7}}, s = 0, d = 3 Output: 1 Explanation: Below is the graph for the given test case:

There are 2 possible routes between 0 and 3 viz. {0->1->2->3} and {0->4->3} after reducing the distance of edge 4->3 to zero, the second route becomes 0->(4, 3) and hence the minimum distance is 1.
Input: N = 5, K = 2, Edges[][] = {{0, 1, 2}, {0, 2, 3}, {2, 1, 2}, {2, 3, 1}, {3, 1, 2}, {3, 4, 3}, {4, 2, 4}}, s = 0, d = 3 Ouput: 2
Approach: The given problem can be solved using DFS Traversal and storing all possible paths between the two given nodes. Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void dfs_all( int n, int s, int d,
vector<vector<pair< int , int > > >& graph,
vector< bool >& vis,
vector<vector< int > >& edge_path,
vector< int >& temp_edge)
{
if (s == d) {
edge_path.push_back(temp_edge);
return ;
}
vis[s] = true ;
int edges_in_a = graph[s].size();
for ( int i = 0; i < edges_in_a; i++) {
if (!vis[graph[s][i].first]) {
temp_edge.push_back(graph[s][i].second);
dfs_all(n, graph[s][i].first, d, graph, vis,
edge_path, temp_edge);
temp_edge.pop_back();
}
}
vis[s] = false ;
}
int getDistance(vector<vector< int > >& edge_path, int k)
{
int shortestDistance = INT_MAX;
if (edge_path.empty())
return -1;
for ( auto x : edge_path) {
if (k == x.size())
return 0;
sort(x.begin(), x.end(), greater< int >());
int sum = 0;
int ksum = 0;
for ( int i = 0; i < x.size(); i++) {
sum += x[i];
if (i < k)
ksum += x[i];
}
shortestDistance
= min(sum - ksum, shortestDistance);
}
return shortestDistance;
}
int solve(vector<vector<pair< int , int > > > graph, int n,
int k, int src, int dest)
{
vector<vector< int > > edge_path;
vector< int > temp_edge;
vector< bool > vis(n, false );
dfs_all(n, src, dest, graph, vis, edge_path, temp_edge);
return getDistance(edge_path, k);
}
int main()
{
int n = 5, e = 5, k = 1;
vector<vector<pair< int , int > > > graph(n);
graph[0].push_back(make_pair(1, 1));
graph[1].push_back(make_pair(0, 1));
graph[0].push_back(make_pair(4, 1));
graph[4].push_back(make_pair(0, 1));
graph[1].push_back(make_pair(2, 2));
graph[2].push_back(make_pair(1, 2));
graph[2].push_back(make_pair(3, 4));
graph[3].push_back(make_pair(2, 4));
graph[4].push_back(make_pair(3, 7));
graph[3].push_back(make_pair(4, 7));
int a = 0, b = 3;
cout << solve(graph, n, k, a, b);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Main {
static void dfsAll( int n, int s, int d,
List<List<Pair>> graph,
boolean [] vis,
List<List<Integer>> edgePath,
List<Integer> tempEdge) {
if (s == d) {
edgePath.add( new ArrayList<>(tempEdge));
return ;
}
vis[s] = true ;
int edgesInA = graph.get(s).size();
for ( int i = 0 ; i < edgesInA; i++) {
if (!vis[graph.get(s).get(i).first]) {
tempEdge.add(graph.get(s).get(i).second);
dfsAll(n, graph.get(s).get(i).first, d, graph, vis,
edgePath, tempEdge);
tempEdge.remove(tempEdge.size() - 1 );
}
}
vis[s] = false ;
}
static int getDistance(List<List<Integer>> edgePath, int k) {
int shortestDistance = Integer.MAX_VALUE;
if (edgePath.isEmpty()) {
return - 1 ;
}
for (List<Integer> x : edgePath) {
if (k == x.size()) {
return 0 ;
}
Collections.sort(x, Comparator.reverseOrder());
int sum = 0 ;
int kSum = 0 ;
for ( int i = 0 ; i < x.size(); i++) {
sum += x.get(i);
if (i < k) {
kSum += x.get(i);
}
}
shortestDistance = Math.min(sum - kSum, shortestDistance);
}
return shortestDistance;
}
static int solve(List<List<Pair>> graph, int n,
int k, int src, int dest) {
List<List<Integer>> edgePath = new ArrayList<>();
List<Integer> tempEdge = new ArrayList<>();
boolean [] vis = new boolean [n];
dfsAll(n, src, dest, graph, vis, edgePath, tempEdge);
return getDistance(edgePath, k);
}
public static void main(String[] args) {
int n = 5 , e = 5 , k = 1 ;
List<List<Pair>> graph = new ArrayList<>();
for ( int i = 0 ; i < n; i++) {
graph.add( new ArrayList<>());
}
graph.get( 0 ).add( new Pair( 1 , 1 ));
graph.get( 1 ).add( new Pair( 0 , 1 ));
graph.get( 0 ).add( new Pair( 4 , 1 ));
graph.get( 4 ).add( new Pair( 0 , 1 ));
graph.get( 1 ).add( new Pair( 2 , 2 ));
graph.get( 2 ).add( new Pair( 1 , 2 ));
graph.get( 2 ).add( new Pair( 3 , 4 ));
graph.get( 3 ).add( new Pair( 2 , 4 ));
graph.get( 4 ).add( new Pair( 3 , 7 ));
graph.get( 3 ).add( new Pair( 4 , 7 ));
int a = 0 , b = 3 ;
System.out.println(solve(graph, n, k, a, b));
}
}
class Pair {
int first, second;
Pair( int first, int second) {
this .first = first;
this .second = second;
}
}
|
Python3
def dfs_all(n, s, d, graph, vis, edge_path, temp_edge):
if s = = d:
edge_path.append(temp_edge)
return
vis[s] = True
edges_in_a = len (graph[s])
for i in range (edges_in_a):
if not vis[graph[s][i][ 0 ]]:
temp_edge.append(graph[s][i][ 1 ])
dfs_all(n, graph[s][i][ 0 ], d, graph, vis, edge_path, temp_edge)
temp_edge.pop()
vis[s] = False
def getDistance(edge_path, k):
shortestDistance = float ( 'inf' )
if not edge_path:
return - 1
for x in edge_path:
if k = = len (x):
return 0
x.sort(reverse = True )
sum = 1
ksum = 0
for i in range ( len (x)):
sum + = x[i]
if i < k:
ksum + = x[i]
shortestDistance = min ( sum - ksum, shortestDistance)
return shortestDistance
def solve(graph, n, k, src, dest):
edge_path = []
temp_edge = []
vis = [ False for _ in range (n)]
dfs_all(n, src, dest, graph, vis, edge_path, temp_edge)
return getDistance(edge_path, k)
n = 5
e = 5
k = 1
graph = [[] for _ in range (n)]
graph[ 0 ].append([ 1 , 1 ])
graph[ 1 ].append(( 0 , 1 ))
graph[ 0 ].append(( 4 , 1 ))
graph[ 4 ].append(( 0 , 1 ))
graph[ 1 ].append(( 2 , 1 ))
graph[ 2 ].append(( 1 , 1 ))
graph[ 1 ].append(( 3 , 3 ))
graph[ 3 ].append(( 1 , 3 ))
graph[ 2 ].append(( 4 , 1 ))
graph[ 4 ].append(( 2 , 1 ))
src = 0
dest = 4
print (solve(graph, n, k, src, dest))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Graph {
private List<List<Tuple< int , int > > > adjList;
public Graph( int n)
{
adjList = new List<List<Tuple< int , int > > >(n);
for ( int i = 0; i < n; i++) {
adjList.Add( new List<Tuple< int , int > >());
}
}
public void AddEdge( int u, int v, int w)
{
adjList[u].Add(Tuple.Create(v, w));
}
public List<List< int > > GetAllPaths( int s, int d)
{
var vis = new bool [adjList.Count];
var tempEdge = new List< int >();
var edgePaths = new List<List< int > >();
DFSAll(s, d, vis, edgePaths, tempEdge);
return edgePaths;
}
private void DFSAll( int s, int d, bool [] vis,
List<List< int > > edgePaths,
List< int > tempEdge)
{
if (s == d) {
edgePaths.Add( new List< int >(tempEdge));
return ;
}
vis[s] = true ;
foreach ( var edge in adjList[s])
{
if (!vis[edge.Item1]) {
tempEdge.Add(edge.Item2);
DFSAll(edge.Item1, d, vis, edgePaths,
tempEdge);
tempEdge.RemoveAt(tempEdge.Count - 1);
}
}
vis[s] = false ;
}
public int
GetMinimumSumOfEdges(List<List< int > > edgePaths, int k)
{
if (edgePaths.Count == 0) {
return -1;
}
int shortestDistance = int .MaxValue;
foreach ( var edgePath in edgePaths)
{
if (k == edgePath.Count) {
return 0;
}
edgePath.Sort((a, b) = > b.CompareTo(a));
int sum = 0;
int ksum = 0;
for ( int i = 0; i < edgePath.Count; i++) {
sum += edgePath[i];
if (i < k) {
ksum += edgePath[i];
}
}
shortestDistance
= Math.Min(shortestDistance, sum - ksum);
}
return shortestDistance;
}
public int Solve( int n, int k, int src, int dest)
{
var edgePaths = GetAllPaths(src, dest);
return GetMinimumSumOfEdges(edgePaths, k);
}
}
class Program {
static void Main( string [] args)
{
int n = 5, e = 5, k = 1;
var graph = new Graph(n);
graph.AddEdge(0, 1, 1);
graph.AddEdge(1, 0, 1);
graph.AddEdge(0, 4, 1);
graph.AddEdge(4, 0, 1);
graph.AddEdge(1, 2, 2);
graph.AddEdge(2, 1, 2);
graph.AddEdge(2, 3, 4);
graph.AddEdge(3, 2, 4);
graph.AddEdge(4, 3, 7);
graph.AddEdge(3, 4, 7);
int a = 0, b = 3;
Console.WriteLine(graph.Solve(n, k, a, b));
}
}
|
Javascript
function dfs_all(n, s, d, graph, vis, edge_path, temp_edge){
if (s === d) {
edge_path.push(temp_edge);
return ;
}
vis[s] = true ;
let edges_in_a = graph[s].length;
for (let i = 0; i < edges_in_a; i++) {
if (!vis[graph[s][i][0]]) {
temp_edge.push(graph[s][i][1]);
dfs_all(n, graph[s][i][0], d, graph, vis, edge_path, temp_edge);
temp_edge.pop();
}
}
vis[s] = false ;
}
function getDistance(edge_path, k){
let shortestDistance = Infinity;
if (edge_path.length === 0) {
return -1;
}
for (let x of edge_path) {
if (k === x.length) {
return 0;
}
x.sort( function (a, b) { return b - a; });
let sum = 1;
let ksum = 0;
for (let i = 0; i < x.length; i++) {
sum += x[i];
if (i < k) {
ksum += x[i];
}
}
shortestDistance = Math.min(sum - ksum, shortestDistance);
}
return shortestDistance;
}
function solve(graph, n, k, src, dest){
let edge_path = [];
let temp_edge = [];
let vis = new Array(n).fill( false );
dfs_all(n, src, dest, graph, vis, edge_path, temp_edge);
return getDistance(edge_path, k);
}
let n = 5;
let e = 5;
let k = 1;
let graph = [];
for (let i = 0; i < n; i++) {
graph[i] = [];
}
graph[0].push([1, 1]);
graph[1].push([0, 1]);
graph[0].push([4, 1]);
graph[4].push([0, 1]);
graph[1].push([2, 1]);
graph[2].push([1, 1]);
graph[1].push([3, 3]);
graph[3].push([1, 3]);
graph[2].push([4, 1]);
graph[4].push([2, 1]);
let src = 0;
let dest = 4;
console.log(solve(graph, n, k, src, dest));
|
Time Complexity: O((N*log N)NN) Auxiliary Space: O(N2)
Efficient Approach: The above approach can also be optimized at the step where sorting is performed after finding all possible paths. Instead of sorting, the idea is to use MinHeap to calculate the sum of K largest weights in the graph to reduce the time complexity to O(N*log K) for that steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void dfs_all( int n, int s, int d,
vector<vector<pair< int , int > > >& graph,
vector< bool >& vis,
vector<vector< int > >& edge_path,
vector< int >& temp_edge)
{
if (s == d) {
edge_path.push_back(temp_edge);
return ;
}
vis[s] = true ;
int edges_in_a = graph[s].size();
for ( int i = 0; i < edges_in_a; i++) {
if (!vis[graph[s][i].first]) {
temp_edge.push_back(
graph[s][i].second);
dfs_all(n, graph[s][i].first,
d, graph, vis,
edge_path, temp_edge);
temp_edge.pop_back();
}
}
vis[s] = false ;
}
int getDistance(
vector<vector< int > >& edge_path, int k)
{
int shortestDistance = INT_MAX;
if (edge_path.empty())
return -1;
for ( auto x : edge_path) {
if (k == x.size())
return 0;
priority_queue< int , vector< int >,
greater< int > >
minHeap;
int sum = 0;
int ksum = 0;
for ( int i = 0; i < x.size(); i++) {
sum += x[i];
ksum += x[i];
minHeap.push(x[i]);
if (minHeap.size() > k) {
ksum -= minHeap.top();
minHeap.pop();
}
}
shortestDistance
= min(sum - ksum, shortestDistance);
}
return shortestDistance;
}
int solve(
vector<vector<pair< int , int > > > graph,
int n, int k, int src, int dest)
{
vector<vector< int > > edge_path;
vector< int > temp_edge;
vector< bool > vis(n, false );
dfs_all(n, src, dest, graph,
vis, edge_path, temp_edge);
return getDistance(edge_path, k);
}
int main()
{
int n = 5, e = 5, k = 1;
vector<vector<pair< int , int > > > graph(n);
graph[0].push_back(make_pair(1, 1));
graph[1].push_back(make_pair(0, 1));
graph[0].push_back(make_pair(4, 1));
graph[4].push_back(make_pair(0, 1));
graph[1].push_back(make_pair(2, 2));
graph[2].push_back(make_pair(1, 2));
graph[2].push_back(make_pair(3, 4));
graph[3].push_back(make_pair(2, 4));
graph[4].push_back(make_pair(3, 7));
graph[3].push_back(make_pair(4, 7));
int a = 0, b = 3;
cout << solve(graph, n, k, a, b);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void dfs_all( int n, int s, int d,
ArrayList<ArrayList<Map.Entry<Integer, Integer>>> graph,
boolean [] vis,
ArrayList<ArrayList<Integer>> edge_path,
ArrayList<Integer> temp_edge) {
if (s == d) {
edge_path.add( new ArrayList<>(temp_edge));
return ;
}
vis[s] = true ;
int edges_in_a = graph.get(s).size();
for ( int i = 0 ; i < edges_in_a; i++) {
if (!vis[graph.get(s).get(i).getKey()]) {
temp_edge.add(graph.get(s).get(i).getValue());
dfs_all(n, graph.get(s).get(i).getKey(),
d, graph, vis,
edge_path, temp_edge);
temp_edge.remove(temp_edge.size() - 1 );
}
}
vis[s] = false ;
}
static int getDistance(
ArrayList<ArrayList<Integer>> edge_path, int k) {
int shortestDistance = Integer.MAX_VALUE;
if (edge_path.isEmpty())
return - 1 ;
for (ArrayList<Integer> x : edge_path) {
if (k == x.size())
return 0 ;
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
int sum = 0 ;
int ksum = 0 ;
for ( int i = 0 ; i < x.size(); i++) {
sum += x.get(i);
ksum += x.get(i);
minHeap.add(x.get(i));
if (minHeap.size() > k) {
ksum -= minHeap.peek();
minHeap.poll();
}
}
shortestDistance
= Math.min(sum - ksum, shortestDistance);
}
return shortestDistance;
}
static int solve(
ArrayList<ArrayList<Map.Entry<Integer, Integer>>> graph,
int n, int k, int src, int dest) {
ArrayList<ArrayList<Integer>> edge_path = new ArrayList<>();
ArrayList<Integer> temp_edge = new ArrayList<>();
boolean [] vis = new boolean [n];
dfs_all(n, src, dest, graph,
vis, edge_path, temp_edge);
return getDistance(edge_path, k);
}
public static void main(String[] args) {
int n = 5 , e = 5 , k = 1 ;
ArrayList<ArrayList<Map.Entry<Integer, Integer>>> graph = new ArrayList<>();
for ( int i = 0 ; i < n; i++) {
graph.add( new ArrayList<>());
}
graph.get( 0 ).add( new AbstractMap.SimpleEntry<>( 1 , 1 ));
graph.get( 1 ).add( new AbstractMap.SimpleEntry<>( 0 , 1 ));
graph.get( 0 ).add( new AbstractMap.SimpleEntry<>( 4 , 1 ));
graph.get( 4 ).add( new AbstractMap.SimpleEntry<>( 0 , 1 ));
graph.get( 1 ).add( new AbstractMap.SimpleEntry<>( 2 , 2 ));
graph.get( 2 ).add( new AbstractMap.SimpleEntry<>( 1 , 2 ));
graph.get( 2 ).add( new AbstractMap.SimpleEntry<>( 3 , 4 ));
graph.get( 3 ).add( new AbstractMap.SimpleEntry<>( 2 , 4 ));
graph.get( 4 ).add( new AbstractMap.SimpleEntry<>( 3 , 7 ));
graph.get( 3 ).add( new AbstractMap.SimpleEntry<>( 4 , 7 ));
int a = 0 , b = 3 ;
System.out.println(solve(graph, n, k, a, b));
}
}
|
Python3
def dfs_all(n, s, d, graph, vis, edge_path, temp_edge):
if s = = d:
edge_path.append(temp_edge[:])
return
vis[s] = True
edges_in_a = len (graph[s])
for i in range (edges_in_a):
if not vis[graph[s][i][ 0 ]]:
temp_edge.append(graph[s][i][ 1 ])
dfs_all(n, graph[s][i][ 0 ], d, graph, vis, edge_path, temp_edge)
temp_edge.pop()
vis[s] = False
def get_distance(edge_path, k):
shortest_distance = float ( 'inf' )
if not edge_path:
return - 1
for x in edge_path:
if k = = len (x):
return 0
min_heap = []
sum_val = 0
k_sum = 0
for i in range ( len (x)):
sum_val + = x[i]
k_sum + = x[i]
min_heap.append(x[i])
if len (min_heap) > k:
k_sum - = min_heap.pop(min_heap.index( min (min_heap)))
shortest_distance = min (sum_val - k_sum, shortest_distance)
return shortest_distance
def solve(graph, n, k, src, dest):
edge_path = []
temp_edge = []
vis = [ False ] * n
dfs_all(n, src, dest, graph, vis, edge_path, temp_edge)
return get_distance(edge_path, k)
n, e, k = 5 , 5 , 1
graph = [[] for _ in range (n)]
graph[ 0 ].append(( 1 , 1 ))
graph[ 1 ].append(( 0 , 1 ))
graph[ 0 ].append(( 4 , 1 ))
graph[ 4 ].append(( 0 , 1 ))
graph[ 1 ].append(( 2 , 2 ))
graph[ 2 ].append(( 1 , 2 ))
graph[ 2 ].append(( 3 , 4 ))
graph[ 3 ].append(( 2 , 4 ))
graph[ 4 ].append(( 3 , 7 ))
graph[ 3 ].append(( 4 , 7 ))
a, b = 0 , 3
print (solve(graph, n, k, a, b))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void DfsAll( int n, int s, int d, List<List<Tuple< int , int >>> graph, bool [] vis, List<List< int >> edgePath, List< int > tempEdge)
{
if (s == d)
{
edgePath.Add( new List< int >(tempEdge));
return ;
}
vis[s] = true ;
int edgesInA = graph[s].Count;
for ( int i = 0; i < edgesInA; i++)
{
if (!vis[graph[s][i].Item1])
{
tempEdge.Add(graph[s][i].Item2);
DfsAll(n, graph[s][i].Item1, d, graph, vis, edgePath, tempEdge);
tempEdge.RemoveAt(tempEdge.Count - 1);
}
}
vis[s] = false ;
}
static int GetDistance(List<List< int >> edgePath, int k)
{
int shortestDistance = int .MaxValue;
if (edgePath.Count == 0)
return -1;
foreach ( var x in edgePath)
{
if (k == x.Count)
return 0;
SortedSet< int > minHeap = new SortedSet< int >();
int sum = 0;
int kSum = 0;
for ( int i = 0; i < x.Count; i++)
{
sum += x[i];
kSum += x[i];
minHeap.Add(x[i]);
if (minHeap.Count > k)
{
kSum -= minHeap.Min;
minHeap.Remove(minHeap.Min);
}
}
shortestDistance = Math.Min(sum - kSum, shortestDistance);
}
return shortestDistance;
}
static int Solve(List<List<Tuple< int , int >>> graph, int n, int k, int src, int dest)
{
List<List< int >> edgePath = new List<List< int >>();
List< int > tempEdge = new List< int >();
bool [] vis = new bool [n];
DfsAll(n, src, dest, graph, vis, edgePath, tempEdge);
return GetDistance(edgePath, k);
}
static void Main()
{
int n = 5, k = 1;
List<List<Tuple< int , int >>> graph = new List<List<Tuple< int , int >>>(n);
for ( int i = 0; i < n; i++)
graph.Add( new List<Tuple< int , int >>());
graph[0].Add( new Tuple< int , int >(1, 1));
graph[1].Add( new Tuple< int , int >(0, 1));
graph[0].Add( new Tuple< int , int >(4, 1));
graph[4].Add( new Tuple< int , int >(0, 1));
graph[1].Add( new Tuple< int , int >(2, 2));
graph[2].Add( new Tuple< int , int >(1, 2));
graph[2].Add( new Tuple< int , int >(3, 4));
graph[3].Add( new Tuple< int , int >(2, 4));
graph[4].Add( new Tuple< int , int >(3, 7));
graph[3].Add( new Tuple< int , int >(4, 7));
int a = 0, b = 3;
Console.WriteLine(Solve(graph, n, k, a, b));
}
}
|
Javascript
function GFG(n, s, d, graph, vis, edge_path, temp_edge) {
if (s === d) {
edge_path.push([...temp_edge]);
return ;
}
vis[s] = true ;
const edges_in_a = graph[s].length;
for (let i = 0; i < edges_in_a; i++) {
if (!vis[graph[s][i][0]]) {
temp_edge.push(graph[s][i][1]);
GFG(n, graph[s][i][0], d, graph, vis, edge_path, temp_edge);
temp_edge.pop();
}
}
vis[s] = false ;
}
function get_distance(edge_path, k) {
let shortest_distance = Infinity;
if (edge_path.length === 0) {
return -1;
}
for (const x of edge_path) {
if (k === x.length) {
return 0;
}
const min_heap = [];
let sum_val = 0;
let k_sum = 0;
for (let i = 0; i < x.length; i++) {
sum_val += x[i];
k_sum += x[i];
min_heap.push(x[i]);
if (min_heap.length > k) {
const minIndex = min_heap.indexOf(Math.min(...min_heap));
k_sum -= min_heap.splice(minIndex, 1)[0];
}
}
shortest_distance = Math.min(sum_val - k_sum, shortest_distance);
}
return shortest_distance;
}
function solve(graph, n, k, src, dest) {
const edge_path = [];
const temp_edge = [];
const vis = new Array(n).fill( false );
GFG(n, src, dest, graph, vis, edge_path, temp_edge);
return get_distance(edge_path, k);
}
const n = 5, e = 5, k = 1;
const graph = Array.from({ length: n }, () => []);
graph[0].push([1, 1]);
graph[1].push([0, 1]);
graph[0].push([4, 1]);
graph[4].push([0, 1]);
graph[1].push([2, 2]);
graph[2].push([1, 2]);
graph[2].push([3, 4]);
graph[3].push([2, 4]);
graph[4].push([3, 7]);
graph[3].push([4, 7]);
const a = 0, b = 3;
console.log(solve(graph, n, k, a, b));
|
Time Complexity: O((N*log K)NN) Auxiliary Space: O(N2)
|