0

Let's say I have a list of lists of every possible transfer between two bus lines.

[a,b]
[b,a]
[b,c]
[c,b]
[b,e]
[e,b]
[c,e]
[e,c]

I want to go from a -> e

Wanted results:

[a,b,c,e]
[a,b,e]

You can read the above as:

[a,b]
[b,e]
a -> b -> e = [a,b,e]

If I went c->b, i can't go back (b->c)

What's the easiest way to get those lists of transfers?

Saeed Hassanvand
  • 843
  • 1
  • 12
  • 29
Heir
  • 21
  • 3
  • 1
    You can save your arrays (the path between tow station) as 2D matrix and then use multiple algorithms that find the path between two nodes in the matrix. https://www.geeksforgeeks.org/find-whether-path-two-cells-matrix/ – Saeed Hassanvand Nov 30 '18 at 15:33

1 Answers1

0
  1. Do create a graph of stations. You can use Adjacency matrix or Adjacency list
  2. Then use Depth-first search to find all possible ways from one vertices to another.
oleg.cherednik
  • 15,340
  • 4
  • 20
  • 31