Skip to content

edmonds_karp keeps only the last capacity when a pair of vertices is listed twice #847

Open

Description

@tachsin

When the same pair of vertices is listed more than once in caps, edmonds_karp keeps only the last capacity rather than adding them up.

use pathfinding::directed::edmonds_karp::edmonds_karp_dense;

let vertices = [0, 1];
let caps = vec![((0, 1), 3), ((0, 1), 4)];
let (_, total, _) = edmonds_karp_dense(&vertices, &0, &1, caps);
assert_eq!(total, 4); // two parallel edges of 3 and 4

edmonds_karp_sparse gives the same result. Each entry goes through set_capacity, which sets the capacity of the pair, so a later entry replaces an earlier one.

I am not sure whether this is intended, so I would rather ask than assume. There are two readings of the documentation, which says caps describes "the positive capacities between the nodes":

  1. One capacity per pair. Listing a pair twice is then a caller error, and it would help to say so, since at the moment the result silently depends on which entry comes last.
  2. Parallel edges. In max-flow terms two edges between the same vertices usually carry the sum of their capacities, like two pipes side by side, so the result above would be 7.

I know that for yen (#516) documenting the behaviour was the right answer, but that was because its result is a sequence of nodes, which cannot say which of two parallel edges a path uses. Here the total flow can represent them without any change to the output types. On the other hand, adding them up would change the result for any caller who currently relies on the last entry winning, unlikely as that seems.

Which would you prefer? I have both ready: a documentation change for the first, or for the second a fix in the edmonds_karp helper, which adds each capacity to the pair's current one instead of replacing it, with tests comparing dense and sparse against an independent max-flow on random multigraphs. The EdmondsKarp trait and set_capacity itself would stay as they are, since setting a pair's capacity is what that method is for.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions