Inherits:
::tycho::Graph
-
Source File -
Contents:
public methods
A directed acyclic graph is a Graph
without cycles. This
class provides methods to build, browse, and edit such a data
structure. The method verifyAcyclic
verifies that the
graph has no cycles. Methods are also provided to topologically sort
the graph and to sort by depth (distance from a root). Data stored on
the nodes is as in the base class Graph
.
Here is an example of how to use the class:
catch {delete object foo}
::tycho::DirectedAcyclicGraph foo
foo add a {}
foo add c {} a
foo add d {} a
foo add e {} {a d}
foo verifyAll
foo verifyAcyclic
::tycho::post [foo describe]
The description that is posted in the last line will be:
"{d {} a} {e {} {a d}} {a {} {}} {c {} a}".
This is a list of nodes in the graph in arbitrary order.
Each node is a list of three items, the name, contents,
and parents.
Public constructs
-
checkArc
parent child
- Return 1 if an arc between the specified parent and child is part of
a cycle. Return 0 otherwise. The arc need not exist in the graph.
It is assumed that without the arc in question, there are no cycles
in the graph (otherwise, an infinite recursion could result). This
can be verified using
verifyAcyclic
prior to adding the arc.
It is an error if either the parent or child do not exist.
-
depth
node {init 1}
- Return the depth of a given node. The depth is the maximum number of
hops from a root node. A root node has depth zero. A "hop" is a
parent-child relationship. As a side effect, an attribute called
"depth" is set for the specified node and all its ancestors. If the
second argument is not given or has a non-zero value, the the "depth"
attribute is reset to -1 for all nodes in the graph before beginning.
It is necessary to do this the first time this method is called, or
if the graph has been modified since the method was last called.
Alternatively, you can directly initialize the attribute using
attributeInit depth -1
. It is assumed that the graph has
no directed loops (or this method goes into an infinite recursion).
This can be verified using verifyAcyclic
.
-
depthSort
- Return a list of lists of node names by depth. The list of root nodes
is first (index 0). A node will be in the (N+1)-th list (index N) if
the maximum number of hops to a node in the first list is N. A "hop"
is simply a parent-child relationship.
Within each depth, nodes are sorted alphabetically by name.
-
subgraphWidth
roots {init 1}
- Return the width of the subgraph with the given roots. The subgraph
includes all nodes that are descendants of the given roots, plus the
roots themselves. The width is the number of nodes in the largest
anti-chain, where an anti-chain is a set of nodes that are neither
ancestors nor descendants of one another. As a side effect, an
attribute called "width" is set for each node in the subgraph. If the
second argument is not given or has a non-zero value, the the "width"
attribute is reset to 0 for all nodes in the graph before beginning.
It is necessary to do this the first time the method is called, or if
the graph has been modified since the method was last called.
Alternatively, it can be initialized directly using
attributeInit width 0
.
-
topologicalSort
roots
- Return a list of node names in the subgraph with the given roots.
This includes the roots themselves and all descendants. The returned
list contains one element for each node in the subgraph, beginning
with the roots, sorted alphabetically, then children of the first
root, followed by children of the second root, etc. The descendants
are listed in a topologically sorted order, meaning that parents are
always described before their children. Note that this method is not
very efficient, so for performance sensitive applications, more
direct scanning of the graph may be more appropriate. Also,
depthSort
provides an alternative topological sort,
although it does not directly support describing a subgraph (it
describes the entire graph).
-
verifyAcyclic
{repair {}}
- Verify that there are no cycles in the graph. If no argument is
given, or if the argument is an empty string, trigger an error on the first
arc closing a cycle that is encountered. Otherwise, repair the graph
by removing the offending reference to an arbitrary arc in each
cycle.
Index of classes
Author: Edward A. Lee
Version: @(#)DirectedAcyclicGraph.itcl 1.8 11/02/96
Copyright: (c) 1995-1996 The Regents of the University of California.