igraph Reference Manual

For using the igraph C library

Search the manual:

Chapter 4. About igraph graphs, the basic interface

1. The igraph data model

The igraph library can handle directed and undirected graphs. The igraph graphs are multisets of ordered (if directed) or unordered (if undirected) labeled pairs. The labels of the pairs plus the number of vertices always starts with zero and ends with the number of edges minus one. In addition to that, a table of metadata is also attached to every graph, its most important entries being the number of vertices in the graph and whether the graph is directed or undirected.

Like the edges, the igraph vertices are also labeled by numbers between zero and the number of vertices minus one. So, to summarize, a directed graph can be imagined like this:

  ( vertices: 6,
    directed: yes,
    {
     (0,2),
     (2,2),
     (3,2),
     (3,3),
     (3,4),
     (3,4),
     (4,3),
     (4,1)
    }
  )

Here the edges are ordered pairs or vertex ids, and the graph is a multiset of edges plus some metadata.

An undirected graph is like this:

  ( vertices: 6,
    directed: no,
    {
     (0,2),
     (2,2),
     (2,3),
     (3,3),
     (3,4),
     (3,4),
     (3,4),
     (1,4)
    }
  )

Here, an edge is an unordered pair of two vertex IDs. A graph is a multiset of edges plus metadata, just like in the directed case.

It is possible to convert between directed and undirected graphs, see the igraph_to_directed() and igraph_to_undirected() functions.

igraph aims to robustly support multigraphs, i.e. graphs which have more than one edge between some pairs of vertices, as well as graphs with self-loops. Most functions which do not support such graphs will check their input and issue an error if it is not valid. Those rare functions which do not perform this check clearly indicate this in their documentation. To eliminate multiple edges from a graph, you can use igraph_simplify().

2. About igraph functions

igraph has a simple and consistent interface. Most functions check their input for validity and display an informative error message when something goes wrong. In order to support this, the majority of functions return an error code. In basic usage, this code can be ignored, as the default behaviour is to abort the program immediately upon error. See the section on error handling for more information on this topic.

Results are typically returned through output arguments, i.e. pointers to a data structure into which the result will be written. In almost all cases, this data structure is expected to be pre-initialized. A few simple functions communicate their result directly through their return value—these functions can never encounter an error.

3. The basic interface

This is the very minimal API in igraph. All the other functions use this minimal set for creating and manipulating graphs.

This is a very important principle since it makes possible to implement other data representations by implementing only this minimal set.

This section lists all the functions and macros that are considered as part of the core API from the point of view of the users of igraph. Some of these functions and macros have sensible default implementations that simply call some other core function (e.g., igraph_empty() calls igraph_empty_attrs() with a null attribute table pointer). If you wish to experiment with implementing an alternative data type, the actual number of functions that you need to replace is lower as you can rely on the same default implementations in most cases.

3.1. Graph constructors and destructors

3.1.1. igraph_empty — Creates an empty graph with some vertices and no edges.

igraph_error_t igraph_empty(igraph_t *graph, igraph_integer_t n, igraph_bool_t directed);

The most basic constructor, all the other constructors should call this to create a minimal graph object. Our use of the term "empty graph" in the above description should be distinguished from the mathematical definition of the empty or null graph. Strictly speaking, the empty or null graph in graph theory is the graph with no vertices and no edges. However by "empty graph" as used in igraph we mean a graph having zero or more vertices, but no edges.

Arguments: 

graph:

Pointer to a not-yet initialized graph object.

n:

The number of vertices in the graph, a non-negative integer number is expected.

directed:

Boolean; whether the graph is directed or not. Supported values are:

IGRAPH_DIRECTED

The graph will be directed.

IGRAPH_UNDIRECTED

The graph will be undirected.

Returns: 

Error code: IGRAPH_EINVAL: invalid number of vertices.

Time complexity: O(|V|) for a graph with |V| vertices (and no edges).

Example 4.1.  File examples/simple/igraph_empty.c

#include <igraph.h>

int main() {

    igraph_t g;
    int ret;

    /* empty directed graph, zero vertices */
    igraph_empty(&g, 0, 1);
    if (igraph_vcount(&g) != 0) {
        return 1;
    }
    if (igraph_ecount(&g) != 0) {
        return 2;
    }
    igraph_destroy(&g);

    /* empty undirected graph, zero vertices */
    igraph_empty(&g, 0, 0);
    if (igraph_vcount(&g) != 0) {
        return 3;
    }
    if (igraph_ecount(&g) != 0) {
        return 4;
    }
    igraph_destroy(&g);

    /* empty directed graph, 20 vertices */
    igraph_empty(&g, 20, 1);
    if (igraph_vcount(&g) != 20) {
        return 5;
    }
    if (igraph_ecount(&g) != 0) {
        return 6;
    }
    igraph_destroy(&g);

    /* empty undirected graph, 30 vertices */
    igraph_empty(&g, 30, 0);
    if (igraph_vcount(&g) != 30) {
        return 7;
    }
    if (igraph_ecount(&g) != 0) {
        return 8;
    }
    igraph_destroy(&g);

    /* error: negative number of vertices */
    igraph_set_error_handler(igraph_error_handler_ignore);
    ret = igraph_empty(&g, -1, 0);
    if (ret != IGRAPH_EINVAL) {
        return 9;
    }

    return 0;
}


3.1.2. igraph_empty_attrs — Creates an empty graph with some vertices, no edges and some graph attributes.

igraph_error_t igraph_empty_attrs(igraph_t *graph, igraph_integer_t n, igraph_bool_t directed, void *attr);

Use this instead of igraph_empty() if you wish to add some graph attributes right after initialization. This function is currently not very interesting for the ordinary user. Just supply 0 here or use igraph_empty().

Arguments: 

graph:

Pointer to a not-yet initialized graph object.

n:

The number of vertices in the graph; a non-negative integer number is expected.

directed:

Boolean; whether the graph is directed or not. Supported values are:

IGRAPH_DIRECTED

Create a directed graph.

IGRAPH_UNDIRECTED

Create an undirected graph.

attr:

The attributes.

Returns: 

Error code: IGRAPH_EINVAL: invalid number of vertices.

Time complexity: O(|V|) for a graph with |V| vertices (and no edges).

3.1.3. igraph_copy — Creates an exact (deep) copy of a graph.

igraph_error_t igraph_copy(igraph_t *to, const igraph_t *from);

This function deeply copies a graph object to create an exact replica of it. The new replica should be destroyed by calling igraph_destroy() on it when not needed any more.

