table of contents
other versions
other sections
struct::graph(3tcl) | Tcl Data Structures | struct::graph(3tcl) |
NAME¶
struct::graph - Create and manipulate directed graph objectsSYNOPSIS¶
package require Tcl 8.4DESCRIPTION¶
A directed graph is a structure containing two collections of elements, called nodes and arcs respectively, together with a relation ("connectivity") 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 either the source or target of an arc both are said to be adjacent. 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 former are called the outgoing arcs of the node, the latter the incoming arcs of the node. The number of arcs in either set is called the in-degree resp. the out-degree of the node. In addition to maintaining the node and arc relationships, this graph implementation allows any number of named attributes to be associated with the graph itself, and each node or arc. Note: The major version of the package struct has been changed to version 2.0, due to backward incompatible changes in the API of this module. Please read the section Changes for 2.0 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. Its support is limited to versions of the package before 2.0. As of version 2.2 of this package a critcl based C implementation is available from here as well. This implementation however requires Tcl 8.4 to run. The main command of the package is:- ::struct::graph ?graphName? ?=|:=| as|deserialize 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:
- graphName option ?arg arg ...?
- Option and the args determine the exact behavior of the command.
::struct::graph mygraph = b
::struct::graph mygraph mygraph = b
::struct::graph mygraph deserialize $b
::struct::graph mygraph mygraph deserialize $b
- 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.
graphName deserialize [sourcegraph serialize]
- 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.
destgraph deserialize [graphName serialize]
- graphName append key value
- Appends a value to one of the keyed values associated with the graph. Returns the new value given to the attribute key.
- graphName deserialize serialization
- This is the complement to serialize. It replaces the graph data in graphName with the graph described by the serialization value. The old contents of graphName are deleted by this operation.
- graphName destroy
- Destroys the graph, including its storage space and associated command.
- graphName arc append 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 arc attr key
- graphName arc attr key -arcs list
- graphName arc attr key -glob globpattern
- graphName arc attr key -regexp repattern
- This method retrieves the value of the attribute named
key, for all arcs in the graph (matching the restriction specified
via one of the possible options) and having the specified attribute.
- -arcs
- The value is a list of arcs. Only the arcs mentioned in this list are searched for the attribute.
- -glob
- The value is a glob pattern. Only the arcs in the graph whose names match this pattern are searched for the attribute.
- -regexp
- The value is a regular expression. Only the arcs in the graph whose names match this pattern are searched for the attribute.
- graphName arc delete arc ?arc ...?
- Remove the specified arcs from the graph.
- graphName arc exists arc
- Return true if the specified arc exists in the graph.
- graphName arc flip arc
- Reverses the direction of the named arc, i.e. the source and target nodes of the arc are exchanged with each other.
- graphName arc get arc key
- Returns the value associated with the key key for the arc.
- graphName arc getall arc ?pattern?
- Returns a dictionary (suitable for use with [array set]) 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 glob pattern.
- graphName arc getunweighted
- Returns a list containing the names of all arcs in the graph which have no weight associated with them.
- graphName arc getweight arc
- Returns the weight associated with the arc. Throws an error if the arc has no weight associated with it.
- graphName arc keys 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 glob pattern.
- graphName arc keyexists arc key
- Return true if the specified key exists for the arc.
- graphName arc insert 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 arc lappend arc key value
- Appends a value (as a list) to one of the keyed values associated with an arc. Returns the new value given to the attribute key.
- graphName arc rename 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 arc set 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 arc setunweighted ?weight?
- Sets the weight of all arcs without a weight to weight. Returns the empty string as its result. If not present weight defaults to 0.
- graphName arc setweight arc weight
- Sets the weight of the arc to weight. Returns weight.
- graphName arc unsetweight arc
- Removes the weight of the arc, if present. Does nothing otherwise. Returns the empty string.
- graphName arc hasweight arc
- Determines if the arc has a weight associated with it. The result is a boolean value, True if a weight is defined, and False otherwise.
- graphName arc source arc
- Return the node the given arc begins at.
- graphName arc target arc
- Return the node the given arc ends at.
- graphName arc nodes arc
- Return the nodes the given arc begins and ends at, as a two-element list.
- graphName arc move-source arc newsource
- Changes the source node of the arc to newsource. It can be said that the arc rotates around its target node.
- graphName arc move-target arc newtarget
- Changes the target node of the arc to newtarget. It can be said that the arc rotates around its source node.
- graphName arc move arc newsource newtarget
- Changes both source and target nodes of the arc to newsource, and newtarget resp.
- graphName arc unset arc key
- Remove a keyed value from the arc arc. The method will do nothing if the key does not exist.
- graphName arc weights
- Returns a dictionary whose keys are the names of all arcs which have a weight associated with them, and the values are these weights.
- graphName arcs ?-key key? ?-value value? ?-filter cmdprefix? ?-in|-out|-adj|-inner|-embedding node node...?
- Returns a list of arcs in the graph. If no restriction is
specified a list containing all arcs is returned. Restrictions can limit
the list of returned arcs based on the nodes that are connected 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 take a variable number of nodes as argument, specified
after the name of the restriction itself.
- -in
- Return a list of all arcs whose target is one of the nodes in the set of nodes. I.e. it computes the union of all incoming arcs of the nodes in the set.
- -out
- Return a list of all arcs whose source is one of the nodes in the set of nodes. I.e. it computes the union of all outgoing arcs of the nodes in the set.
- -adj
- Return a list of all arcs adjacent to at least one of the nodes in the set. This is the union of the nodes returned by -in and -out.
- -inner
- Return a list of all arcs which are adjacent to two of the nodes in the set. This is the set of arcs in the subgraph spawned by the specified nodes.
- -embedding
- Return a list of all arcs adjacent to exactly one of the nodes in the set. This is the set of arcs connecting the subgraph spawned by the specified nodes to the rest of the graph.
- -key key
- Limit the list of arcs that are returned to those arcs that have an associated key key.
- -value value
- This restriction can only be used in combination with -key. It limits the list of arcs that are returned to those arcs whose associated key key has the value value.
- -filter cmdrefix
- Limit the list of arcs that are returned to those arcs that pass the test. The command in cmdprefix is called with two arguments, the name of the graph object, and the name of the arc in question. It is executed in the context of the caller and has to return a boolean value. Arcs for which the command returns false are removed from the result list before it is returned to the caller.
- graphName lappend key value
- Appends a value (as a list) to one of the keyed values associated with the graph. Returns the new value given to the attribute key.
- graphName node append 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 node attr key
- graphName node attr key -nodes list
- graphName node attr key -glob globpattern
- graphName node attr key -regexp repattern
- This method retrieves the value of the attribute named
key, for all nodes in the graph (matching the restriction specified
via one of the possible options) and having the specified attribute.
- -nodes
- The value is a list of nodes. Only the nodes mentioned in this list are searched for the attribute.
- -glob
- The value is a glob pattern. Only the nodes in the graph whose names match this pattern are searched for the attribute.
- -regexp
- The value is a regular expression. Only the nodes in the graph whose names match this pattern are searched for the attribute.
- graphName node degree ?-in|-out? node
- Return the number of arcs adjacent to the specified node. If one of the restrictions -in or -out is given only the incoming resp. outgoing arcs are counted.
- graphName node delete node ?node...?
- Remove the specified nodes from the graph. All of the nodes' arcs will be removed as well to prevent unconnected arcs.
- graphName node exists node
- Return true if the specified node exists in the graph.
- graphName node get node key
- Return the value associated with the key key for the node.
- graphName node getall node ?pattern?
- Returns a dictionary (suitable for use with [array set]) 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 glob pattern.
- graphName node keys node ?pattern?
- Returns a list of keys for the node. If the pattern is specified only the attributes whose names match the pattern will be part of the returned list. The pattern is a glob pattern.
- graphName node keyexists node key
- Return true if the specified key exists for the node.
- graphName node insert ?node...?
- Insert one or more nodes into the graph. The new nodes have no arcs connected to them. If no node is specified one node will be inserted, and the system will generate a unique name of the form node x for it.
- graphName node lappend node key value
- Appends a value (as a list) to one of the keyed values associated with an node. Returns the new value given to the attribute key.
- graphName node opposite node arc
- Return the node at the other end of the specified arc, which has to be adjacent to the given node.
- graphName node rename 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 node set 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 node unset node key
- Remove a keyed value from the node node. The method will do nothing if the key does not exist.
- graphName nodes ?-key key? ?-value value? ?-filter cmdprefix? ?-in|-out|-adj|-inner|-embedding node node...?
- 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.
- -in
- Return a list of all nodes with at least one outgoing arc ending in a node found in the specified set of nodes. Alternatively specified as the set of source nodes for the -in arcs of the node set. The incoming neighbours.
- -out
- Return a list of all nodes with at least one incoming arc starting in a node found in the specified set of nodes. Alternatively specified as the set of target nodes for the -out arcs of the node set. The outgoing neighbours.
- -adj
- This is the union of the nodes returned by -in and -out. The neighbours.
- -inner
- The set of neighbours (see -adj above) which are also in the set of nodes. I.e. the intersection between the set of nodes and the neighbours per -adj.
- -embedding
- The set of neighbours (see -adj above) which are not in the set of nodes. I.e. the difference between the neighbours as per -adj, and the set of nodes.
- -key key
- Limit the list of nodes that are returned to those nodes that have an associated key key.
- -value value
- This restriction can only be used in combination with -key. It limits the list of nodes that are returned to those nodes whose associated key key has the value value.
- -filter cmdrefix
- Limit the list of nodes that are returned to those nodes that pass the test. The command in cmdprefix is called with two arguments, the name of the graph object, and the name of the node in question. It is executed in the context of the caller and has to return a boolean value. Nodes for which the command returns false are removed from the result list before it is returned to the caller.
- graphName get key
- Return the value associated with the key key for the graph.
- graphName getall ?pattern?
- Returns a dictionary (suitable for use with [array set]) for the whole graph. If the pattern is specified only the attributes whose names match the pattern will be part of the returned dictionary. The pattern is a glob pattern.
- graphName keys ?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 glob pattern.
- graphName keyexists key
- Return true if the specified key exists for the whole graph.
- graphName serialize ?node...?
- This method serializes the sub-graph spanned up by the
nodes. In other words it returns a tcl value completely describing
that graph. If no nodes are specified the whole graph will be serialized.
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.
- [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.
- [1]
- The name of the arc described by the element,
- [2]
- A reference to the destination node of the arc. This reference is an integer number given the index of that node in the main serialization list. As that it is greater than or equal to zero, less than the length of the serialization, and a multiple of three. Note: For internal consistency no arc name may be used twice, whether in the same node, or at some other node. This is a global consistency requirement for the serialization.
- [3]
- And a dictionary containing the attributes associated with the arc.
- [4]
- The weight associated with the arc. This value is optional.
Its non-presence means that the arc in question has no weight associated
with it.
# 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 neither attribute data nor weighted arcs.
- graphName set 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 swap node1 node2
- Swap the position of node1 and node2 in the graph.
- graphName unset key
- Remove a keyed value from the graph. The method will do nothing if the key does not exist.
- graphName walk node ?-order order? ?-type type? ?-dir direction? -command cmd
- Perform a breadth-first or depth-first walk of the graph
starting at the node node going in either the direction of outgoing
or opposite to the incoming arcs.
CHANGES FOR 2.0¶
The following noteworthy changes have occurred:- [1]
- The API for accessing attributes and their values has been
simplified.
- [2]
- The methods keys and getall now take an optional pattern argument 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 arc rename and node rename.
- [4]
- The structure has been extended with API's for the
serialization and deserialization of graph objects, and a number of
operations based on them (graph assignment, copy construction).
- [5]
- A new method, attr, was added to both arc and node allowing the query and retrieval of attribute data without regard to arc and node relationships.
- [6]
- Both methods arcs and nodes have been extended with the ability to select arcs and nodes based on an arbitrary filtering criterium.
BUGS, IDEAS, FEEDBACK¶
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category struct :: graph of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation.KEYWORDS¶
adjacent, arc, cgraph, degree, edge, graph, loop, neighbour, node, serialization, subgraph, vertexCATEGORY¶
Data structuresCOPYRIGHT¶
Copyright (c) 2002-2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
2.4 | struct |