NAME
graph - Create and manipulate directed graph objects
SYNOPSIS
package require TTccll 88..22 package require ssttrruucctt ??22..00??::::ssttrruucctt::::ggrraapphh ?graphName? ?==|::==|aass|ddeesseerriiaalliizzee source?
ggrraapphhNNaammee option ?arg arg ...?graphName == sourcegraph
graphName -->> destgraph
graphName aappppeenndd key value
graphName ddeesseerriiaalliizzee serialization
graphName ddeessttrrooyy
graphName aarrcc aappppeenndd arc key value
graphName aarrcc aattttrr key
graphName aarrcc aattttrr key -aarrccss list
graphName aarrcc aattttrr key -gglloobb globpattern
graphName aarrcc aattttrr key -rreeggeexxpp repattern
graphName aarrcc ddeelleettee arc ?arc ...?
graphName aarrcc eexxiissttss arc
graphName aarrcc ggeett arc key
graphName aarrcc ggeettaallll arc ?pattern?
graphName aarrcc kkeeyyss arc ?pattern?
graphName aarrcc kkeeyyeexxiissttss arc key
graphName aarrcc iinnsseerrtt start end ?child?
graphName aarrcc llaappppeenndd arc key value
graphName aarrcc rreennaammee arc newname
graphName aarrcc sseett arc key ?value?
graphName aarrcc ssoouurrccee arc
graphName aarrcc ttaarrggeett arc
graphName aarrcc uunnsseett arc key
graphName aarrccss ?-key key? ?-value value? ?-filter cmdprefix?
?-in|-out|-adj|-inner|-embedding nodelist?
graphName llaappppeenndd key value
graphName nnooddee aappppeenndd node key value
graphName nnooddee aattttrr key
graphName nnooddee aattttrr key -nnooddeess list
graphName nnooddee aattttrr key -gglloobb globpattern
graphName nnooddee aattttrr key -rreeggeexxpp repattern
graphName nnooddee ddeeggrreeee ?-in|-out? node
graphName nnooddee ddeelleettee node ?node ...?
graphName nnooddee eexxiissttss node
graphName nnooddee ggeett node key
graphName nnooddee ggeettaallll node ?pattern?
graphName nnooddee kkeeyyss node ?pattern?
graphName nnooddee kkeeyyeexxiissttss node key
graphName nnooddee iinnsseerrtt ?child?
graphName nnooddee llaappppeenndd node key value
graphName nnooddee ooppppoossiittee node arc
graphName nnooddee rreennaammee node newname
graphName nnooddee sseett node key ?value?
graphName nnooddee uunnsseett node key
graphName nnooddeess ?-key key? ?-value value? ?-filter cmdprefix?
?-in|-out|-adj|-inner|-embedding nodelist?
graphName ggeett key
graphName ggeettaallll ?pattern?
graphName kkeeyyss ?pattern?
graphName kkeeyyeexxiissttss key
graphName sseerriiaalliizzee ?node...?
graphName sseett key ?value?
graphName sswwaapp node1 node2
graphName uunnsseett key
graphName wwaallkk node ?-order order? ?-type type? ?-dir direction? -com-
mand cmdDESCRIPTION
A directed graph is a structure containing two collections of elements,
called nodes and arcs respectively, together with a relation ("connec-
tivity") that places a general structure upon the nodes and arcs. Each arc is connected to two nodes, one of which is called the source and the other the target. This imposes a direction upon the arc, which is said to go from the source to the target. It is allowed that source and target of an arc are the same node. Such an arc is called a loop.Whenever a node is source or target of an arc both are said to be adja-
cent. This extends into a relation between nodes, i.e. if two nodes are connected through at least one arc they are said to be adjacent too.Each node can be the source and target for any number of arcs. The for-
mer are called the outgoing arcs of the node, the latter the incomingarcs of the node. The number of edges in either set is called the in-
resp. the out-degree of the node.
In addition to maintaining the node and arc relationships, this graph
implementation allows any number of keyed values to be associated with each node and arc.Note: The major version of the package ssttrruucctt has been changed to ver-
sion 2.0, due to backward incompatible changes in the API of this mod-
ule. Please read the section CChhaannggeess ffoorr 22..00 for a full list of all changes, incompatible and otherwise.Note: A C-implementation of the command can be had from the location
http://www.purl.org/NET/schlenker/tcl/cgraph. See also
http://wiki.tcl.tk/cgraph. This implementation uses a bit less memory
than the tcl version provided here directly, and is faster. The main command of the package is:::::ssttrruucctt::::ggrraapphh ?graphName? ?==|::==|aass|ddeesseerriiaalliizzee source?
The command creates a new graph object with an associated global
Tcl command whose name is graphName. This command may be used
to invoke various operations on the graph. It has the following
general form: ggrraapphhNNaammee option ?arg arg ...? Option and the args determine the exact behavior of the command.If graphName is not specified a unique name will be generated by the
package itself. If a source is specified the new graph will be initial-
ized to it. For the operators ==, ::==, and aass source is interpreted asthe name of another graph object, and the assignment operator == will be
executed. For ddeesseerriiaalliizzee the source is a serialized graph object and
ddeesseerriiaalliizzee will be executed. In other words::struct::graph mygraph = b
is equivalent to::struct::graph mygraph
mygraph = b
and::struct::graph mygraph deserialize $b
is equivalent to::struct::graph mygraph
mygraph deserialize $b
The following commands are possible for graph objects:
graphName == sourcegraph
This is the assignment operator for graph objects. It copies the
graph contained in the graph object sourcegraph over the graph
data in graphName. The old contents of graphName are deleted by
this operation. This operation is in effect equivalent tographName ddeesseerriiaalliizzee [sourcegraph sseerriiaalliizzee]
graphName -->> destgraph
This is the reverse assignment operator for graph objects. It
copies the graph contained in the graph object graphName over
the graph data in the object destgraph. The old contents of
destgraph are deleted by this operation.
This operation is in effect equivalent todestgraph ddeesseerriiaalliizzee [graphName sseerriiaalliizzee]
graphName aappppeenndd key value
Appends a value to one of the keyed values associated with thegraph. Returns the new value given to the attribute key.
graphName ddeesseerriiaalliizzee serialization
This is the complement to sseerriiaalliizzee. It replaces graph data in
graphName with the graph described by the serialization value.
The old contents of graphName are deleted by this operation.
graphName ddeessttrrooyy
Destroy the graph, including its storage space and associated
command.graphName aarrcc aappppeenndd arc key value
Appends a value to one of the keyed values associated with an arc. Returns the new value given to the attribute key.graphName aarrcc aattttrr key
graphName aarrcc aattttrr key -aarrccss list
graphName aarrcc aattttrr key -gglloobb globpattern
graphName aarrcc aattttrr key -rreeggeexxpp repattern
This method retrieves the value of the attribute named key, forall arcs in the graph (matching the restriction specified via
one of the possible options) and having the specified attribute. The result is a dictionary mapping from arc names to the value of attribute key at that arc. Arcs not having the attribute key, or not passing a specified restriction, are not listed in the result. The possible restrictions are:-aarrccss The value is a list of arcs. Only the arcs mentioned in
this list are searched for the attribute.-gglloobb The value is a glob pattern. Only the arcs in the graph
whose names match this pattern are searched for the attribute.-rreeggeexxpp
The value is a regular expression. Only the arcs in thegraph whose names match this pattern are searched for the
attribute.graphName aarrcc ddeelleettee arc ?arc ...?
Remove the specified arcs from the graph.
graphName aarrcc eexxiissttss arc
Return true if the specified arc exists in the graph.
graphName aarrcc ggeett arc key
Return the value associated with the key key for the arc.graphName aarrcc ggeettaallll arc ?pattern?
Returns a dictionary (suitable for use with [aarrrraayy sseett]) for the arc. If the pattern is specified only the attributes whose names match the pattern will be part of the returned dictionary. The pattern is a gglloobb pattern.graphName aarrcc kkeeyyss arc ?pattern?
Returns a list of keys for the arc. If the pattern is specified only the attributes whose names match the pattern will be part of the returned list. The pattern is a gglloobb pattern.graphName aarrcc kkeeyyeexxiissttss arc key
Return true if the specified key exists for the arc.graphName aarrcc iinnsseerrtt start end ?child?
Insert an arc named child into the graph beginning at the node
start and ending at the node end. If the name of the new arc is not specified the system will generate a unique name of the form arcx.graphName aarrcc llaappppeenndd arc key value
Appends a value (as a list) to one of the keyed values associ-
ated with an arc. Returns the new value given to the attribute key.graphName aarrcc rreennaammee arc newname
Renames the arc arc to newname. An error is thrown if either the arc does not exist, or a arc with name newname does exist. The result of the command is the new name of the arc.graphName aarrcc sseett arc key ?value?
Set or get one of the keyed values associated with an arc. An arc may have any number of keyed values associated with it. If value is not specified, this command returns the current value assigned to the key; if value is specified, this command assigns that value to the key, and returns that value.graphName aarrcc ssoouurrccee arc
Return the node the given arc begins at.graphName aarrcc ttaarrggeett arc
Return the node the given arc ends at.graphName aarrcc uunnsseett arc key
Remove a keyed value from the arc arc. The method will do noth-
ing if the key does not exist.graphName aarrccss ?-key key? ?-value value? ?-filter cmdprefix?
?-in|-out|-adj|-inner|-embedding nodelist?
Return a list of arcs in the graph. If no restriction is speci-
fied a list containing all arcs is returned. Restrictions canlimit the list of returned arcs based on the nodes that are con-
nected by the arc, on the keyed values associated with the arc, or both. A general filter command can be used as well. The restrictions that involve connected nodes have a list of nodes as argument, specified after the name of the restriction itself.-iinn Return a list of all arcs whose target is one of the
nodes in the nodelist.-oouutt Return a list of all arcs whose source is one of the
nodes in the nodelist.-aaddjj Return a list of all arcs adjacent to at least one of the
nodes in the nodelist. This is the union of the nodesreturned by -iinn and -oouutt.
-iinnnneerr Return a list of all arcs adjacent to two of the nodes in
the nodelist. This is the set of arcs in the subgraph
spawned by the specified nodes.-eemmbbeeddddiinngg
Return a list of all arcs adjacent to exactly one of the nodes in the nodelist. This is the set of arcs connectingthe subgraph spawned by the specified nodes to the rest
of the graph.
-kkeeyy key
Limit the list of arcs that are returned to those arcs that have an associated key key.-vvaalluuee value
This restriction can only be used in combination with-kkeeyy. It limits the list of arcs that are returned to
those arcs whose associated key key has the value value.-ffiilltteerr cmdrefix
Limit the list of arcs that are returned to those arcs that pass the test. The command in cmdprefix is calledwith two arguments, the name of the graph object, and the
name of the arc in question. It is executed in the con-
text of the caller and has to return a boolean value. Arcs for which the command returns ffaallssee are removed from the result list before it is returned to the caller.The restrictions imposed by either -iinn, -oouutt, -aaddjj, -iinnnneerr, or -eemmbbeedd-
ddeedd are applied first. After that the restrictions set via -kkeeyy (and
-vvaalluuee) are applied. Any restriction set through -ffiilltteerr is applied
last.graphName llaappppeenndd key value
Appends a value (as a list) to one of the keyed values associ-
ated with the graph. Returns the new value given to the
attribute key.graphName nnooddee aappppeenndd node key value
Appends a value to one of the keyed values associated with an node. Returns the new value given to the attribute key.graphName nnooddee aattttrr key
graphName nnooddee aattttrr key -nnooddeess list
graphName nnooddee aattttrr key -gglloobb globpattern
graphName nnooddee aattttrr key -rreeggeexxpp repattern
This method retrieves the value of the attribute named key, forall nodes in the graph (matching the restriction specified via
one of the possible options) and having the specified attribute. The result is a dictionary mapping from node names to the value of attribute key at that node. Nodes not having the attribute key, or not passing a specified restriction, are not listed in the result. The possible restrictions are:-nnooddeess The value is a list of nodes. Only the nodes mentioned in
this list are searched for the attribute.-gglloobb The value is a glob pattern. Only the nodes in the graph
whose names match this pattern are searched for the attribute.-rreeggeexxpp
The value is a regular expression. Only the nodes in thegraph whose names match this pattern are searched for the
attribute.graphName nnooddee ddeeggrreeee ?-in|-out? node
Return the number of arcs adjacent to the specified node. If oneof the restrictions -iinn or -oouutt is given only the incoming resp.
outgoing arcs are counted.graphName nnooddee ddeelleettee node ?node ...?
Remove the specified nodes from the graph. All of the nodes'
arcs will be removed as well to prevent unconnected arcs.graphName nnooddee eexxiissttss node
Return true if the specified node exists in the graph.
graphName nnooddee ggeett node key
Return the value associated with the key key for the node.graphName nnooddee ggeettaallll node ?pattern?
Returns a dictionary (suitable for use with [aarrrraayy sseett]) for the node. If the pattern is specified only the attributes whose names match the pattern will be part of the returned dictionary. The pattern is a gglloobb pattern.graphName nnooddee kkeeyyss node ?pattern?
Returns a list of keys for the node. If the pattern is speci-
fied only the attributes whose names match the pattern will be part of the returned list. The pattern is a gglloobb pattern.graphName nnooddee kkeeyyeexxiissttss node key
Return true if the specified key exists for the node.graphName nnooddee iinnsseerrtt ?child?
Insert a node named child into the graph. The nodes has no arcs
connected to it. If the name of the new child is not specified the system will generate a unique name of the form nodex.graphName nnooddee llaappppeenndd node key value
Appends a value (as a list) to one of the keyed values associ-
ated with an node. Returns the new value given to the attribute key.graphName nnooddee ooppppoossiittee node arc
Return the node at the other end of the specified arc, which has to be adjacent to the given node.graphName nnooddee rreennaammee node newname
Renames the node node to newname. An error is thrown if either the node does not exist, or a node with name newname does exist. The result of the command is the new name of the node.graphName nnooddee sseett node key ?value?
Set or get one of the keyed values associated with a node. A node may have any number of keyed values associated with it. If value is not specified, this command returns the current value assigned to the key; if value is specified, this command assigns that value to the key.graphName nnooddee uunnsseett node key
Remove a keyed value from the node node. The method will do nothing if the key does not exist.graphName nnooddeess ?-key key? ?-value value? ?-filter cmdprefix?
?-in|-out|-adj|-inner|-embedding nodelist?
Return a list of nodes in the graph. Restrictions can limit the
list of returned nodes based on neighboring nodes, or based on the keyed values associated with the node. The restrictions that involve neighboring nodes have a list of nodes as argument, specified after the name of the restriction itself. The possible restrictions are the same as for method aarrccss. The set of nodes to return is computed as the union of all source and target nodes for all the arcs satisfying the restrictions as defined for aarrccss.Note that here the -ffiilltteerr command is applied to the list of
nodes, not arcs.graphName ggeett key
Return the value associated with the key key for the graph.
graphName ggeettaallll ?pattern?
Returns a dictionary (suitable for use with [aarrrraayy sseett]) for thewhole graph. If the pattern is specified only the attributes
whose names match the pattern will be part of the returned dic-
tionary. The pattern is a gglloobb pattern.graphName kkeeyyss ?pattern?
Returns a list of keys for the whole graph. If the pattern is
specified only the attributes whose names match the pattern will be part of the returned list. The pattern is a gglloobb pattern.graphName kkeeyyeexxiissttss key
Return true if the specified key exists for the whole graph.
graphName sseerriiaalliizzee ?node...?
This method serializes the sub-graph spanned up by the nodes. In
other words it returns a tcl value completely describing thatgraph. If no nodes are specified the whole graph will be serial-
ized. This allows, for example, the transfer of graph objects
(or parts thereof) over arbitrary channels, persistence, etc. This method is also the basis for both the copy constructor and the assignment operator. The result of this method has to be semantically identical overall implementations of the graph interface. This is what will
enable us to copy graph data between different implementations
of the same interface. The result is a list containing a multiple of three items, plusone! In other words, '[llength $serial] % 3 == 1'. Valid values
include 1, 4, 7, ... The last element of the list is a dictionary containing theattributes associated with the whole graph. Regarding the other
elements; each triple consists of [1] The name of the node to be described, [2] A dictionary containing the attributes associated with the node, [3] And a list describing all the arcs starting at that node. The elements of the arc list are lists containing three elements each, i.e. [1] The name of the arc described by the element,[2] A reference to the destination node of the arc. This ref-
erence is an integer number given the index of that node in the main serialization list. As that it is greaterthan or equal to zero, less than the length of the seri-
alization, and a multiple of three. Note: For internal consistency no arc name may be used twice, whether in thesame node, or at some other node. This is a global con-
sistency requirement for the serialization. [3] And a dictionary containing the attributes associated with the arc. For all attribute dictionaries they keys are the names of the attributes, and the values are the values for each name. Note: The order of the nodes in the serialization has no relevance, nor has the order of the arcs per node.# A possible serialization for the graph structure
#
# d ---> %2
# / ^ \\
# / / \\
# / b \\
# / / \\
# %1 <- a - %0 e
# ^ \\ /
# \\ c /
# \\ \\ /
# \\ v v
# f --- %3
# is
#
# %3 {} {{f 6 {}}} %0 {} {{a 6 {}} {b 9 {}} {c 0 {}}} %1 {} {{d 9 {}}} %2 {} {{e 0 {}}} {}
#
# This assumes that the graph has no attribute data.
graphName sseett key ?value?
Set or get one of the keyed values associated with a graph. A
graph may have any number of keyed values associated with it. If
value is not specified, this command returns the current value assigned to the key; if value is specified, this command assigns that value to the key.graphName sswwaapp node1 node2
Swap the position of node1 and node2 in the graph.
graphName uunnsseett key
Remove a keyed value from the graph. The method will do nothing
if the key does not exist.graphName wwaallkk node ?-order order? ?-type type? ?-dir direction? -com-
mand cmdPerform a breadth-first or depth-first walk of the graph start-
ing at the node node going in either the direction of outgoing or opposite to the incoming arcs.The type of walk, breadth-first or depth-first, is determined by
the value of type; bbffss indicates breadth-first, ddffss indicates
depth-first. Depth-first is the default.
The order of the walk, pre-order, post-order or both-order is
determined by the value of order; pprree indicates pre-order, ppoosstt
indicates post-order, bbootthh indicates both-order. Pre-order is
the default. Pre-order walking means that a node is visited
before any of its neighbors (as defined by the direction, seebelow). Post-order walking means that a parent is visited after
any of its neighbors. Both-order walking means that a node is
visited before and after any of its neighbors. The combinationof a bread-first walk with post- or both-order is illegal.
The direction of the walk is determined by the value of dir; bbaacckkwwaarrdd indicates the direction opposite to the incoming arcs, ffoorrwwaarrdd indicates the direction of the outgoing arcs. As the walk progresses, the command cmd will be evaluated at each node, with the mode of the call (eenntteerr or lleeaavvee) and valuesgraphName and the name of the current node appended. For a pre-
order walk, all nodes are eenntteerred, for a post-order all nodes
are left. In a both-order walk the first visit of a node eenntteerrs
it, the second visit lleeaavvees it. CChhaannggeess ffoorr 22..00 The following noteworthy changes have occurred:[1] The API for accessing attributes and their values has been sim-
plified. All functionality regarding the default attribute "data" has been removed. This default attribute does not exist anymore. All accesses to attributes have to specify the name of the attribute in question. This backward incompatible change allowed us to simplify the signature of all methods handling attributes.Especially the flag -kkeeyy is not required anymore, even more, its
use is now forbidden. Please read the documentation for the arc and node methods sseett, ggeett, ggeettaallll, uunnsseett, aappppeenndd, llaappppeenndd, kkeeyyeexxiissttss and kkeeyyss for a description of the new API's.[2] The methods kkeeyyss and ggeettaallll now take an optional pattern argu-
ment and will return only attribute data for keys matching this pattern. [3] Arcs and nodes can now be renamed. See the documentation for the methods aarrcc rreennaammee and nnooddee rreennaammee. [4] The structure has been extended with API's for the serializationand deserialization of graph objects, and a number of operations
based on them (graph assignment, copy construction).
Please read the documentation for the methods sseerriiaalliizzee, ddeesseerrii-
aalliizzee, ==, and -->>, and the documentation on the construction of
graph objects.
Beyond the copying of whole graph objects these new API's also
enable the transfer of graph objects over arbitrary channels and
for easy persistence. [5] A new method, aattttrr, was added to both aarrcc and nnooddee allowing the query and retrieval of attribute data without regard to arc and node relationships. [6] Both methods aarrccss and nnooddeess have been extended with the abilityto select arcs and nodes based on an arbitrary filtering cri-
terium. KKEEYYWWOORRDDSScgraph, graph, serialization
COPYRIGHT Copyright (c) 2002 Andreas Kupriesstruct 2.0 graph(n)