You can also create a shallow copy of a graph by simply using the standard assignment operator, but be careful and do not destroy a shallow replica. To avoid this mistake, creating shallow copies is not recommended.

Arguments: 

to:

Pointer to an uninitialized graph object.

from:

Pointer to the graph object to copy.

Returns: 

Error code.

Time complexity: O(|V|+|E|) for a graph with |V| vertices and |E| edges.

Example 4.2.  File examples/simple/igraph_copy.c

#include <igraph.h>

int main() {

    igraph_t g1, g2;
    igraph_vector_int_t v1, v2;

    igraph_vector_int_init(&v1, 8);
    VECTOR(v1)[0] = 0;
    VECTOR(v1)[1] = 1;
    VECTOR(v1)[2] = 1;
    VECTOR(v1)[3] = 2;
    VECTOR(v1)[4] = 2;
    VECTOR(v1)[5] = 3;
    VECTOR(v1)[6] = 2;
    VECTOR(v1)[7] = 2;

    igraph_create(&g1, &v1, 0, 0);
    igraph_copy(&g2, &g1);

    igraph_vector_int_init(&v2, 0);
    igraph_get_edgelist(&g2, &v2, 0);
    if (!igraph_vector_int_all_e(&v1, &v2)) {
        return 1;
    }

    igraph_vector_int_destroy(&v1);
    igraph_vector_int_destroy(&v2);
    igraph_destroy(&g1);
    igraph_destroy(&g2);

    return 0;
}


3.1.4. igraph_destroy — Frees the memory allocated for a graph object.

void igraph_destroy(igraph_t *graph);

This function should be called for every graph object exactly once.

This function invalidates all iterators (of course), but the iterators of a graph should be destroyed before the graph itself anyway.

Arguments: 

graph:

Pointer to the graph to free.

Time complexity: operating system specific.

3.2. Basic query operations

3.2.1. igraph_vcount — The number of vertices in a graph.

igraph_integer_t igraph_vcount(const igraph_t *graph);

Arguments: 

graph:

The graph.

Returns: 

Number of vertices.

Time complexity: O(1)

3.2.2. igraph_ecount — The number of edges in a graph.

igraph_integer_t igraph_ecount(const igraph_t *graph);

Arguments: 

graph:

The graph.

Returns: 

Number of edges.

Time complexity: O(1)

3.2.3. igraph_is_directed — Is this a directed graph?

igraph_bool_t igraph_is_directed(const igraph_t *graph);

Arguments: 

graph:

The graph.

Returns: 

Logical value, TRUE if the graph is directed, FALSE otherwise.

Time complexity: O(1)

Example 4.3.  File examples/simple/igraph_is_directed.c

#include <igraph.h>

int main() {

    igraph_t g;

    igraph_empty(&g, 0, 0);
    if (igraph_is_directed(&g)) {
        return 1;
    }
    igraph_destroy(&g);

    igraph_empty(&g, 0, 1);
    if (!igraph_is_directed(&g)) {
        return 2;
    }
    igraph_destroy(&g);

    return 0;
}


3.2.4. igraph_edge — Returns the head and tail vertices of an edge.

igraph_error_t igraph_edge(
    const igraph_t *graph, igraph_integer_t eid,
    igraph_integer_t *from, igraph_integer_t *to
);

Arguments: 

graph:

The graph object.

eid:

The edge ID.

from:

Pointer to an igraph_integer_t. The tail (source) of the edge will be placed here.

to:

Pointer to an igraph_integer_t. The head (target) of the edge will be placed here.

Returns: 

Error code. The current implementation always returns with success.

See also: 

igraph_get_eid() for the opposite operation; igraph_edges() to get the endpoints of several edges; IGRAPH_TO(), IGRAPH_FROM() and IGRAPH_OTHER() for a faster but non-error-checked version.

Added in version 0.2.

Time complexity: O(1).

3.2.5. igraph_edges — Gives the head and tail vertices of a series of edges.

igraph_error_t igraph_edges(const igraph_t *graph, igraph_es_t eids, igraph_vector_int_t *edges);

Arguments: 

graph:

The graph object.

eids:

Edge selector, the series of edges.

edges:

Pointer to an initialized vector. The start and endpoints of each edge will be placed here.

Returns: 

Error code.

See also: 

igraph_get_edgelist() to get the endpoints of all edges; igraph_get_eids() for the opposite operation; igraph_edge() for getting the endpoints of a single edge; IGRAPH_TO(), IGRAPH_FROM() and IGRAPH_OTHER() for a faster but non-error-checked method.

Time complexity: O(k) where k is the number of edges in the selector.

3.2.6. IGRAPH_FROM — The source vertex of an edge.

#define IGRAPH_FROM(graph,eid)

Faster than igraph_edge(), but no error checking is done: eid is assumed to be valid.

Arguments: 

graph:

The graph.

eid:

The edge ID.

Returns: 

The source vertex of the edge.

See also: 

igraph_edge() if error checking is desired.

3.2.7. IGRAPH_TO — The target vertex of an edge.

#define IGRAPH_TO(graph,eid)

Faster than igraph_edge(), but no error checking is done: eid is assumed to be valid.

Arguments: 

graph:

The graph object.

eid:

The edge ID.

Returns: 

The target vertex of the edge.

See also: 

igraph_edge() if error checking is desired.

3.2.8. IGRAPH_OTHER — The other endpoint of an edge.

#define IGRAPH_OTHER(graph,eid,vid)

Typically used with undirected edges when one endpoint of the edge is known, and the other endpoint is needed. No error checking is done: eid and vid are assumed to be valid.

Arguments: 

graph:

The graph object.

eid:

The edge ID.

vid:

The vertex ID of one endpoint of an edge.

Returns: 

The other endpoint of the edge.

See also: 

IGRAPH_TO() and IGRAPH_FROM() to get the source and target of directed edges.

3.2.9. igraph_get_eid — Get the edge ID from the end points of an edge.

igraph_error_t igraph_get_eid(const igraph_t *graph, igraph_integer_t *eid,
                   igraph_integer_t pfrom, igraph_integer_t pto,
                   igraph_bool_t directed, igraph_bool_t error);

For undirected graphs pfrom and pto are exchangeable.

Arguments: 

graph:

The graph object.

eid:

Pointer to an integer, the edge ID will be stored here.

pfrom:

The starting point of the edge.

pto:

The end point of the edge.

directed:

Logical constant, whether to search for directed edges in a directed graph. Ignored for undirected graphs.

