2017-12-04 10 views
1

edgelistに変換してからグラフに変換できるデータフレームの例を次に示します。 edgelistに属性として「km」を追加したことに注目してください。igraph(R)のエッジ属性のパス/直径の計算方法は?

エッジ属性(2ノード間の距離)として「km」を追加する方法はわかりませんが、完了したふりをします。

inst2 = c(2, 3, 4, 5, 6) 
motherinst2 = c(7, 8, 9, 10, 11) 
km = c(20, 30, 40, 25, 60) 
df2 = data.frame(inst2, motherinst2) 
edgelist = cbind(df2, km) 
g = graph_from_data_frame(edgelist) 

ここで、これらのkm距離に基づいてパス長を計算するにはどうすればよいですか?私は、経路内の辺や頂点の数には関心がありません。ルートから葉までのkmの合計だけです。

答えて

1

kmエッジ属性は既に存在します。 graph_from_data_frame()を使用すると、3列目以降に格納された情報がエッジに格納されます。 igraph::E()機能を使用して、エッジから情報を引き出すことができます。

E(g) #identifies all of the edges 
E(g)$km #identifies all of the `km` attributes for each edge 
E(g)$km[1] #identifies the `km` attribute for the first edge (connecting 2 -> 7) 

完全にするために、あなたが

#lets add two more edges to the network 
#and create a new and longer path between vertex named '2', and vertex named '7' 
g <- g + 
    edge('2', '6', km = 10) + 
    edge('6', '7', km = 120) 


#find all paths between 2 and 7 
#don't forget that the names of vertices are strings, not numbers 
paths <- igraph::all_simple_paths(g, '2', '7') 
paths 

#find the edge id for each of the connecting edges 
#the below function accepts a vector of pairwise vectors. 
#the ids are the edges between each pair of vectors 
connecting_267 <- igraph::get.edge.ids(g, c('2','6' , '6','7')) 
connecting_267 

#get the km attribute for each of the edges 
connecting_kms <- igraph::E(g)[connecting_267]$km 
connecting_kms 

sum(connecting_kms) 

igraph 1より大きいが、かなり強力ですであるノードのパスを持っているとしましょう。間違いなく時間を費やす価値があり、exploring its documentationです。また、Katherine Ognyanovaは誰もが価値のある価値を持つan AWESOME tutorialを作成しました。

関連する問題