1

こんにちは私はRで大量のデータを処理していますが、構造が1つ以上のセクションで構成されているかどうかを確認する必要があります。最初に私は方法のベクトルを持っています

wayIds <- [way1, way2, way3, etc..]

次に、各ウェイの最初と最後のノードを持つ行列があります

endWays
           wayId firstNode lastNode
      [1,]  way1  node1      node2
      [2,]  way2  node4      node8
      [3,]  way3  node5      node1...

どちらも大きいです。したがって、接続の方法に従って構造に1つ以上のセクションがあるかどうかを判断する方法が必要です。例えば

_|______/ 1 section (all the ways are connected)
_|__   ____/ 2 sections (NOT all the ways are connected)

したがって、これまでは、すべてのオープンエンド(ブランチの終わりにあるもの)を特定できます。たとえば、オープンノードが2つしかない場合、解決策は簡単です。したがって、ループを使用せずに、開いているすべてのノードが相互に接続されているかどうかを判断するための効果的な方法が必要です。

ありがとう!!

4

1 に答える 1

0

これを試して:

library(igraph)

data <- read.table(text=
'wayId firstNode endNode
way1 node1 node2
way2 node2 node4
way3 node2 node3
way4 node4 node3
way5 node5 node6
way6 node5 node8
way7 node8 node7'
,header=TRUE,sep=' ')

# I reorder the columns before passing the data.frame to the function, 
# because the first 2 columns have to be node from and node to
g <- graph.data.frame(data[,c(2,3,1)],directed=FALSE)

# plot the graph (don't do it if your graph is really big)
plot.igraph(g,vertex.label=V(g)$name,
            vertex.size=30,vertex.label.cex=0.7,
            vertex.shape='square')

subGraphs <- decompose.graph(g,mode='strong')

for(i in 1:length(subGraphs)){
  subGraph <- subGraphs[[i]]

  message(paste('Sub graph:',i))
  # find vertices having just one connected node
  openVertices <- V(subGraph)[sapply(as.vector(V(subGraph)),FUN=function(v){length(E(subGraph)[from(v) | to(v)]) == 1})]

  message(paste('Open vertices:',toString(openVertices$name)))

  # plot the sub-graph (don't do it if your graph is really big)
  plot.igraph(subGraph,vertex.label=V(subGraph)$name,
              vertex.size=30,vertex.label.cex=0.7,
              vertex.shape='square')
}
于 2012-07-25T10:08:54.210 に答える