Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries

Given a graph with N nodes, a node S and Q queries each consisting of a node D and K, the task is to find the shortest path consisting of exactly K edges from node S to node D for each query. If no such path exists then print -1.

Note: K will always be lesser than 2 * N.

Examples:



Input: N = 3, edges[][] = {{1, 2, 5}, {2, 3, 3}, {3, 1, 4}}, S = 1, Q = {{1, 0}, {2, 1}, {3, 1}, {3, 2}, {3, 5}}
Output: 0 5 -1 8 20
1. The shortest path from 1 to 1 using 0 edge will be 0.
2. The shortest path from 1 to 2 using 1 edge will be 5 i.e 1->2.
3. No path of 1 edge exists between nodes 1 and 3.
4. The shortest path from 1 to 3 using 2 edges will be 8 i.e 1->2->3.
5. The shortest path from 1 to 3 using 5 edges will be 20 i.e 1->2->3->1->2->3.

Input: N = 4, edges[][] = {{1, 2, 8}, {2, 3, 5}, {3, 4, 7}}, S = 1, Q = {{1, 0}, {2, 1}, {3, 1}, {3, 2}, {4, 5}}
Output: 0 8 -1 13 -1

Approach:

  • This problem can be solved with the help of dynamic programming to create a linear solution.
  • Initialise a 2-d array, dp[N][2*N] with initial value as ‘inf’ except dp[S][0] as 0.
  • Pre-process the graph to find the shortest distance of each and every node from the source for every edge length between {0 to N-1}. The array dp[][] will be used to store the results of the pre processing.
  • For the pre-processing, run a loop for J in range [1, 2*N-1] to find the dp[X][J] for each edge in the graph, where dp[X][J] be the shortest path from node ‘S’ to node ‘X’ using exactly ‘J’ edges in total.
  • We can find dp[X][J+1] with the help of a recurrence relation:

    dp[ edge.second ][ i ] = min(dp[ edge.second ][ i ], dp[ edge.first ][ i-1 ] + weight(edge))

  • For every query in Q, if(dp[X][k] == inf) then return -1, else return dp[X][k]

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
  
#include <bits/stdc++.h>
using namespace std;
  
#define inf 100000000
  
// Function to find the shortest path
// between S and D with exactly K edges
void ansQueries(int s,
                vector<pair<pair<int, int>, int> > ed,
                int n, vector<pair<int, int> > q)
{
  
    // To store the dp states
    int dp[n + 1][2 * n];
  
    // Initialising the dp[][] array
    for (int i = 0; i <= n; i++)
        dp[i][0] = inf;
    dp[s][0] = 0;
  
    // Pre-Processing
    for (int i = 1; i <= 2 * n - 1; i++) {
  
        // Initialising current state
        for (int j = 0; j <= n; j++)
            dp[j][i] = inf;
  
        // Updating current state
        for (auto it : ed) {
            dp[it.first.second][i]
                = min(
                    dp[it.first.second][i],
                    dp[it.first.first][i - 1] + it.second);
        }
    }
  
    for (int i = 0; i < q.size(); i++) {
        if (dp[q[i].first][q[i].second] == inf)
            cout << -1 << endl;
        else
            cout << dp[q[i].first][q[i].second]
                 << endl;
    }
}
  
// Driver code
int main()
{
    int n = 3;
    vector<pair<pair<int, int>, int> > ed;
  
    // Edges
    ed = { { { 1, 2 }, 5 },
           { { 2, 3 }, 3 },
           { { 3, 1 }, 4 } };
  
    // Source
    int s = 1;
  
    // Queries
    vector<pair<int, int> > q = { { 1, 0 },
                                  { 2, 1 },
                                  { 3, 1 },
                                  { 3, 2 },
                                  { 3, 5 } };
  
    // Function to answer queries
    ansQueries(s, ed, n, q);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
import sys,numpy as np
  
inf = sys.maxsize;
  
# Function to find the shortest path 
# between S and D with exactly K edges 
def ansQueries(s, ed, n, q) :
  
    # To store the dp states 
    dp = np.zeros((n + 1, 2 * n)); 
  
    # Initialising the dp[][] array 
    for i in range(n + 1) : 
        dp[i][0] = inf; 
          
    dp[s][0] = 0
  
    # Pre-Processing 
    for i in range( 1, 2 * n) :
  
        # Initialising current state 
        for j in range( n + 1) :
            dp[j][i] = inf; 
  
        # Updating current state 
        for it in ed :
            dp[it[1]][i] = min( dp[it[1]][i], 
                                dp[it[0]][i - 1] + ed[it]); 
      
    for i in range(len(q)) :
        if (dp[q[i][0]][q[i][1]] == inf) :
            print(-1); 
        else :
            print(dp[q[i][0]][q[i][1]]);
  
# Driver code 
if __name__ == "__main__"
  
    n = 3
  
    # Edges 
    ed = { ( 1, 2 ) : 5
        ( 2, 3 ) : 3
        ( 3, 1 ) : 4 }; 
  
    # Source 
    s = 1
  
    # Queries 
    q =
        ( 1, 0 ), 
        ( 2, 1 ), 
        ( 3, 1 ), 
        ( 3, 2 ), 
        ( 3, 5 )
        ]; 
  
    # Function to answer queries 
    ansQueries(s, ed, n, q); 
      
# This code is contributed by AnkitRai01

chevron_right


Output:

0
5
-1
8
20

Time Complexity: O(Q + N*E)
Space Complexity: O(N*N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.