本文介绍了更改igraph图中子图的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来绘制图形的最小生成树

I have the following code to plot the minimum spanning tree of a graph

## g is an igraph graph
mst = minimum.spanning.tree(g)
E(g)$color <- "SkyBlue2"

## how to I make mst a different color
E(g)[E(mst)]$color = "red"  ### <---- I WANT TO DO ESSENTIALLY THIS

plot(g,  edge.label=E(g)$weight)

也就是说,对于一个简单的图形,我找到了mst.我想将mst更改为红色并绘制mst作为主图的一部分.为此,我想选择也在mst中的g的边缘.我该怎么做?

That is, for a simple graph, I find the mst. I want to change the mst to red and plot the mst as part of the main graph. To do this, I want to select the edges of g that are also in mst. How do I do this?

更新:

更一般而言,我有一个图g0,它是g的mst,它具有n顶点.它的构造如下

More generally, I have a graph g0 which is the mst of g, which has n vertices. It was constructed as follows

## implementing the Dijkstra-Prim algorithm
v0 = sample(1:n, 1)
g0 = graph.empty(n=n, directed=FALSE)
weight.g0 = 0
while(length(setdiff(1:n, v0) > 0)) {
  ## chose the shortest edge in the cut set of g

  ## to find the cut, figure out the set of edges where vertex is
  ## in v0 and the other is not
  cutset = E(g)[ v0 %->% setdiff(1:n, v0)]

  ## find the lightest weight edge
  cutweights = E(g)$weight[cutset]
  lightest_edge_idx = which(cutweights == min(cutweights))[1]
  weight.g0 = weight.g0 + min(cutweights)

  ## get the vertices of the lightest weight edge, add to path
  lightest_edge = cutset[as.numeric(cutset)[lightest_edge_idx]]
  vertices = get.edges(g, as.numeric(lightest_edge))

  g0 <- add.edges(g0, vertices, weight=min(cutweights))


  ## now that we have the vertices, add the one that is not in the
  ## graph already
  for(vtx in vertices) {
    if(!(vtx %in% v0)) {
      v0 = c(vtx, v0)
    }
  }

} 

我知道我可能没有使用igraph的许多有用功能,但是在此循环结束时,我确实将g0设置为mst.有了这个,我有

I know I am probably not using a lot of useful features of igraph, but I do get g0 to be a mst at the end of this loop. Given this, I have

E(g0)
Edge sequence:

[1]   8 --  1
[2]   2 --  1
[3]   9 --  8
[4]   9 --  5
[5]   3 --  2
[6]   4 --  3
[7]   7 --  3
[8]  11 --  4
[9]   7 --  6
[10] 11 -- 10
> E(g)
Edge sequence:

[1]   2 --  1
[2]   5 --  1
[3]   8 --  1
[4]   3 --  2
[5]   5 --  2
[6]   6 --  2
[7]   4 --  3
[8]   6 --  3
[9]   7 --  3
[10]  7 --  4
[11] 11 --  4
[12]  6 --  5
[13]  8 --  5
[14]  9 --  5
[15]  7 --  6
[16]  9 --  6
[17] 10 --  6
[18] 10 --  7
[19] 11 --  7
[20]  9 --  8
[21] 10 --  9
[22] 11 -- 10

我的问题是,如何为E(g0)和E(g0)中的边缘分配一个属性?

My question was, how do I assign an attribute to the edges in E(g) that are also in E(g0)?

推荐答案

这实际上很容易,因为minimum.spanning.tree()保留了边缘属性.因此,您只需要分配一个边缘ID属性,就可以看到哪些边缘会变成红色.它是这样的:

This is actually quite easy because minimum.spanning.tree() keeps edge attributes. So you just need to assign an edge id attribute, and you'll see which edges to color red. It goes like this:

# Some test data, no edge weights, quite boring
g <- erdos.renyi.game(20,2/20)
g
# IGRAPH U--- 20 24 -- Erdos renyi (gnp) graph
# + attr: name (g/c), type (g/c), loops (g/l), p (g/n)

E(g)$id <- seq_len(ecount(g))
mst <- minimum.spanning.tree(g)
mst
# IGRAPH U--- 20 18 -- Erdos renyi (gnp) graph
# + attr: name (g/c), type (g/c), loops (g/l), p (g/n), id (e/n)
E(mst)$id
# [1]  1  2  3  6  7  8  9 10 11 12 13 16 18 19 20 22 23 24

E(g)$color <- "black"
E(g)$color[E(mst)$id] <- "red"
plot(g)

这篇关于更改igraph图中子图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 14:45