graph icon indicating copy to clipboard operation
graph copied to clipboard

Not possible to do BFS on a graph of City

Open riberr opened this issue 1 year ago • 1 comments

Hi,

This example is not possible:

type City struct {
    Name string
}

cityHash := func(c City) string {
    return c.Name
}

g := graph.New(cityHash)

//...

f := func(c City) bool {
    return c.Name == "London"
}

graph.DFS(g, "Paris", f)

Warning: "Cannot use 'f' (type func(c City) bool) as the type func(string) bool"

Am I missing something? Because the docs says this should be possible.

riberr avatar Oct 22 '24 09:10 riberr

@riberr The DFS visit function takes K (the generic type for the hash, as opposed to the contained vertex type). So you would need to do the following to start at Paris and descend until London:

f := func(name string) bool {
    return name == "London"
}

graph.DFS(g, "Paris", f)

GeorgeMac avatar Nov 12 '24 12:11 GeorgeMac