Skip to content

The Graph class

A graph object.

attrs property

Provides access to the user-defined attributes of the graph.

eattrs property

Provides access to the user-defined attributes of the edges of the graph.

This property is experimental; it might be removed any time.

vattrs property

Provides access to the user-defined attributes of the vertices of the graph.

This property is experimental; it might be removed any time.

__init__(*args, _wrap=None, **kwds)

Constructor.

Creates an empty graph. All positional and keyword arguments are forwarded to create_empty_graph(), except _wrap, which may be used to let the graph take the ownership of a low-level ctypes wrapper object for an igraph graph. Typically you will not need to use _wrap.

add_vertices(n)

Adds new vertices to the graph.

Parameters:

Name Type Description Default
n int

the number of vertices to add

required

Returns:

Type Description
C

the graph itself

convert_to_directed(mode='mutual')

Converts the graph in-place to a directed graph if it is undirected.

Parameters:

Name Type Description Default
mode Literal['arbitrary', 'mutual', 'random', 'acyclic']

specifies how to convert the graph to directed. "arbitrary" picks a direction for each edge in an arbitrary but deterministic manner. "mutual" creates a mutual directed edge pair for each undirected edge. "random" picks a direction for each edge randomly. "acyclic" picks a direction for each edge in a way that ensures that the directed graph is acyclic.

'mutual'

Returns:

Type Description
C

the graph itself

convert_to_undirected(mode='collapse', edge_attr_comb=None)

Converts the graph in-place to an undirected graph if it is directed.

Parameters:

Name Type Description Default
mode Literal['collapse', 'each', 'mutual']

specifies how to convert the graph to undirected. `"eachcreates a single undirected edge for each directed edge.mutualcreates a single undirected edge for every directed mutual edge pair and removes directed edges that do not have a pair in the opposite direction.collapse`` collapses multiple directed edges (irrespectively of their direction) between the same vertex pair into a single undirected edge.

'collapse'
edge_attr_comb Optional[AttributeCombinationSpecification]

specifies what to do with the attributes of edges when multiple edges are collapsed into a single edge during the conversion process.

None

Returns:

Type Description
C

the graph itself

copy()

Creates a copy of the graph.

The copy will have a vertex and an edge set that is independent from the original graph. Graph, vertex and edge attributes are copied in a shallow manner, i.e. the attribute mapping itself is copied but the values will point to the same objects.

ecount()

Returns the number of edges in the graph.

edge(eid)

Returns the endpoints of the edge with the given index from the graph.

get_edge_id(from_, to, *, directed=True, error=True)

Returns the ID of an arbitrary edge between the given source and target vertices.

is_directed()

Returns whether the graph is directed.

neighbors(vid, mode=NeighborMode.ALL)

Returns the list of neighbors of a vertex.

vcount()

Returns the number of vertices in the graph.