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 GraphAdjacencyMatrixTestGraphGenerator.__generate_graphs when the inclusive integer range [min_val, max_val] is smaller than vertex_count. Vertices are sampled uniquely via random.sample, so the request is infeasible and the generator raises before producing a graph. Note this message variant lacks the trailing period of the adjacency-list twin (line 255 version).

Source

Thrown at graphs/graph_adjacency_matrix.py:266

        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[GraphAdjacencyMatrix, GraphAdjacencyMatrix, 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 = GraphAdjacencyMatrix(
            vertices=random_vertices, edges=random_edges, directed=False
        )
        directed_graph = GraphAdjacencyMatrix(
            vertices=random_vertices, edges=random_edges, directed=True

View on GitHub (pinned to f5988cc097)

Solutions

  1. Widen the range so max_val - min_val + 1 >= vertex_count.
  2. Lower vertex_count to fit the range.
  3. Compute max_val from vertex_count at the call site instead of hard-coding both.

Example fix

# before
graphs = generator(vertex_count=100, min_val=0, max_val=50)

# after
vertex_count = 100
graphs = generator(
    vertex_count=vertex_count, min_val=0, max_val=10 * vertex_count
)
Defensive patterns

Strategy: validation

Validate before calling

if max_val - min_val + 1 < vertex_count:
    raise ValueError(f"need range >= {vertex_count}, got {max_val - min_val + 1}")

Prevention

When it happens

Trigger: Calling the matrix generator with max_val - min_val + 1 < vertex_count, e.g. vertex_count=100 with min_val=0, max_val=50.

Common situations: Scaling tests up without widening label ranges; parameter sweeps that vary vertex_count independently of min_val/max_val; defaults copied from smaller test fixtures.

Related errors


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