TheAlgorithms/Python · error · ValueError

Will result in duplicate vertices. Either increase range bet

Error message

Will result in duplicate vertices. Either increase range between min_val and max_val or decrease vertex count.

What it means

Raised by GraphAdjacencyListTestGraphGenerator.__generate_graphs (the private test-graph builder) when the inclusive value range [min_val, max_val] has fewer distinct integers than the requested vertex_count. Since vertices are drawn with random.sample (unique values), the request is impossible and the generator aborts before sampling.

Source

Thrown at graphs/graph_adjacency_list.py:255

        random_source_vertices: list[int] = random.sample(
            vertices[0 : int(len(vertices) / 2)], edge_pick_count
        )
        random_destination_vertices: list[int] = random.sample(
            vertices[int(len(vertices) / 2) :], edge_pick_count
        )
        random_edges: list[list[int]] = []

        for source in random_source_vertices:
            for dest in random_destination_vertices:
                random_edges.append([source, dest])

        return random_edges

    def __generate_graphs(
        self, vertex_count: int, min_val: int, max_val: int, edge_pick_count: int
    ) -> tuple[GraphAdjacencyList, GraphAdjacencyList, list[int], list[list[int]]]:
        if max_val - min_val + 1 < vertex_count:
            raise ValueError(
                "Will result in duplicate vertices. Either increase range "
                "between min_val and max_val or decrease vertex count."
            )

        # generate graph input
        random_vertices: list[int] = random.sample(
            range(min_val, max_val + 1), vertex_count
        )
        random_edges: list[list[int]] = self.__generate_random_edges(
            random_vertices, edge_pick_count
        )

        # build graphs
        undirected_graph = GraphAdjacencyList(
            vertices=random_vertices, edges=random_edges, directed=False
        )
        directed_graph = GraphAdjacencyList(
            vertices=random_vertices, edges=random_edges, directed=True

View on GitHub (pinned to f5988cc097)

Solutions

  1. Widen the range: ensure max_val - min_val + 1 >= vertex_count (e.g. set max_val = min_val + vertex_count - 1 or larger).
  2. Reduce vertex_count to fit within the available range.
  3. Derive the range from the count programmatically instead of hard-coding both.

Example fix

# before
vertices, edges = generator(vertex_count=50, min_val=0, max_val=20)

# after
vertex_count = 50
vertices, edges = generator(
    vertex_count=vertex_count, min_val=0, max_val=vertex_count - 1
)
Defensive patterns

Strategy: validation

Validate before calling

if max_val - min_val + 1 < vertex_count:
    max_val = min_val + vertex_count - 1  # widen range to minimum feasible

Try / catch

try:
    graphs = generator(vertex_count, min_val, max_val)
except ValueError:
    graphs = generator(vertex_count, min_val, min_val + vertex_count - 1)

Prevention

When it happens

Trigger: Calling the generator with max_val - min_val + 1 < vertex_count, e.g. vertex_count=10 with min_val=0, max_val=5; or large vertex_count with a narrow range.

Common situations: Parameterized/scaled test runs that grow vertex_count but forget to widen the value range; copy-pasted generator calls with defaults that no longer fit the new size; requesting more unique vertices than the label space allows.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/5d3f00460ed22ea0. Report an issue: GitHub.