1
$\begingroup$

Here is problem in Sprinklr Interview Experience | Set 5 (On campus – FTE for Product Engineer).

You are given a graph of $n$ nodes with $m$ bidirectional edges. Each edge has some value associated with it. Vertex 1ドル$ is source vertex. You have $K$ wildcards. In the path from vertex 1ドル$ to vertex $i$ (2ドル \leq i \leq n$), you can use at most $K$ wildcards while traversing. When you use a wildcard on an edge, you can pass that edge in summing the cost of path (i.e., value of that edge will be 0ドル$ if you use a wildcard on an edge). Note: You can use at most $K$ wildcards from vertex 1ドル$ to vertex 2ドル$. Now you can again use at most $K$ wildcards from vertex 1ドル$ to vertex 3ドル$ and so on to vertex $n$. In other words, you can use at most $K$ wildcards in each path from source to destination. You have to find minimum distances from node 1ドル$ to all other nodes in graph.

Constraints: 1ドル \leq n, m \leq 500000, 1 \leq K \leq 15$

Expected Approach: DP with shortest path algorithms on graph.

What I am doing is that, we can choose $k$ edges from $m$ edges in $\binom{m}{k}$ ways. And for each case find shortest path, but that will be too much time complexity.

asked Feb 10, 2019 at 8:47
$\endgroup$
3
  • 1
    $\begingroup$ What have you tried? Where did you get stuck? $\endgroup$ Commented Feb 10, 2019 at 9:31
  • $\begingroup$ I tried this,choose edges k from m,which is m choose k. Remove them and find shortest path for each case.that is too much time complexity. $\endgroup$ Commented Feb 10, 2019 at 9:47
  • $\begingroup$ geeksforgeeks.org/… $\endgroup$ Commented Feb 10, 2019 at 10:06

1 Answer 1

4
$\begingroup$

Take $K+1$ copies of your graph. For each edge $(x,y)$ and for each $i \in \{1,\ldots,K\}$, connect the $i$'th copy of $x$ to the $(i+1)$'th copy of $y$ with a zero weight edge. Also connect the $i$'th and $(i+1)$'th copies of each vertex with a zero weight edge. Now calculate the minimum distance between vertex 1 on the first copy to all vertices in the last copy.

I'll let you figure out why this works.

answered Feb 10, 2019 at 11:41
$\endgroup$
1
  • 1
    $\begingroup$ The graph that I construct is directed. You can only go forward. $\endgroup$ Commented Oct 20, 2020 at 20:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.