Documentation
¶
Overview ¶
Package viz exports the gga graph structs to .dot files, the format supported by Graphviz (https://graphviz.org/). The main use case is the ability to visualize a graph before, during and after the execution of an algorithm.
Index ¶
- Variables
- func DotAttrs(f ds.FAttrs) string
- func ExportViz(vi AlgoViz, w io.Writer) error
- func Quoted(i ds.Item) string
- func ResetGraphFmt(g *ds.G)
- func SetTheme(e *Exporter, t Theme)
- func Snapshot(g *ds.G, w io.Writer, t Theme)
- type AlgoViz
- type BFSViz
- type CCViz
- type DFSViz
- type Exporter
- type GSCCViz
- type LightBreezeTheme
- type MSTViz
- type SCCViz
- type TSortViz
- type Theme
- type ThemedGraphViz
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Themes = struct { LightBreeze LightBreezeTheme }{}
Functions ¶
func DotAttrs ¶
DotAttrs converts an object holding formatting attributes to its DOT language equivalent. Full list of DOT attributes can be found here: https://graphviz.org/doc/info/attrs.html.
func ExportViz ¶ added in v0.0.7
ExportViz guides the execution of an AlgoViz implementation and then export its results.
func Quoted ¶ added in v0.0.7
Quoted adds quotes to the label of a ds.Item, which is useful for labels containing special characters.
func ResetGraphFmt ¶ added in v0.0.4
ResetGraphFmt resets custom formatting attributes for every vertex and edge of a graph.
Types ¶
type AlgoViz ¶ added in v0.0.6
An AlgoViz implementer can traverse the results of a graph algorithm, provide its input graph, and also support theming.
type BFSViz ¶ added in v0.0.4
type BFSViz struct {
ThemedGraphViz
Tree algo.BFTree
Source int
// OnUnVertex is called when an unreachable vertex is found.
OnUnVertex func(int, algo.BFNode)
// OnSourceVertex is called when the source vertex is found.
OnSourceVertex func(int, algo.BFNode)
// OnTreeVertex is called when any tree vertex is found, including the source vertex.
OnTreeVertex func(int, algo.BFNode)
// OnTreeEdge is called when a tree edge is found.
OnTreeEdge func(int, int)
}
BFSViz formats and exports a graph after the execution of the BFS algorithm. The output of the algorithm is traversed, and hooks are provided so that custom formatting can be applied to the graph, its vertices and edges.
type CCViz ¶ added in v0.0.10
type CCViz struct {
ThemedGraphViz
CCs []algo.CC
// OnCCVertex is called for every vertex, along with the index of its CC.
OnCCVertex func(int, int)
// OnCCEdge is called for any edge connecting vertices in the same CC.
OnCCEdge func(int, int, int)
}
CCViz formats and exports an undirected graph after the execution of any algorithm that discovers connected components. The output of the algorithm is traversed, and hooks are provided so that custom formatting can be applied to the graph, its vertices and edges.
type DFSViz ¶ added in v0.0.6
type DFSViz struct {
ThemedGraphViz
Forest algo.DFForest
Edges algo.EdgeTypes
// OnTreeVertex is called for every vertex in the graph.
OnTreeVertex func(int, algo.DFNode)
// OnRootVertex is called when the root of a DF tree is found.
OnRootVertex func(int, algo.DFNode)
// OnTreeEdge is called when a tree edge is found.
OnTreeEdge func(int, int)
// OnForwardEdge is called when a forward edge is found.
OnForwardEdge func(int, int)
// OnBackEdge is called when a back edge is found.
OnBackEdge func(int, int)
// OnCrossEdge is called when a cross edge is found.
OnCrossEdge func(int, int)
}
DFSViz formats and exports a graph after the execution of the DFS algorithm. The output of the algorithm is traversed, and hooks are provided so that custom formatting can be applied to the graph, its vertices and edges.
type Exporter ¶ added in v0.0.4
type Exporter struct {
Lines []string
Extra []string
DefaultGraphFmt ds.FAttrs
DefaultVertexFmt ds.FAttrs
DefaultEdgeFmt ds.FAttrs
UndirectedArrow string
DirectedArrow string
}
Exporter implements the ds.GraphVisitor interface in order to traverse a ds.Graph and build a sequence of lines in the DOT language. After a successful visit, these lines can then be exported to an io.Writer by calling its Export method.
Full specification of the DOT language, by Graphviz can be found here: https://graphviz.org/doc/info/lang.html
Example ¶
package main
import (
"fmt"
"os"
"github.com/vc-souza/gga/ds"
)
type pokemon struct {
Type string
Name string
}
func (c pokemon) Label() string {
return fmt.Sprintf("%s (%s)", c.Name, c.Type)
}
func main() {
pwag := &pokemon{Type: "water", Name: "Poliwag"}
pwl := &pokemon{Type: "water", Name: "Poliwhirl"}
pot := &pokemon{Type: "water", Name: "Politoed"}
pow := &pokemon{Type: "water/fighting", Name: "Poliwrath"}
g := ds.NewDigraph()
e := NewExporter()
g.AddVertex(pwag)
g.AddVertex(pwl)
g.AddVertex(pot)
g.AddVertex(pow)
g.AddEdge(pwag, pwl, 0)
g.AddEdge(pwl, pot, 0)
g.AddEdge(pwl, pow, 0)
e.DefaultGraphFmt = ds.FAttrs{
"rankdir": "LR",
}
e.DefaultVertexFmt = ds.FAttrs{
"style": "filled",
}
e.DefaultEdgeFmt = ds.FAttrs{
"arrowhead": "open",
}
if i, ok := g.VertexIndex(pwag); ok {
g.V[i].SetFmtAttr("shape", "square")
}
if v, e, ok := g.EdgeIndex(pwag, pwl); ok {
g.V[v].E[e].SetFmtAttr("label", "Level 25")
}
if v, e, ok := g.EdgeIndex(pwl, pot); ok {
g.V[v].E[e].SetFmtAttr("label", "Trade holding King's Rock")
}
if v, e, ok := g.EdgeIndex(pwl, pow); ok {
g.V[v].E[e].SetFmtAttr("label", "Water Stone")
}
e.Export(g, os.Stdout)
}
Output: strict digraph { graph [ rankdir="LR" ] node [ style="filled" ] edge [ arrowhead="open" ] "Poliwag (water)" [ shape="square" ] "Poliwag (water)" -> "Poliwhirl (water)" [ label="Level 25" ] "Poliwhirl (water)" "Poliwhirl (water)" -> "Politoed (water)" [ label="Trade holding King's Rock" ] "Poliwhirl (water)" -> "Poliwrath (water/fighting)" [ label="Water Stone" ] "Politoed (water)" "Poliwrath (water/fighting)" }
func NewExporter ¶ added in v0.0.4
func NewExporter() *Exporter
NewExporter creates an initialized Exporter.
func (*Exporter) AddExtra ¶ added in v0.0.7
AddExtra adds extra lines to the end of the DOT file, which is a feature needed by more complex visualizations that need to control more precisely how a graph is rendered, like adding invisible, structural edges, etc.
func (*Exporter) Export ¶ added in v0.0.4
Export writes the data it has accumulated to an io.Writer.
func (*Exporter) VisitGraphEnd ¶ added in v0.0.4
func (*Exporter) VisitGraphStart ¶ added in v0.0.4
type GSCCViz ¶ added in v0.0.9
type GSCCViz struct {
ThemedGraphViz
// OnGSCCVertex is called for every vertex of the GSCC.
OnGSCCVertex func(v int)
}
GSCCViz formats and exports a GSCC graph after it has been calculated. Its vertices are traversed, and hooks are provided so that custom formatting can be applied.
func NewGSCCViz ¶ added in v0.0.9
NewGSCCViz initializes a new GSCCViz with NOOP hooks.
type LightBreezeTheme ¶ added in v0.0.7
type LightBreezeTheme struct{}
func (LightBreezeTheme) SetEdgeFmt ¶ added in v0.0.7
func (t LightBreezeTheme) SetEdgeFmt(attrs ds.FAttrs)
func (LightBreezeTheme) SetGraphFmt ¶ added in v0.0.7
func (t LightBreezeTheme) SetGraphFmt(attrs ds.FAttrs)
func (LightBreezeTheme) SetVertexFmt ¶ added in v0.0.7
func (t LightBreezeTheme) SetVertexFmt(attrs ds.FAttrs)
type MSTViz ¶ added in v0.0.11
type MSTViz struct {
ThemedGraphViz
MST algo.MST
// OnMSTEdge is called for any edge that is a part of an MST.
OnMSTEdge func(int, int)
}
MSTViz formats and exports an undirected graph after the execution of any algorithm that builds a minimum spanning tree. The output of the algorithm is traversed, and hooks are provided so that custom formatting can be applied to the graph edges.
type SCCViz ¶ added in v0.0.8
type SCCViz struct {
ThemedGraphViz
SCCs []algo.SCC
// OnSCCVertex is called for every vertex, along with the index of its SCC.
OnSCCVertex func(int, int)
// OnSCCEdge is called for any edge connecting vertices in the same SCC.
OnSCCEdge func(int, int, int)
// OnCrossSCCEdge is called for any edge connecting vertices in different SCCs.
OnCrossSCCEdge func(int, int, int, int)
}
SCCViz formats and exports a directed graph after the execution of any algorithm that discovers strongly connected components. The output of the algorithm is traversed, and hooks are provided so that custom formatting can be applied to the graph, its vertices and edges.
type TSortViz ¶ added in v0.0.7
type TSortViz struct {
ThemedGraphViz
Order []int
/*
OnVertexRank is called for every vertex in the graph, along with the rank
of the vertex in the final topological ordering of vertices.
*/
OnVertexRank func(int, int)
/*
OnOrderEdge is called for every edge that connects two contiguous vertices
in the final topological ordering. Note that such an edge might not actually
exist in the graph, which gives the caller the opportunity to either create
it or take any other action.
*/
OnOrderEdge func(int, int, int, bool)
}
TSortViz formats and exports a directed graph after the execution of the Topological Sort algorithm. The output of the algorithm is traversed, and hooks are provided so that custom formatting can be applied to the graph, its vertices and edges.
func NewTSortViz ¶ added in v0.0.7
NewTSortViz initializes a new TSortViz with NOOP hooks.
type Theme ¶ added in v0.0.7
A Theme implementation is capable of setting default formatting for a graph, its vertices and edges.
type ThemedGraphViz ¶ added in v0.0.7
ThemedGraphViz contains data that is useful for graph algorithm visualizations. When embedded, it also provides a good part of the AlgoViz interface for free.
func (*ThemedGraphViz) GetExtra ¶ added in v0.0.7
func (v *ThemedGraphViz) GetExtra() []string
func (*ThemedGraphViz) GetGraph ¶ added in v0.0.7
func (v *ThemedGraphViz) GetGraph() *ds.G
func (*ThemedGraphViz) GetTheme ¶ added in v0.0.7
func (v *ThemedGraphViz) GetTheme() Theme