Igraph入门指南 2

3、图的基本要素——边(Edge|Arc)

图的最本质的内容是一种二元关系,如果给这种二元关系赋予一个方向,就产生了有向图和无向图的分类,在教材中,无向的边叫Edge,有向的边叫Arc,另外,根据两个顶点间边的数量,还有多重边的概念,指那些两个顶点间的边多于一条的情况。

边和顶点不同,顶点是实实在在的实体,而边是实体间关系的抽象,是无法直接生成的,必须借助顶点才能准确描述。虽然igraph同样用数字序列标志图中的边,但这只是计算机内部的检索方法,如果想明确边的具体意义,必须借助顶点。

3-1 检索边序列: E(g)函数(结果是边序列)

E(g)的结果也是向量,同样需要用集合的模式去理解,可以求交、并。

E(g)的结果可以用普通向量的模式操作,比如用数字进行索引。

> E(g)
+ 10/10 edges from 6ce6a8b:
 [1] 1-- 2 2-- 3 3-- 4 4-- 5 5-- 6 6-- 7 7-- 8 8-- 9 9--10 1--10
> g <- make_ring(10) %>% set_vertex_attr('name',value = letters[1:10])
> E(g)
+ 10/10 edges from 95167b6 (vertex names):
 [1] a--b b--c c--d d--e e--f f--g g--h h--i

> E(g)[3:5]
+ 3/10 edges from 95167b6 (vertex names):
[1] c--d d--e e--fj a--j

边序列的数据格式主要为igraph其他函数的参数提供值,并方便检索,但在展示图的结构方面并不直观,所以igraph另外提供了三个命令,从图的结构角度展示边,分别是边列表、邻接列表、邻接矩阵、这部分内容放在图的结构部分。

3-2 E(g)的二次检索:[] & [[]]函数(igraph-es-indexing)

E(g)同样可以与单方括号、双方括号联用,检索边。方括号中可以是数字(边的索引号),也可以是边的属性(如果有的话)。

单方括号只显示符合条件的边,而双方括号则把符合条件的边的所有属性一并显示。

> edge_attr(g) <- list(weight=rep(1:5,2))
> edge_attr(g)
$weight
 [1] 1 2 3 4 5 1 2 3 4 5

> E(g)[weight==2]
+ 2/10 edges from 95167b6 (vertex names):
[1] b--c g--h
> E(g)[[weight==2]]
+ 2/10 edges from 95167b6 (vertex names):
  tail head tid hid weight
2    b    c   2   3      2
7    g    h   7   8      2
> E(g)[[1:3]]
+ 3/10 edges from 95167b6 (vertex names):
  tail head tid hid weight
1    a    b   1   2      1
2    b    c   2   3      2
3    c    d   3   4      3

请注意,图可能有边名称和顶点名称,两者都可以用于选择边。边名称可以看作数字边id向量的名称(name)。顶点名称实际上只适用于没有多重边的图形,并且必须用|字符分隔才能选择与两个给定顶点相关的边。

3-3 方括号中的特殊函数:%--% %->% %<-%

如前所述,边描述的是顶点间的关系,而图应该基于集合的理念图操作,那么问题来了,可否用顶点的集合来检索边序列?当然可以,igraph提供了三个函数:

> g <- sample_pa(20)
> plot(g)
> E(g)[1:3 %--% 4:7]
+ 3/19 edges from ae4102a:
[1] 4->1 5->3 6->1
> E(g)[1:3 %->% 4:7]
+ 0/19 edges from ae4102a:
> E(g)[1:3 %<-% 4:7]
+ 3/19 edges from ae4102a:
[1] 4->1 5->3 6->1

在这里插入图片描述

igraph中一些函数的返回结果,只返回符合条件的顶点索引号(或顶点名,如果设置了的话),如果想返回这些顶点的边序列,可以用E()重新检索一下:

> d <- get_diameter(g)
> E(g,path = d)
+ 3/19 edges from ae4102a:
[1] 17->11 11-> 6  6-> 1
> V(g)$name <- paste0('L',1:gorder(g))
> get_diameter(g)
+ 4/20 vertices, named, from ae4102a:
[1] L17 L11 L6  L1 
> E(g,path = d)
+ 3/19 edges from ae4102a (vertex names):
[1] L17->L11 L11->L6  L6 ->L1 
3-4 显示全部或指定边的属性:edge_att函数
> g <- make_ring(10) %>% 
+   set_vertex_attr('name',value = letters[1:10]) %>% 
+   set_edge_attr('weight',value=rep(1:5,2)) %>% 
+   set_edge_attr('color',value = rep(c('orange','blue'),5))
> plot(g)
> g
IGRAPH 36b121b UNW- 10 10 -- Ring graph
+ attr: name (g/c), mutual (g/l), circular (g/l), name (v/c), weight
| (e/n), color (e/c)
+ edges from 36b121b (vertex names):
 [1] a--b b--c c--d d--e e--f f--g g--h h--i i--j a--j
> edge_attr(g)
$weight
 [1] 1 2 3 4 5 1 2 3 4 5

$color
 [1] "orange" "blue"   "orange" "blue"   "orange" "blue"   "orange" "blue"  
 [9] "orange" "blue" 

本函数同样可以检索指定边的属性,但要注意的是,在调用函数时,如果不能按形参位置分配实参的话,需要在实参前面明示形参名,比如:

> edge_attr(g,1:3)
Error in .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_edge)[[name]] :1层没有这一索引
> edge_attr(g,index = 1:3)
$weight
[1] 1 2 3

$color
[1] "orange" "blue"   "orange"

如同其他OOP语言的对象,在对象之外是无法直接引用对象的属性的,需要通过对象来引用其属性。igraph中边的属性也是这样,如需引用,用E(g)$weight的方式

在这里插入图片描述

3-5 设置全部或指定边的属性:edge_attr <-函数,单独使用,每次调用可以同时设置多个属性
> g <- make_ring(10)
> edge_attr(g) <- list(
+   weight=1:10,
+   color=rep('red',10)
+ )
> edge_attr(g)
$weight
 [1]  1  2  3  4  5  6  7  8  9 10

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

注意事项同vertex_attr <-函数

3-6 只显示边属性名:edge_attr_names函数
> edge_attr_names(g)
[1] "weight" "color"
3-7 设置顶点属性:set_edge_attr函数,在管道函数中使用,每次调用只能设置一个属性

示例见前。

3-8 设置边属性:语法糖E(g)$,每次调用只能设置一个属性
g <- make_star(10, center=10)
E(g)$width <- sample(1:10, ecount(g), replace=TRUE)
3-9 图的阶数(order)和规模(size)函数:gorder() & gsize()
> # ecount() 和 gsize()相同,指图中边的数量
> ecount(g)
[1] 10
> gsize(g)
[1] 10
> # vcount() 和 gorder()相同,指图中顶点的数量
> vcount(g)
[1] 10
> gorder(g)
[1] 10
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值