AddVertexByLabel undefined
Hi, I am learning go currently and am trying to get a first example working (ideally with my own struct as vertices).
But, ` graph := gograph.Newstring
vA := gograph.AddVertexByLabel("A")
vB := gograph.AddVertexByLabel("B")
vC := gograph.AddVertexByLabel("C")
vD := gograph.AddVertexByLabel("D")
graph.AddEdge(vA, vB, gograph.WithEdgeWeight(4))
graph.AddEdge(vA, vD, gograph.WithEdgeWeight(3))
graph.AddEdge(vB, vC, gograph.WithEdgeWeight(3))
graph.AddEdge(vB, vD, gograph.WithEdgeWeight(1))
graph.AddEdge(vC, vD, gograph.WithEdgeWeight(2))
results inundefined: gograph.AddVertexByLabel`
How can I get this here working:
g = gograph.New[MyVertexStruct]() v := MyVertexStruct{} g.AddVertex(v)
Thanks a lot!
Hi @RobinHeitz,
The Graph interface has generic parameter type T constrained by the comparable constraint. https://github.com/hmdsefi/gograph/blob/ba840a185f3bcd39a14dccf64d36b65db663392a/base.go#L10
The comparable constraint means T must be a type that supports equality operations (== and !=). From the golang doc:
// comparable is an interface that is implemented by all comparable types // (booleans, numbers, strings, pointers, channels, arrays of comparable types, // structs whose fields are all comparable types). // The comparable interface may only be used as a type parameter constraint, // not as the type of a variable.
So, all your struct's fields must have comparable types(booleans, numbers, strings, pointers, channels, arrays of comparable types), and if you have non-comparable types in your struct like func type, or map you will get runtime error.
@hmdsefi thanks for your answer.
I did the mistake that I cannot directly add MyStruct to the graph but use NewVertex on it first.
I would like to see an example of a BFS. Therefore, MyStruct needs to implement the Iterator interface, right? Is there an example somewhere?
@RobinHeitz You don't need to implement iterator for your struct(MyStruct). You can use the BFS iterator from the traverse package. You can check the README here: https://github.com/hmdsefi/gograph/tree/master/traverse
In traverse package we have iterator for the BFS, DFS, Random Walk, and Closest First algorithms. For BFS you can create an iterator by using NewBreadthFirstIterator function and pass your graph and starting vertex to it. https://github.com/hmdsefi/gograph/blob/ba840a185f3bcd39a14dccf64d36b65db663392a/traverse/breadth_first_iterator.go#L19
Look at this example:
g := gograph.New[Person]()
john:= g.AddVertexByLabel(Person{ID:1, Name: "John"})
sara:= g.AddVertexByLabel(Person{ID:2, Name: "Sara"})
robin:=g.AddVertexByLabel(Person{ID:3, Name: "Robin"})
hamed:=g.AddVertexByLabel(Person{ID:4, Name: "Hamed"})
_, _ = g.AddEdge(john, robin)
_, _ = g.AddEdge(john, hamed)
_, _ = g.AddEdge(hamed, sara)
iter,err := NewBreadthFirstIterator(g, john.Label())
if err != nil {
return err
}
for iter.HasNext() {
fmt.Printf("Person: %#v \n",iter.Next().Label())
}
To find more examples, you can check the unit tests. Unit tests are a good reference to see how each feature works. BFS iterator unit tests: https://github.com/hmdsefi/gograph/blob/e3d55454621355b7a839b67fc979cc1f16ad2c35/traverse/breadth_first_iterator_test.go#L11
Thanks a lot for your help!