-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.go
More file actions
31 lines (25 loc) · 686 Bytes
/
Copy pathoutput.go
File metadata and controls
31 lines (25 loc) · 686 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package go_directed_acyclic_graph
import (
"fmt"
"github.com/tmc/dot"
)
// DOTGraph returns a textual representation of the graph in the DOT graph
// description language.
func (g *DirectedGraph) DOTGraph() string {
graph := dot.NewGraph("G")
graph.SetType(dot.DIGRAPH)
itemsByNode := make(map[Node]*dot.Node)
for _, node := range g.Nodes() {
item := dot.NewNode(fmt.Sprintf("%v", node))
itemsByNode[node] = item
graph.AddNode(item)
}
for fromNode, fromItem := range itemsByNode {
for _, toNode := range g.OutgoingEdges(fromNode) {
if toItem, ok := itemsByNode[toNode]; ok {
graph.AddEdge(dot.NewEdge(fromItem, toItem))
}
}
}
return graph.String()
}