• Stars
    star
    170
  • Rank 223,357 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fastest golang Dijkstra path finder

dijkstra

Golangs fastest Dijkstra's shortest (and longest) path calculator

Documentation

godoc

How to

Generate a graph

Importing from file

The package can import dijkstra files in the format:

0 1,1 2,1
1 0,1 2,2
2

using;

graph, err := dijkstra.Import("path/to/file")

i.e. node then each arc and it's weight. The default is to use nodes with numbers starting from 0, but the package will map string appropriately.

Creating a graph

package main

func main(){
  graph:=dijkstra.NewGraph()
  //Add the 3 verticies
  graph.AddVertex(0)
  graph.AddVertex(1)
  graph.AddVertex(2)
  //Add the arcs
  graph.AddArc(0,1,1)
  graph.AddArc(0,2,1)
  graph.AddArc(1,0,1)
  graph.AddArc(1,2,2)
}

Finding paths

Once the graph is created, shortest or longest paths between two points can be generated.

best, err := graph.Shortest(0, 2)
if err!=nil{
  log.Fatal(err)
}
fmt.Println("Shortest distance ", best.Distance, " following path ", best.Path)

best, err := graph.Longest(0, 2)
if err!=nil{
  log.Fatal(err)
}
fmt.Println("Longest distance ", best.Distance, " following path ", best.Path)

best, err := graph.ShortestSafe(0, 2)
if err!=nil{
  log.Fatal(err)
}
fmt.Println("Shortest distance with thread safety", best.Distance, " following path ", best.Path)