前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R包—iGraph

R包—iGraph

作者头像
学到老
发布2018-03-16 15:53:28
1.4K0
发布2018-03-16 15:53:28
举报
文章被收录于专栏:深度学习之tensorflow实战篇

这几天收到师兄的任务,熟悉iGRaph包的使用,通过查资料,外加自己的实践,在此做个简单的学习笔记。 以下例子均是在R 3.0.1版本下测试的。

1.用igraph创建图表

代码语言:javascript
复制
 g<- graph(c(1,2, 1,3, 1,4, 2,4, 3,4), directed=T) 
 > g IGRAPH D--- 4 5 --  
 > plot(g, layout=layout.fruchterman.reingold)

2.创建多种图形的图表

代码语言:javascript
复制
 > g1 <- graph.full(4) > g1 IGRAPH U--- 4 6 -- Full graph + attr: name (g/c), loops (g/x) 
 > g2 <- graph.ring(3) > g2 IGRAPH U--- 3 3 -- Ring graph + attr: name (g/c), mutual (g/x), circular (g/x) 
 > g3 = graph.lattice(c(3,4,2))#create a lattice > g3 IGRAPH U--- 24 46 -- Lattice graph + attr: name (g/c), dimvector (g/n), nei (g/n), mutual (g/x), circular (g/x) 
 >g4 = graph.tree(50, children=2)#create a tree > g4 IGRAPH D--- 50 49 -- Tree + attr: name (g/c), children (g/n), mode (g/c) 
 >plot(g4,layout=layout.fruchterman.reingold,vertex.label.dist=0.3,edge.arrow.size=0.5,vertex.color="red") 

3.随机图表和优先连接的生成

代码语言:javascript
复制
 > g <- erdos.renyi.game(20, 0.2)#create a random graph and fix probability  
 > g IGRAPH U--- 20 43 -- Erdos renyi (gnp) graph + attr: name (g/c), type (g/c), loops (g/x), p (g/n) 
 > plot(g, layout=layout.fruchterman.reingold, vertex.label=NA, vertex.size=5,vertex.color="green") 

 > g <- erdos.renyi.game(20, 15, type='gnm')# Generate random graph, fixed number of arcs > g IGRAPH U--- 20 15 -- Erdos renyi (gnm) graph + attr: name (g/c), type (g/c), loops (g/x), m (g/n) 
 > plot(g, layout=layout.reingold.tilford, vertex.label=NA, vertex.size=5,vertex.color="green") 

4.简单图表的算法

代码语言:javascript
复制
 g <- erdos.renyi.game(12, 0.35) > g IGRAPH U--- 12 21 -- Erdos renyi (gnp) graph + attr: name (g/c), type (g/c), loops (g/x), p (g/n) 
 > E(g)$weight <- round(runif(length(E(g))),2) * 50#Create the graph and assign random edge weights 

 > mst <- minimum.spanning.tree(g)#Compute the minimum spanning tree > mst IGRAPH U-W- 12 11 -- Erdos renyi (gnp) graph + attr: name (g/c), type (g/c), loops (g/x), p (g/n), weight (e/n) 

最短路径算法:

代码语言:javascript
复制
> pa <- get.shortest.paths(g, 5, 9)[[1]]
> pa
[1] 5 3 9
> V(g)[pa]$color <- 'green'
> E(g)$color <- 'grey'
> E(g, path=pa)$color <- 'red'
> E(g, path=pa)$width <- 3
> plot(g, layout=layout.fruchterman.reingold) 

 > plot(mst, layout=layout.reingold.tilford, edge.label=E(mst)$weight) 
 > plot(g, layout=layout.fruchterman.reingold, edge.label=E(g)$weight) 

http://blog.sina.com.cn/s/blog_60034d4a0101e12h.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档