Efficient algorithm to find all the paths from A to Z?

前端 未结 4 1304
失恋的感觉
失恋的感觉 2020-12-23 15:01

With a set of random inputs like this (20k lines):

A B
U Z
B A
A C
Z A
K Z
A Q
D A
U K
P U
U P
B Y
Y R
Y U
C R
R Q
A D
Q Z

Find all the pat

4条回答
  •  -上瘾入骨i
    2020-12-23 15:23

    Your data is essentially an adjacency list which allows you to construct a tree rooted at the node corresponding to A. In order to obtain all the paths between A & Z, you can run any tree traversal algorithm.

    Of course, when you're building the tree you have to ensure that you don't introduce cycles.

提交回复
热议问题