graph module
- class graph.Graph(shape, dimensions=None, coord_scale=1)
Bases:
objectA class for a Graph keeps track of a collection of nodes.
- Parameters
shape – The shape of the collection of Nodes (Tuple).
dimensions – Optional, default None - The amount of dimensions a node uses for its coordinates. If none is given dimensions = len(shape).
coord_scale – Optional, default 1 - Nodes are initialized with unique coordinates based on their index, coord_scale is a scalar for these initial coordinates.
- add_node(coordinates, attrs=None)
Adds a node to the graph with given coordinates.
- Parameters
coordinates – A tuple with the coordinates of the Node.
attrs – Optional, default None - An optional dictionary parameter for additional attributes you want to connect to the node.
- Returns
The new node, of class Node.
- get_edges()
Get a list of all edges in the Graph
- Returns
A list of Tuples with the indices of connected Nodes.
- remove_node(node)
Removes a given node from the graph.
- Parameters
node – The Node to be removed.
- Returns
None
- class graph.Node(coordinates, attrs=None)
Bases:
objectA class for Nodes in a Graph, keeps track of neighbours, and coordinates.
- Parameters
coordinates – The coordinates of this Node, should be a tuple or list of any dimension.
attrs – Optional, default None - An optional dictionary parameter for additional attributes you want to connect to the node.
- connect(other)
Connect this node to the other Node. Will add other to this node’s neighbourhood, and this node to the other’s neighbourhood.
- Parameters
other – The Node to connect this node to.
- Returns
None
- disconnect(other)
Opposite of Connect(), removes this node and other from each other’s neighbourhood.
- Parameters
other – The Node to disconnect from this node.
- Returns
None
- get_nth_neighbours(depth)
Get all nodes that are neighbour of this node at a given depth
- Parameters
depth – The depth of the neighbouring nodes to get.
- Returns
A list of Nodes that are neighbour of this node at the given depth. Returns [self] if depth = 0.
- graph.connect_to_neighbours(index, graph)
Used to create a square-grid graph. Connects a node to all its direct neighbours in all dimensions based on the index of nodes.
- Parameters
index – The index of the node that should be connected to its neighbouring nodes.
graph – The graph containing the nodes.
- Returns
None
- graph.create_grid(shape, coord_scale=1, dimensions=None)
Create a square-grid graph of a given shape. Where all nodes that are next to each other are connected.
- Parameters
shape – The shape of the grid graph to be created.
coord_scale – Optional, default 1 - Nodes are initialized with unique coordinates based on their index, coord_scale is a scalar for these initial coordinates.
dimensions – Optional, default None - The amount of dimensions a node uses for its coordinates. If none is given dimensions = len(shape).
- Returns
A Graph containing nodes in the grid shape, with neighbours connected.