error:

Logical scalar, whether to report an error if the edge was not found. If it is false, then -1 will be assigned to eid. Note that invalid vertex IDs in input arguments (pfrom or pto) always return an error code.

Returns: 

Error code.

See also: 

igraph_edge() for the opposite operation, igraph_get_all_eids_between() to retrieve all edge IDs between a pair of vertices.

Time complexity: O(log (d)), where d is smaller of the out-degree of pfrom and in-degree of pto if directed is true. If directed is false, then it is O(log(d)+log(d2)), where d is the same as before and d2 is the minimum of the out-degree of pto and the in-degree of pfrom.

Example 4.4.  File examples/simple/igraph_get_eid.c

#include <igraph.h>

int main() {
    igraph_t g;
    igraph_integer_t eid;
    igraph_vector_int_t hist;
    igraph_integer_t i;
    igraph_error_t ret;

    /* DIRECTED */

    igraph_star(&g, 10, IGRAPH_STAR_OUT, 0);

    igraph_vector_int_init(&hist, 9);

    for (i = 1; i < 10; i++) {
        igraph_get_eid(&g, &eid, 0, i, IGRAPH_DIRECTED, /*error=*/ 1);
        VECTOR(hist)[ eid ] = 1;
    }

    igraph_vector_int_print(&hist);

    igraph_vector_int_destroy(&hist);
    igraph_destroy(&g);

    /* UNDIRECTED */

    igraph_star(&g, 10, IGRAPH_STAR_UNDIRECTED, 0);

    igraph_vector_int_init(&hist, 9);

    for (i = 1; i < 10; i++) {
        igraph_get_eid(&g, &eid, 0, i, IGRAPH_UNDIRECTED, /*error=*/ 1);
        VECTOR(hist)[ eid ] += 1;
        igraph_get_eid(&g, &eid, i, 0, IGRAPH_DIRECTED, /*error=*/ 1);
        VECTOR(hist)[ eid ] += 1;
    }
    igraph_vector_int_print(&hist);

    igraph_vector_int_destroy(&hist);
    igraph_destroy(&g);

    /* NON-EXISTENT EDGE */

    igraph_star(&g, 10, IGRAPH_STAR_UNDIRECTED, 0);

    igraph_set_error_handler(igraph_error_handler_ignore);

    ret = igraph_get_eid(&g, &eid, 5, 6, IGRAPH_UNDIRECTED, /*error=*/ 1);
    if (ret != IGRAPH_EINVAL) {
        return 1;
    }

    /* INVALID VERTEX ID */

    ret = igraph_get_eid(&g, &eid, 171, 6, IGRAPH_UNDIRECTED, /*error=*/ 1);
    if (ret != IGRAPH_EINVVID) {
        return 1;
    }

    /* INVALID VERTEX ID even if error == 0 */

    ret = igraph_get_eid(&g, &eid, 171, 6, IGRAPH_UNDIRECTED, /*error=*/ 0);
    if (ret != IGRAPH_EINVVID) {
        return 1;
    }

    igraph_destroy(&g);

    return 0;
}

/* Stress test */

/* int main() { */

/*   igraph_t g; */
/*   igraph_integer_t i, n; */
/*   igraph_integer_t from, to, eid; */

/*   igraph_barabasi_game(&g, 10000, 100, 0, 0, 1); */
/*   n=igraph_ecount(&g); */
/*   for (i=0; i<n; i++) { */
/*     igraph_edge(&g, i, &from, &to); */
/*     igraph_get_eid(&g, &eid, from, to, 1, 1); */
/*     igraph_get_eid(&g, &eid, to, from, 0, 1); */
/*     igraph_get_eid(&g, &eid, from, to, 0, 1); */
/*   } */
/*   igraph_destroy(&g); */

/*   igraph_barabasi_game(&g, 10000, 100, 0, 0, 0); */
/*   n=igraph_ecount(&g); */
/*   for (i=0; i<n; i++) { */
/*     igraph_edge(&g, i, &from, &to); */
/*     igraph_get_eid(&g, &eid, from, to, 0, 1); */
/*     igraph_get_eid(&g, &eid, to, from, 0, 1); */
/*   } */
/*   igraph_destroy(&g); */

/*   igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, */
/*            2000, 100.0/2000, 0, 0); */
/*   n=igraph_ecount(&g); */
/*   for (i=0; i<n; i++) { */
/*     igraph_edge(&g, i, &from, &to); */
/*     igraph_get_eid(&g, &eid, from, to, 0, 1); */
/*     igraph_get_eid(&g, &eid, to, from, 0, 1); */
/*   } */
/*   igraph_destroy(&g); */

/*   igraph_full(&g, 500, 0, 0); */
/*   n=igraph_ecount(&g); */
/*   for (i=0; i<n; i++) { */
/*     igraph_edge(&g, i, &from, &to); */
/*     igraph_get_eid(&g, &eid, from, to, 0, 1); */
/*   } */
/*   igraph_destroy(&g); */

/*   igraph_star(&g, 20000, IGRAPH_STAR_OUT, 0); */
/*   n=igraph_ecount(&g); */
/*   for (i=0; i<n; i++) { */
/*     igraph_edge(&g, i, &from, &to); */
/*     igraph_get_eid(&g, &eid, from, to, 0, 1); */
/*   } */
/*   igraph_destroy(&g); */

/*   igraph_star(&g, 20000, IGRAPH_STAR_IN, 0); */
/*   n=igraph_ecount(&g); */
/*   for (i=0; i<n; i++) { */
/*     igraph_edge(&g, i, &from, &to); */
/*     igraph_get_eid(&g, &eid, from, to, 0, 1); */
/*   } */
/*   igraph_destroy(&g); */

/*   igraph_star(&g, 2000000, IGRAPH_STAR_UNDIRECTED, 1999999); */
/*   n=igraph_ecount(&g); */
/*   for (i=0; i<n; i++) { */
/*     igraph_edge(&g, i, &from, &to); */
/*     igraph_get_eid(&g, &eid, from, to, 0, 1); */
/*     igraph_get_eid(&g, &eid, to, from, 0, 1); */
/*   } */
/*   igraph_destroy(&g); */

/*   return 0; */
/* } */


Added in version 0.2.

3.2.10. igraph_get_eids — Return edge IDs based on the adjacent vertices.

igraph_error_t igraph_get_eids(const igraph_t *graph, igraph_vector_int_t *eids,
                    const igraph_vector_int_t *pairs,
                    igraph_bool_t directed, igraph_bool_t error);

