Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ShortestPath faster #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,45 @@ package graph
// The time complexity is O((|E| + |V|)⋅log|V|), where |E| is the number of edges
// and |V| the number of vertices in the graph.
func ShortestPath(g Iterator, v, w int) (path []int, dist int64) {
parent, distances := ShortestPaths(g, v)
path, dist = []int{}, distances[w]
if dist == -1 {
return
}
for v := w; v != -1; v = parent[v] {
path = append(path, v)
n := g.Order()
dists := make([]int64, n)
parents := make([]int, n)
for i := range dists {
dists[i], parents[i] = -1, -1
}
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
path[i], path[j] = path[j], path[i]
dists[v] = 0

// Dijkstra's algorithm
q := emptyPrioQueue(dists)
q.Push(v)
for q.Len() > 0 {
u := q.Pop()
if u == w {
for x := w; x != -1; x = parents[x] {
path = append(path, x)
}
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
path[i], path[j] = path[j], path[i]
}
return path, dists[w]
}
g.Visit(u, func(x int, d int64) (skip bool) {
if d < 0 {
return
}
alt := dists[u] + d
switch {
case dists[x] == -1:
dists[x], parents[x] = alt, u
q.Push(x)
case alt < dists[x]:
dists[x], parents[x] = alt, u
q.Fix(x)
}
return
})
}
return
return []int{}, -1
}

// ShortestPaths computes the shortest paths from v to all other vertices.
Expand Down
66 changes: 63 additions & 3 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,76 @@ func TestShortestPath(t *testing.T) {
}
}

func randomGraph(n int) (*Mutable, int) {
g := New(n)
h := n / 2
var t int
for i := 0; i < n; i++ {
g.Add(0, rand.Intn(n))
if i == h {
t = rand.Intn(n)
g.Add(rand.Intn(n), t)
} else {
g.Add(rand.Intn(n), rand.Intn(n))
}
}
return g, t
}

// Store benchmark results as global variables to prevent unwanted optimizations.
var path []int
var dist int64

func BenchmarkShortestPath250(b *testing.B) {
g, t := randomGraph(250)
b.ResetTimer()
var p []int
var d int64
for i := 0; i < b.N; i++ {
p, d = ShortestPath(g, 0, t)
}
path, dist = p, d
}

func BenchmarkShortestPath500(b *testing.B) {
g, t := randomGraph(500)
b.ResetTimer()
var p []int
var d int64
for i := 0; i < b.N; i++ {
p, d = ShortestPath(g, 0, t)
}
path, dist = p, d
}

func BenchmarkShortestPath1000(b *testing.B) {
g, t := randomGraph(1000)
b.ResetTimer()
var p []int
var d int64
for i := 0; i < b.N; i++ {
p, d = ShortestPath(g, 0, t)
}
path, dist = p, d
}

var (
parent []int
distances []int64
)

func BenchmarkShortestPaths(b *testing.B) {
n := 1000
b.StopTimer()
g := New(n)
for i := 0; i < n; i++ {
g.Add(0, rand.Intn(n))
g.Add(rand.Intn(n), rand.Intn(n))
}
b.StartTimer()
b.ResetTimer()
var p []int
var d []int64
for i := 0; i < b.N; i++ {
_, _ = ShortestPaths(g, 0)
p, d = ShortestPaths(g, 0)
}
parent, distances = p, d
}