I have a graph g
which contains a bunch of nodes/vertexes, which are connected by edges, each of the edges has a particular numeric attribute value. An edge list might look like:
v1 v2 att
a b 17
a c 6
a d 2
b c 9
For visualisation purposes I need to add the highest and lowest att
numbers connected to each vertex (coming or going; direction doesn't matter here).
So for example I'd like to set v$max
for a to 17 and v$min
for a to 2, because the largest att-value of a link connected to a is 17, and the smallest is 2.
I tried some variants along the theme of this:
V(g)$min=min(E(g)[incident(g,V(g), mode='total')]$att)
This seems to be very nearly right in that it appears to give the right answer when I try it on a single node, as in
min(E(g)[incident(g,V(g)[2], mode='total')]$att)
but it doesn't work when I try it across all nodes. The aim is to get V(g)$min
and V(g)$max
set for every vertex.
答案 0 :(得分:1)
希望这不是一个家庭作业问题,你只是错过了概括部分
library(igraph)
library(plyr)
df <- data.frame(read.table(text="v1 v2 weight
a b 17
a c 6
a d 2
b c 9", header=T))
df$weight <- as.numeric(df$weight)
#transform df into igraph
g <- graph.data.frame(df, directed=F, vertices=NULL)
#for each vertex, get all incidental edges, weights and use range to find min & max
ldply(V(g), function(v) range(incident(g, v, mode='total')$weight))