Nodes, Edges, Networks¶
-
class
pynetworks.Edge(node1, node2, weight=None)¶ Represent an optionally weighted edge between two
Nodeobjects.Parameters: -
node1¶
-
node2¶
-
weight¶
-
-
class
pynetworks.Network(all_nodes=None, name=None)¶ Contain a network of interconnected
Nodeobjects.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¶
-
isolated_nodes¶ All nodes with no edges.
Type: set
-
strongly_connected¶ Trueif every node in this network has a path to every other node.Type: bool
-
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¶
-
connect(other, weight=None)¶ Add
Edgebetweenselfandotherwithweight.Parameters: - other (
Node) - weight (numerical, optional)
- other (
-
degree¶ Deegree of this node (number of outgoing edges).
Type: int
-
-
pynetworks.generate_network(n_nodes=10, lower_bound=1, upper_bound=11, edge_prob=0.8, strongly_connected=True)¶ Create a
NetworkofNodeobjects.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_connectedis set toTrue,edges_probmay 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_nodesinterconnectedNodeobjects.Return type:
DOT Representation¶
-
pynetworks.dotgraph(isolated_nodes=None, edges=None, name='')¶ Generate a DOT graph out of nodes and edges.
Parameters: Returns: A valid DOT graph of all the given nodes and edges with name
name.Return type: str
Examples
Graphing one isolated
Nodeobject.>>> a = Node('A') >>> print(dotgraph(isolated_nodes=[a], name='My Graph')) graph "My Graph" { "A" }
Graphing connected
Nodeobjects.>>> 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
Edgeobjects connecting twoNodeobjects.Parameters: edges (iterable, optional) – All Edgeobjects in thisPath. If left out, the path is initialized empty.-
edges¶
-
-
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
startandend.Parameters: Returns: Trueif a path exists, otherwiseFalse.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
startandend.Parameters: Returns: If a path exists from
starttoend, return thatPathobject. Otherwise, returnNone.Return type: PathNoneNote
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
startthrough all otherNodeobjects innetwork.Parameters: Returns: If a path exists from
starttoend, return thatPathobject. Otherwise, returnNone.Return type: PathNoneNote
This function is cached by the
memoize()function.