1

The problem goes as follows: you have n domino pieces and the two numbers on the every domino piece (n pieces), also there is an extra set of m domino pieces but you can use only one piece from this set (at most) to help you do the following: calculate the minimum number of domino pieces that you can use to go from a given starting point S to an ending point D. meaning that the starting piece should have the number (S) and the ending piece should have the number (D). Input: n and the domino pieces' numbers (n pairs). m and the extra domino pieces' numbers (m pairs). starting point S and a destination D. Output: the minimum number of domino pieces.

I am thinking of using BFS for this problem where I can start from S and find the minimum path to D with constantly removing node m(i) from the graph and adding node m(i+1) but doing this the time complexity will be O(n * m). but not only this, there could be multiple starting points S so the complexity would be O(|S| * n * m). can it be solved in a better way? The teacher said it could be solved in a Linear Time but I am just very confused.

Muaz Kassm
  • 31
  • 3
  • 1
    Language-agnostic algorithm questions with an attempted solution are on topic, but I'm lacking clarity on the problem statement. Can you provide a few examples with concrete input and output with explanation? – ggorlen May 17 '19 at 01:36
  • See: https://stackoverflow.com/questions/50319901/shortest-path-in-a-maze-with-health-loss/50321206#50321206 – Matt Timmermans May 17 '19 at 03:43

1 Answers1

1

I initially missed that your question allows multiple sources, and wrote a somewhat long answer explaining how to approach that problem. Let me post it here anyway, because it might still be helpful. Scroll further for the solution to the original question.

Finding shortest paths from single S to D in linear time

Let's build the idea incrementally. First, let's try to solve a simpler version of the problem, where we just need to find out whether you can get from a single S to a single D at all by using at most one domino from the set of extra M dominoes.

I suggest to approach it this way: do some preprocessing on the N dominoes that will let you, for each of the M additional dominoes, quickly (in constant time) answer whether there exists a path from S to D that goes through this domino. (And of course we need to remember the edge case when we don't need an extra domino at all, but it's easy to cover in linear time.)

What kind of information lets you answer this question? Let's say you are looking at a domino with numbers A and B on its ends. If you knew that you can get from S to A, and from B to D, you use this domino to get from S to D, right? Alternatively, if there was a path from S to B and from A to D, it would also work. If neither is true, then there is no way this domino can help you to get from S to D.

That's great, but if we run BFS from every possible B, we won't achieve linear time complexity. However, note that you can reverse the second problem (detecting if a path from B's to D exists) and pose it as "can I get from D to every possible B"? That is easily answered with a single BFS.

Can you see how this approach can be adapted to find length of the shortest path through each domino, as opposed to just detecting if a path exists?

Finding shortest paths from multiple S to D in linear time

Let's reverse the problem and say we want to find the shortest paths from D to multiple S. You could create a directed graph with twice as many nodes as there were unique numbers written on dominoes. That is, for each number there are nodes V and V', and if you are in V, it means you haven't used an extra domino yet, but if you are in V', it means you already used one. Each core (that is, one of the original N) domino (A, B) corresponds to 4 edges in this graph: (A -> B), (B -> A), (A' -> B'), (B' -> A'). Each extra domino corresponds to 2 edges: (A -> B'), (B -> A'). Note that once we get into a node with ', we can never get out of it, so we will only use at most one extra domino this way. A single BFS from D in this graph will answer the problem.

eldar
  • 404
  • 2
  • 8