Package CedarBackup2 :: Module util :: Class DirectedGraph
[show private | hide private]
[frames | no frames]

Type DirectedGraph

object --+
         |
        DirectedGraph


Represents a directed graph.

A graph G=(V,E) consists of a set of vertices V together with a set E of vertex pairs or edges. In a directed graph, each edge also has an associated direction (from vertext v1 to vertex v2). A DirectedGraph object provides a way to construct a directed graph and execute a depth- first search.

This data structure was designed based on the graphing chapter in The Algorithm Design Manual, by Steven S. Skiena.

This class is intended to be used by Cedar Backup for dependency ordering. Because of this, it's not quite general-purpose. Unlike a "general" graph, every vertex in this graph has at least one edge pointing to it, from a special "start" vertex. This is so no vertices get "lost" either because they have no dependencies or because nothing depends on them.
Method Summary
  __init__(self, name)
Directed graph constructor.
  __cmp__(self, other)
Definition of equals operator for this class.
  __repr__(self)
Official string representation for class instance.
  __str__(self)
Informal string representation for class instance.
  createEdge(self, start, finish)
Adds an edge with an associated direction, from start vertex to finish vertex.
  createVertex(self, name)
Creates a named vertex.
  topologicalSort(self)
Implements a topological sort of the graph.
  _getName(self)
Property target used to get the graph name.
  _topologicalSort(self, vertex, ordering)
Recursive depth first search function implementing topological sort.
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value

Property Summary
  name: Name of the graph.

Class Variable Summary
int _DISCOVERED = 1                                                                     
int _EXPLORED = 2                                                                     
int _UNDISCOVERED = 0                                                                     

Method Details

__init__(self, name)
(Constructor)

Directed graph constructor.
Parameters:
name - Name of this graph.
           (type=String value.)
Overrides:
__builtin__.object.__init__

__cmp__(self, other)
(Comparison operator)

Definition of equals operator for this class.
Parameters:
other - Other object to compare to.
Returns:
-1/0/1 depending on whether self is <, = or > other.

__repr__(self)
(Representation operator)

Official string representation for class instance.
Overrides:
__builtin__.object.__repr__

__str__(self)
(Informal representation operator)

Informal string representation for class instance.
Overrides:
__builtin__.object.__str__

createEdge(self, start, finish)

Adds an edge with an associated direction, from start vertex to finish vertex.
Parameters:
start - Name of start vertex.
finish - Name of finish vertex.
Raises:
ValueError - If one of the named vertices is unknown.

createVertex(self, name)

Creates a named vertex.
Parameters:
name - vertex name
Raises:
ValueError - If the vertex name is None or empty.

topologicalSort(self)

Implements a topological sort of the graph.

This method also enforces that the graph is a directed acyclic graph, which is a requirement of a topological sort.

A directed acyclic graph (or "DAG") is a directed graph with no directed cycles. A topological sort of a DAG is an ordering on the vertices such that all edges go from left to right. Only an acyclic graph can have a topological sort, but any DAG has at least one topological sort.

Since a topological sort only makes sense for an acyclic graph, this method throws an exception if a cycle is found.

A depth-first search only makes sense if the graph is acyclic. If the graph contains any cycles, it is not possible to determine a consistent ordering for the vertices.
Returns:
Ordering on the vertices so that all edges go from left to right.
Raises:
ValueError - If a cycle is found in the graph.

Note: If a particular vertex has no edges, then its position in the final list depends on the order in which the vertices were created in the graph. If you're using this method to determine a dependency order, this makes sense: a vertex with no dependencies can go anywhere (and will).

_getName(self)

Property target used to get the graph name.

_topologicalSort(self, vertex, ordering)

Recursive depth first search function implementing topological sort.
Parameters:
vertex - Vertex to search
ordering - List of vertices in proper order

Property Details

name

Name of the graph.
Get Method:
_getName(self)

Class Variable Details

_DISCOVERED

Type:
int
Value:
1                                                                     

_EXPLORED

Type:
int
Value:
2                                                                     

_UNDISCOVERED

Type:
int
Value:
0                                                                     

Generated by Epydoc 2.1 on Thu Mar 29 20:58:27 2007 http://epydoc.sf.net