MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / SPpaths_k_minimal

Function SPpaths_k_minimal

src/procedures/proc_sp_paths.c:606–662  ·  view source on GitHub ↗

find k minimal weighted path (path can have different weight)

Source from the content-addressed store, hash-verified

604
605// find k minimal weighted path (path can have different weight)
606static void SPpaths_k_minimal
607(
608 SinglePairCtx *ctx
609) {
610 // initialize heap that contains the result where top path is the highest weight
611 ctx->heap = Heap_new(path_cmp, NULL);
612
613 // get first path
614 WeightedPath p = {0};
615 double max_weight = DBL_MAX;
616 SPpaths_next(ctx, &p, max_weight);
617
618 // iterate over all paths
619 while (p.path != NULL && Heap_count(ctx->heap) < ctx->path_count - 1) {
620 // fill the heap
621 _add_path(&ctx->heap, &p);
622
623 // get next path where path weight is <= max_weight
624 SPpaths_next(ctx, &p, max_weight);
625 }
626
627 if(p.path == NULL) return;
628
629 // fill the heap
630 _add_path(&ctx->heap, &p);
631
632 // update the max weight so we will get better paths
633 WeightedPath *pp = Heap_peek(ctx->heap);
634 max_weight = pp->weight;
635
636 // get next path where path weight is <= max_weight
637 SPpaths_next(ctx, &p, max_weight);
638
639 while (p.path != NULL) {
640 // if the heap is full check if the current path is better
641 // than the worst path if yes replace it
642 pp = Heap_peek(ctx->heap);
643 if(p.weight < pp->weight ||
644 p.cost < pp->cost ||
645 (p.cost == pp->cost &&
646 Path_Len(p.path) < Path_Len(pp->path))) {
647 Heap_poll(ctx->heap);
648 Path_Free(pp->path);
649 pp->path = Path_Clone(p.path);
650 pp->weight = p.weight;
651 pp->cost = p.cost;
652 Heap_offer(&ctx->heap, pp);
653
654 // update the max weight so we will get better paths
655 pp = Heap_peek(ctx->heap);
656 max_weight = pp->weight;
657 }
658
659 // get next path where path weight is <= max_weight
660 SPpaths_next(ctx, &p, max_weight);
661 }
662}
663

Callers 1

Proc_SPpathsInvokeFunction · 0.85

Calls 10

Heap_newFunction · 0.85
SPpaths_nextFunction · 0.85
Heap_countFunction · 0.85
Heap_peekFunction · 0.85
Path_LenFunction · 0.85
Heap_pollFunction · 0.85
Path_FreeFunction · 0.85
Path_CloneFunction · 0.85
Heap_offerFunction · 0.85
_add_pathFunction · 0.70

Tested by

no test coverage detected