The pairs of vertex IDs for which the edges are looked up are taken consecutively from the pairs vector, i.e. VECTOR(pairs)[0] and VECTOR(pairs)[1] specify the first pair, VECTOR(pairs)[2] and VECTOR(pairs)[3] the second pair, etc.

If you have a sequence of vertex IDs that describe a path on the graph, use igraph_expand_path_to_pairs() to convert them to a list of vertex pairs along the path.

If the error argument is true, then it is an error to specify pairs of vertices that are not connected. Otherwise -1 is reported for vertex pairs without at least one edge between them.

If there are multiple edges in the graph, then these are ignored; i.e. for a given pair of vertex IDs, igraph always returns the same edge ID, even if the pair appears multiple times in pairs.

Arguments: 

graph:

The input graph.

eids:

Pointer to an initialized vector, the result is stored here. It will be resized as needed.

pairs:

Vector giving pairs of vertices to fetch the edges for.

directed:

Logical scalar, whether to consider edge directions in directed graphs. This is ignored for undirected graphs.

error:

Logical scalar, whether it is an error to supply non-connected vertices. If false, then -1 is returned for non-connected pairs.

Returns: 

Error code.

Time complexity: O(n log(d)), where n is the number of queried edges and d is the average degree of the vertices.

See also: 

igraph_get_eid() for a single edge.

Example 4.5.  File examples/simple/igraph_get_eids.c

#include <igraph.h>
#include <stdlib.h>

void print_vector_int(igraph_vector_int_t *v, FILE *f) {
    igraph_integer_t i;
    for (i = 0; i < igraph_vector_int_size(v); i++) {
        fprintf(f, " %" IGRAPH_PRId, VECTOR(*v)[i]);
    }
    fprintf(f, "\n");
}

int check_simple() {

    igraph_t g;
    igraph_integer_t nodes = 100;
    igraph_integer_t edges = 1000;
    igraph_real_t p = 3.0 / nodes;
    igraph_integer_t runs = 10;
    igraph_integer_t r, e, ecount;
    igraph_vector_int_t eids, pairs, path;

    igraph_rng_seed(igraph_rng_default(), 42); /* make tests deterministic */

    igraph_vector_int_init(&pairs, edges * 2);
    igraph_vector_int_init(&path, 0);
    igraph_vector_int_init(&eids, 0);

    for (r = 0; r < runs; r++) {
        igraph_vector_int_resize(&pairs, edges * 2);
        igraph_vector_int_clear(&path);
        igraph_vector_int_clear(&eids);

        igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, nodes, p,
                                /*directed=*/ 0, /*loops=*/ 0);
        ecount = igraph_ecount(&g);
        for (e = 0; e < edges; e++) {
            igraph_integer_t edge = RNG_INTEGER(0, ecount - 1);
            VECTOR(pairs)[2 * e] = IGRAPH_FROM(&g, edge);
            VECTOR(pairs)[2 * e + 1] = IGRAPH_TO(&g, edge);
        }
        igraph_get_eids(&g, &eids, &pairs, /* directed= */ 0, /*error=*/ 1);
        for (e = 0; e < edges; e++) {
            igraph_integer_t edge = VECTOR(eids)[e];
            igraph_integer_t from1 = VECTOR(pairs)[2 * e];
            igraph_integer_t to1 = VECTOR(pairs)[2 * e + 1];
            igraph_integer_t from2 = IGRAPH_FROM(&g, edge);
            igraph_integer_t to2 = IGRAPH_TO(&g, edge);
            igraph_integer_t min1 = from1 < to1 ? from1 : to1;
            igraph_integer_t max1 = from1 < to1 ? to1 : from1;
            igraph_integer_t min2 = from2 < to2 ? from2 : to2;
            igraph_integer_t max2 = from2 < to2 ? to2 : from2;
            if (min1 != min2 || max1 != max2) {
                return 11;
            }
        }

        igraph_diameter(&g, /*res=*/ 0, /*from=*/ 0, /*to=*/ 0, &path, NULL,
                        IGRAPH_UNDIRECTED, /*unconn=*/ 1);
        igraph_vector_int_update(&pairs, &path);
        igraph_expand_path_to_pairs(&pairs);
        igraph_get_eids(&g, &eids, &pairs, 0, /*error=*/ 1);
        for (e = 0; e < igraph_vector_int_size(&path) - 1; e++) {
            igraph_integer_t edge = VECTOR(eids)[e];
            igraph_integer_t from1 = VECTOR(path)[e];
            igraph_integer_t to1 = VECTOR(path)[e + 1];
            igraph_integer_t from2 = IGRAPH_FROM(&g, edge);
            igraph_integer_t to2 = IGRAPH_TO(&g, edge);
            igraph_integer_t min1 = from1 < to1 ? from1 : to1;
            igraph_integer_t max1 = from1 < to1 ? to1 : from1;
            igraph_integer_t min2 = from2 < to2 ? from2 : to2;
            igraph_integer_t max2 = from2 < to2 ? to2 : from2;
            if (min1 != min2 || max1 != max2) {
                return 12;
            }
        }

        igraph_destroy(&g);
    }

    igraph_vector_int_destroy(&path);
    igraph_vector_int_destroy(&pairs);
    igraph_vector_int_destroy(&eids);

    return 0;
}

int main() {
    int ret;

    RNG_BEGIN();

    if ( (ret = check_simple()) != 0) {
        return ret;
    }

    RNG_END();

    return 0;
}


3.2.11. igraph_get_all_eids_between — Returns all edge IDs between a pair of vertices.

igraph_error_t igraph_get_all_eids_between(
    const igraph_t *graph, igraph_vector_int_t *eids,
    igraph_integer_t source, igraph_integer_t target, igraph_bool_t directed
);

For undirected graphs source and target are exchangeable.

Arguments: 

graph:

The input graph.

eids:

Pointer to an initialized vector, the result is stored here. It will be resized as needed.

source:

The ID of the source vertex

target:

The ID of the target vertex

directed:

Logical scalar, whether to consider edge directions in directed graphs. This is ignored for undirected graphs.

Returns: 

Error code.

Time complexity: TODO

See also: 

igraph_get_eid() for a single edge.

3.2.12. igraph_neighbors — Adjacent vertices to a vertex.

igraph_error_t igraph_neighbors(const igraph_t *graph, igraph_vector_int_t *neis, igraph_integer_t pnode,
        igraph_neimode_t mode);

