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

Unify vertex heaps #32

Merged
merged 2 commits into from
Sep 19, 2022
Merged
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
72 changes: 34 additions & 38 deletions bidirectional_ch_one_to_n.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,19 @@ func (graph *Graph) ShortestPathOneToMany(source int64, targets []int64) ([]floa
queryDist[source] = 0
revQueryDist[target] = 0

forwQ := &forwardHeap{}
backwQ := &backwardHeap{}
forwQ := &vertexDistHeap{}
backwQ := &vertexDistHeap{}

heap.Init(forwQ)
heap.Init(backwQ)

heapSource := &bidirectionalVertex{
id: source,
queryDist: 0,
revQueryDistance: Infinity,
heapSource := &vertexDist{
id: source,
dist: 0,
}
heapTarget := &bidirectionalVertex{
id: target,
queryDist: Infinity,
revQueryDistance: 0,
heapTarget := &vertexDist{
id: target,
dist: 0,
}

heap.Push(forwQ, heapSource)
Expand All @@ -98,39 +96,39 @@ func (graph *Graph) shortestPathOneToManyCore(
prevReverse map[int64]int64,
queryDist, revQueryDist []float64,
forwProcessed, revProcessed []int64,
forwQ *forwardHeap,
backwQ *backwardHeap,
forwQ *vertexDistHeap,
backwQ *vertexDistHeap,
) (float64, []int64) {
estimate := Infinity

var middleID int64

for forwQ.Len() != 0 || backwQ.Len() != 0 {
if forwQ.Len() != 0 {
vertex1 := heap.Pop(forwQ).(*bidirectionalVertex)
if vertex1.queryDist <= estimate {
vertex1 := heap.Pop(forwQ).(*vertexDist)
if vertex1.dist <= estimate {
forwProcessed[vertex1.id] = nextQueue
graph.relaxEdgesBiForwardOneToMany(vertex1, forwQ, prev, queryDist, nextQueue, forwProcessed)
}
if revProcessed[vertex1.id] == nextQueue {
if vertex1.queryDist+revQueryDist[vertex1.id] < estimate {
if vertex1.dist+revQueryDist[vertex1.id] < estimate {
middleID = vertex1.id
estimate = vertex1.queryDist + revQueryDist[vertex1.id]
estimate = vertex1.dist + revQueryDist[vertex1.id]
}
}
}

if backwQ.Len() != 0 {
vertex2 := heap.Pop(backwQ).(*bidirectionalVertex)
if vertex2.revQueryDistance <= estimate {
vertex2 := heap.Pop(backwQ).(*vertexDist)
if vertex2.dist <= estimate {
revProcessed[vertex2.id] = nextQueue
graph.relaxEdgesBiBackwardOneToMany(vertex2, backwQ, prevReverse, revQueryDist, nextQueue, revProcessed)
}

if forwProcessed[vertex2.id] == nextQueue {
if vertex2.revQueryDistance+queryDist[vertex2.id] < estimate {
if vertex2.dist+queryDist[vertex2.id] < estimate {
middleID = vertex2.id
estimate = vertex2.revQueryDistance + queryDist[vertex2.id]
estimate = vertex2.dist + queryDist[vertex2.id]
}
}
}
Expand Down Expand Up @@ -181,8 +179,8 @@ func (graph *Graph) ShortestPathOneToManyWithAlternatives(sourceAlternatives []V
targetAlternativesInternal = append(targetAlternativesInternal, targetAlternativeInternal)
}

forwQ := &forwardHeap{}
backwQ := &backwardHeap{}
forwQ := &vertexDistHeap{}
backwQ := &vertexDistHeap{}

heap.Init(forwQ)
heap.Init(backwQ)
Expand All @@ -191,21 +189,19 @@ func (graph *Graph) ShortestPathOneToManyWithAlternatives(sourceAlternatives []V
forwProcessed[sourceAlternative.vertexNum] = nextQueue
queryDist[sourceAlternative.vertexNum] = sourceAlternative.additionalDistance

heapSource := &bidirectionalVertex{
id: sourceAlternative.vertexNum,
queryDist: sourceAlternative.additionalDistance,
revQueryDistance: Infinity,
heapSource := &vertexDist{
id: sourceAlternative.vertexNum,
dist: sourceAlternative.additionalDistance,
}
heap.Push(forwQ, heapSource)
}
for _, targetAlternative := range targetAlternativesInternal {
revProcessed[targetAlternative.vertexNum] = nextQueue
revQueryDist[targetAlternative.vertexNum] = targetAlternative.additionalDistance

heapTarget := &bidirectionalVertex{
id: targetAlternative.vertexNum,
queryDist: Infinity,
revQueryDistance: targetAlternative.additionalDistance,
heapTarget := &vertexDist{
id: targetAlternative.vertexNum,
dist: targetAlternative.additionalDistance,
}
heap.Push(backwQ, heapTarget)
}
Expand All @@ -219,7 +215,7 @@ func (graph *Graph) ShortestPathOneToManyWithAlternatives(sourceAlternatives []V
return estimateAll, pathAll
}

func (graph *Graph) relaxEdgesBiForwardOneToMany(vertex *bidirectionalVertex, forwQ *forwardHeap, prev map[int64]int64, queryDist []float64, cid int64, forwProcessed []int64) {
func (graph *Graph) relaxEdgesBiForwardOneToMany(vertex *vertexDist, forwQ *vertexDistHeap, prev map[int64]int64, queryDist []float64, cid int64, forwProcessed []int64) {
vertexList := graph.Vertices[vertex.id].outIncidentEdges
for i := range vertexList {
temp := vertexList[i].vertexID
Expand All @@ -230,17 +226,17 @@ func (graph *Graph) relaxEdgesBiForwardOneToMany(vertex *bidirectionalVertex, fo
queryDist[temp] = alt
prev[temp] = vertex.id
forwProcessed[temp] = cid
node := &bidirectionalVertex{
id: temp,
queryDist: alt,
node := &vertexDist{
id: temp,
dist: alt,
}
heap.Push(forwQ, node)
}
}
}
}

func (graph *Graph) relaxEdgesBiBackwardOneToMany(vertex *bidirectionalVertex, backwQ *backwardHeap, prev map[int64]int64, revQueryDist []float64, cid int64, revProcessed []int64) {
func (graph *Graph) relaxEdgesBiBackwardOneToMany(vertex *vertexDist, backwQ *vertexDistHeap, prev map[int64]int64, revQueryDist []float64, cid int64, revProcessed []int64) {
vertexList := graph.Vertices[vertex.id].inIncidentEdges
for i := range vertexList {
temp := vertexList[i].vertexID
Expand All @@ -251,9 +247,9 @@ func (graph *Graph) relaxEdgesBiBackwardOneToMany(vertex *bidirectionalVertex, b
revQueryDist[temp] = alt
prev[temp] = vertex.id
revProcessed[temp] = cid
node := &bidirectionalVertex{
id: temp,
revQueryDistance: alt,
node := &vertexDist{
id: temp,
dist: alt,
}
heap.Push(backwQ, node)
}
Expand Down
68 changes: 32 additions & 36 deletions dijkstra_bidirectional.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (graph *Graph) ShortestPath(source, target int64) (float64, []int64) {
func (graph *Graph) initShortestPath() (
queryDist, revQueryDist []float64,
forwProcessed, revProcessed []bool,
forwQ *forwardHeap,
backwQ *backwardHeap,
forwQ *vertexDistHeap,
backwQ *vertexDistHeap,
) {
queryDist = make([]float64, len(graph.Vertices))
revQueryDist = make([]float64, len(graph.Vertices))
Expand All @@ -42,8 +42,8 @@ func (graph *Graph) initShortestPath() (
forwProcessed = make([]bool, len(graph.Vertices))
revProcessed = make([]bool, len(graph.Vertices))

forwQ = &forwardHeap{}
backwQ = &backwardHeap{}
forwQ = &vertexDistHeap{}
backwQ = &vertexDistHeap{}

heap.Init(forwQ)
heap.Init(backwQ)
Expand All @@ -60,15 +60,13 @@ func (graph *Graph) shortestPath(source, target int64) (float64, []int64) {
queryDist[source] = 0
revQueryDist[target] = 0

heapSource := &bidirectionalVertex{
id: source,
queryDist: 0,
revQueryDistance: Infinity,
heapSource := &vertexDist{
id: source,
dist: 0,
}
heapTarget := &bidirectionalVertex{
id: target,
queryDist: Infinity,
revQueryDistance: 0,
heapTarget := &vertexDist{
id: target,
dist: 0,
}

heap.Push(forwQ, heapSource)
Expand All @@ -80,8 +78,8 @@ func (graph *Graph) shortestPath(source, target int64) (float64, []int64) {
func (graph *Graph) shortestPathCore(
queryDist, revQueryDist []float64,
forwProcessed, revProcessed []bool,
forwQ *forwardHeap,
backwQ *backwardHeap,
forwQ *vertexDistHeap,
backwQ *vertexDistHeap,
) (float64, []int64) {
forwardPrev := make(map[int64]int64)
backwardPrev := make(map[int64]int64)
Expand All @@ -93,8 +91,8 @@ func (graph *Graph) shortestPathCore(
for forwQ.Len() != 0 || backwQ.Len() != 0 {
// Upward search
if forwQ.Len() != 0 {
forwardVertex := heap.Pop(forwQ).(*bidirectionalVertex)
if forwardVertex.queryDist <= estimate {
forwardVertex := heap.Pop(forwQ).(*vertexDist)
if forwardVertex.dist <= estimate {
forwProcessed[forwardVertex.id] = true
// Edge relaxation in a forward propagation
neighborsUpward := graph.Vertices[forwardVertex.id].outIncidentEdges
Expand All @@ -106,26 +104,26 @@ func (graph *Graph) shortestPathCore(
if queryDist[temp] > alt {
queryDist[temp] = alt
forwardPrev[temp] = forwardVertex.id
node := &bidirectionalVertex{
id: temp,
queryDist: alt,
node := &vertexDist{
id: temp,
dist: alt,
}
heap.Push(forwQ, node)
}
}
}
}
if revProcessed[forwardVertex.id] {
if forwardVertex.queryDist+revQueryDist[forwardVertex.id] < estimate {
if forwardVertex.dist+revQueryDist[forwardVertex.id] < estimate {
middleID = forwardVertex.id
estimate = forwardVertex.queryDist + revQueryDist[forwardVertex.id]
estimate = forwardVertex.dist + revQueryDist[forwardVertex.id]
}
}
}
// Backward search
if backwQ.Len() != 0 {
backwardVertex := heap.Pop(backwQ).(*bidirectionalVertex)
if backwardVertex.revQueryDistance <= estimate {
backwardVertex := heap.Pop(backwQ).(*vertexDist)
if backwardVertex.dist <= estimate {
revProcessed[backwardVertex.id] = true
// Edge relaxation in a backward propagation
vertexList := graph.Vertices[backwardVertex.id].inIncidentEdges
Expand All @@ -137,9 +135,9 @@ func (graph *Graph) shortestPathCore(
if revQueryDist[temp] > alt {
revQueryDist[temp] = alt
backwardPrev[temp] = backwardVertex.id
node := &bidirectionalVertex{
id: temp,
revQueryDistance: alt,
node := &vertexDist{
id: temp,
dist: alt,
}
heap.Push(backwQ, node)
}
Expand All @@ -148,9 +146,9 @@ func (graph *Graph) shortestPathCore(

}
if forwProcessed[backwardVertex.id] {
if backwardVertex.revQueryDistance+queryDist[backwardVertex.id] < estimate {
if backwardVertex.dist+queryDist[backwardVertex.id] < estimate {
middleID = backwardVertex.id
estimate = backwardVertex.revQueryDistance + queryDist[backwardVertex.id]
estimate = backwardVertex.dist + queryDist[backwardVertex.id]
}
}
}
Expand Down Expand Up @@ -208,20 +206,18 @@ func (graph *Graph) shortestPathWithAlternatives(sources, targets []vertexAltern
for _, source := range sources {
forwProcessed[source.vertexNum] = true
queryDist[source.vertexNum] = source.additionalDistance
heapSource := &bidirectionalVertex{
id: source.vertexNum,
queryDist: source.additionalDistance,
revQueryDistance: Infinity,
heapSource := &vertexDist{
id: source.vertexNum,
dist: source.additionalDistance,
}
heap.Push(forwQ, heapSource)
}
for _, target := range targets {
revProcessed[target.vertexNum] = true
revQueryDist[target.vertexNum] = target.additionalDistance
heapTarget := &bidirectionalVertex{
id: target.vertexNum,
queryDist: Infinity,
revQueryDistance: target.additionalDistance,
heapTarget := &vertexDist{
id: target.vertexNum,
dist: target.additionalDistance,
}
heap.Push(backwQ, heapTarget)
}
Expand Down
24 changes: 0 additions & 24 deletions heap_backward.go

This file was deleted.

19 changes: 19 additions & 0 deletions heap_dist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ch

type vertexDist struct {
id int64
dist float64
}

type vertexDistHeap []*vertexDist

func (h vertexDistHeap) Len() int { return len(h) }
func (h vertexDistHeap) Less(i, j int) bool { return h[i].dist < h[j].dist }
func (h vertexDistHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *vertexDistHeap) Push(x interface{}) { *h = append(*h, x.(*vertexDist)) }
func (h *vertexDistHeap) Pop() interface{} {
heapSize := len(*h)
lastNode := (*h)[heapSize-1]
*h = (*h)[0 : heapSize-1]
return lastNode
}
20 changes: 0 additions & 20 deletions heap_forward.go

This file was deleted.