Package pygraph :: Package classes :: Module Graph :: Class graph

Class graph

object --+
         |
        graph

Graph class.

Graphs are built of nodes and edges.

Instance Methods
 
__init__(self)
Initialize a graph.
iterator
__getitem__(self, node)
Return a iterator passing through all neighbors of the given node.
iterator
__iter__(self)
Return a iterator passing through all nodes in the graph.
number
__len__(self)
Return the order of the graph when requested by len().
string
__str__(self)
Return a string representing the graph when requested by str() (or print).
 
add_edge(self, u, v, wt=1, label='', attrs=[])
Add an edge (u,v) to the graph connecting nodes u and v.
 
add_edge_attribute(self, u, v, attr)
Add attribute to the given edge.
 
add_graph(self, graph)
Add other graph to the graph.
 
add_node(self, node, attrs=[])
Add given node to the graph.
 
add_node_attribute(self, node, attr)
Add attribute to the given node.
 
add_nodes(self, nodelist)
Add given nodes to the graph.
 
add_spanning_tree(self, st)
Add a spanning tree to the graph.
 
complete(self)
Make the graph a complete graph.
 
del_edge(self, u, v)
Remove an edge (u, v) from the graph.
 
del_node(self, node)
Remove a node from the graph.
list
edges(self)
Return all edges in the graph.
list
edge_attributes(self, u, v)
Return the attributes of the given edge.
string
edge_label(self, u, v)
Get the label of an edge.
number
edge_weight(self, u, v)
Get the weight of an edge.
list
node_attributes(self, node)
Return the attributes of the given node.
boolean
has_edge(self, u, v)
Return whether an edge between nodes u and v exists.
boolean
has_node(self, node)
Return whether the requested node exists.
graph
inverse(self)
Return the inverse of the graph.
list
neighbors(self, node)
Return all nodes that are directly accessible from given node.
number
node_order(self, node)
Return the order of the given node.
list
nodes(self)
Return node list.
 
set_edge_label(self, u, v, label)
Set the label of an edge.
 
set_edge_weight(self, u, v, wt)
Set the weight of an edge.
iterator
traversal(self, node, order='pre')
Graph traversal iterator.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self)
(Constructor)

 

Initialize a graph.

Overrides: object.__init__

__getitem__(self, node)
(Indexing operator)

 

Return a iterator passing through all neighbors of the given node.

Returns: iterator
Iterator passing through all neighbors of the given node.

__iter__(self)

 

Return a iterator passing through all nodes in the graph.

Returns: iterator
Iterator passing through all nodes in the graph.

__len__(self)
(Length operator)

 

Return the order of the graph when requested by len().

Returns: number
Size of the graph.

__str__(self)
(Informal representation operator)

 

Return a string representing the graph when requested by str() (or print).

Returns: string
String representing the graph.
Overrides: object.__str__

add_edge(self, u, v, wt=1, label='', attrs=[])

 

Add an edge (u,v) to the graph connecting nodes u and v.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
  • wt (number) - Edge weight.
  • label (string) - Edge label.
  • attrs (list) - List of node attributes specified as (attribute, value) tuples.

add_edge_attribute(self, u, v, attr)

 

Add attribute to the given edge.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
  • attr (tuple) - Node attribute specified as a tuple in the form (attribute, value).

add_graph(self, graph)

 

Add other graph to the graph.

Parameters:
  • graph (graph) - Graph

Attention: Attributes and labels are not preserved.

add_node(self, node, attrs=[])

 

Add given node to the graph.

Parameters:
  • node (node) - Node identifier.
  • attrs (list) - List of node attributes specified as (attribute, value) tuples.

Attention: While nodes can be of any type, it's strongly recommended to use only numbers and single-line strings as node identifiers if you intend to use write().

add_node_attribute(self, node, attr)

 

Add attribute to the given node.

Parameters:
  • node (node) - Node identifier
  • attr (tuple) - Node attribute specified as a tuple in the form (attribute, value).

add_nodes(self, nodelist)

 

Add given nodes to the graph.

Parameters:
  • nodelist (list) - List of nodes to be added to the graph.

Attention: While nodes can be of any type, it's strongly recommended to use only numbers and single-line strings as node identifiers if you intend to use write().

add_spanning_tree(self, st)

 

Add a spanning tree to the graph.

Parameters:
  • st (dictionary) - Spanning tree.

complete(self)

 

Make the graph a complete graph.

Attention: This will modify the current graph.

del_edge(self, u, v)

 

Remove an edge (u, v) from the graph.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.

del_node(self, node)

 

Remove a node from the graph.

Parameters:
  • node (node) - Node identifier.

edges(self)

 

Return all edges in the graph.

Returns: list
List of all edges in the graph.

edge_attributes(self, u, v)

 

Return the attributes of the given edge.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
Returns: list
List of attributes specified tuples in the form (attribute, value).

edge_label(self, u, v)

 

Get the label of an edge.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
Returns: string
Edge label

edge_weight(self, u, v)

 

Get the weight of an edge.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
Returns: number
Edge weight.

node_attributes(self, node)

 

Return the attributes of the given node.

Parameters:
  • node (node) - Node identifier
Returns: list
List of attributes specified tuples in the form (attribute, value).

has_edge(self, u, v)

 

Return whether an edge between nodes u and v exists.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
Returns: boolean
Truth-value for edge existence.

has_node(self, node)

 

Return whether the requested node exists.

Parameters:
  • node (node) - Node identifier
Returns: boolean
Truth-value for node existence.

inverse(self)

 

Return the inverse of the graph.

Returns: graph
Complement graph for the graph.

neighbors(self, node)

 

Return all nodes that are directly accessible from given node.

Parameters:
  • node (node) - Node identifier
Returns: list
List of nodes directly accessible from given node.

node_order(self, node)

 

Return the order of the given node.

Returns: number
Order of the given node.

nodes(self)

 

Return node list.

Returns: list
Node list.

set_edge_label(self, u, v, label)

 

Set the label of an edge.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
  • label (string) - Edge label.

set_edge_weight(self, u, v, wt)

 

Set the weight of an edge.

Parameters:
  • u (node) - One node.
  • v (node) - Other node.
  • wt (number) - Edge weight.

traversal(self, node, order='pre')

 

Graph traversal iterator.

Parameters:
  • node (node) - Node.
  • order (string) - traversal ordering. Possible values are:
    1. 'pre' - Preordering (default)
    1. 'post' - Postordering
Returns: iterator
Traversal iterator.