Arguments: 

graph:

The graph to work on.

neis:

This vector will contain the result. The vector should be initialized beforehand and will be resized. Starting from igraph version 0.4 this vector is always sorted, the vertex IDs are in increasing order. If one neighbor is connected with multiple edges, the neighbor will be returned multiple times.

pnode:

The id of the node for which the adjacent vertices are to be searched.

mode:

Defines the way adjacent vertices are searched in directed graphs. It can have the following values: IGRAPH_OUT, vertices reachable by an edge from the specified vertex are searched; IGRAPH_IN, vertices from which the specified vertex is reachable are searched; IGRAPH_ALL, both kinds of vertices are searched. This parameter is ignored for undirected graphs.

Returns: 

Error code: IGRAPH_EINVVID: invalid vertex ID. IGRAPH_EINVMODE: invalid mode argument. IGRAPH_ENOMEM: not enough memory.

Time complexity: O(d), d is the number of adjacent vertices to the queried vertex.

Example 4.6.  File examples/simple/igraph_neighbors.c

#include <igraph.h>

void print_vector(igraph_vector_int_t *v, FILE *f) {
    igraph_integer_t i;
    for (i = 0; i < igraph_vector_int_size(v); i++) {
        fprintf(f, " %" IGRAPH_PRId "", VECTOR(*v)[i]);
    }
    fprintf(f, "\n");
}

int main() {

    igraph_t g;
    igraph_vector_int_t v;
    igraph_error_t ret;

    igraph_vector_int_init(&v, 8);
    VECTOR(v)[0] = 0;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 1;
    VECTOR(v)[3] = 2;
    VECTOR(v)[4] = 2;
    VECTOR(v)[5] = 3;
    VECTOR(v)[6] = 2;
    VECTOR(v)[7] = 2;
    igraph_create(&g, &v, 0, 1);

    igraph_neighbors(&g, &v, 2, IGRAPH_OUT);
    igraph_vector_int_sort(&v);
    print_vector(&v, stdout);

    igraph_neighbors(&g, &v, 2, IGRAPH_IN);
    igraph_vector_int_sort(&v);
    print_vector(&v, stdout);

    igraph_neighbors(&g, &v, 2, IGRAPH_ALL);
    igraph_vector_int_sort(&v);
    print_vector(&v, stdout);

    /* Errors */
    igraph_set_error_handler(igraph_error_handler_ignore);
    ret = igraph_neighbors(&g, &v, 2, (igraph_neimode_t)0); /* conv for c++ */
    if (ret != IGRAPH_EINVMODE) {
        return 1;
    }

    ret = igraph_neighbors(&g, &v, 4, IGRAPH_ALL);
    if (ret != IGRAPH_EINVVID) {
        return 2;
    }

    igraph_vector_int_destroy(&v);
    igraph_destroy(&g);
    return 0;
}


3.2.13. igraph_incident — Gives the incident edges of a vertex.

igraph_error_t igraph_incident(const igraph_t *graph, igraph_vector_int_t *eids, igraph_integer_t pnode,
        igraph_neimode_t mode);

Arguments: 

graph:

The graph object.

eids:

An initialized vector. It will be resized to hold the result.

pnode:

A vertex ID.

mode:

Specifies what kind of edges to include for directed graphs. IGRAPH_OUT means only outgoing edges, IGRAPH_IN only incoming edges, IGRAPH_ALL both. This parameter is ignored for undirected graphs.

Returns: 

Error code. IGRAPH_EINVVID: invalid pnode argument, IGRAPH_EINVMODE: invalid mode argument.

Added in version 0.2.

Time complexity: O(d), the number of incident edges to pnode.

3.2.14. igraph_degree — The degree of some vertices in a graph.

igraph_error_t igraph_degree(const igraph_t *graph, igraph_vector_int_t *res,
                  const igraph_vs_t vids,
                  igraph_neimode_t mode, igraph_bool_t loops);

This function calculates the in-, out- or total degree of the specified vertices.

This function returns the result as a vector of igraph_integer_t values. In applications where igraph_real_t is desired, use igraph_strength() with NULL weights.

Arguments: 

graph:

The graph.

res:

Integer vector, this will contain the result. It should be initialized and will be resized to be the appropriate size.

vids:

Vertex selector, giving the vertex IDs of which the degree will be calculated.

mode:

Defines the type of the degree for directed graphs. Valid modes are: IGRAPH_OUT, out-degree; IGRAPH_IN, in-degree; IGRAPH_ALL, total degree (sum of the in- and out-degree). This parameter is ignored for undirected graphs.

loops:

Boolean, gives whether the self-loops should be counted.

Returns: 

Error code: IGRAPH_EINVVID: invalid vertex ID. IGRAPH_EINVMODE: invalid mode argument.

Time complexity: O(v) if loops is TRUE, and O(v*d) otherwise. v is the number of vertices for which the degree will be calculated, and d is their (average) degree.

See also: 

igraph_strength() for the version that takes into account edge weights.

Example 4.7.  File examples/simple/igraph_degree.c

#include <igraph.h>

void print_vector(igraph_vector_int_t *v, FILE *f) {
    igraph_integer_t i;
    for (i = 0; i < igraph_vector_int_size(v); i++) {
        fprintf(f, " %" IGRAPH_PRId "", VECTOR(*v)[i]);
    }
    fprintf(f, "\n");
}

