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

Function SSpaths_k_minimal

src/procedures/proc_ss_paths.c:597–653  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 1

Proc_SSpathsInvokeFunction · 0.85

Calls 10

Heap_newFunction · 0.85
SSpaths_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