0
グラフで2つの未接続ノードを見つけたこととその受け入れられた応答(How to find two randoms nodes with no edges between them in graph?)に従った前の質問に続きます。私はpython/igraphで同じことをしたい。 私はこのコードを使ってグラフを作成したり、グラフ内に2つの未接続ノードを見つけて、それらの間に1つのエッジを追加したいとします。私はDEF select_2_random_unconnected_nodes(考えます。node_list、G)のpython/IGRAPH に機能を書いた:python/igraphを使ってグラフ内の2つのランダムな未接続ノードを見つける方法は?
selected_node = random.choice(node_list)
# obtain all the nodes connected to the selected node
connected_nodes1 = [n for _, n in G.es.select(_source=selected_node)]
connected_nodes2 = [n for _, n in G.es.select(_target=selected_node)]
#connected_nodes1 = [n for n in G.neighbors(selected_node, mode='out')]
#connected_nodes2 = [n for n in G.neighbors(selected_node, mode='in')]
#print(connected_nodes + [selected_node])
# a feasible node is one not in connected_nodes and also not the first selected_node
feasible_nodes = [feasible_n for feasible_n in node_list if feasible_n not in connected_nodes1 + connected_nodes2 + [selected_node]]
# select a second node from the feasible_nodes list
select_second_node = random.choice(feasible_nodes)
return selected_node, select_second_node
Iの両方G.es.selectとG.neighbors機能を試してみました。しかし、2つのノード間で複数のエッジを返すか、自己ループする!誰もが解決策を知っていますか?これはどのように
おかげで、しかし、この機能はまた、ノード間のselfloopsと複数のエッジを返します! – Maryam
セルフループを必要としない場合は、単に 'u!= v'を' if'条件に追加してください。私はあなたが複数のエッジによって何を意味するのか分かりません。 'get_random_unconnected_node_pair()'を複数回呼び出すと、同じ '(u、v)'ペアが複数回返されることがあります。 'get_random_unconnected_node_pair()'を呼び出す前に2つのノードの間にエッジを追加して、それを避けたり、単に 'get_random_unconnected_node_pair()'の結果をリストに格納してから 'set'に変換することができます。 –