アイグラフを使っています。
2 つのノード間のすべての可能なパスを見つけてください。
現時点では、igraph の 2 つのノード間のすべてのパスを見つける機能は存在しないようです。
Pythonでコードを提供するこの件名を見つけました: 有向ツリー内の1つのノードから別のノードへのすべての可能なパス(igraph)
Rに移植しようとしましたが、少し問題があります。エラーが表示されます:
Error of for (newpath in newpaths) { :
for() loop sequence incorrect
コードは次のとおりです。
find_all_paths <- function(graph, start, end, mypath=vector()) {
mypath = append(mypath, start)
if (start == end) {
return(mypath)
}
paths = list()
for (node in graph[[start]][[1]]) {
if (!(node %in% mypath)){
newpaths <- find_all_paths(graph, node, end, mypath)
for (newpath in newpaths){
paths <- append(paths, newpath)
}
}
}
return(paths)
}
test <- find_all_paths(graph, farth[1], farth[2])
サンプル グラフとノードを取得するための igrah パッケージから取得したダミー コードを次に示します。
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
"Esmeralda"),
age=c(48,33,45,34,21),
gender=c("F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3))
g <- graph.data.frame(relations, directed=FALSE, vertices=actors)
farth <- farthest.nodes(g)
test <- find_all_paths(graph, farth[1], farth[2])
ありがとう!
誰かが問題の場所を見れば、それは大きな助けになるでしょう...
マチュー