質問するログイン新規登録

回答編集履歴

1

d

2019/02/07 08:27

投稿

tiitoi
tiitoi

スコア21962

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[0] for edge in nx.dfs_edges(G, source=start)]
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
  ![イメージ説明](c282768afa0a016de0e0f507cf67a3cb.png)