int main() {

    igraph_t g;
    igraph_vector_int_t v;
    igraph_vector_int_t v2;
    igraph_vector_int_t seq;
    igraph_error_t ret;
    igraph_integer_t i, mdeg, ndeg, nedges;

    /* Create graph */
    igraph_vector_int_init(&v, 8);
    igraph_vector_int_init(&v2, 8);
    VECTOR(v2)[0] = 0;
    VECTOR(v2)[1] = 1;
    VECTOR(v2)[2] = 1;
    VECTOR(v2)[3] = 2;
    VECTOR(v2)[4] = 2;
    VECTOR(v2)[5] = 3;
    VECTOR(v2)[6] = 2;
    VECTOR(v2)[7] = 2;
    igraph_create(&g, &v2, 0, IGRAPH_DIRECTED);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_NO_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_NO_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    print_vector(&v, stdout);

    igraph_set_error_handler(igraph_error_handler_ignore);

    /* Consistency check of the handshaking lemma. */
    /* If d is the sum of all vertex degrees, then d = 2|E|. */
    ndeg = 0;
    nedges = igraph_ecount(&g);
    for (i = 0; i < igraph_vector_int_size(&v); i++) {
        ndeg += VECTOR(v)[i];
    }
    if (ndeg != 2 * nedges) {
        return 1;
    }

    igraph_destroy(&g);

    igraph_vector_int_resize(&v2, 8);
    VECTOR(v2)[0] = 0;
    VECTOR(v2)[1] = 1;
    VECTOR(v2)[2] = 1;
    VECTOR(v2)[3] = 2;
    VECTOR(v2)[4] = 2;
    VECTOR(v2)[5] = 3;
    VECTOR(v2)[6] = 2;
    VECTOR(v2)[7] = 2;
    igraph_create(&g, &v2, 0, IGRAPH_UNDIRECTED);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_NO_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_NO_LOOPS);
    print_vector(&v, stdout);

    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    print_vector(&v, stdout);

    /* Consistency check of the handshaking lemma. */
    /* If d is the sum of all vertex degrees, then d = 2|E|. */
    ndeg = 0;
    nedges = igraph_ecount(&g);
    for (i = 0; i < igraph_vector_int_size(&v); i++) {
        ndeg += VECTOR(v)[i];
    }
    if (ndeg != 2 * nedges) {
        return 2;
    }

    /* Degree of the same vertex multiple times */

    igraph_vector_int_init(&seq, 3);
    VECTOR(seq)[0] = 2;
    VECTOR(seq)[1] = 0;
    VECTOR(seq)[2] = 2;
    igraph_degree(&g, &v, igraph_vss_vector(&seq), IGRAPH_ALL, IGRAPH_LOOPS);
    print_vector(&v, stdout);

    /* Errors */
    ret = igraph_degree(&g, &v, igraph_vss_vector(&seq), (igraph_neimode_t)0,
                        IGRAPH_LOOPS);
    if (ret != IGRAPH_EINVMODE) {
        return 3;
    }

    VECTOR(seq)[0] = 4;
    ret = igraph_degree(&g, &v, igraph_vss_vector(&seq), IGRAPH_ALL, IGRAPH_LOOPS);
    if (ret != IGRAPH_EINVVID) {
        return 4;
    }

    igraph_destroy(&g);
    igraph_vector_int_destroy(&seq);

    /* Maximum degree */

    igraph_ring(&g, 10, 0 /*undirected*/, 0 /*undirected*/, 0/*uncircular*/);
    igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    if (mdeg != 2) {
        return 5;
    }
    /* Consistency check of the handshaking lemma. */
    /* If d is the sum of all vertex degrees, then d = 2|E|. */
    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    ndeg = 0;
    nedges = igraph_ecount(&g);
    for (i = 0; i < igraph_vector_int_size(&v); i++) {
        ndeg += VECTOR(v)[i];
    }
    if (ndeg != 2 * nedges) {
        return 6;
    }
    igraph_destroy(&g);

    igraph_full(&g, 10, 0 /*undirected*/, 0/*no loops*/);
    igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    if (mdeg != 9) {
        return 7;
    }
    /* Consistency check of the handshaking lemma. */
    /* If d is the sum of all vertex degrees, then d = 2|E|. */
    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    ndeg = 0;
    nedges = igraph_ecount(&g);
    for (i = 0; i < igraph_vector_int_size(&v); i++) {
        ndeg += VECTOR(v)[i];
    }
    if (ndeg != 2 * nedges) {
        return 8;
    }
    igraph_destroy(&g);

    igraph_star(&g, 10, IGRAPH_STAR_OUT, 0);
    igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS);
    if (mdeg != 9) {
        return 9;
    }
    igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS);
    if (mdeg != 1) {
        return 10;
    }
    igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    if (mdeg != 9) {
        return 11;
    }
    /* Consistency check of the handshaking lemma. */
    /* If d is the sum of all vertex degrees, then d = 2|E|. */
    igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    ndeg = 0;
    nedges = igraph_ecount(&g);
    for (i = 0; i < igraph_vector_int_size(&v); i++) {
        ndeg += VECTOR(v)[i];
    }
    if (ndeg != 2 * nedges) {
        return 12;
    }
    igraph_destroy(&g);

    igraph_vector_int_destroy(&v);
    igraph_vector_int_destroy(&v2);

    return 0;
}


3.3. Adding and deleting vertices and edges

3.3.1. igraph_add_edge — Adds a single edge to a graph.

igraph_error_t igraph_add_edge(igraph_t *graph, igraph_integer_t from, igraph_integer_t to);

For directed graphs the edge points from from to to.

Note that if you want to add many edges to a big graph, then it is inefficient to add them one by one, it is better to collect them into a vector and add all of them via a single igraph_add_edges() call.

Arguments: 

igraph:

The graph.

from:

The id of the first vertex of the edge.

to:

The id of the second vertex of the edge.

Returns: 

Error code.

See also: 

igraph_add_edges() to add many edges, igraph_delete_edges() to remove edges and igraph_add_vertices() to add vertices.

Time complexity: O(|V|+|E|), the number of edges plus the number of vertices.

3.3.2. igraph_add_edges — Adds edges to a graph object.

igraph_error_t igraph_add_edges(igraph_t *graph, const igraph_vector_int_t *edges,
                     void *attr);

The edges are given in a vector, the first two elements define the first edge (the order is from, to for directed graphs). The vector should contain even number of integer numbers between zero and the number of vertices in the graph minus one (inclusive). If you also want to add new vertices, call igraph_add_vertices() first.

Arguments: 

graph:

The graph to which the edges will be added.

edges:

The edges themselves.

attr:

The attributes of the new edges. You can supply a null pointer here if you do not need edge attributes.

Returns: 

Error code: IGRAPH_EINVEVECTOR: invalid (odd) edges vector length, IGRAPH_EINVVID: invalid vertex ID in edges vector.

This function invalidates all iterators.

Time complexity: O(|V|+|E|) where |V| is the number of vertices and |E| is the number of edges in the new, extended graph.

Example 4.8.  File examples/simple/igraph_add_edges.c

#include <igraph.h>

