For using the igraph C library
A bipartite network contains two kinds of vertices and connections are only possible between two vertices of different kind. There are many natural examples, e.g. movies and actors as vertices and a movie is connected to all participating actors, etc.
igraph does not have direct support for bipartite networks, at
least not at the C language level. In other words the igraph_t
structure does not contain information about the vertex types.
The C functions for bipartite networks usually have an additional
input argument to graph, called types
, a boolean vector giving
the vertex types.
Most functions creating bipartite networks are able to create this extra vector, you just need to supply an initialized boolean vector to them.
int igraph_create_bipartite(igraph_t *graph, const igraph_vector_bool_t *types, const igraph_vector_t *edges, igraph_bool_t directed);
This is a simple wrapper function to create a bipartite graph. It
does a little more than igraph_create()
, e.g. it checks that
the graph is indeed bipartite with respect to the given types
vector. If there is an edge connecting two vertices of the same
kind, then an error is reported.
Arguments:
|
Pointer to an uninitialized graph object, the result is created here. |
|
Boolean vector giving the vertex types. The length of the vector defines the number of vertices in the graph. |
|
Vector giving the edges of the graph. The highest
vertex id in this vector must be smaller than the length of the
|
|
Boolean scalar, whether to create a directed graph. |
Returns:
Error code. |
Time complexity: O(|V|+|E|), linear in the number of vertices and edges.
Example 31.1. File examples/simple/igraph_bipartite_create.c
/* -*- mode: C -*- */ /* IGraph library. Copyright (C) 2008-2012 Gabor Csardi <csardi.gabor@gmail.com> 334 Harvard street, Cambridge, MA 02139 USA This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include <igraph.h> int main() { igraph_real_t edges2[] = {0, 1, 1, 2, 3, 4, 5, 6, 6, 5, 1, 4, 1, 6, 0, 3 }; igraph_real_t edges3[] = {0, 1, 1, 2, 3, 4, 5, 6, 6, 5, 2, 4, 1, 6, 0, 3 }; igraph_t g; igraph_vector_bool_t types; igraph_vector_t edges; long int i; int ret; igraph_vector_view(&edges, edges2, sizeof(edges2) / sizeof(igraph_real_t)); igraph_vector_bool_init(&types, igraph_vector_max(&edges) + 1); for (i = 0; i < igraph_vector_bool_size(&types); i++) { VECTOR(types)[i] = i % 2; } igraph_create_bipartite(&g, &types, &edges, /*directed=*/ 1); igraph_write_graph_edgelist(&g, stdout); igraph_vector_bool_destroy(&types); igraph_destroy(&g); /* Error handling */ igraph_set_error_handler(igraph_error_handler_ignore); igraph_vector_view(&edges, edges3, sizeof(edges3) / sizeof(igraph_real_t)); igraph_vector_bool_init(&types, igraph_vector_max(&edges) + 1); for (i = 0; i < igraph_vector_bool_size(&types); i++) { VECTOR(types)[i] = i % 2; } ret = igraph_create_bipartite(&g, &types, &edges, /*directed=*/ 1); if (ret != IGRAPH_EINVAL) { return 1; } igraph_vector_bool_destroy(&types); igraph_destroy(&g); return 0; }
int igraph_full_bipartite(igraph_t *graph, igraph_vector_bool_t *types, igraph_integer_t n1, igraph_integer_t n2, igraph_bool_t directed, igraph_neimode_t mode);
A bipartite network contains two kinds of vertices and connections are only possible between two vertices of different kind. There are many natural examples, e.g. movies and actors as vertices and a movie is connected to all participating actors, etc.
igraph does not have direct support for bipartite networks, at
least not at the C language level. In other words the igraph_t
structure does not contain information about the vertex types.
The C functions for bipartite networks usually have an additional
input argument to graph, called types
, a boolean vector giving
the vertex types.
Most functions creating bipartite networks are able to create this extra vector, you just need to supply an initialized boolean vector to them.
Arguments:
|
Pointer to an igraph_t object, the graph will be created here. |
|
Pointer to a boolean vector. If not a null pointer, then the vertex types will be stored here. |
|
Integer, the number of vertices of the first kind. |
|
Integer, the number of vertices of the second kind. |
|
Boolean, whether to create a directed graph. |
|
A constant that gives the type of connections for
directed graphs. If |
Returns:
Error code. |
Time complexity: O(|V|+|E|), linear in the number of vertices and edges.
See also:
|
int igraph_bipartite_game(igraph_t *graph, igraph_vector_bool_t *types, igraph_erdos_renyi_t type, igraph_integer_t n1, igraph_integer_t n2, igraph_real_t p, igraph_integer_t m, igraph_bool_t directed, igraph_neimode_t mode);
Similarly to unipartite (one-mode) networks, we can define the G(n,p), and G(n,m) graph classes for bipartite graphs, via their generating process. In G(n,p) every possible edge between top and bottom vertices is realized with probablity p, independently of the rest of the edges. In G(n,m), we uniformly choose m edges to realize.
Arguments:
|
Pointer to an uninitialized igraph graph, the result is stored here. |
||||
|
Pointer to an initialized boolean vector, or a null pointer. If not a null pointer, then the vertex types are stored here. Bottom vertices come first, n1 of them, then n2 top vertices. |
||||
|
The type of the random graph, possible values:
|
||||
|
The number of bottom vertices. |
||||
|
The number of top verices. |
||||
|
The connection probability for G(n,p) graphs. It is ignored for G(n,m) graphs. |
||||
|
The number of edges for G(n,m) graphs. It is ignored for G(n,p) graphs. |
||||
|
Boolean, whether to generate a directed graph. See
also the |
||||
|
Specifies how to direct the edges in directed
graphs. If it is |
Returns:
Error code. |
See also:
Time complexity: O(|V|+|E|), linear in the number of vertices and edges.
int igraph_incidence(igraph_t *graph, igraph_vector_bool_t *types, const igraph_matrix_t *incidence, igraph_bool_t directed, igraph_neimode_t mode, igraph_bool_t multiple);
A bipartite (or two-mode) graph contains two types of vertices and edges always connect vertices of different types. An incidence matrix is an nxm matrix, n and m are the number of vertices of the two types, respectively. Nonzero elements in the matrix denote edges between the two corresponding vertices.
Note that this function can operate in two modes, depending on the
multiple
argument. If it is FALSE (i.e. 0), then a single edge is
created for every non-zero element in the incidence matrix. If multiple
is TRUE (i.e. 1), then the matrix elements are rounded up
to the closest non-negative integer to get the number of edges to
create between a pair of vertices.
This function does not create multiple edges if multiple
is
FALSE, but might create some if it is TRUE.
Arguments:
|
Pointer to an uninitialized graph object. |
|
Pointer to an initialized boolean vector, or a null pointer. If not a null pointer, then the vertex types are stored here. It is resized as needed. |
|
The incidence matrix. |
|
Gives whether to create an undirected or a directed graph. |
|
Specifies the direction of the edges in a directed
graph. If |
|
How to interpret the incidence matrix elements. See details below. |
Returns:
Error code. |
Time complexity: O(n*m), the size of the incidence matrix.
int igraph_get_incidence(const igraph_t *graph, const igraph_vector_bool_t *types, igraph_matrix_t *res, igraph_vector_t *row_ids, igraph_vector_t *col_ids);
Arguments:
|
The input graph, edge directions are ignored. |
|
Boolean vector containing the vertex types. |
|
Pointer to an initialized matrix, the result is stored here. An element of the matrix gives the number of edges (irrespectively of their direction) between the two corresponding vertices. |
|
Pointer to an initialized vector or a null pointer. If not a null pointer, then the vertex ids (in the graph) corresponding to the rows of the result matrix are stored here. |
|
Pointer to an initialized vector or a null pointer. If not a null pointer, then the vertex ids corresponding to the columns of the result matrix are stored here. |
Returns:
Error code. |
Time complexity: O(n*m), n and m are number of vertices of the two different kind.
See also:
|
int igraph_bipartite_projection_size(const igraph_t *graph, const igraph_vector_bool_t *types, igraph_integer_t *vcount1, igraph_integer_t *ecount1, igraph_integer_t *vcount2, igraph_integer_t *ecount2);
This function calculates the number of vertices and edges in the two projections of a bipartite network. This is useful if you have a big bipartite network and you want to estimate the amount of memory you would need to calculate the projections themselves.
Arguments:
|
The input graph. |
|
Boolean vector giving the vertex types of the graph. |
|
Pointer to an |
|
Pointer to an |
|
Pointer to an |
|
Pointer to an |
Returns:
Error code. |
See also:
|
Time complexity: O(|V|*d^2+|E|), |V| is the number of vertices, |E| is the number of edges, d is the average (total) degree of the graphs.
Example 31.2. File examples/simple/igraph_bipartite_projection.c
/* -*- mode: C -*- */ /* IGraph library. Copyright (C) 2008-2012 Gabor Csardi <csardi.gabor@gmail.com> 334 Harvard street, Cambridge, MA 02139 USA This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include <igraph.h> #include <stdlib.h> int check_projection(const igraph_t *graph, const igraph_vector_bool_t *types, const igraph_t *proj1, const igraph_t *proj2) { igraph_integer_t vcount1, ecount1, vcount2, ecount2; igraph_bipartite_projection_size(graph, types, &vcount1, &ecount1, &vcount2, &ecount2); if (proj1 && igraph_vcount(proj1) != vcount1) { exit(10); } if (proj1 && igraph_ecount(proj1) != ecount1) { exit(11); } if (proj2 && igraph_vcount(proj2) != vcount2) { exit(12); } if (proj2 && igraph_ecount(proj2) != ecount2) { exit(13); } return 0; } int main() { igraph_t g, p1, p2, full, ring; igraph_vector_bool_t types; igraph_bool_t iso; long int i, m2 = 0, w, f, t; igraph_vector_t mult1, mult2; /*******************************************************/ /* Full bipartite graph -> full graphs */ /*******************************************************/ igraph_vector_bool_init(&types, 0); igraph_full_bipartite(&g, &types, 5, 3, /*directed=*/ 0, /*mode=*/ IGRAPH_ALL); /* Get both projections */ igraph_bipartite_projection(&g, &types, &p1, &p2, 0, 0, /*probe1=*/ -1); check_projection(&g, &types, &p1, &p2); /* Check first projection */ igraph_full(&full, igraph_vcount(&p1), /*directed=*/0, /*loops=*/0); igraph_isomorphic_bliss(&p1, &full, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 1; } igraph_destroy(&full); /* Check second projection */ igraph_full(&full, igraph_vcount(&p2), /*directed=*/0, /*loops=*/0); igraph_isomorphic_bliss(&p2, &full, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 2; } igraph_destroy(&full); igraph_destroy(&p1); igraph_destroy(&p2); igraph_destroy(&g); igraph_vector_bool_destroy(&types); /*******************************************************/ /* More sophisticated test */ /*******************************************************/ igraph_ring(&g, 100, /*directed=*/ 1, /*mutual=*/ 1, /*circular=*/ 1); igraph_vector_bool_init(&types, igraph_vcount(&g)); for (i = 0; i < igraph_vector_bool_size(&types); i++) { VECTOR(types)[i] = i % 2 ? 0 : 1; } /* Get both projections */ igraph_bipartite_projection(&g, &types, &p1, &p2, 0, 0, /*probe1=*/ -1); check_projection(&g, &types, &p1, &p2); /* Check first projection */ igraph_ring(&ring, igraph_vcount(&g) / 2, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1); igraph_isomorphic_bliss(&p1, &ring, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 1; } /* Check second projection */ igraph_isomorphic_bliss(&p2, &ring, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 2; } igraph_destroy(&ring); igraph_destroy(&p1); igraph_destroy(&p2); igraph_destroy(&g); igraph_vector_bool_destroy(&types); /*******************************************************/ /* Multiplicity test */ /*******************************************************/ igraph_small(&g, 10, IGRAPH_UNDIRECTED, 0, 8, 1, 8, 2, 8, 3, 8, 4, 8, 4, 9, 5, 9, 6, 9, 7, 9, 0, 9, -1); igraph_vector_bool_init(&types, igraph_vcount(&g)); igraph_vector_bool_fill(&types, 1); VECTOR(types)[8] = VECTOR(types)[9] = 0; igraph_vector_init(&mult1, 0); igraph_vector_init(&mult2, 0); igraph_bipartite_projection(&g, &types, &p1, &p2, &mult1, &mult2, /*probe=*/ -1); check_projection(&g, &types, &p1, &p2); if (igraph_vector_size(&mult1) != igraph_ecount(&p1)) { return 21; } if (igraph_vector_size(&mult2) != igraph_ecount(&p2)) { return 22; } if (VECTOR(mult1)[0] != 2) { return 23; } for (i = 0; i < igraph_vector_size(&mult2); i++) { if (VECTOR(mult2)[i] != 1 && VECTOR(mult2)[i] != 2) { return 24; } if (VECTOR(mult2)[i] == 2) { m2++; w = i; } } if (m2 != 1) { return 25; } f = IGRAPH_FROM(&p2, w); t = IGRAPH_TO(&p2, w); if (fmin(f, t) != 0 || fmax(f, t) != 4) { return 26; } igraph_vector_destroy(&mult1); igraph_vector_destroy(&mult2); igraph_destroy(&p1); igraph_destroy(&p2); igraph_destroy(&g); igraph_vector_bool_destroy(&types); return 0; }
int igraph_bipartite_projection(const igraph_t *graph, const igraph_vector_bool_t *types, igraph_t *proj1, igraph_t *proj2, igraph_vector_t *multiplicity1, igraph_vector_t *multiplicity2, igraph_integer_t probe1);
Creates one or both projections of a bipartite graph.
Arguments:
|
The bipartite input graph. Directedness of the edges is ignored. |
|
Boolean vector giving the vertex types of the graph. |
|
Pointer to an uninitialized graph object, the first
projection will be created here. It a null pointer, then it is
ignored, see also the |
|
Pointer to an uninitialized graph object, the second
projection is created here, if it is not a null pointer. See also
the |
|
Pointer to a vector, or a null pointer. If not the latter, then the multiplicity of the edges is stored here. E.g. if there is an A-C-B and also an A-D-B triple in the bipartite graph (but no more X, such that A-X-B is also in the graph), then the multiplicity of the A-B edge in the projection will be 2. |
|
The same as |
|
This argument can be used to specify the order of the
projections in the resulting list. When it is non-negative, then
it is considered as a vertex ID and the projection containing
this vertex will be the first one in the result. Setting this
argument to a non-negative value implies that |
Returns:
Error code. |
See also:
|
Time complexity: O(|V|*d^2+|E|), |V| is the number of vertices, |E| is the number of edges, d is the average (total) degree of the graphs.
Example 31.3. File examples/simple/igraph_bipartite_projection.c
/* -*- mode: C -*- */ /* IGraph library. Copyright (C) 2008-2012 Gabor Csardi <csardi.gabor@gmail.com> 334 Harvard street, Cambridge, MA 02139 USA This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include <igraph.h> #include <stdlib.h> int check_projection(const igraph_t *graph, const igraph_vector_bool_t *types, const igraph_t *proj1, const igraph_t *proj2) { igraph_integer_t vcount1, ecount1, vcount2, ecount2; igraph_bipartite_projection_size(graph, types, &vcount1, &ecount1, &vcount2, &ecount2); if (proj1 && igraph_vcount(proj1) != vcount1) { exit(10); } if (proj1 && igraph_ecount(proj1) != ecount1) { exit(11); } if (proj2 && igraph_vcount(proj2) != vcount2) { exit(12); } if (proj2 && igraph_ecount(proj2) != ecount2) { exit(13); } return 0; } int main() { igraph_t g, p1, p2, full, ring; igraph_vector_bool_t types; igraph_bool_t iso; long int i, m2 = 0, w, f, t; igraph_vector_t mult1, mult2; /*******************************************************/ /* Full bipartite graph -> full graphs */ /*******************************************************/ igraph_vector_bool_init(&types, 0); igraph_full_bipartite(&g, &types, 5, 3, /*directed=*/ 0, /*mode=*/ IGRAPH_ALL); /* Get both projections */ igraph_bipartite_projection(&g, &types, &p1, &p2, 0, 0, /*probe1=*/ -1); check_projection(&g, &types, &p1, &p2); /* Check first projection */ igraph_full(&full, igraph_vcount(&p1), /*directed=*/0, /*loops=*/0); igraph_isomorphic_bliss(&p1, &full, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 1; } igraph_destroy(&full); /* Check second projection */ igraph_full(&full, igraph_vcount(&p2), /*directed=*/0, /*loops=*/0); igraph_isomorphic_bliss(&p2, &full, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 2; } igraph_destroy(&full); igraph_destroy(&p1); igraph_destroy(&p2); igraph_destroy(&g); igraph_vector_bool_destroy(&types); /*******************************************************/ /* More sophisticated test */ /*******************************************************/ igraph_ring(&g, 100, /*directed=*/ 1, /*mutual=*/ 1, /*circular=*/ 1); igraph_vector_bool_init(&types, igraph_vcount(&g)); for (i = 0; i < igraph_vector_bool_size(&types); i++) { VECTOR(types)[i] = i % 2 ? 0 : 1; } /* Get both projections */ igraph_bipartite_projection(&g, &types, &p1, &p2, 0, 0, /*probe1=*/ -1); check_projection(&g, &types, &p1, &p2); /* Check first projection */ igraph_ring(&ring, igraph_vcount(&g) / 2, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1); igraph_isomorphic_bliss(&p1, &ring, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 1; } /* Check second projection */ igraph_isomorphic_bliss(&p2, &ring, 0, 0, &iso, 0, 0, IGRAPH_BLISS_FM, 0, 0); if (!iso) { return 2; } igraph_destroy(&ring); igraph_destroy(&p1); igraph_destroy(&p2); igraph_destroy(&g); igraph_vector_bool_destroy(&types); /*******************************************************/ /* Multiplicity test */ /*******************************************************/ igraph_small(&g, 10, IGRAPH_UNDIRECTED, 0, 8, 1, 8, 2, 8, 3, 8, 4, 8, 4, 9, 5, 9, 6, 9, 7, 9, 0, 9, -1); igraph_vector_bool_init(&types, igraph_vcount(&g)); igraph_vector_bool_fill(&types, 1); VECTOR(types)[8] = VECTOR(types)[9] = 0; igraph_vector_init(&mult1, 0); igraph_vector_init(&mult2, 0); igraph_bipartite_projection(&g, &types, &p1, &p2, &mult1, &mult2, /*probe=*/ -1); check_projection(&g, &types, &p1, &p2); if (igraph_vector_size(&mult1) != igraph_ecount(&p1)) { return 21; } if (igraph_vector_size(&mult2) != igraph_ecount(&p2)) { return 22; } if (VECTOR(mult1)[0] != 2) { return 23; } for (i = 0; i < igraph_vector_size(&mult2); i++) { if (VECTOR(mult2)[i] != 1 && VECTOR(mult2)[i] != 2) { return 24; } if (VECTOR(mult2)[i] == 2) { m2++; w = i; } } if (m2 != 1) { return 25; } f = IGRAPH_FROM(&p2, w); t = IGRAPH_TO(&p2, w); if (fmin(f, t) != 0 || fmax(f, t) != 4) { return 26; } igraph_vector_destroy(&mult1); igraph_vector_destroy(&mult2); igraph_destroy(&p1); igraph_destroy(&p2); igraph_destroy(&g); igraph_vector_bool_destroy(&types); return 0; }
int igraph_is_bipartite(const igraph_t *graph, igraph_bool_t *res, igraph_vector_bool_t *types);
This function simply checks whether a graph \emph{could} be bipartite. It tries to find a mapping that gives a possible division of the vertices into two classes, such that no two vertices of the same class are connected by an edge.
The existence of such a mapping is equivalent of having no circuits of odd length in the graph. A graph with loop edges cannot bipartite.
Note that the mapping is not necessarily unique, e.g. if the graph has at least two components, then the vertices in the separate components can be mapped independently.
Arguments:
|
The input graph. |
|
Pointer to a boolean, the result is stored here. |
|
Pointer to an initialized boolean vector, or a null pointer. If not a null pointer and a mapping was found, then it is stored here. If not a null pointer, but no mapping was found, the contents of this vector is invalid. |
Returns:
Error code. |
Time complexity: O(|V|+|E|), linear in the number of vertices and edges.
← Chapter 30. Using BLAS, LAPACK and ARPACK for igraph matrices and graphs | Chapter 32. Advanced igraph programming → |