Editorial for Shortest Path Constrained from CodeNation Hiring Test

Revision en4, by AakashHanda, 2019-02-04 13:55:58

I decided to write this blog, as a lot of people were requesting for the editorial of Shortest Path Constrained problem from CodeNation Hiring Test, held on 26th January, 2019

Problem

Statement

There is a weighted undirected graph with N nodes and M edges. You are also given X different sets of nodes — S1 to Sx. You have to give the shortest possible distance between a start node — a and a destination node — b, along a path following the following rule: For all i < j, before visiting a node in Sj, you must visit all the nodes in Si.

The distance is defined as sum of weights of edges along the path traversed.

Note :-

  1. The path must contain all the nodes in union of Si for all 1 ≤ i ≤ X
  2. for all 1 ≤ i, j ≤ X and i ≠ j.
  3. The path may go through any node, including a and b, more than once. The constraint is that it must end at b

Constraints

  • 2 ≤ N ≤ 100
  • 0 ≤ w ≤ 100000
  • 0 ≤ X ≤ 10
  • 1 ≤ |Si| ≤ 10 for all i ≤ X
  • Nodes are numbered from 0 to N - 1
  • The graph does not contains multiple edges or self edges, and is connected

Solution

Let us first solve the problem of how to visit all the nodes in a set Si, in shortest possible distance, such that we end up at node x. This problem can be solved using dp + bitmask. Suppose dp[bitmask][x] stores the minimum distance, for this particular set, such that the nodes with bits set to 1 in bitmask are visited, and we end up at node x. Now for all k such that kth bit is 0 in bitmask, we can update dp[bitmask|k][k] = min(dp[bitmask][x] + dist(x, k), dp[bitmask|k][k]). dist(x, k) gives us the smallest distance from x to k and can be pre-calculated using Floyd-Warshall Algorithm.

Now, in this problem, we need to do the above for all the sets, while making sure the nodes from next sets are not visited. To do that, we maintain a list of illegal nodes. When we visit a set Si, we remove the nodes of this set from the illegal list. Now, we create an auxiliary adjacency matrix, which does not contain the nodes from the illegal list. Now we can run the above dp.

We can initialize the dp for Si + 1 using the dp for Si

Code

Click to Expand

Sample Cases (Including corner cases)

Click to Expand
Tags dp bitmask, #graph theory, floyd-warshall, #editorial

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en9 English AakashHanda 2019-02-04 14:01:08 4
en8 English AakashHanda 2019-02-04 13:59:49 0 (published)
en7 English AakashHanda 2019-02-04 13:59:30 1
en6 English AakashHanda 2019-02-04 13:58:19 1 Tiny change: '========\n\n<spoiler' -> '========\n<spoiler'
en5 English AakashHanda 2019-02-04 13:56:50 29 Tiny change: ' for $S_i$\n\n\nCode' -> ' for $S_i$, as shown in the code below.\n\n\nCode'
en4 English AakashHanda 2019-02-04 13:55:58 22
en3 English AakashHanda 2019-02-04 13:54:30 48
en2 English AakashHanda 2019-02-04 13:52:51 4311 Tiny change: ' summary="Problem">\n### St' -> ' summary="Click to Expand">\n### St'
en1 English AakashHanda 2019-02-04 13:31:58 1795 Initial revision (saved to drafts)