The go package github.com/gonum/graph provides various graph data structures and algorithms. The Node interface defines the behavior of a node in a graph, with methods for getting and setting its ID and retrieving its neighbors (i.e., nodes that are adjacent to it).
Example 1: Creating a node with a unique ID
package main
import ( "fmt" "github.com/gonum/graph" )
func main() { n := graph.Node(42) fmt.Println("ID:", n.ID()) }
Output: ID: 42
Example 2: Retrieving neighbors of a node
package main
import ( "fmt" "github.com/gonum/graph" )
func main() { nodes := make([]graph.Node, 5) for i := 0; i < 5; i++ { nodes[i] = graph.Node(i) }
g := graph.New(simple.NewUndirectedGraph()) for _, n := range nodes { g.AddNode(n) } for _, e := range edges { g.SetEdge(e) }
// Get neighbors of node 2 node := nodes[2] neighbors := g.From(node) fmt.Printf("Neighbors of node %d:", node.ID()) for _, n := range neighbors { fmt.Printf(" %d", n.ID()) } fmt.Println() }
Output: Neighbors of node 2: 1 3
Golang Node - 30 examples found. These are the top rated real world Golang examples of github.com/gonum/graph.Node extracted from open source projects. You can rate examples to help us improve the quality of examples.