Igraph入门指南 1

Igraph入门指南

一、图的结构

图是顶点和边的集合,而边是通过顶点来描述。顶点和边,要用集合的理念去操作。

1、igraph中与图结构相关的函数

 +.igraph(),
add_edges(), add_vertices(), 
complementer(),compose(), connect(), contract(), 
delete_edges(), delete_vertices(), 
difference(), difference.igraph(),
disjoint_union(),
edge(), 
igraph-minus, intersection(), intersection.igraph(), 
path(),
permute(),
rep.igraph(), reverse_edges(), 
simplify(), union(), union.igraph(), 
vertex()

2、图的基本要素——顶点(vertex)

2-1 检索顶点序列:V(g)函数

V(g)得到图的顶点序列。这个顶点序列通常用作igraph其他函数的参数,用于指代图的顶点。顶点序列被限定到具体图的特定顶点,一个图的V(g)不能与另一个图一起使用。

在实现级别上,顶点序列只是一个包含数字顶点id的向量,但它有一个特殊的类属性,可以对其执行特定于图的操作,例如基于图结构或顶点属性选择顶点的子集。

# 未名图的顶点id
> g <- make_ring(10)
> V(g)
+ 10/10 vertices, from d684cd6:
 [1]  1  2  3  4  5  6  7  8  9 10


# 命名图的顶点id
> g <- make_ring(10) %>% set_vertex_attr('name',value=letters[1:10])
> V(g)
+ 10/10 vertices, named, from 2f141f4:
 [1] a b c d e f g h i j

 # 注意,命名图的名只能用'name',用'label'不会给图的顶点命名
 > g <- make_ring(10) %>% set_vertex_attr('label',value=letters[1:10])
> V(g)
+ 10/10 vertices, from 935689e:
 [1]  1  2  3  4  5  6  7  8  9 10

顶点序列通常由V(g)函数创建。这样做的结果包括按顶点id递增顺序排列的所有顶点。顶点序列可以通过数字向量进行索引,就像其他R向量一样。

V(g)可以查询或设置图顶点的所有属性,在实际操作中,可以把V(g)的结果当作一个对象,与图顶点相关的属性是这个对象的属性(也就是变量),可以通过V(g)$attrib的语法来访问和设置这些顶点属性,这与面向对象编程的语法也是相似的。

> V(g)[3:5]
+ 3/10 vertices, from 935689e:
[1] 3 4 5
> V(g)[3:5]$color <- 'red'
> plot(g)

在这里插入图片描述

顶点序列可以像普通的数字R向量一样进行索引,但需要一些额外的功能。

2-2 V(g)的二次检索:[] & [[]]函数(igraph-vs-indexing)

在igraph中,顶点序列可以使用单方括号和双双括号运算符进行索引,并且它们的工作方式相同。它们之间唯一的区别是双方括号操作符会标记打印顶点属性的结果。

方括号中的索引共有正数向量、负数向量、逻辑向量、字符向量四种模式:

  • 用正数向量进行索引时,会选择序列中给定位置的顶点。

  • 用负数值向量进行索引时,序列中给定位置的顶点将被省略。

  • 用逻辑向量进行索引时,顶点序列的长度和索引必须匹配,并选择索引为TRUE的顶点。

  • 命名图可以使用字符向量进行索引,以选择具有给定名称的顶点。

