百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

dijkstra

> 编程语言
开源

快速的 Golang Dijkstra 路径查找器

181 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

快速的 Golang Dijkstra 路径查找器

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Godijkstradijkstra-algorithmgraphpathfinding

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

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