2

I have an undirected graph and i want to list all possible paths from a starting node. Each connection between 2 nodes is unique in a listed path is unique, for example give this graph representation:

{A: [B, C, D],
 B: [A, C, D],
 C: [A, B, D],
 D: [A, B, C]}

some listed path starting from A

A, B, C, D, A, C  in this path we have a connection between
A and B but we can't have a connection between B and A 

I can't accomplish it using the existing algorithm that i know like DFS . Any help will be very appreciated .

lafi raed
  • 113
  • 2
  • 13
  • 1
    what do you mean by 'Each connection between 2 nodes is unique in a listed path is unique' and why can't we have a connection between B and A, please clarify? – guroosh Sep 12 '18 at 16:47
  • I believe what you are looking for is a collection of all paths in the graph that do not contain [cycles](https://en.wikipedia.org/wiki/Cycle_(graph_theory)). Is this assumption correct? – Stratadox Sep 12 '18 at 17:32
  • @gc7 by unique connection i mean that in a listed path for example if we have A connected to be then we can't have B connected to A , this listed path is wrong: A, B, A. – lafi raed Sep 13 '18 at 10:00
  • @Stratadox no the listed path may contain cycles , in this path A, B, C, D, A, C we have cycles ( A, D, C), (A, B, C) – lafi raed Sep 13 '18 at 10:02
  • If you mean that you can't visit that edge again either from A to B or B to A, maybe [this](https://stackoverflow.com/questions/10032525/graph-how-to-avoid-reprocessing-same-edge-twice-in-depth-first-search) can help. – guroosh Sep 13 '18 at 12:21
  • In a graph with cycles there are an infinite number of paths. To go from A to B, we can go `A, B` or `A, B, C, D, A, B` or `A, B, C, D, A, B, C, D, A, B` etc etc.... Without additional constraints, you will inevitably run out of memory at some point. – Stratadox Sep 13 '18 at 16:52
  • @Stratadox yes exactly but we can visit an edge only once , for example this path is wrong A, B, C, D, A, B because we already have visted A, B so we don't have to visit it again, also the edges A,B and B,A ARE THE SAME – lafi raed Sep 13 '18 at 20:51
  • Ok I think I understand what you mean.Just to check, in a graph `{ A[B], B[A,C], C[B] }` the expected result would be `[A,B], [A,B,C], [B, C]` in any order or direction, correct? – Stratadox Sep 13 '18 at 21:44
  • @Stratadox yes exactly – lafi raed Sep 13 '18 at 22:27
  • In that case, see [my answer](https://stackoverflow.com/a/52322526/9254201) :) – Stratadox Sep 13 '18 at 22:28

2 Answers2

2

The simplest way would be to recursively try each neighbor and combine all the results.

This assumes there are no loops - if you allow loops (as in your example) there will be infinitely-many paths. In this case, you can make a path-generator by limiting the path-length to check for, then looping over all possible path-lengths.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
-1

A probably most intuitive way would be to, as previously suggested, iterate over all neighbours of each potential starting point until all paths are exhausted.

However, the risk with this method is that it tends to loop forever if the graph has cycles. This can be mitigated by using a list of visited vertices (or, probably preferable in your case, visited edges)

In pseudocode, that might give something like this:

paths = []
for node in graph
  visited = []
  path = [node]
  add node and path to stack-or-queue
  while node on stack-or-queue
    pop node and path
    for edges of node
      if edge is not visited
        add edge to visited
        add neighbour to path
        add neighbour and path to stack-or-queue
        add path to paths

It produces an algorithm of relatively high complexity, so be sure to test it well to avoid crap.

Writing it recursively might be easier, although it removes the possibility of easily changing between DFS and BFS.

Stratadox
  • 1,291
  • 8
  • 21