Skip to content
Open
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
20 changes: 18 additions & 2 deletions query/shortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ func (h *priorityQueue) Pop() interface{} {
return val
}

// removeMax removes and returns the highest cost item from the priority queue.
// This is used to evict the least promising node when the frontier exceeds its
// size limit, preserving the lowest cost nodes needed for shortest paths.
func (h *priorityQueue) removeMax() {
if len(*h) == 0 {
return
}
maxIdx := 0
for i := 1; i < len(*h); i++ {
if (*h)[i].cost > (*h)[maxIdx].cost {
maxIdx = i
}
}
heap.Remove(h, maxIdx)
}

type mapItem struct {
attr string
cost float64
Expand Down Expand Up @@ -406,7 +422,7 @@ func runKShortestPaths(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
path: route{route: curPath},
}
if int64(pq.Len()) > sg.Params.MaxFrontierSize {
pq.Pop()
pq.removeMax()
}
heap.Push(&pq, node)
}
Expand Down Expand Up @@ -562,7 +578,7 @@ func shortestPath(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
hop: item.hop + 1,
}
if int64(pq.Len()) > sg.Params.MaxFrontierSize {
pq.Pop()
pq.removeMax()
}
heap.Push(&pq, node)
} else {
Expand Down