2017-12-04 4 views
1

edgelist로 변환 한 다음 그래프로 변환 할 수있는 데이터 프레임의 예입니다. edgelist에 속성으로 'km'을 추가했음을 주목하십시오.igraph (R)에서 모서리 속성의 경로/직경을 계산하는 방법은 무엇입니까?

가장자리 속성으로 '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()을 사용하면 세 번째 열에 저장된 정보가 위쪽에 저장됩니다. 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을 만들었습니다.

관련 문제