Documentation
¶
Overview ¶
Package dot writes graphs in the Graphviz DOT format (https://graphviz.org/doc/info/lang.html). Useful for quick visual inspection — pipe the output through 'dot -Tsvg' or 'dot -Tpng'.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Write ¶
Write streams a DOT document representing a to w. The header uses 'digraph' for directed graphs and 'graph' for undirected. Edge weights are emitted as a label="..." attribute when non-zero.
Example ¶
ExampleWrite renders a single directed, weighted edge as Graphviz DOT. The output is a digraph whose body lists "src -> dst" with the weight as the edge label; pipe it through `dot -Tsvg` to visualise.
package main
import (
"bytes"
"fmt"
"github.com/FlavioCFOliveira/GoGraph/graph/adjlist"
"github.com/FlavioCFOliveira/GoGraph/graph/io/dot"
)
func main() {
g := adjlist.New[string, int64](adjlist.Config{Directed: true})
_ = g.AddEdge("a", "b", 5)
var buf bytes.Buffer
if err := dot.Write(&buf, g); err != nil {
panic(err)
}
fmt.Print(buf.String())
}
Output: digraph G { a -> b [label="5"]; }
Example (MultiEdge) ¶
ExampleWrite_multiEdge renders a small directed graph. The DOT writer emits one line per edge in an internal NodeID order, so the example sorts the body lines before printing to keep the output stable.
package main
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/FlavioCFOliveira/GoGraph/graph/adjlist"
"github.com/FlavioCFOliveira/GoGraph/graph/io/dot"
)
func main() {
g := adjlist.New[string, int64](adjlist.Config{Directed: true})
_ = g.AddEdge("a", "b", 1)
_ = g.AddEdge("b", "c", 2)
var buf bytes.Buffer
if err := dot.Write(&buf, g); err != nil {
panic(err)
}
// Collect just the indented edge lines and sort them for a
// deterministic, layout-independent rendering.
var edges []string
for _, line := range strings.Split(buf.String(), "\n") {
if strings.HasPrefix(line, " ") {
edges = append(edges, strings.TrimSpace(line))
}
}
sort.Strings(edges)
for _, e := range edges {
fmt.Println(e)
}
}
Output: a -> b [label="1"]; b -> c [label="2"];
Types ¶
This section is empty.