int main() {

    igraph_t g;
    igraph_vector_int_t v;
    int ret;

    /* Create graph */
    igraph_vector_int_init(&v, 8);
    VECTOR(v)[0] = 0;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 1;
    VECTOR(v)[3] = 2;
    VECTOR(v)[4] = 2;
    VECTOR(v)[5] = 3;
    VECTOR(v)[6] = 2;
    VECTOR(v)[7] = 2;
    igraph_create(&g, &v, 0, 1);

    /* Add edges */
    igraph_vector_int_resize(&v, 4);
    VECTOR(v)[0] = 2;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 3;
    VECTOR(v)[3] = 3;
    igraph_add_edges(&g, &v, 0);

    /* Check result */
    igraph_get_edgelist(&g, &v, 0);
    igraph_vector_int_sort(&v);
    igraph_vector_int_print(&v);

    /* Error, vector length */
    igraph_set_error_handler(igraph_error_handler_ignore);
    igraph_vector_int_resize(&v, 3);
    VECTOR(v)[0] = 0;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 2;
    ret = igraph_add_edges(&g, &v, 0);
    if (ret != IGRAPH_EINVEVECTOR) {
        return 1;
    }

    /* Check result */
    igraph_get_edgelist(&g, &v, 0);
    igraph_vector_int_sort(&v);
    igraph_vector_int_print(&v);

    /* Error, vector IDs */
    igraph_vector_int_resize(&v, 4);
    VECTOR(v)[0] = 0;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 2;
    VECTOR(v)[3] = 4;
    ret = igraph_add_edges(&g, &v, 0);
    if (ret != IGRAPH_EINVVID) {
        return 2;
    }

    /* Check result */
    igraph_get_edgelist(&g, &v, 0);
    igraph_vector_int_sort(&v);
    igraph_vector_int_print(&v);

    igraph_vector_int_destroy(&v);
    igraph_destroy(&g);

    return 0;
}


3.3.3. igraph_add_vertices — Adds vertices to a graph.

igraph_error_t igraph_add_vertices(igraph_t *graph, igraph_integer_t nv, void *attr);

This function invalidates all iterators.

Arguments: 

graph:

The graph object to extend.

nv:

Non-negative integer specifying the number of vertices to add.

attr:

The attributes of the new vertices. You can supply a null pointer here if you do not need vertex attributes.

Returns: 

Error code: IGRAPH_EINVAL: invalid number of new vertices.

Time complexity: O(|V|) where |V| is the number of vertices in the new, extended graph.

Example 4.9.  File examples/simple/igraph_add_vertices.c

#include <igraph.h>

int main() {

    igraph_t g1;
    igraph_vector_int_t v1;
    int ret;

    /* Create a graph */
    igraph_vector_int_init(&v1, 8);
    VECTOR(v1)[0] = 0;
    VECTOR(v1)[1] = 1;
    VECTOR(v1)[2] = 1;
    VECTOR(v1)[3] = 2;
    VECTOR(v1)[4] = 2;
    VECTOR(v1)[5] = 3;
    VECTOR(v1)[6] = 2;
    VECTOR(v1)[7] = 2;
    igraph_create(&g1, &v1, 0, 0);
    igraph_vector_int_destroy(&v1);

    /* Add more vertices */
    igraph_add_vertices(&g1, 10, 0);
    if (igraph_vcount(&g1) != 14) {
        return 1;
    }

    /* Add more vertices */
    igraph_add_vertices(&g1, 0, 0);
    if (igraph_vcount(&g1) != 14) {
        return 2;
    }

    /* Error */
    igraph_set_error_handler(igraph_error_handler_ignore);
    ret = igraph_add_vertices(&g1, -1, 0);
    if (ret != IGRAPH_EINVAL) {
        return 3;
    }

    igraph_destroy(&g1);

    return 0;
}


3.3.4. igraph_delete_edges — Removes edges from a graph.

igraph_error_t igraph_delete_edges(igraph_t *graph, igraph_es_t edges);

The edges to remove are specified as an edge selector.

This function cannot remove vertices; vertices will be kept even if they lose all their edges.

This function invalidates all iterators.

Arguments: 

graph:

The graph to work on.

edges:

The edges to remove.

Returns: 

Error code.

Time complexity: O(|V|+|E|) where |V| and |E| are the number of vertices and edges in the original graph, respectively.

Example 4.10.  File examples/simple/igraph_delete_edges.c

#include <igraph.h>

int main() {

    igraph_t g;
    igraph_vector_int_t v;
    int ret;
    igraph_es_t es;

    igraph_vector_int_init(&v, 8);
    VECTOR(v)[0] = 0;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 1;
    VECTOR(v)[3] = 2;
    VECTOR(v)[4] = 2;
    VECTOR(v)[5] = 3;
    VECTOR(v)[6] = 2;
    VECTOR(v)[7] = 2;
    igraph_create(&g, &v, 0, 0);

    igraph_es_pairs_small(&es, IGRAPH_DIRECTED, 3, 2, -1);
    igraph_delete_edges(&g, es);
    if (igraph_ecount(&g) != 3) {
        return 1;
    }

    /* error test, no such edge to delete */
    igraph_set_error_handler(igraph_error_handler_ignore);
    ret = igraph_delete_edges(&g, es);
    if (ret != IGRAPH_EINVAL) {
        printf("Error code: %i\n", ret);
        return 2;
    }
    if (igraph_ecount(&g) != 3) {
        return 3;
    }

    /* error test, invalid vertex ID */
    igraph_es_destroy(&es);
    igraph_es_pairs_small(&es, IGRAPH_DIRECTED, 10, 2, -1);
    ret = igraph_delete_edges(&g, es);
    if (ret != IGRAPH_EINVVID) {
        return 4;
    }
    if (igraph_ecount(&g) != 3) {
        return 5;
    }

    /* error test, invalid (odd) length */
    igraph_es_destroy(&es);
    igraph_es_pairs_small(&es, IGRAPH_DIRECTED, 0, 1, 2, -1);
    ret = igraph_delete_edges(&g, es);
    if (ret != IGRAPH_EINVAL) {
        return 6;
    }
    if (igraph_ecount(&g) != 3) {
        return 7;
    }

    igraph_es_destroy(&es);
    igraph_vector_int_destroy(&v);
    igraph_destroy(&g);

    return 0;
}


3.3.5. igraph_delete_vertices — Removes some vertices (with all their edges) from the graph.

igraph_error_t igraph_delete_vertices(igraph_t *graph, const igraph_vs_t vertices);

This function changes the IDs of the vertices (except in some very special cases, but these should not be relied on anyway).

This function invalidates all iterators.

Arguments: 

graph:

The graph to work on.

vertices:

The IDs of the vertices to remove, in a vector. The vector may contain the same ID more than once.

Returns: 

Error code: IGRAPH_EINVVID: invalid vertex ID.

Time complexity: O(|V|+|E|), |V| and |E| are the number of vertices and edges in the original graph.

