用于使用 igraph C 库
本节介绍在图中查找小型诱导子图的函数。这些函数最初由 Holland 和 Leinhardt 定义,用于两个和三个顶点的子图,并命名为二元组普查和三元组普查。
igraph_error_t igraph_dyad_census(const igraph_t *graph, igraph_real_t *mut, igraph_real_t *asym, igraph_real_t *null);
二元组普查意味着将有向图的每对顶点分为三类:互惠(至少存在一条从 a
到 b
以及从 b
到 a
的边);非对称(至少存在一条从 a
到 b
或从 b
到 a
的边,但不是反向)和空(a
和 b
之间在任何方向上都没有边)。
Holland, P.W. 和 Leinhardt, S. (1970). A Method for Detecting Structure in Sociometric Data. American Journal of Sociology, 70, 492-513.
参数:
|
输入图。对于无向图,不存在非对称连接。 |
|
指向实数的指针,互惠二元组的数量存储在此处。 |
|
指向实数的指针,非对称二元组的数量存储在此处。 |
|
指向实数的指针,空二元组的数量存储在此处。 |
返回值:
错误代码。 |
另请参阅:
时间复杂度:O(|V|+|E|),顶点数加上边数。
igraph_error_t igraph_triad_census(const igraph_t *graph, igraph_vector_t *res);
计算三元组普查意味着根据它包含的成对连接的类型(即互惠、非对称或无连接)对有向图中的每三个顶点进行分类。一个三元组可以处于 16 种状态之一,通常使用 Davis 和 Leinhardt 的“MAN 标签”来描述。 res
向量将包含这些计数,顺序如下
|
A、B、C,空图。 |
|
A->B, C,具有单条有向边的图。 |
|
A<->B, C,两个顶点之间具有互惠连接的图。 |
|
A<-B->C,二叉出树。 |
|
A->B<-C,二叉入树。 |
|
A->B->C,有向线。 |
|
A<->B<-C. |
|
A<->B->C. |
|
A->B<-C, A->C. |
|
A<-B<-C, A->C. |
|
A<->B<->C. |
|
A<-B->C, A<->C. |
|
A->B<-C, A<->C. |
|
A->B->C, A<->C. |
|
A->B<->C, A<->C. |
|
A<->B<->C, A<->C,完全图。 |
此函数用于有向图。如果输入是无向图,则会显示警告,并且无向边将被解释为互惠。
此函数调用 igraph_motifs_randesu()
,它是 FANMOD 基序查找器工具的实现,有关详细信息,请参见 igraph_motifs_randesu()
。请注意,igraph_triad_census()
和 igraph_motifs_randesu()
的三元组顺序不同。
参考
Davis, J.A. 和 Leinhardt, S. (1972). The Structure of Positive Interpersonal Relations in Small Groups. In J. Berger (Ed.), Sociological Theories in Progress, Volume 2, 218-251. Boston: Houghton Mifflin.
参数:
|
输入图。 |
|
指向已初始化向量的指针,结果按照上面列表给出的相同顺序存储在此处。请注意,此顺序与 |
返回值:
错误代码。 |
另请参阅:
时间复杂度:待定。
igraph_error_t igraph_count_adjacent_triangles(const igraph_t *graph, igraph_vector_t *res, const igraph_vs_t vids);
参数:
|
输入图。边方向和多重性将被忽略。 |
|
已初始化向量,结果存储在此处。 |
|
要执行计算的顶点。 |
返回值:
错误模式。 |
另请参阅:
|
时间复杂度:O(d^2 n),d 是查询顶点的平均顶点度,n 是它们的数量。
igraph_error_t igraph_count_triangles(const igraph_t *graph, igraph_real_t *res);
此函数计算图中的三角形总数,即完全连接的顶点三元组。边方向、边多重性和自环将被忽略。
参数:
|
图对象。边方向和多重性将被忽略。 |
|
指向实变量的指针,结果将存储在此处。 |
返回值:
错误代码: |
另请参阅:
时间复杂度:O(|V|*d^2),|V| 是图中顶点的数量,d 是平均节点度。
igraph_error_t igraph_list_triangles(const igraph_t *graph, igraph_vector_int_t *res);
三角形报告为顶点 ID 三元组的长列表。使用 igraph_matrix_view_from_vector()
的 int
变体创建一个矩阵视图到向量中,其中每个三角形都存储在矩阵的一列中(请参见示例)。
参数:
|
输入图,边方向将被忽略。多条边将被忽略。 |
|
指向已初始化整数向量的指针,结果存储在此处,以顶点 ID 三元组的长列表形式存储。每个三元组都是图中的一个三角形。每个三角形只列出一次。 |
返回值:
错误代码。 |
另请参阅:
|
时间复杂度:O(d^2 n),d 是平均度,n 是顶点的数量。
示例 19.1. 文件 examples/simple/igraph_list_triangles.c
#include <igraph.h> int main(void) { igraph_t g; igraph_vector_int_t v; igraph_matrix_int_t result; igraph_full(&g, 5, 0, IGRAPH_NO_LOOPS); printf("Triangles in a full graph of 5 vertices:\n"); igraph_vector_int_init(&v, 0); igraph_list_triangles(&g, &v); igraph_matrix_int_view_from_vector(&result, &v, /* nrow = */ 3); igraph_matrix_int_print(&result); igraph_vector_int_destroy(&v); igraph_destroy(&g); return 0; }
igraph_error_t igraph_motifs_randesu(const igraph_t *graph, igraph_vector_t *hist, igraph_integer_t size, const igraph_vector_t *cut_prob);
基序是图中给定结构的小型弱连接诱导子图。有人认为,基序配置文件(即图中不同基序的数量)是不同类型的网络的特征,并且网络功能与图中的基序相关。
此函数能够查找大小为三和四的有向基序以及大小为三到六的无向基序(即网络中具有三到六个顶点的不同子图的数量)。
在大型网络中,基序的总数可能非常大,因此找到所有基序需要花费大量时间。在这种情况下,可以使用抽样方法。此函数能够通过 cut_prob
参数进行抽样。此参数给出了不探索基序搜索树的一个分支的概率。有关详细信息,请参见 S. Wernicke 和 F. Rasche:FANMOD:一种用于快速网络基序检测的工具,Bioinformatics 22(9), 1152--1153, 2006。 https://doi.org/10.1093/bioinformatics/btl038
将 cut_prob
参数设置为零向量以查找所有基序。
有向基序将在有向图中计数,无向基序将在无向图中计数。
参数:
|
要在其中查找基序的图。 |
|
计算结果,它给出了为每个同构类找到的基序数量。有关同构类的帮助,请参见 |
|
要搜索的基序大小。对于有向图,仅实现了 3 和 4,对于无向图,实现了 3 到 6。限制不在于基序查找代码,而在于图同构代码。 |
|
在给定级别切割搜索树的概率向量。第一个元素是第一个级别,依此类推。要执行完整搜索并找到所有基序,请提供长度为 |
返回值:
错误代码。 |
另请参阅:
|
时间复杂度:待定。
示例 19.2. 文件 examples/simple/igraph_motifs_randesu.c
#include <igraph.h> /* This is a callback function suitable for use with igraph_motifs_randesu_callback(). * It prints each motif it is calld with. */ igraph_error_t print_motif(const igraph_t *graph, igraph_vector_int_t *vids, igraph_integer_t isoclass, void* extra) { printf("Found isoclass %2" IGRAPH_PRId ": ", isoclass); igraph_vector_int_print(vids); return IGRAPH_SUCCESS; /* Return 'IGRAPH_SUCCESS': do not interrupt the search. */ } int main(void) { igraph_t graph; igraph_vector_t hist; /* Compute the 4-motif distritbuion in Zachary's karate club network. */ igraph_famous(&graph, "Zachary"); igraph_vector_init(&hist, 0); igraph_motifs_randesu(&graph, &hist, 4, NULL); /* Compute the total number of motifs (connected 4-vertex subgraphs) * so that we can print the normalized distribution. */ igraph_real_t sum = 0.0; igraph_integer_t n = igraph_vector_size(&hist); for (igraph_integer_t i=0; i < n; i++) { if (!isnan(VECTOR(hist)[i])) { sum += VECTOR(hist)[i]; } } printf("4-motif distribution:\n"); for (igraph_integer_t i=0; i < n; i++) { /* Print NaN values in a platform-independent manner: */ igraph_real_printf(VECTOR(hist)[i] / sum); printf(" "); } printf("\n\n"); igraph_vector_destroy(&hist); igraph_destroy(&graph); /* Identify the vertices of each three-motif in a small Kautz graph. */ igraph_kautz(&graph, 2, 1); igraph_motifs_randesu_callback(&graph, 3, NULL, &print_motif, NULL); igraph_destroy(&graph); return 0; }
igraph_error_t igraph_motifs_randesu_no(const igraph_t *graph, igraph_integer_t *no, igraph_integer_t size, const igraph_vector_t *cut_prob);
此函数计算 size
个顶点上的(弱)连接诱导子图的总数,而无需将同构类分配给它们。支持任意大的基序大小。
参数:
|
要研究的图对象。 |
|
指向整数类型的指针,结果将存储在此处。 |
|
要计数的基序的大小。 |
|
在给定级别切割搜索树的概率向量。第一个元素是第一个级别,依此类推。要执行完整搜索并找到所有连接的子图,请提供长度为 |
返回值:
错误代码。 |
另请参阅:
时间复杂度:待定。
igraph_error_t igraph_motifs_randesu_estimate(const igraph_t *graph, igraph_integer_t *est, igraph_integer_t size, const igraph_vector_t *cut_prob, igraph_integer_t sample_size, const igraph_vector_int_t *parsample);
此函数估计 size
个顶点上的(弱)连接诱导子图的总数。例如,n
个顶点上的无向完全图将具有一个大小为 n
的基序,以及 n
个大小为 size
n - 1
的基序。作为另一个示例,一个三角形和一个单独的顶点将具有零个大小为四的基序。
此函数对于无法计算所有连接子图的大型图非常有用,因为它们太多了。
通过获取顶点样本并计算包含这些顶点的所有连接子图来进行估计。还有一个 cut_prob
参数,它给出了切割搜索树分支的概率。
参数:
|
要研究的图对象。 |
|
指向整数的指针,结果将存储在此处。 |
|
要查找的子图的大小。 |
|
在给定级别切割搜索树的概率向量。第一个元素是第一个级别,依此类推。要执行完整搜索并找到所有基序,请提供长度为 |
|
用作样本的顶点数。仅当 |
|
指向已初始化向量的指针或空指针。如果是一个向量,则向量中的顶点 ID 将用作样本。如果是一个空指针,则 |
返回值:
错误代码。 |
另请参阅:
时间复杂度:待定。
igraph_error_t igraph_motifs_randesu_callback(const igraph_t *graph, igraph_integer_t size, const igraph_vector_t *cut_prob, igraph_motifs_handler_t *callback, void* extra);
与 igraph_motifs_randesu()
类似,此函数能够查找大小为三和四的有向基序以及大小为三到六的无向基序(即网络中具有三到六个顶点的不同子图的数量)。但是,该函数不是计数它们,而是会为找到的每个基序调用一个回调函数,以允许进一步测试或后处理。
与 igraph_motifs_randesu()
一样,cut_prob
参数也允许对基序进行抽样。将 cut_prob
参数设置为零向量以查找所有基序。
参数:
|
要在其中查找基序的图。 |
|
要搜索的基序的大小。目前仅实现了三和四。限制不在于基序查找代码,而在于图同构代码。 |
|
在给定级别切割搜索树的概率向量。第一个元素是第一个级别,依此类推。要执行完整搜索并找到所有基序,请提供长度为 |
|
指向 |
|
要传递给回调函数的额外参数。 |
返回值:
错误代码。 |
时间复杂度:待定。
示例 19.3. 文件 examples/simple/igraph_motifs_randesu.c
#include <igraph.h> /* This is a callback function suitable for use with igraph_motifs_randesu_callback(). * It prints each motif it is calld with. */ igraph_error_t print_motif(const igraph_t *graph, igraph_vector_int_t *vids, igraph_integer_t isoclass, void* extra) { printf("Found isoclass %2" IGRAPH_PRId ": ", isoclass); igraph_vector_int_print(vids); return IGRAPH_SUCCESS; /* Return 'IGRAPH_SUCCESS': do not interrupt the search. */ } int main(void) { igraph_t graph; igraph_vector_t hist; /* Compute the 4-motif distritbuion in Zachary's karate club network. */ igraph_famous(&graph, "Zachary"); igraph_vector_init(&hist, 0); igraph_motifs_randesu(&graph, &hist, 4, NULL); /* Compute the total number of motifs (connected 4-vertex subgraphs) * so that we can print the normalized distribution. */ igraph_real_t sum = 0.0; igraph_integer_t n = igraph_vector_size(&hist); for (igraph_integer_t i=0; i < n; i++) { if (!isnan(VECTOR(hist)[i])) { sum += VECTOR(hist)[i]; } } printf("4-motif distribution:\n"); for (igraph_integer_t i=0; i < n; i++) { /* Print NaN values in a platform-independent manner: */ igraph_real_printf(VECTOR(hist)[i] / sum); printf(" "); } printf("\n\n"); igraph_vector_destroy(&hist); igraph_destroy(&graph); /* Identify the vertices of each three-motif in a small Kautz graph. */ igraph_kautz(&graph, 2, 1); igraph_motifs_randesu_callback(&graph, 3, NULL, &print_motif, NULL); igraph_destroy(&graph); return 0; }
typedef igraph_error_t igraph_motifs_handler_t(const igraph_t *graph, igraph_vector_int_t *vids, igraph_integer_t isoclass, void* extra);
igraph_motifs_randesu_callback()
在基序搜索期间找到新基序时调用指定的回调函数。此回调函数必须是 igraph_motifs_handler_t
类型。它具有以下参数
参数:
|
算法正在处理的图。当然,这不能修改。 |
|
刚刚找到的基序中顶点的 ID。此向量归基序搜索算法所有,因此请勿修改或销毁它;如果以后需要它,请复制它。 |
|
刚刚找到的基序的同构类。使用 |
|
传递给 |
返回值:
|
另请参阅:
igraph_error_t igraph_adjacent_triangles(const igraph_t *graph, igraph_vector_t *res, const igraph_vs_t vids);
自 0.10.15 版本起已弃用。请不要在新代码中使用此函数;请改用 igraph_count_adjacent_triangles()
。
参数:
|
输入图。边方向和多重性将被忽略。 |
|
已初始化向量,结果存储在此处。 |
|
要执行计算的顶点。 |
返回值:
错误模式。 |
另请参阅:
|
时间复杂度:O(d^2 n),d 是查询顶点的平均顶点度,n 是它们的数量。
← 第 18 章. 图着色 | 第 20 章. 生成用于图形绘制的布局 → |