回答編集履歴
1
d
answer
CHANGED
@@ -10,13 +10,17 @@
|
|
10
10
|
pairs = [('鈴木', '佐藤'), ('山田', '田中'), ('田中', '佐藤'), ('渡辺', '鈴木')]
|
11
11
|
start = '山田'
|
12
12
|
|
13
|
-
# グラフを作成
|
14
13
|
G = nx.Graph()
|
15
14
|
G.add_edges_from(pairs)
|
16
15
|
|
16
|
+
|
17
17
|
# 深さ優先探索
|
18
|
+
print(list(nx.dfs_edges(G, source=start)))
|
19
|
+
# [('山田', '田中'), ('田中', '佐藤'), ('佐藤', '鈴木'), ('鈴木', '渡辺')]
|
20
|
+
# のようにエッジの情報が得られるので、
|
21
|
+
|
18
|
-
ret = [edge[
|
22
|
+
ret = [start] + [edge[1] for edge in nx.dfs_edges(G, source=start)]
|
19
|
-
print(ret) # ['山田', '田中', '佐藤', '鈴木']
|
23
|
+
print(ret) # ['山田', '田中', '佐藤', '鈴木', '渡辺']
|
20
24
|
```
|
21
25
|
|
22
26
|

|