igraph软件包创建图和网络(创建邻接矩阵)

2019-02-14 15:00:51 浏览数 (4)

一、igraph软件包创建图和网络

igraph 是一个独立的库,底层是 C,上层有 Python 和 R 接口,主要做图和网络方面的计算,附带绘图功能。

调试顶点的大小(参数vertex.size)和顶点标签(参数vertex.label.cex)的大小。

igraph中图的数据结构 igraph中基本的graph structure采用的是EdgeList,所以在igraph中自然而然的允许multiedge的存在,当然它也提供了Adjancency list(对某些算法,大部分算法接受的参数是edgelist)。数据结构igraph_t定义如下: typedef struct igraph_s { igraph_integer_t n; #图的顶点个数 igraph_bool_t directed; #有向图,无向图 igraph_vector_t from; #边的起点 igraph_vector_t to; #边的终点 igraph_vector_t oi; #尾结点下标 igraph_vector_t ii; #头结点下标 igraph_vector_t os; igraph_vector_t is; void *attr; } igraph_t;

igraph中顶点和边都是从0开始编号。n是图的顶点个数,directed是有向图标识。所有边的顶点存储在from和to两个向量(igraph_vector_t)中,oi[e]对应的是编号为e的边所对应的尾结点在from中的index,同样ii[e]对应于e的头节点在to中的index(也就是是说e 可以表示为 from[oi[e]] -> to[ii[e]])。所以from,to,oi,ii都是长度与边数相同的向量。

os和is则和oi,ii相反,表示的是从顶点到边的映射,从顶点v出发的第一条边为 from[oi[os[v]]] -> to[ii[os[v]]],所以当os[v] == os[v 1]时候就表示从该顶点没有出边。向量is同理。os,is都是长度为顶点数加一的向量。

操作igraph_t的一些基本API如igraph_empty, igraph_adjacent等见于文档手册。

因为采用的是edgelist的结构,所以增/减边(顶点)的操作在igraph中是相当耗费时间的。add和delete操作的时间复杂度基本上都是O(|V| |E|)或者O(|V|)。

二、例题

eg1.有weight的图

require(igraph) d = data.frame(p1 = c('a', 'b', 'c'), p2 = c('b', 'c', 'a'), weight = c(1, 2, 4)) g = graph.data.frame(d, directed = TRUE) #有向图 plot(g, edge.width=E(g)$weight)

eg2. 顶点的颜色

ramp =colorRamp(c("red", "white","blue"));

#ramp(seq(0, 1, length = length(unique(label))))

panel=rgb( ramp(seq(0, 1, length = length(unique(label)))), max = 255)#设定颜色

用户可以根据color、rgb值和hsv值来设定不同的颜色

注释:R语言设定颜色的方法

library(grDevices); ramp <- colorRamp(c("red", "white","blue")); ramp(seq(0, 1, length = 5)) [,1] [,2] [,3] [1,] 255.0 0.0 0.0 [2,] 255.0 127.5 127.5 [3,] 255.0 255.0 255.0 [4,] 127.5 127.5 255.0 [5,] 0.0 0.0 255.0 color<-rgb( ramp(seq(0, 1, length = 5)), max = 255) color [1] "#FF0000" "#FF7F7F" "#FFFFFF" "#7F7FFF" "#0000FF"

eg3. 邻接矩阵的图

library(igraph) cells<-c(0,0,1,0,1,1,0,1,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,1,0,3,0,3,3,3,0,0,0,0,0,0,0,0,3,0,3,1,1,1,0,0,0,0,0,0,1,1 ,0,3,0,0,0,0,1,0,0,0,0,0,1,1,3,1,0,0,3,0,0,0,0,0,0,0,0,0,3,1,0,3,0,0,3,1,0,3,0,0,1,1,3,1,0,0,0,0,0,3,0,3,1,1,0,0,0,0,1,3,3,0,0,3,1,3,0,0,0,0,0,0,0,0,1,3,3,0,0,3,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,0,0,1,1,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,1,1,0) cells=matrix(cells,14,14,byrow=T) #创建邻接矩阵 > cells [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] 0 0 1 0 1 1 0 1 0 0 0 0 0 [2,] 0 0 1 0 1 1 0 1 0 0 0 0 0 [3,] 1 1 0 3 0 3 3 3 0 0 0 0 0 [4,] 0 0 3 0 3 1 1 1 0 0 0 0 0 [5,] 1 1 0 3 0 0 0 0 1 0 0 0 0 [6,] 1 1 3 1 0 0 3 0 0 0 0 0 0 [7,] 0 0 3 1 0 3 0 0 3 1 0 3 0 [8,] 1 1 3 1 0 0 0 0 0 3 0 3 1 [9,] 0 0 0 0 1 3 3 0 0 3 1 3 0 [10,] 0 0 0 0 0 0 1 3 3 0 0 3 1 [11,] 0 0 0 0 0 0 0 0 1 0 0 0 0 [12,] 0 0 0 0 0 0 3 3 3 3 0 0 1 [13,] 0 0 0 0 0 0 0 1 0 1 0 1 0 [14,] 0 0 0 0 0 0 0 1 0 1 0 1 1 [,14] [1,] 0 [2,] 0 [3,] 0 [4,] 0 [5,] 0 [6,] 0 [7,] 0 [8,] 1 [9,] 0 [10,] 1 [11,] 0 [12,] 1 [13,] 1 [14,] 0 myCoord<-c(1,2,7.5,5,3,6,6,8,8,11,8,10,11,13,2,1,2,4,5.5,1,6,4,9,8,14,5.5,2.2,4) myCoord<-matrix(myCoord,14,2,byrow=F) #创建顶点坐标 > myCoord [,1] [,2] [1,] 1.0 2.0 [2,] 2.0 1.0 [3,] 7.5 2.0 [4,] 5.0 4.0 [5,] 3.0 5.5 [6,] 6.0 1.0 [7,] 6.0 6.0 [8,] 8.0 4.0 [9,] 8.0 9.0 [10,] 11.0 8.0 [11,] 8.0 14.0 [12,] 10.0 5.5 [13,] 11.0 2.2 [14,] 13.0 4.0 cnames=paste("e",1:14,sep="") #顶点标签 > cnames [1] "e1" "e2" "e3" "e4" "e5" "e6" "e7" "e8" "e9" "e10" "e11" "e12" [13] "e13" "e14" g=graph.adjacency(cells,mode="undirected",weighted=T) #创建图 > g IGRAPH U-W- 14 35 -- attr: weight (e/n) edges: [1] 1-- 3 1-- 5 1-- 6 1-- 8 2-- 3 2-- 5 2-- 6 2-- 8 3-- 4 3-- 6 [11] 3-- 7 3-- 8 4-- 5 4-- 6 4-- 7 4-- 8 5-- 9 6-- 7 6-- 9 7-- 9 [21] 7--10 7--12 8--10 8--12 8--13 8--14 9--10 9--11 9--12 10--12 [31] 10--13 10--14 12--13 12--14 13--14 plot(g,vertex.color="green",layout=myCoord,vertex.shape="square", vertex.label=cnames,vertex.label.font=2,vertex.label.dist=-1, vertex.label.degree=-pi/2,vertex.label.color="black", vertex.frame.color="gray", edge.width=E(g)$weight,edge.color="gray")

igraph创建图 三、函数应用

1.输出图中所有节点

  V(g)$name

  g是相应的图

2.根据节点degree输出节点

  V(g)[degree(g)>3] 将图中degree大于3的节点输出

  g是相应的图

3.

V(g) #返回图g的顶点 E(g) #返回图g的边

4.图形建立:

(1) > g=graph(c(1,2,5,6,1,4),n=6,directed=T) > plot(g) (2) >g=graph.formula(Alice-Bob-Cecil-Alice,Daniel-Cecil-Engene,Cecil-Gordon) > plot(g) (3) graph.data.frame() #从数据框画图 graph.adjacency() #从邻接矩阵创建图 (4) erdos.renyi.game() #根据Erdos-Renyi模型生成随机图 ba.game() #根据Barabasi-Albert模型生成scale-free图 (5) vcount(g)/ecount(g) #返回图g的定点数、边数 is.connected(g) #图g是否连通 is.clusters(g) #图g有多少分支 (6) 设置图的属性:set/get.graph/vertex/edge() # 具体用法详见help 5.可视化 (1)plot()命令 :画普通的二维图 (2)tkplot():交互绘图命令 例: > library(igraph) > g=barabasi.game(100,m=1) >id=tkplot(g,vertex.size=4,vertex.label=NA,edge.color="black",edge.arrow.size=0.7,vertex.color="red") > coords <- tkplot.getcoords(id) (3)rgplot:画3D图

0 人点赞