Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

dijkstra

> 编程语言
Open source

Fast golang Dijkstra path finder

181 stars0 likes0 views
WebsiteGitHub

About

Fast golang Dijkstra path finder

dijkstra

Fast golang Dijkstra's shortest (and longest) path finder

Documentation

godoc

How to

Install

bash
go get github.com/RyanCarrier/dijkstra/v2

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;

go
data, _ := os.ReadFile("path/to/file")
graph, err := dijkstra.Import(string(data))

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 (using ImportStringMapped instead.

Creating a graph

go
package main

import "github.com/RyanCarrier/dijkstra/v2"

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

Finding paths

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

go

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

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

Finding multiple paths

go
graph := dijkstra.NewGraph()
//Add the 3 verticies
graph.AddVertexAndArcs(0, map[int]uint64{1: 1, 2: 1})
graph.AddVertexAndArcs(1, map[int]uint64{3: 1})
graph.AddVertexAndArcs(2, map[int]uint64{3: 1})
graph.AddVertexAndArcs(3, map[int]uint64{4: 1})

best, err := graph.ShortestAll(0, 4)
if err != nil {
  fmt.Println(graph)
  log.Fatal(err)
}
fmt.Println("Shortest distances are", best.Distance, "with paths; ", best.Paths)

best, err = graph.LongestAll(0, 4)
if err != nil {
  log.Fatal(err)
}
fmt.Println("Longest distances are", best.Distance, "following path ", best.Paths)

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Godijkstradijkstra-algorithmgraphpathfinding

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言