Documentation
¶
Index ¶
- Constants
- func Reverse(a []string)
- type Graph
- func (g *Graph) AddEdge(from string, to string) error
- func (g *Graph) AddEdgeTuple(fromTo ...[2]string) (err error)
- func (g *Graph) AddNode(names ...string)
- func (g *Graph) ContainsNode(name string) bool
- func (g *Graph) DOTString() string
- func (g *Graph) DepthFirst(names ...string) ([]string, error)
- func (g *Graph) ParseLines(edgeSep, pairSep string, lineReader func() (string, error)) (err error)
- func (g *Graph) ParseString(data, edgeSep, pairSep string)
- func (g *Graph) TopSort(names ...string) ([]string, error)
Examples ¶
Constants ¶
View Source
const EDGE_SEP = ">"
View Source
const PAIR_SEP = ","
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
func (*Graph) AddEdgeTuple ¶
Example ¶
fmt.Println(`
import fmt
// Initialize the graph
graph := topsort.NewGraph()
// Add nodes
graph.AddNode("A", "B")
// Add edges: A -> B and B -> C
graph.AddEdgeEdgeTuple([2]string{"A", "B"}, [2]string{"B", "C"})
`)
func (*Graph) ContainsNode ¶
func (*Graph) DOTString ¶
DOTString Generate GraphViz DOT string
Example ¶
data := `// digraph G {
// "A" -> "B";
// "B" -> "C";
// "B" -> "D";
// "C"
// "D"
// "E" -> "D";
// }`
fmt.Println(`
import fmt
// Initialize the graph
graph := topsort.NewGraph()
graph := NewGraph()
graph.ParseString("A>B,E,B>C,B>D,E>D", "", "")
println(graph.DOTString())
` + data)
func (*Graph) DepthFirst ¶
DepthFirst Depth-first classifier. The order is defined by the default ordering of the key in the edges map and edges of edges. If `names` is empty, classify all nodes in the graph.
Example ¶
fmt.Println(`
import fmt
// Initialize the graph
graph := topsort.NewGraph()
// Add nodes
graph.AddNode("A", "B", "C", "D", "E")
// Add edges
graph.AddEdge("A", "B")
graph.AddEdge("B", "C")
graph.AddEdge("B", "D")
graph.AddEdge("E", "D")
// in depth-first order
results, err = graph.DepthFirst("A")
if err != nil {
panic(err)
}
fmt.Println(results) // => [A B D C]
// all nodes in depth-first order
results, err = graph.DepthFirst()
if err != nil {
panic(err)
}
fmt.Println(results) // => [E A B D C]
`)
func (*Graph) ParseLines ¶
ParseLines Parse nodes and egdes from lines
Example ¶
fmt.Println(`
import fmt
// Initialize the graph
graph := topsort.NewGraph()
lines := []string{"A>B", "B>C,C>D", ""}
i := -1
lineReader := func() string {
i++
return lines[i]
}
// default separators
graph.ParseLines("", "", lineReader)
// custom separators
lines := []string{"A-B", "B-C|C-D", ""}
i = -1
graph.ParseLines("-", "|", lineReader)
`)
func (*Graph) ParseString ¶
ParseString Parse nodes and egdes from string
Example ¶
fmt.Println(`
import fmt
// Initialize the graph
graph := topsort.NewGraph()
// default separators
graph.ParseString("A>B,B>C", "", "")
// custom separators
graph.ParseString("A-B|B-C", "-", "|")
`)
func (*Graph) TopSort ¶
TopSort Topological node classifier. The order is defined by the default ordering of the key in the edges map and edges of edges. If `names` is empty, sort all nodes in the graph.
Example ¶
fmt.Println(`
import fmt
// Initialize the graph
graph := topsort.NewGraph()
// Add nodes
graph.AddNode("A", "B", "C", "D", "E")
// Add edges
graph.AddEdge("A", "B")
graph.AddEdge("B", "C")
graph.AddEdge("B", "D")
graph.AddEdge("E", "D")
// Topologically sort only node A in dependency order and your edges, but not sort D and E.
results, err := graph.TopSort("A")
if err != nil {
panic(err)
}
fmt.Println(results) // => [C D B A]
// Topologically sort all nodes in the graph
results, err := graph.TopSort()
if err != nil {
panic(err)
}
fmt.Println(results) // => [B C D B A E]
`)
Click to show internal directories.
Click to hide internal directories.