如何使用R?

时间:2018-06-08 03:19:18

标签: igraph adjacency-matrix strongly-connected-graph

我想用R来提取图的巨大组成部分的邻接矩阵。

例如,我可以创建Erdos-Renyi g(n,p)

n = 100
p = 1.5/n
g = erdos.renyi.game(n, p)
coords = layout.fruchterman.reingold(g)
plot(g, layout=coords, vertex.size = 3, vertex.label=NA)

# Get the components of an undirected graph
cl = clusters(g)

# How many components?
cl$no           

# How big are these (the first row is size, the second is the number of components of that size)?
table(cl$csize) 

cl$membership
# Get the giant component
nodes = which(cl$membership == which.max(cl$csize))

# Color in red the nodes in the giant component and in sky blue the rest
V(g)$color  = "SkyBlue2"
V(g)[nodes]$color = "red"
plot(g, layout=coords, vertex.size = 3, vertex.label=NA)

这里,我只想提取那些红色节点的邻接矩阵。

enter image description here

1 个答案:

答案 0 :(得分:0)

很容易将巨型组件作为下面的新图形获得,然后获得邻接矩阵。

g  <- erdos.renyi.game(100, .015)
co <- components(g)
gi_vids <- which(co$membership == which.max(co$csize))
gi_ad <- get.adjacency(g)[gi_vids, gi_vids]
# you can even add the names of the nodes
# as row and column names.
# generating dummy node names:
V(g)$name <- sapply(
    seq(vcount(g)),
    function(i){
        paste(letters[ceiling(runif(5) * 26)], collapse = '')
    }
)
rownames(gi_ad) <- V(g)$name[gi_vids]
colnames(gi_ad) <- V(g)$name[gi_vids]

但您可能希望保留原始图形的顶点ID。在这种情况下,只是邻接矩阵的子集:

public class PersonError
{
    private List<string> _roles;

    public string Name { get; set; }
    public int Age { get; set; }

    public List<string> Roles
    {
        get
        {
            if (_roles == null)
            {
                throw new Exception("Roles not loaded!");
            }

            return _roles;
        }
        set { _roles = value; }
    }

    public string Title { get; set; }

    [OnError]
    internal void OnError(StreamingContext context, ErrorContext errorContext)
    {
        errorContext.Handled = true;
    }
}