dominikbraun / graph

A library for creating generic graph data structures and modifying, analyzing, and visualizing them.
https://graph.dominikbraun.io
Apache License 2.0
1.77k stars 95 forks source link

Return a slice of all vertices in the graph. #130

Open filiperecharte opened 1 year ago

filiperecharte commented 1 year ago

I want to iterate through the vertices of a graph, but the graph type does not have that method, and visiting each one using DFS does not work since, during runtime, I have vertices with no edges connected. Even if I had, I could only iterate through edges and then access Vertices, using the method Edges() of the graph, but that will not make me go through a vertice only once. Is there any way I can get a slice of them like the method that exists in type Store ListVertices()?

dominikbraun commented 1 year ago

Hi! You can iterate over all vertices like so:

adjacencyMap, _ := g.AdjacencyMap()

for vertex := range adjacencyMap {
    // Do something with vertex.
}

The adjacency map is guaranteed to contain all vertices in the graph. If you want them as a slice, you can build it yourself – but it is likely that there will be a Vertices method in the future.