src.graph package

Submodules

src.graph.chimera_graph module

class src.graph.chimera_graph.ChimeraGraphLayout

Bases: src.graph.undirected_graph.UndirectedGraphAdjList

A Chimera Graph representation.

Note

As of now, this Graph is limited to one single unit cell with shore size 4.

src.graph.undirected_graph module

class src.graph.undirected_graph.AdjListEntryWithCosts

Bases: object

One entry of an adjacency list.

Describes edges to other nodes and assigns costs to these edges.

Warning

This AdjacencyList does not check for invalid indexing. Expect KeyErrors to arise if the caller does not handle indexing correctly.

Note

It is guaranteed that an edge from node u to node v cannot exist multiple times with different costs.

get_neighbors() list[int]

Returns all neighboring nodes.

Returns

All neighboring nodes (from the current node).

Return type

list[int]

get_neighbors_with_costs() list[tuple[int, int]]

Returns all neighboring nodes with costs (from the current node).

Returns

All neighboring nodes (from the current node) with costs.

Return type

list[tuple[int, int]]

remove_edge_to(to: int) None

Removes an edge to a node.

Parameters

to (int) – Other node to specify the edge that should be removed.

set_edge_to(to: int, cost: int) None

Sets an undirected edge with cost to a node.

Warning

This may override an existing edge with new costs.

Parameters
  • to (int) – Other node to specify the edge.

  • cost (int) – Cost for the edge

exception src.graph.undirected_graph.GraphNodeIndexError(nodes_count: int, index: int)

Bases: Exception

Error that occurs when accessing a node that does not exist in the Graph.

Parameters
  • nodes_count (int) – Number of nodes in the Graph.

  • index (int) – Invalid node (represented as integer index in the Graph).

class src.graph.undirected_graph.UndirectedGraphAdjList(nodes_count: int)

Bases: object

Undirected Graph using an adjacency list.

add_node() int

Adds a new node to this Graph.

Returns

The new node number.

Return type

int

exists_edge(frm: int, to: int) bool

Checks if an edge between two nodes exists.

Parameters
  • frm (int) – One node of the edge.

  • to (int) – The other node of the edge.

Returns

True if the edge exists.

Return type

bool

Raises

GraphNodeIndexError – If the given node frm or to does not exist in the Graph.

get_edges() list[tuple[int, int, int]]

Returns all edges of this Graph.

Returns

A List of entries (frm, to, cost) describing all edges of this Graph with their respective costs.

Return type

list[tuple[int, int, int]]

get_neighbor_nodes(frm: int) list[int]

Returns all neighboring nodes.

Neighboring nodes are nodes that are connected via an edge to this node.

Parameters

frm (int) – Node from which the neighboring nodes should be retrieved.

Returns

List of integers describing the neighboring nodes.

Return type

list[int]

Raises

GraphNodeIndexError – If the given node frm does not exist in the Graph.

get_neighbor_nodes_with_costs(frm: int) list[tuple[int, int]]

Returns all neighboring nodes with costs.

Neighboring nodes are nodes that are connected via an edge to this node.

Parameters

frm (int) – Node from which the neighboring nodes should be retrieved.

Returns

List of tuples (to, cost) describing edges with costs.

Return type

list[tuple[int, int]]

Raises

GraphNodeIndexError – If the given node frm does not exist in the Graph.

get_nodes() list[int]

Returns all nodes of this Graph.

Returns

All nodes of this Graph.

Return type

list[int]

has_neighbor_nodes(frm: int) bool

Checks if the given node has neighboring nodes.

Neighboring nodes are nodes that are connected via an edge to this node.

Parameters

frm (int) – Node from which the neighboring nodes should be retrieved.

Returns

True if the given node has neighboring nodes.

Return type

bool

Raises

GraphNodeIndexError – If the given node frm does not exist in the Graph.

remove_all_edges_from_node(frm: int) None

Removes all edges connected to the given node.

Parameters

frm (int) – node from which all edges should be removed.

Raises

GraphNodeIndexError – If the given node frm does not exist in the Graph.

remove_edge(frm: int, to: int) None

Removes the edge (if present) between nodes frm and to.

Parameters
  • frm (int) – One node of the edge.

  • to (int) – The other node of the edge.

Raises

GraphNodeIndexError – If the given node frm or to does not exist in the Graph.

set_edge(frm: int, to: int, cost=0) None

Sets an edge between nodes frm and to and assigns the given cost.

Warning

This may override an existing edge with new costs.

Parameters
  • frm (int) – One node of the edge.

  • to (int) – The other node of the edge.

  • cost (int) – The costs for the edge

Raises

GraphNodeIndexError – If the given node frm or to does not exist in the Graph.

Module contents