Undirected graph

The UndirectedGraph class is suited to represent general undirected graphs.

class UndirectedGraph

A class to represent sparse undirected graph as adjacency lists.

__init__(self: higra.higram.UndirectedGraph, number_of_vertices: int = 0, reserved_edges: int = 0, reserved_edge_per_vertex: int = 0) → None

Create a new graph with no edge.

Parameters
  • number_of_vertices – initial number of vertices in the graph

  • reserved_edges – pre-allocate space for the given number of edges

  • reserved_edge_per_vertex – pre-allocate space for the given number of edge per vertex

add_edge(self: higra.higram.UndirectedGraph, source: int, target: int) → tuple

Add an (undirected) edge between ‘vertex1’ and ‘vertex2’. Returns the new edge.

add_edges(*args, **kwargs)

Overloaded function.

  1. add_edges(self: higra.higram.UndirectedGraph, sources: numpy.ndarray[numpy.int32], targets: numpy.ndarray[numpy.int32]) -> None

Add all edges given as a pair of arrays (sources, targets) to the graph.

  1. add_edges(self: higra.higram.UndirectedGraph, sources: numpy.ndarray[numpy.uint32], targets: numpy.ndarray[numpy.uint32]) -> None

  2. add_edges(self: higra.higram.UndirectedGraph, sources: numpy.ndarray[numpy.int64], targets: numpy.ndarray[numpy.int64]) -> None

  3. add_edges(self: higra.higram.UndirectedGraph, sources: numpy.ndarray[numpy.uint64], targets: numpy.ndarray[numpy.uint64]) -> None

add_vertex(self: higra.higram.UndirectedGraph) → int

Add a vertex to the graph, the index of the new vertex is returned

add_vertices(self: higra.higram.UndirectedGraph, num: int) → None

Add the given number of vertices to the graph.

adjacent_vertices(self: higra.higram.UndirectedGraph, vertex: int) → Iterator

Iterator over all vertices adjacent to the given vertex.

degree(*args, **kwargs)

Overloaded function.

  1. degree(self: higra.higram.UndirectedGraph, vertex: int) -> int

Return the degree of the given vertex.

  1. degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.int32]) -> xt::xtensor

Return the degree of the given vertices.

  1. degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.uint32]) -> xt::xtensor

  2. degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.int64]) -> xt::xtensor

  3. degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.uint64]) -> xt::xtensor

edge_from_index(self: higra.higram.UndirectedGraph, edge_index: int) → tuple

Get an edge from its index.

edge_list()

Returns a tuple of two arrays (sources, targets) defining all the edges of the graph.

Example

>>> g = UndirectedGraph(3)
>>> g.add_edges((0, 1, 0), (1, 2, 2))
>>> g.edge_list()
(array([0, 1, 0]), array([1, 2, 2]))
Returns

pair of two 1d arrays

edges(self: higra.higram.UndirectedGraph) → Iterator

Iterator over all edges of the graph.

in_degree(*args, **kwargs)

Overloaded function.

  1. in_degree(self: higra.higram.UndirectedGraph, vertex: int) -> int

Return the in degree of the given vertex.

  1. in_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.int32]) -> xt::xtensor

Return the in degree of the given vertices.

  1. in_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.uint32]) -> xt::xtensor

  2. in_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.int64]) -> xt::xtensor

  3. in_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.uint64]) -> xt::xtensor

in_edges(self: higra.higram.UndirectedGraph, vertex: int) → Iterator

Iterator over all in edges from ‘vertex’. An in edge is a tuple ‘(adjacent_vertex, vertex)’.

index(self: higra.higram.UndirectedGraph, edge: tuple) → detail::accessor<detail::accessor_policies::tuple_item>

Get the index of an edge.

num_edges(self: higra.higram.UndirectedGraph) → int

Return the number of edges in the graph

num_vertices(self: higra.higram.UndirectedGraph) → int

Return the number of vertices in the graph

out_degree(*args, **kwargs)

Overloaded function.

  1. out_degree(self: higra.higram.UndirectedGraph, vertex: int) -> int

Return the out degree of the given vertex.

  1. out_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.int32]) -> xt::xtensor

Return the out degree of the given vertices.

  1. out_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.uint32]) -> xt::xtensor

  2. out_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.int64]) -> xt::xtensor

  3. out_degree(self: higra.higram.UndirectedGraph, vertices_array: numpy.ndarray[numpy.uint64]) -> xt::xtensor

out_edges(self: higra.higram.UndirectedGraph, vertex: int) → Iterator

Iterator over all out edges from ‘vertex’. An out edge is a tuple ‘(vertex, adjacent_vertex)’.

remove_edge(self: higra.higram.UndirectedGraph, edge_index: int) → None

Remove the given edge from the graph (the edge is not really removed: its source and target are attached to a virtual node of index -1).

set_edge(self: higra.higram.UndirectedGraph, edge_index: int, source: int, target: int) → None

Modify the source and the target of the given edge.

source(self: higra.higram.UndirectedGraph, edge: tuple) → detail::accessor<detail::accessor_policies::tuple_item>

Get the source vertex of an edge.

sources()

Source vertex of every edge of the graph.

Example

>>> g = UndirectedGraph(3)
>>> g.add_edges((0, 1, 0), (1, 2, 2))
>>> g.sources()
array([0, 1, 0])
Returns

a 1d array of size self.num_edges()

target(self: higra.higram.UndirectedGraph, edge: tuple) → detail::accessor<detail::accessor_policies::tuple_item>

Get the target vertex of an edge.

targets()

Target vertex of every edge of the graph.

Example

>>> g = UndirectedGraph(3)
>>> g.add_edges((0, 1, 0), (1, 2, 2))
>>> g.targets()
array([1, 2, 2])
Returns

a 1d array of size self.num_edges()

vertices(self: higra.higram.UndirectedGraph) → Iterator

Iterator over all vertices of the graph.