| |
- builtins.object
-
- Graph
-
- Bigraph
- TreeNode
class Bigraph(Graph) |
|
Bigraph(links=None)
A Graph that represents an undirected bipartite graph (or bigraph).
The constructor call is something like:
g = Bigraph({'A': {'C': 2},
'B': {'C':1, 'D':2},
'dummy': {'E': 0, 'F': 0}})
This makes a bigraph with bipartition (V1, V2), where V1 = ['A', 'B'] and
V2 = ['C', 'D', 'E', 'F']. Note how the keys of the input dictionary are
put into V1, while nodes (vertices) in their associated values into V2. A
'dummy' node key can be used to add nodes into V2 which are not connected to
any node in V1; the 'dummy' node itself is not kept.
The partition method returns the (V1, V2) tuple representing the
bipartition. |
|
- Method resolution order:
- Bigraph
- Graph
- builtins.object
Methods defined here:
- __init__(self, links=None)
- Initialize self. See help(type(self)) for accurate signature.
- partition(self)
- Return a tuple with two node lists representing the bipartition.
- remove_nodes(self, *nodes)
- Remove specified node(s) and the corresponding incident edges.
Methods inherited from Graph:
- connect(self, a, b, weight=1)
- Add a link from a to b only if both a and b exist in the graph.
If the graph is undirected also the inverse link is added. The weight
of the link(s) is specified by the weight argument.
- disconnect(self, a, b)
- Remove link from a to b.
Also the inverse link is removed if the graph is undirected.
- get(self, a, b=None)
- Return a link weight or a dict of {node: weight} entries.
.get(A,B) returns the weight or None if link doesn't exist;
.get(A) returns a dict of {node: weight} entries (possibly {}) or None
if A doesn't exist.
- nodes(self)
- Return a list of nodes in the graph.
- update_weight(self, a, b, weight=1)
- Update weight of link from a to b (if the link already exists).
Also the weight of the inverse link is updated if the graph is
undirected.
Data descriptors inherited from Graph:
- __dict__
dictionary for instance variables (if defined)
- __weakref__
list of weak references to the object (if defined)
|
class Graph(builtins.object) |
|
Graph(links=None, directed=True)
A general graph.
A graph connects nodes (vertices) by edges (links). Each edge can also
have a weight associated with it. The constructor call is something like:
g = Graph({'A': {'B': 1, 'C': 2}})
This makes a graph with 3 nodes, A, B, and C, with an edge of weight 1 from
A to B, and an edge of weight 2 from A to C. You can also do:
g = Graph({'A': {'B': 1, 'C': 2}}, directed=False)
This makes an undirected graph, so inverse links are also added. The graph
stays undirected; if you add more links with g.connect('B', 'C', 3), then
inverse link is also added. You can use g.nodes() to get a list of nodes,
g.get('A') to get a dict of links out of A, and g.get('A', 'B') to get the
weight of the link from A to B (or None if link doesn't exist). 'Weights'
can actually be any object at all, and nodes can be any hashable object.
Extra functionality:
g.disconnect('A', 'B') removes link from A to B (and the inverse link if
graph is undirected).
g.update_weight('A', 'B', 3) sets weight of A->B link to 3, only if link
exists (the inverse link's weight is also updated in undirected case).
g.remove_nodes('A', 'B') removes the specified nodes and their incident
edges (in our example, the updated g contains C only).
This class has been adapted from:
Russell, S. J.; Norvig, P. Artificial Intelligence: a Modern Approach, 2nd
ed.; Prentice Hall/Pearson Education: Upper Saddle River, NJ, 2003.
The original class is covered by the MIT licence:
http://www.opensource.org/licenses/MIT |
|
Methods defined here:
- __init__(self, links=None, directed=True)
- Initialize self. See help(type(self)) for accurate signature.
- connect(self, a, b, weight=1)
- Add a link from a to b only if both a and b exist in the graph.
If the graph is undirected also the inverse link is added. The weight
of the link(s) is specified by the weight argument.
- disconnect(self, a, b)
- Remove link from a to b.
Also the inverse link is removed if the graph is undirected.
- get(self, a, b=None)
- Return a link weight or a dict of {node: weight} entries.
.get(A,B) returns the weight or None if link doesn't exist;
.get(A) returns a dict of {node: weight} entries (possibly {}) or None
if A doesn't exist.
- nodes(self)
- Return a list of nodes in the graph.
- remove_nodes(self, *nodes)
- Remove specified node(s) and its (their) incident edges.
- update_weight(self, a, b, weight=1)
- Update weight of link from a to b (if the link already exists).
Also the weight of the inverse link is updated if the graph is
undirected.
Data descriptors defined here:
- __dict__
dictionary for instance variables (if defined)
- __weakref__
list of weak references to the object (if defined)
|
class TreeNode(builtins.object) |
|
TreeNode(element=None, parent=None)
A node in a tree data structure.
Contains a pointer to the parent node and to the element this node
represents. (Based on the search.Node class.) A tree can be represented by
a list of nodes, each of them being a tree leaf. Recursive inquiries of
parent attribute starting at a leaf, leads to the root node. |
|
Methods defined here:
- __init__(self, element=None, parent=None)
- Create a search tree Node, derived from a parent.
- __repr__(self)
- Return repr(self).
- path(self)
- Create a list of nodes from the root to this node.
Data descriptors defined here:
- __dict__
dictionary for instance variables (if defined)
- __weakref__
list of weak references to the object (if defined)
| |