0

Given a directed graph G=(V, E) with pair of vertices s,t∈V, Find an algorithm that counts all the unique paths from s to t in the best time complexity. (A unique path from s to t is defined to be a path from s to t that doesn't contain any common vertices with any other path from s to t except s and t.) I've tried to evolve the algorithm using the algorithm described in this link (Print all paths from a given source to a destination - https://www.geeksforgeeks.org/find-paths-given-source-destination/) by deleting the vertices that the path passed through but still can't see that this algorithm is working in a good time-complexity. And now I'm trying to see if I can do this by using DFS in less than |V| or |E| times but nothing is working with me. Any idea?

MT0
  • 143,790
  • 11
  • 59
  • 117
Black Hat
  • 23
  • 6
  • _Hint._ Suppose you ran a breadth-first search or a depth-first search -- whatever you like more. And marked each vertex in one of three ways: not visited, visited once, visited more than once. Perhaps also store the immediate predecessor in your search for each vertex. Can you construct the answer from the information you now have? – Gassa Jan 07 '22 at 21:08
  • Do you want to find "a single set of unique paths in the best time complexity" or do you want to find "the largest possible set of unique paths in the best time complexity"? They are very different problems. – MT0 Jan 07 '22 at 21:17
  • This may be a duplicate of https://stackoverflow.com/q/11440353/1509264 or https://stackoverflow.com/q/11915742/1509264 – MT0 Jan 07 '22 at 21:27
  • 1
    The question is somewhat ambiguous, while the maximum number of unique paths is well defined, the set of unique paths is not (at least it's not unique), consider for example the graph S->A, S->B, A->C, A->D, B->E, B->F, C->T, D->T, E->T, F->T, the number of unique paths is 2 but you have 4 possible sets of unique paths – Rocco Jan 07 '22 at 21:31
  • I think that the number of unique paths is the same number in whatever way you find these paths, so you need to count all unique paths in the best time complexity. – Black Hat Jan 07 '22 at 21:32
  • Also see [Remove minimum number of vertices to disconnect the graph](https://cstheory.stackexchange.com/questions/2877/minimum-cut-through-vertices-nodes-not-edges) (since all the paths must pass through those vertices that would be removed otherwise there would be another path and the graph would not be disconnected by the removal of those vertices). – MT0 Jan 07 '22 at 21:33
  • I think that finding MINIMUM CUT THROUGH VERTICES actually is the best way to solve the problem. THX! – Black Hat Jan 07 '22 at 21:39
  • The number of unique paths is not the same in whatever way you find the paths. Given a graph with edges (s->a),(s->b),(a->b),(a->t),(b-t) if you generate the path `s->a->b->t` then you cannot generate any other unique paths. However, if you generate `s->a->t` then you can also generate `s->b->t`. – MT0 Jan 07 '22 at 21:50
  • But if you generate the shortest path and repeat you'll always get the same number and it is the greatest number of unique paths you can get. – Black Hat Jan 07 '22 at 21:58
  • Actually, That's what I asked, I need only to count the unique paths so I just need their number. – Black Hat Jan 07 '22 at 22:13
  • 2
    [Finding the shortest path is definitely **not** the correct approach.](https://i.stack.imgur.com/PptkX.png). If you take `S-A-B-T` first, removing A and B, then there's only one path. But you can get two paths: `S-C-C-B-T` and `S-A-D-D-T`. – user3386109 Jan 07 '22 at 22:55
  • One obvious observation is that the number of unique paths cannot exceed the number of nodes connected to S or T. In my example, there are two nodes connected to S and two connected to T. So there can be at most two paths. If the C nodes weren't in the graph, then there would only be one node connected to S, and only one path. – user3386109 Jan 07 '22 at 23:03
  • Sorry I misread the question. The notion of "unique path" is ill-defined. – n. m. could be an AI Jan 08 '22 at 14:31

0 Answers0