私は、networkxのバージョンに基づいて、Pythonですべて単純なパスアルゴリズムに取り組んできました。現時点で私が行おうとしている唯一の変更は、O(n^2) 二重ループ内で実行せずに、すべてのノードのすべてのパスを取得することです。
メインのアルゴリズムはソートされていると思いますが、Python のリスト構造の可変性に慣れていないため、アルゴリズムがめちゃくちゃになっていると思います。
私はJavaで同様のプログラムを書いたので、テストグラフにいくつのパスがあるべきかを知っていますが、数字を合理的に近づけることさえできないようです.
def _all_simple_paths_graph(DG, cutoff):
uniquePaths = []
nlist = DG.nodes()
for source in nlist:
uniqueTreePaths = []
if cutoff < 1:
return
visited = [source]
stack = [iter(DG[source])]
while stack:
children = stack[-1]
child = next(children, None)
if child is None:
stack.pop()
visited.pop()
elif len(visited) < cutoff:
if child not in visited:
visited.append(child)
stack.append(iter(DG[child]))
if visited not in uniqueTreePaths:
yield visited
uniqueTreePaths.append(visited)
else: #len(visited) == cutoff:
if visited not in uniqueTreePaths:
yield visited + [child]
uniqueTreePaths.append(visited)
stack.pop()
visited.pop()
uniquePaths.extend(uniqueTreePaths)
どこが間違っているのか誰か教えてもらえますか? 私はそれvisited
が可変であるべき場所とそうでない場所に関係していると信じています。ただし、基本的な機能が間違っている可能性もあります。私はPythonにかなり慣れていません!