struct::graph_v1(3tcl)
π struct::graph_v1 π
it's the Tcl Data Structures manual, section 3tcl, and bestie we are going to have a TIME β¨
β¨ NAME β¨
struct::graph_v1 β Create and manipulate directed graph objects π¦
πΈ SYNOPSIS πΈ
package require Tcl 8.5 9
package require struct::graph ?1.2.2?
graphName option ?arg arg ...?
graphName destroy
graphName arc append arc ?-key key? value
graphName arc delete arc ?arc ...?
graphName arc exists arc
graphName arc get arc ?-key key?
graphName arc getall arc
graphName arc keys arc
graphName arc keyexists arc ?-key key?
graphName arc insert start end ?child?
graphName arc lappend arc ?-key key? value
graphName arc set arc ?-key key? ?value?
graphName arc source arc
graphName arc target arc
graphName arc unset arc ?-key key?
graphName arcs ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding nodelist?
graphName node append node ?-key key? value
graphName node degree ?-in|-out? node
graphName node delete node ?node ...?
graphName node exists node
graphName node get node ?-key key?
graphName node getall node
graphName node keys node
graphName node keyexists node ?-key key?
graphName node insert ?child?
graphName node lappend node ?-key key? value
graphName node opposite node arc
graphName node set node ?-key key? ?value?
graphName node unset node ?-key key?
graphName nodes ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding nodelist?
graphName get ?-key key?
graphName getall
graphName keys
graphName keyexists ?-key key?
graphName set ?-key key? ?value?
graphName swap node1 node2
graphName unset ?-key key?
graphName walk node ?-order order? ?-type type? ?-dir direction? -command cmd
π DESCRIPTION π
okay so. The ::struct::graph command creates a brand new graph object, and that object comes with an associated global Tcl command whose name is graphName. iconic behavior tbh β¨ This command is what you use to invoke all the various operations on the graph. It has this general form:
graphName option ?arg arg ...?
optionand theargsβ these determine the exact behavior of the command π
A directed graph is a structure containing two collections of elements, called nodes and arcs respectively, together with a relation (the "connectivity") that places a general structure upon the nodes and arcs. πΈοΈ
Every arc is connected to two nodes. One of them is called the source, the other is called the target. The way thatβ¦ this imposes a direction on the arc, which is said to go from the source to the target π It is allowed for the source and target of an arc to be the same node β such an arc is called a loop π Whenever a node is the source or the target of an arc, both are said to be adjacent. And that 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 the 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 edges in either set is called the in- resp. the out-degree of the node. π
On top of maintaining the node and arc relationships, this graph implementation also lets you associate any number of keyed values with each node and each arc. ποΈ
The following commands are possible for graph objects:
graphName destroyβ Destroy the graph, including its storage space and associated command. it's gone, bestie, no take-backs πgraphName arc append arc ?-key key? valueβ Appends a value to one of the keyed values associated with an arc. If no key is specified, the keydatais assumed.graphName arc delete arc ?arc ...?β Remove the specified arcs from the graph. βοΈgraphName arc exists arcβ Returntrueif the specified arc exists in the graph.graphName arc get arc ?-key key?β Return the value associated with the key key for the arc. If no key is specified, the keydatais assumed.graphName arc getall arcβ Returns a serialized list of key/value pairs (suitable for use with[array set]) for the arc. π¦graphName arc keys arcβ Returns a list of keys for the arc.graphName arc keyexists arc ?-key key?β Returntrueif the specified key exists for the arc. If no key is specified, the keydatais assumed.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 formarcx. β¨graphName arc lappend arc ?-key key? valueβ Appends a value (as a list) to one of the keyed values associated with an arc. If no key is specified, the keydatais assumed.graphName arc set arc ?-key key? ?value?β Set or get one of the keyed values associated with an arc. If no key is specified, the keydatais assumed. Every arc that is added to a graph has the empty string assigned to the keydataautomatically. 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. π«Ά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 unset arc ?-key key?β Remove a keyed value from the arc arc. If no key is specified, the keydatais assumed.graphName arcs ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding nodelist?β Return 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. The restrictions that involve connected nodes have a list 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 nodelist.-outβ Return a list of all arcs whose source is one of the nodes in the nodelist.-adjβ Return a list of all arcs adjacent to at least one of the nodes in the nodelist. This is the union of the nodes returned by-inand-out.-innerβ 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.-embeddingβ Return a list of all arcs adjacent to exactly one of the nodes in the nodelist. 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.
The restrictions imposed by either -in, -out, -adj, -inner, or -embedded are applied first. Specifying more than one of them is illegal. At last the restrictions set via -key (and -value) are applied. Specifying more than one -key (and -value) is illegal.
graphName node append node ?-key key? valueβ Appends a value to one of the keyed values associated with a node. If no key is specified, the keydatais assumed.graphName node degree ?-in|-out? nodeβ Return the number of arcs adjacent to the specified node. If one of the restrictions-inor-outis 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β Returntrueif the specified node exists in the graph.graphName node get node ?-key key?β Return the value associated with the key key for the node. If no key is specified, the keydatais assumed.graphName node getall nodeβ Returns a serialized list of key/value pairs (suitable for use with[array set]) for the node. π¦graphName node keys nodeβ Returns a list of keys for the node.graphName node keyexists node ?-key key?β Returntrueif the specified key exists for the node. If no key is specified, the keydatais assumed.graphName node insert ?child?β Insert a node named child into the graph. The node 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 formnodex. β¨graphName node lappend node ?-key key? valueβ Appends a value (as a list) to one of the keyed values associated with a node. If no key is specified, the keydatais assumed.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 set node ?-key key? ?value?β Set or get one of the keyed values associated with a node. If no key is specified, the keydatais assumed. Every node that is added to a graph has the empty string assigned to the keydataautomatically. 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 key?β Remove a keyed value from the node node. If no key is specified, the keydatais assumed.graphName nodes ?-key key? ?-value value? ?-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.
okay so the restriction options you can throw at it? literally identical to the ones for method arcs. and the set of nodes it hands back? it's computed as the union of all source AND target nodes for all the arcs that satisfy the restriction β as defined for arcs. every single one. that's the whole set. periodt. π
graphName get ?-key key?
Gives you back the value associated with the key key for the graph. and if you don't specify a key at all? it just assumes the key data. simple as that bestie π
graphName getall
Returns a serialized list of key/value pairs for the whole graph β and yes it's literally suitable for use with [array set], iykyk β¨
graphName keys
Returns a list of keys for the whole graph. that's it. that's the tweet. π¦
graphName keyexists ?-key key?
Returns true if the specified key exists for the whole graph. no key specified? it assumes the key data, as always π
graphName set ?-key key? ?value?
Set or get one of the keyed values associated with a graph. no key given β the key data is assumed, standard behaviour at this point ngl.
every graph has the empty string assigned to the key data automatically. and a graph can have any number of keyed values attached to it, no cap.
- if value is not specified β the command returns the current value assigned to the key
- if value is specified β the command assigns that value to the key
graphName swap node1 node2
Swaps the position of node1 and node2 in the graph. switcheroo π iconic
graphName unset ?-key key?
Removes a keyed value from the graph. no key specified? you already know β the key data is assumed. bye bye value π
graphName walk node ?-order order? ?-type type? ?-dir direction?
-command cmd
graphName walk node ?-order order? ?-type type? ?-dir direction?
-command cmd
okay so this one's the main character of the page, pay attention π«Ά
It performs a breadth-first or depth-first walk of the graph, starting at the node node, going in either the direction of outgoing arcs or opposite to the incoming arcs.
The type of walk β breadth-first or depth-first β is determined by the value of type:
bfsβ breadth-firstdfsβ 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:
preβ pre-orderpostβ post-orderbothβ both-order
Pre-order is the default.
- Pre-order walking means a node is visited before any of its neighbors (as defined by the direction, see below).
- Post-order walking means a parent is visited after any of its neighbors.
- Both-order walking means a node is visited before and after any of its neighbors.
careful bestie: the combination of a breadth-first walk with post-order or both-order is illegal. not allowed. don't do it. π
The direction of the walk is determined by the value of dir:
backwardβ the direction opposite to the incoming arcsforwardβ 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 (enter or leave) and the values graphName and the name of the current node appended. πΈ
- For a pre-order walk, all nodes are entered.
- For a post-order walk, all nodes are left.
- In a both-order walk, the first visit of a node enters it, and the second visit leaves it.
BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. ngl, that's just how it is π
Please report such in the category struct :: graph of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist].
Also please report any ideas you may have for enhancements β for either the package and/or the documentation. we want them fr fr π«Ά
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. π
KEYWORDS π
cgraph, graph
CATEGORY π
Data structures
COPYRIGHT π
Copyright (c) 2002 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's a wrap bestie!! this whole thing ships as part of tcllib, version 1.2.2, and you'll find it filed under struct::graph_v1(3tcl) in your manual pages. go forth and walk some graphs, you absolute legend πβ¨π
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2002 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms