1

I'm currently trying to implement a path finding algorithm in Go. I'm currently committed to get Dijkstra working since that's supposed to be one of the simpler algorithms regarding path findinig. However I have a hard time getting it right. Most of my tests results in infinite loops.

For my tests I'm currently using a ESRI Shapefile as a source and build a graph with it that consists of Vertexes and Edge's (or Node's and Arcs, whatever suits you). This seems to work great, but it's a little memory hungry at the moment.

I started building on this question which seems to work according to the example. But once I adjusted it for my needs, which are real-world coordinates and no id's in the vertex, something seems wrong...

Relevant code that I'm suffering with (part of a gist):

const MAX_LENGTH = 1000000
type MinDistanceFromSource map[*Vertex]float64

func (G *Graph) Dijkstra(source, destination *Vertex) MinDistanceFromSource {
    D := make(MinDistanceFromSource)
    for _, vertex := range G.Vertexes {
        D[vertex] = MAX_LENGTH
    }

    D[source] = 0

    for edge := range source.GetAdjacentEdges() {
        D[edge.Destination] = edge.Length
    }

    // calculateDistance(source, destination, D) // Fix me...?

    for _, vertex := range G.Vertexes {
        vertex.visited = false // fixme
    }

    return D
}

The main thing I'm asking myself is if I'm doing this right or am I just off by miles? And how can I make this work? Or am I better of trying to implement something like A*?

Also; the relevant Shapefile that I'm trying this with: NWB_01-01-2015 (100MB zip)

Community
  • 1
  • 1
Alex van den Hoogen
  • 744
  • 1
  • 5
  • 22
  • Dijkstra's (well, UCS if you're hair splitting) is literally just A* with the heuristic always set to the Null Heuristic, so no, you won't have any better luck there. – Linear Feb 17 '15 at 21:03
  • Is there a reason you can't map coordinates->ID so you can use the other one? That's what most graph packages will have you do (for instance, github.com/gonum/graph does it too) – Linear Feb 17 '15 at 21:04
  • @Jsor, thanks for the info regarding Dijkstra / A*. I currently use the coordinates as ID, as you can see here: https://gist.github.com/alex3305/a76daf1c74f25d3a0a12#file-models-go-L15 . The package you refer to seems quite decent, but I don't see any documentation or examples which is really a problem for me since I'm quite new to golang. – Alex van den Hoogen Feb 17 '15 at 21:19
  • 1
    @AlexvandenHoogen [documentation](http://godoc.org/github.com/gonum/graph), also see [this](http://godoc.org/?q=dijkstra) – thwd Feb 17 '15 at 21:56
  • @thwd Sure I saw all of those, but couldn't really find a good implementation that suits my needs. Or at least I don't see one. The only that I can tryout probably is http://godoc.org/github.com/soniakeys/graph2/search . – Alex van den Hoogen Feb 17 '15 at 22:16
  • You ought to include your code for `calculateDistance`, since that's where most of the magic is. If `calculateDistance` is fixed, there's probably no reason to be setting visited on all the vertices. – MattyB Mar 12 '15 at 20:07

0 Answers0