相关疑难解决方法(0)

如何订购连接列表

我目前有一个存储在列表中的连接列表,其中每个连接是一个连接两个点的有向链接,没有任何点链接到多个点或链接到多个点.例如:

connections = [ (3, 7), (6, 5), (4, 6), (5, 3), (7, 8), (1, 2), (2, 1) ]
Run Code Online (Sandbox Code Playgroud)

应该产生:

ordered = [ [ 4, 6, 5, 3, 7, 8 ], [ 1, 2, 1 ] ]
Run Code Online (Sandbox Code Playgroud)

我尝试使用一种算法来做到这一点,该算法采用输入点和连接列表,并递归调用自身以找到下一个点并将其添加到增长的有序列表中.但是,当我没有从正确的点开始时,我的算法会崩溃(尽管这应该只是反向重复相同的算法),但是当有多个未连接的链时

编写有效算法来订购这些连接的最佳方法是什么?

python algorithm graph-theory graph-traversal

9
推荐指数
1
解决办法
275
查看次数

标签 统计

algorithm ×1

graph-theory ×1

graph-traversal ×1

python ×1