struct::graph_v1(3tcl)

πŸ“¦
version 2.0 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’… 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 ...?
  • option and the args β€” 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 key data is assumed.
  • 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 get arc ?-key key? β€” Return the value associated with the key key for the arc. If no key is specified, the key data is 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? β€” Return true if the specified key exists for the arc. If no key is specified, the key data is 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 form arcx. ✨
  • 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 key data is 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 key data is assumed. Every arc that is added to a graph has the empty string assigned to the key data automatically. 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 key data is 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 -in and -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 key data is assumed.
  • 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 key? β€” Return the value associated with the key key for the node. If no key is specified, the key data is 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? β€” Return true if the specified key exists for the node. If no key is specified, the key data is 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 form nodex. ✨
  • 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 key data is 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 key data is assumed. Every node that is added to a graph has the empty string assigned to the key data automatically. 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 key data is 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-first
  • dfs β†’ 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-order
  • post β†’ post-order
  • both β†’ 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 arcs
  • forward β†’ 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 (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