Graph Interface

class DegreeView

Degree view yielding sorted tuples (node, degree). It supports indexing.

class EdgeView

Edge view yielding tuples of nodes.

class Graph(nodes_or_graph=None, edges=None)

Undirected, unweighted, unattributed graph that is compatible with networkx.Graph by duck-typing. Detailed descriptions of all methods can be found in the networkx documentation.

Parameters
  • nodes_or_graph – Nodes to add to the graph or a graph instance to make a copy of.

  • edges – Edges to add to the graph.

add_edge(self, node_t u, node_t v) int

Add the edge (u, v).

Parameters
  • u – First node in the edge pair.

  • v – Second node in the edge pair.

Returns

addedTrue if (u, v) was added, False if it already existed.

add_edges_from(self, edge_list_t edges) int

Add multiple edges.

Parameters

edges – Container of pairs of nodes, constituting an edge to be added each.

Returns

num_added – Number of newly added edges.

add_node(self, node_t node) int

Add a single node.

Parameters

node – Node to add.

add_nodes_from(self, node_set_t nodes) int

Add multiple nodes.

Parameters

nodes – Container of nodes to add.

adj

Adjacency map, mapping nodes to sets of neighbors.

Type

typing.Dict[int, typing.Set[int]]

degree

View of node degrees, supporting indexing by node label.

Type

DegreeView

edges

View of connectivity as an edge list.

Type

EdgeView

has_edge(self, node_t u, node_t v) int

Returns whether the edge (u, v) exists.

Parameters
  • u – First node in the edge pair.

  • v – Second node in the edge pair.

Returns

existsTrue if (u, v) exists, False otherwise.

has_node(self, node_t node) int

Returns whether node exists.

Parameters

node – Node to check.

Returns

existsTrue if node exists, False otherwise.

is_directed(self) int

Returns False because directed graphs are not supported.

is_multigraph(self) int

Returns False because multigraphs are not supported.

name

Name of the graph (mostly for compatibility with networkx).

Type

str

neighbors

View of node neighbors, supporting indexing by node label.

Type

NeighborView

nodes

View of nodes, supporting relatively fast iteration.

Type

NodeView

number_of_edges(self) int

Returns the number of edges.

number_of_nodes(self) int

Returns the number of nodes.

remove_edge(self, node_t u, node_t v) int

Remove the edge (u, v).

Parameters
  • u – First node in the edge pair.

  • v – Second node in the edge pair.

Raises

KeyError – If the edge between u and v does not exist.

remove_edges_from(self, edge_list_t edges) int

Remove multiple edges.

Parameters

edges – Container of pairs of nodes, constituting an edge to be removed each.

Returns

num_removed – Number of removed edges.

Note

In contrast to remove_edge(), this method does not raise a KeyError if edges contains an edge that does not exist.

remove_node(self, node_t node) int

Remove node from the graph.

Parameters

node – Node to remove.

Raises

KeyError – If the node does not exist.

remove_nodes_from(self, node_set_t nodes) int

Remove multiple nodes.

Parameters

nodes – Nodes to remove.

Returns

num_removed – Number of nodes removed.

Note

In contrast to remove_node(), this method does not raise a KeyError if nodes contains a node that does not exist.

size(self) int

Returns the number of edges. number_of_edges() is preferred.

class NeighborView

Neighbor view exposing neighbors using the __call__ interface.

class NodeView

Node view, yielding nodes.

are_node_labels_normalized(Graph graph: Graph) bool

Return whether node labels are consecutive starting at zero.

Parameters

graph – Graph whose node labels to check.

Returns

normalizedTrue if node labels are consecutive starting at zero. False otherwise.

Note

This operation is relatively expensive because it creates an intermediate sorted vector of node labels.

assert_normalized_node_labels(Graph graph: Graph) Graph

Assert that node labels are consecutive starting at zero.

Parameters

graph – Graph whose node labels to check.

Returns

graph – Input graph if node labels are consecutive.

Raises

ValueError – If the node labels are not normalized.