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
  • 931
  • 1
  • 14
  • 31
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
  • 17,377
  • 4
  • 21
  • 35