在命名图中,可以用V(g)[name=="foo"](等价于V(g)[V(g)$name=="foo"]的语法索引图的顶点,假如当前环境中有一个变量的名字也叫 foo, igraph会优先引用顶点名,如果想引用变量,需要用V(g)[.env$foo]

# 顶点名优先
> V(g)[.env$soo]$color <- 'orange'
> plot(g)
> foo <- 'pee'
> g <- make_ring(3) %>% set_vertex_attr('name',value=c('foo','pee','duu'))
> V(g)[name=='foo']$color <- 'red'
> plot(g)

# 引用环境中的变量需要加前缀.env$
> V(g)[.env$soo]$color <- 'orange'
> plot(g)

在这里插入图片描述

方括号中可以使用表达式,igraph并提供了几个专用函数

.nei:邻居,将顶点序列作为其参数,并选择这些顶点的邻居。如果是有向图,还可以通过mode=“out”mode=”in“来设置邻居的模式。

> g <- make_graph(c(1, 2, 2, 3, 2, 4, 4, 2,5,6))
> plot(g)
> V(g)[.nei(c(2, 5))]
+ 4/6 vertices, from 7205bdc:
[1] 1 3 4 6
> V(g)[.nei(c(2, 4), "in")]
+ 3/6 vertices, from 7205bdc:
[1] 1 2 4
> V(g)[.nei(c(2, 5), "in")]
+ 2/6 vertices, from 7205bdc:
[1] 1 4
> V(g)[.nei(c(2, 5), "out")]
+ 3/6 vertices, from 7205bdc:
[1] 3 4 6

在这里插入图片描述

前面说过,顶点的属性可以视为对象的属性(变量),所以也可以用这些属性的值来检索顶点

g <- make_graph(~ A -+ B, B -+ C:D, D -+ B)
V(g)$color <- c("red", "red", "green", "green")
V(g)[color == "red"]
+ 2/4 vertices, named, from e0ac217:
[1] A B

注意[[[的区别,[[可以打印顶点的所有属性

> g <- make_ring(10) %>% 
+   set_vertex_attr('name',value=letters[1:10]) %>% 
+   set_vertex_attr('color',value='red')
 V(g)
+ 10/10 vertices, named, from 8ea2563:
 [1] a b c d e f g h i j
> V(g)[]
+ 10/10 vertices, named, from 8ea2563:
 [1] a b c d e f g h i j
> V(g)[[]]
+ 10/10 vertices, named, from 8ea2563:
   name color
1     a   red
2     b   red
3     c   red
4     d   red
5     e   red
2-3 显示全部或指定顶点的属性:vertex_attr函数

函数参数可以提供属性的名称,如果不提供该参数,则打印所有属性

可以通过指定顶点的索引号来限制显示的具体内容

> vertex_attr(g)
$name
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

$color
 [1] "red" "red" "red" "red" "red" "red" "red" "red" "red" "red"

> vertex_attr(g,'color')
 [1] "red" "red" "red" "red" "red" "red" "red" "red" "red" "red"
> vertex_attr(g,'name',4:7)
[1] "d" "e" "f" "g"
2-4 只显示顶点属性名:vertex_attr_names函数
> vertex_attr_names(g)
[1] "name"  "color"
2-5 设置指定顶点的属性:vertex_attr <-函数,单独使用,每次调用可以同时设置多个属性
q <- make_ring(10)
vertex_attr(q)<-list('label'=LETTERS[1:10],
                     'color'=rep('red',gorder(g)))

本函数有两个坑:

一是需要用list()包裹各个属性;

二是属性值必须是gorder(g)长度的向量,如果是单一值,比如color='red',需要用rep()函数来确保向量长度符合要求

本函数与set_vertex_attr()对比:

从帮助文件示例看,set_vertex_attr()用在图创建过程中,可以接在make_函数后面,而本函数却是独立成行,不能用在make_函数后面,因为本函数带有赋值函数<-,自然无法用在管道函数中间

2-6 设置指定顶点的属性:with_vertex_函数,在管道函数中使用,每次调用可以同时设置多个属性

用于添加顶点属性的构造函数修饰符

vertex_attr <-相比:

一是不需要list()

二是两者设置顶点属性都用‘名=值’的形式

三是本函数中的属性值不必须具备固定长度

四是本函数中的属性名不能加引号

五是本函数似乎只能用在make_

六是本函数用在管道函数中,得到的对象是igraph_constructor_modifier

> make_(ring(10),
+   with_vertex_(
+     color='red',
+     weight=3,
+     name=letters[1:10]
+   )) %>%vertex_attr()
$color
 [1] "red" "red" "red" "red" "red" "red" "red" "red" "red" "red"

$weight
 [1] 3 3 3 3 3 3 3 3 3 3

$name
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
2-7 设置顶点属性:set_vertex_attr函数,在管道函数中使用,每次调用只能设置一个属性
> g <- make_ring(10) %>% 
+   set_vertex_attr('name',value=letters[1:10]) %>% 
+   set_vertex_attr('color',value='red')
2-8 设置顶点属性:语法糖V(g)$,每次调用只能设置一个属性
> V(g)$weight <- 3
> V(g)[[]]
+ 10/10 vertices, named, from 8ea2563:
   name color weight
1     a   red      3
2     b   red      3
3     c   red      3
4     d   red      3
  • 25
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iGraph is a popular library for network analysis and graph visualization. It provides a wide range of functions for creating, manipulating, and analyzing graphs. To use iGraph on Windows, you need to follow these steps: 1. Install Python: First, make sure you have Python installed on your Windows machine. You can download the latest Python installer from the official Python website (https://www.python.org/downloads/windows/). Choose the appropriate version (Python 2.x or Python 3.x) based on your requirements. 2. Install pip: Pip is a package manager for Python that allows you to install and manage software packages. The latest versions of Python include pip by default. Open a command prompt and type `pip --version` to check if pip is installed. If not, you can install it by following the instructions on the pip website (https://pip.pypa.io/en/stable/installing/). 3. Install iGraph: Once pip is installed, you can use it to install iGraph. Open a command prompt and type `pip install python-igraph`. This command will download and install the iGraph library along with its dependencies. 4. Verify the installation: After the installation is complete, you can verify if iGraph is installed correctly by importing it in a Python script or an interactive Python shell. Open a command prompt and type `python` to start the Python interpreter. Then, type `import igraph` and press Enter. If there are no errors, the installation was successful. That's it! Now you can start using iGraph in your Python projects on Windows. Remember to consult the iGraph documentation (https://igraph.org/python/) for detailed usage instructions and examples.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值