Skip to content

Commit 9d0d61c

Browse files
committed
query: fix MaxFrontierSize trimming in shortest path
1 parent 974923a commit 9d0d61c

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

query/shortest.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ func (h *priorityQueue) Pop() interface{} {
8888
return val
8989
}
9090

91+
func (pq *priorityQueue) TrimToMax(max int64) {
92+
if max <= 0 || int64(pq.Len()) <= max {
93+
return
94+
}
95+
imax := 0
96+
maxCost := (*pq)[0].cost
97+
for i := 1; i < pq.Len(); i++ {
98+
if (*pq)[i].cost > maxCost {
99+
imax = i
100+
maxCost = (*pq)[i].cost
101+
}
102+
}
103+
heap.Remove(pq, imax)
104+
}
105+
91106
type mapItem struct {
92107
attr string
93108
cost float64
@@ -405,10 +420,8 @@ func runKShortestPaths(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
405420
hop: item.hop + 1,
406421
path: route{route: curPath},
407422
}
408-
if int64(pq.Len()) > sg.Params.MaxFrontierSize {
409-
pq.Pop()
410-
}
411423
heap.Push(&pq, node)
424+
pq.TrimToMax(sg.Params.MaxFrontierSize)
412425
}
413426
// Return the popped nodes path to pool.
414427
pathPool.Put(item.path.route)
@@ -561,10 +574,8 @@ func shortestPath(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
561574
cost: nodeCost,
562575
hop: item.hop + 1,
563576
}
564-
if int64(pq.Len()) > sg.Params.MaxFrontierSize {
565-
pq.Pop()
566-
}
567577
heap.Push(&pq, node)
578+
pq.TrimToMax(sg.Params.MaxFrontierSize)
568579
} else {
569580
// We've already seen this node. So, just update the cost
570581
// and fix the priority in the heap and map.

0 commit comments

Comments
 (0)