Nodes, Edges, Networks

class pynetworks.Edge(node1, node2, weight=None)

Represent an optionally weighted edge between two Node objects.

Parameters:
  • node1 (Node) – first Node in this Edge.
  • node2 (Node) – second Node in this Edge.
  • weight (numerical, optional) – weight of this Edge.
node1
node2
weight
copy_to_clipboard()

Copy the DOT language representation of this Edge to the native clipboard.

dot()
Returns:A DOT language representation of this Edge object.
Return type:str
reverse()
Returns:A Edge with the same node1, node2, and weight but with node1 and node2 swapped.
Return type:Edge
class pynetworks.Network(all_nodes=None, name=None)

Contain a network of interconnected Node objects.

Parameters:
  • all_nodes (iterable, optional) – All nodes in this network. If left out, the network is initialized empty.
  • name (optional) – The name of this network.
all_nodes
name
edges

All edges in this Network.

Type:list
isolated_nodes

All nodes with no edges.

Type:set
copy_to_clipboard()

Copy the DOT language representation of this Network to the native clipboard.

strongly_connected

True if every node in this network has a path to every other node.

Type:bool
update()

Update edges and isolated_nodes, to be used when Node objects in this network have changed.

class pynetworks.Node(name)

Contain a named node, with weighted edges.

Parameters:name – name of this Node.

Example

>>> a = Node('A')
>>> b = Node('B')
>>> a.connect(b, 3)
>>> print(a)
graph {
    "A" -- "B" [label=3]
}
name
edges

list of edges containing this Node.

Type:list of Edge
connect(other, weight=None)

Add Edge between self and other with weight.

Parameters:
  • other (Node)
  • weight (numerical, optional)
copy_to_clipboard()

Copy the DOT language representation of this Node to the native clipboard.

degree

Deegree of this node (number of outgoing edges).

Type:int
disconnect(other, weight=None)

Remove Edge between self and other with weight.

Parameters:
  • other (Node)
  • weight (numerical, optional)
isolate()

Disconnect from all connected Node objects.

pynetworks.generate_network(n_nodes=10, lower_bound=1, upper_bound=11, edge_prob=0.8, strongly_connected=True)

Create a Network of Node objects.

Parameters:
  • n_nodes (int, optional) – Number of nodes in the returned network.
  • lower_bound (int, optional) – Lower bound (inclusive) of range of edges’ weights.
  • upper_bound (int, optional) – Upper bound (exclusive) for range of edges’ weights.
  • edges_prob (float, optional) – Probability betweeen 0 and 1 of any two nodes being connected. If strongly_connected is set to True, edges_prob may be overridden to ensure a strongly connected network.
  • strongly_connected (bool) – If False, output does not need to be a strongly connected network.
Returns:

A network n_nodes interconnected Node objects.

Return type:

Network

DOT Representation

pynetworks.dotgraph(isolated_nodes=None, edges=None, name='')

Generate a DOT graph out of nodes and edges.

Parameters:
  • isolated_nodes (list of Node, optional) – Isolated nodes to graph.
  • edges (list of Edge, optional) – Edges to graph.
  • name (optional) – Name of the returned DOT graph.
Returns:

A valid DOT graph of all the given nodes and edges with name name.

Return type:

str

Examples

Graphing one isolated Node object.

>>> a = Node('A')
>>> print(dotgraph(isolated_nodes=[a], name='My Graph'))
graph "My Graph" {
    "A"
}

Graphing connected Node objects.

>>> b = Node('B')
>>> c = Node('C')
>>> b.connect(c, 5)
>>> print(dotgraph(edges=b.edges))
graph {
    "B" -- "C" [label=5]
}
pynetworks.escape_dot_id(string)

Surround in double quotes and escape all double quotes.

Parameters:string (str) – ID to escape.
Returns:A valid, escaped ID for a DOT graph.
Return type:str

Example

>>> escape_dot_id('A"B')
'"A\"B"'

Path-finding

class pynetworks.Path

Store Edge objects connecting two Node objects.

Parameters:edges (iterable, optional) – All Edge objects in this Path. If left out, the path is initialized empty.
edges
copy_to_clipboard()

Copy the DOT language representation of this Path to the native clipboard.

weight

Sum of the weights of all of the Edge objects in this Path.

Type:numerical
pynetworks.memoize(shortest_path_func)

Cache a path-finding function that expects two input parameters.

The path-finding function may additionally accept some number of private parameters that don’t affect cache.

Examples

Accessing cache.

>>> shortest_path.cache  # see note for supported functions
{...}

Clearing cache.

>>> shortest_path.cache_clear()  # see note for supported functions
>>> shortest_path.cache
{}

Calling path-finding functions without caching result.

>>> old_cache = shortest_path.cache.copy()
>>> shortest_path(node_a, node_b, cache=False)
>>> shortest_path.cache == old_cache
True

Note

Supported path-finding functions:

pynetworks.path_exists(start, end, *, save_to_cache=True, _visited=None)

Check if a path exists between start and end.

Parameters:
Returns:

True if a path exists, otherwise False.

Return type:

bool

Note

This function is cached by the memoize() function.

pynetworks.shortest_path(start, end, *, save_to_cache=True, _visited=None, _tail_weight=None, _best_path_weight=None)

Find the shortest path between start and end.

Parameters:
Returns:

If a path exists from start to end, return that Path object. Otherwise, return None.

Return type:

Path None

Note

This function is cached by the memoize() function.

pynetworks.shortest_path_through_network(start, network, *, save_to_cache=True, _visited=None, _tail_weight=None, _best_path_weight=None)

Find the shortest path from start through all other Node objects in network.

Parameters:
  • start (Node) – Start of the returned Path.
  • network (Network) – Fully-connected network through which the returned Path travels.
Returns:

If a path exists from start to end, return that Path object. Otherwise, return None.

Return type:

Path None

Note

This function is cached by the memoize() function.