简体   繁体   中英

Python possible path finding

I have a question about finding all possible paths from Python. I was given cities and paths of each cities. Paths are:

B:C D F G A M N O
C:E G H
D:F H J N O
F:G M
G:A M
A:K N
L:N
H:I A M
I:K M N
J:K
E:A K O
K:N O

And cities are: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O.

However each city traveler starting from one of the cities can go maximum 3 steps and each city has multiple arrows branching to other cities so that lets say Traveler starting from C can reach all these cities {'E', 'G', 'H', 'A', 'K', 'N', 'O', 'M', 'I'}.

I thought something like that, since sets store same value only once lets say city C has paths through E,G,H and also these cities has paths through other cities so that set should keep all the paths of C and its components. I know I need to use dictionaries, sets and lists but could not construct an algorithm for it.

Are you trying to find minimal path between two cities ?

If yes,then look for Breadth First Search Algorithm . For this ,you need to construct Adjacency List or Adjacency Matrix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM