Created
May 9, 2020 19:47
-
-
Save gillesdemey/633bc7d8f50a711cefc9e2832f675323 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"gonum.org/v1/gonum/graph" | |
"gonum.org/v1/gonum/graph/simple" | |
"gonum.org/v1/gonum/graph/traverse" | |
) | |
// GraphNode is a node in the DAG | |
type GraphNode struct { | |
graph.Node | |
Foo string | |
} | |
func main () { | |
dag := simple.NewDirectedGraph() | |
n1 := &GraphNode{ | |
Node: dag.NewNode(), | |
Foo: "bar", | |
} | |
dag.AddNode(n1) | |
n2 := &GraphNode{ | |
Node: dag.NewNode(), | |
Foo: "baz", | |
} | |
dag.AddNode(n2) | |
n3 := &GraphNode{ | |
Node: dag.NewNode(), | |
Foo: "qux", | |
} | |
dag.AddNode(n3) | |
e1 := dag.NewEdge(n1, n2) | |
dag.SetEdge(e1) | |
e2 := dag.NewEdge(n1, n3) | |
dag.SetEdge(e2) | |
root := dag.Node(0) | |
// walk the graph | |
w := &traverse.BreadthFirst{ | |
Visit: handleVisit, | |
Traverse: handleTraverse, | |
} | |
// keep walking until the end | |
w.Walk(dag, root, nil) | |
} | |
func handleVisit (g graph.Node) { | |
fmt.Printf("Found %+v\n", g) | |
} | |
func handleTraverse (e graph.Edge) bool { | |
to := e.To() | |
fmt.Printf("Edge to %+v\n", to) | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment