kwant.graph.CGraph#

class kwant.graph.CGraph[source]#

Bases: object

A compressed graph which can be efficiently queried for the existence of edges and outgoing neighbors.

Objects of this class do not initialize the members themselves, but expect that they hold usable values. A good way to create them is by compressing a Graph.

Iterating over a graph yields a sequence of (tail, head) pairs of all edges. The number of an edge in this sequence equals its edge ID. The built-in function enumerate can thus be used to easily iterate over all edges along with their edge IDs.

Methods

all_edge_ids(tail, head)[source]#

Return an iterator over all edge IDs of edges with a given tail and head.

Parameters:
  • tail (integer)

  • head (integer)

Returns:

edge_id

Return type:

integer

Raises:
  • NodeDoesNotExist

  • EdgeDoesNotExistError

  • DisabledFeatureError – If tail is negative and the graph is not two-way compressed.

edge_id(edge_nr)[source]#

Return the edge ID of an edge given its sequential number.

Parameters:

edge_nr (integer)

Returns:

edge_id

Return type:

integer

Raises:
  • DisabledFeatureError – If edge_nr_translation was not enabled during graph compression.

  • EdgeDoesNotExistError

first_edge_id(tail, head)[source]#

Return the edge ID of the first edge (tail, head).

Parameters:
  • tail (integer)

  • head (integer)

Returns:

edge_id

Return type:

integer

Raises:
  • NodeDoesNotExist

  • EdgeDoesNotExistError

  • DisabledFeatureError – If tail is negative and the graph is not two-way compressed.

Notes

This method is useful for graphs where each edge occurs only once.

has_dangling_edges()[source]#
has_edge(tail, head)[source]#

Does the graph contain the edge (tail, head)?

Parameters:
  • tail (integer)

  • head (integer)

Returns:

had_edge

Return type:

boolean

Raises:
  • NodeDoesNotExistError

  • EdgeDoesNotExistError

  • DisabledFeatureError – If tail is negative and the graph is not two-way compressed.

head(edge_id)[source]#

Return the head of an edge, given its edge ID.

Parameters:

edge_id (integer)

Raises:

EdgeDoesNotExistError

Notes

This method executes in constant time. It works for all edge IDs, returning both positive and negative heads.

in_edge_ids(node)[source]#

Return the IDs of incoming edges of a node.

Parameters:

node (integer)

Returns:

edge_ids

Return type:

sequence of integers

Raises:
  • NodeDoesNotExistError

  • DisabledFeatureError – If the graph is not two-way compressed.

in_neighbors(node)[source]#

Return the nodes which point to a node.

Parameters:

node (integer)

Returns:

nodes

Return type:

sequence of integers

Raises:
  • NodeDoesNotExistError

  • DisabledFeatureError – If the graph is not two-way compressed.

out_edge_ids(node)[source]#

Return the IDs of outgoing edges of node.

Parameters:

node (integer)

Returns:

edge_ids

Return type:

sequence of integers

Raises:

NodeDoesNotExistError

out_neighbors(node)[source]#

Return the nodes a node points to.

Parameters:

node (integer)

Returns:

nodes

Return type:

sequence of integers

Raises:

NodeDoesNotExistError

tail(edge_id)[source]#

Return the tail of an edge, given its edge ID.

Parameters:

edge_id (integer)

Returns:

  • tail (integer) – If the edge exists and is positive.

  • None – If the tail is negative.

Raises:

EdgeDoesNotExistError

Notes

The average performance of this method is O(log num_nodes) for non-negative tails and O(1) for negative ones.

write_dot(file)[source]#

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

Parameters:

file (file-like object)

Notes

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

Attributes

edge_nr_translation[source]#
num_edges[source]#
num_nodes[source]#
num_px_edges[source]#
num_xp_edges[source]#
twoway[source]#