find k minimal weighted path (path can have different weight)
| 595 | |
| 596 | // find k minimal weighted path (path can have different weight) |
| 597 | static 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 |
no test coverage detected