# kwant.graph.Graph¶

class kwant.graph.Graph[source]

Bases: object

An uncompressed graph. Used to make compressed graphs. (See CGraph.)

Methods

add_edge()[source]

Add the directed edge (tail, head) to the graph.

Parameters
• tail (integer) –

• head (integer) –

Raises

ValueError – If a negative node is added when this has not been allowed explicitly or if an edge is doubly-dangling.

Returns

edge_nr – The sequential number of the edge. This number can be used to query for the edge ID of an edge in the compressed graph.

Return type

integer

add_edges()[source]

Add multiple edges in one pass.

Parameters

edges (iterable of 2-sequences of integers) – The parameter edges must be an iterable of elements which describe the edges to be added. For each edge-element, edge[0] and edge[1] must give, respectively, the tail and the head. Valid edges are, for example, a list of 2-integer-tuples, or an numpy.ndarray of integers with a shape (n, 2). The latter case is optimized.

Returns

first_edge_nr – The sequential number of the first of the added edges. The numbers of the other added edges are consecutive integers following the number of the first. Edge numbers can be used to query for the edge ID of an edge in the compressed graph.

Return type

integer

compressed()[source]

Build a CGraph from this graph.

Parameters
• twoway (boolean (default: False)) – If set, it will be possible to query the compressed graph for incoming neighbors.

• edge_nr_translation (boolean (default: False)) – If set, it will be possible to call the method edge_id.

• allow_lost_edges (boolean (default: False)) – If set, negative tails are accepted even with one-way compression.

Raises

ValueError – When negative tails occur while twoway and allow_lost_edges are both false.

Notes

In a one-way compressed graph, an edge with a negative tail is present only minimally: it is only possible to query the head of such an edge, given the edge ID. This is why one-way compression of a graph with a negative tail leads to a ValueError being raised, unless allow_lost_edges is true.

reserve()[source]

Reserve space for edges.

Parameters

capacity (integer) – Number of edges for which to reserve space.

Notes

It is not necessary to call this method, but using it can speed up the creation of graphs.

write_dot()[source]

Write a representation of the graph in dot format to file.

That resulting file can be visualized with dot(1) or neato(1) form the graphviz package.

Attributes

num_nodes[source]

#### Previous topic

5.2. kwant.graph – Low-level, efficient directed graphs

#### Next topic

kwant.graph.CGraph