Example 4.11.  File examples/simple/igraph_delete_vertices.c

#include <igraph.h>

int main() {

    igraph_t g;
    igraph_vector_int_t v;
    int ret;

    /* without edges */
    igraph_empty(&g, 5, IGRAPH_DIRECTED);
    igraph_add_vertices(&g, 2, 0);
    igraph_add_vertices(&g, 3, 0);
    igraph_add_vertices(&g, 1, 0);
    igraph_add_vertices(&g, 4, 0);
    if (igraph_vcount(&g) != 15)  {
        return 1;
    }
    igraph_delete_vertices(&g, igraph_vss_1(2));
    if (igraph_vcount(&g) != 14)  {
        return 2;
    }
    igraph_destroy(&g);

    igraph_vector_int_init(&v, 8);
    VECTOR(v)[0] = 0;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 1;
    VECTOR(v)[3] = 2;
    VECTOR(v)[4] = 2;
    VECTOR(v)[5] = 3;
    VECTOR(v)[6] = 2;
    VECTOR(v)[7] = 2;
    igraph_create(&g, &v, 0, 0);
    igraph_vector_int_destroy(&v);

    /* resize vector */
    igraph_delete_vertices(&g, igraph_vss_1(2));
    if (igraph_vcount(&g) != 3) {
        return 3;
    }
    if (igraph_ecount(&g) != 1) {
        return 4;
    }

    /* error test */
    igraph_set_error_handler(igraph_error_handler_ignore);
    ret = igraph_delete_vertices(&g, igraph_vss_1(3));
    if (ret != IGRAPH_EINVVID) {
        return 5;
    }

    igraph_destroy(&g);

    return 0;
}


3.3.6. igraph_delete_vertices_idx — Removes some vertices (with all their edges) from the graph

igraph_error_t igraph_delete_vertices_idx(
    igraph_t *graph, const igraph_vs_t vertices, igraph_vector_int_t *idx,
    igraph_vector_int_t *invidx
);

This function changes the IDs of the vertices (except in some very special cases, but these should not be relied on anyway). You can use the idx argument to obtain the mapping from old vertex IDs to the new ones, and the newidx argument to obtain the reverse mapping.

This function invalidates all iterators.

Arguments: 

graph:

The graph to work on.

vertices:

The IDs of the vertices to remove, in a vector. The vector may contain the same ID more than once.

idx:

An optional pointer to a vector that provides the mapping from the vertex IDs before the removal to the vertex IDs after the removal. You can supply NULL here if you are not interested.

invidx:

An optional pointer to a vector that provides the mapping from the vertex IDs after the removal to the vertex IDs before the removal. You can supply NULL here if you are not interested.

Returns: 

Error code: IGRAPH_EINVVID: invalid vertex ID.

Time complexity: O(|V|+|E|), |V| and |E| are the number of vertices and edges in the original graph.

Example 4.12.  File examples/simple/igraph_delete_vertices.c

#include <igraph.h>

int main() {

    igraph_t g;
    igraph_vector_int_t v;
    int ret;

    /* without edges */
    igraph_empty(&g, 5, IGRAPH_DIRECTED);
    igraph_add_vertices(&g, 2, 0);
    igraph_add_vertices(&g, 3, 0);
    igraph_add_vertices(&g, 1, 0);
    igraph_add_vertices(&g, 4, 0);
    if (igraph_vcount(&g) != 15)  {
        return 1;
    }
    igraph_delete_vertices(&g, igraph_vss_1(2));
    if (igraph_vcount(&g) != 14)  {
        return 2;
    }
    igraph_destroy(&g);

    igraph_vector_int_init(&v, 8);
    VECTOR(v)[0] = 0;
    VECTOR(v)[1] = 1;
    VECTOR(v)[2] = 1;
    VECTOR(v)[3] = 2;
    VECTOR(v)[4] = 2;
    VECTOR(v)[5] = 3;
    VECTOR(v)[6] = 2;
    VECTOR(v)[7] = 2;
    igraph_create(&g, &v, 0, 0);
    igraph_vector_int_destroy(&v);

    /* resize vector */
    igraph_delete_vertices(&g, igraph_vss_1(2));
    if (igraph_vcount(&g) != 3) {
        return 3;
    }
    if (igraph_ecount(&g) != 1) {
        return 4;
    }

    /* error test */
    igraph_set_error_handler(igraph_error_handler_ignore);
    ret = igraph_delete_vertices(&g, igraph_vss_1(3));
    if (ret != IGRAPH_EINVVID) {
        return 5;
    }

    igraph_destroy(&g);

    return 0;
}


4. Miscellaneous helper functions

4.1. igraph_expand_path_to_pairs — Helper function to convert a sequence of vertex IDs describing a path into a "pairs" vector.

igraph_error_t igraph_expand_path_to_pairs(igraph_vector_int_t* path);

This function is useful when you have a sequence of vertex IDs in a graph and you would like to retrieve the IDs of the edges between them. The function duplicates all but the first and the last elements in the vector, effectively converting the path into a vector of vertex IDs that can be passed to igraph_get_eids().

Arguments: 

path:

the input vector. It will be modified in-place and it will be resized as needed. When the vector contains less than two vertex IDs, it will be cleared.

Returns: 

Error code: IGRAPH_ENOMEM if there is not enough memory to expand the vector.

4.2. igraph_is_same_graph — Are two graphs identical as labelled graphs?

igraph_error_t igraph_is_same_graph(const igraph_t *graph1, const igraph_t *graph2, igraph_bool_t *res);

Two graphs are considered to be the same if they have the same vertex and edge sets. Graphs which are the same may have multiple different representations in igraph, hence the need for this function.

This function verifies that the two graphs have the same directedness, the same number of vertices, and that they contain precisely the same edges (regardless of their ordering) when written in terms of vertex indices. Graph attributes are not taken into account.

This concept is different from isomorphism. For example, the graphs 0-1, 2-1 and 1-2, 0-1 are considered the same because they only differ in the ordering of their edge lists and the ordering of vertices in an undirected edge. However, they are not the same as 0-2, 1-2, even though they are isomorphic to it. Note that this latter graph contains the edge 0-2 while the former two do not — thus their edge sets differ.

Arguments: 

graph1:

The first graph object.

graph2:

The second graph object.

res:

The result will be stored here.

Returns: 

Error code.

Time complexity: O(E), the number of edges in the graphs.

See also: 

igraph_isomorphic() to test if two graphs are isomorphic.