node

Node and Link classes for use in markov graphs

Besides initializing Node ‘s, you will rarely need to directly interact with these objects, as Graph provides much easier and more powerful interactions.

A one-way link pointing to a Node with a weight.

For use in conjunction with the Node and Graph classes. You will rarely need to deal with Link ‘s directly. The best way to create a Link from one Node to another is by calling some_node.add_link(another_node, 5) instead.

Parameters:
  • target (Node) – The Node this Link will point to
  • weight (float or int) – The numerical weight for this Link
class blur.markov.node.Node(value=None, self_destruct=False)

A node to be used in a Markov graph.

Parameters:
  • value (Any) – Value of the node
  • self_destruct (bool) – whether this note deletes itself after being picked by a Graph

Merge links from another node with self.link_list.

Copy links from another node, merging when copied links point to a node which this already links to.

Parameters:
  • other_node (Node) – The node to merge links from
  • merge_same_value_targets (bool) – Whether or not to merge links whose targets have the same value (but are not necessarily the same Node). If False, links will only be merged when link_in_other.target == link_in_self.target. If True, links will be merged when link_in_other.target.value == link_in_self.target.value

Returns: None

Example

>>> node_1 = Node('One')
>>> node_2 = Node('Two')
>>> node_1.add_link(node_1, 1)
>>> node_1.add_link(node_2, 3)
>>> node_2.add_link(node_1, 4)
>>> node_1.merge_links_from(node_2)
>>> print(node_1)
node.Node instance with value One with 2 links:
    0: 5 --> One
    1: 3 --> Two

Find the link that points to target_node if it exists.

If no link in self points to target_node, return None

Parameters:target_node (Node) – The node to look for in self.link_list
Returns:Link – An existing link pointing to target_node if found

None: If no such link exists

Example

>>> node_1 = Node('One')
>>> node_2 = Node('Two')
>>> node_1.add_link(node_2, 1)
>>> link_1 = node_1.link_list[0]
>>> found_link = node_1.find_link(node_2)
>>> found_link == link_1
True

Add link(s) pointing to targets.

If a link already exists pointing to a target, just add weight to that link’s weight

Parameters:
  • targets (Node or list[Node]) – node or nodes to link to
  • weight (int or float) – weight for the new link(s)

Returns: None

Example

>>> node_1 = Node('One')
>>> node_2 = Node('Two')
>>> node_1.add_link(node_2, 1)
>>> new_link = node_1.link_list[0]
>>> print(new_link)
node.Link instance pointing to node with value "Two" with weight 1

Create and add a Link from a source node to self.

Parameters:
  • source (Node) – The node that will own the new Link pointing to self
  • weight (int or float) – The weight of the newly created Link

Returns: None

Example

>>> node_1 = Node('One')
>>> node_2 = Node('Two')
>>> node_1.add_link_to_self(node_2, 5)
>>> new_link = node_2.link_list[0]
>>> print('{} {}'.format(new_link.target.value, new_link.weight))
One 5
>>> print(new_link)
node.Link instance pointing to node with value "One" with weight 5

Add links pointing in either direction between self and target.

This creates a Link from self to target and a Link from target to self of equal weight. If target is a list of Node ‘s, repeat this for each one.

Parameters:
  • target (Node or list[Node]) –
  • weight (int or float) –

Returns: None

Example

>>> node_1 = Node('One')
>>> node_2 = Node('Two')
>>> node_1.add_reciprocal_link(node_2, 5)
>>> new_link_1 = node_1.link_list[0]
>>> new_link_2 = node_2.link_list[0]
>>> print(new_link_1)
node.Link instance pointing to node with value "Two" with weight 5
>>> print(new_link_2)
node.Link instance pointing to node with value "One" with weight 5

Remove any link in self.link_list whose target is self.

Returns: None

Example

>>> node_1 = Node('One')
>>> node_1.add_link(node_1, 5)
>>> node_1.remove_links_to_self()
>>> len(node_1.link_list)
0
get_value()

Get the value of this Node.

For this class, this simply returns self.value, but for subclasses with more complex behavior, this could be more powerful. For example, a Node might have a value which is a SoftColor, in which case this method could return a SoftColor.get() value.

Returns: Any

Example

>>> node_1 = Node('One')
>>> node_1.get_value()
'One'