{"record":{"id":"8ac93e1484bac978","repo":"TheAlgorithms/Python","slug":"will-result-in-duplicate-vertices-either-increase-8ac93e","errorCode":null,"errorMessage":"Will result in duplicate vertices. Either increase range between min_val and max_val or decrease vertex count","messagePattern":"Will result in duplicate vertices\\. Either increase range between min_val and max_val or decrease vertex count","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_matrix.py","lineNumber":266,"sourceCode":"        random_source_vertices: list[int] = random.sample(\n            vertices[0 : int(len(vertices) / 2)], edge_pick_count\n        )\n        random_destination_vertices: list[int] = random.sample(\n            vertices[int(len(vertices) / 2) :], edge_pick_count\n        )\n        random_edges: list[list[int]] = []\n\n        for source in random_source_vertices:\n            for dest in random_destination_vertices:\n                random_edges.append([source, dest])\n\n        return random_edges\n\n    def __generate_graphs(\n        self, vertex_count: int, min_val: int, max_val: int, edge_pick_count: int\n    ) -> tuple[GraphAdjacencyMatrix, GraphAdjacencyMatrix, list[int], list[list[int]]]:\n        if max_val - min_val + 1 < vertex_count:\n            raise ValueError(\n                \"Will result in duplicate vertices. Either increase \"\n                \"range between min_val and max_val or decrease vertex count\"\n            )\n\n        # generate graph input\n        random_vertices: list[int] = random.sample(\n            range(min_val, max_val + 1), vertex_count\n        )\n        random_edges: list[list[int]] = self.__generate_random_edges(\n            random_vertices, edge_pick_count\n        )\n\n        # build graphs\n        undirected_graph = GraphAdjacencyMatrix(\n            vertices=random_vertices, edges=random_edges, directed=False\n        )\n        directed_graph = GraphAdjacencyMatrix(\n            vertices=random_vertices, edges=random_edges, directed=True","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_matrix.py#L248-L284","documentation":"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).","triggerScenarios":"Calling the matrix generator with max_val - min_val + 1 < vertex_count, e.g. vertex_count=100 with min_val=0, max_val=50.","commonSituations":"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.","solutions":["Widen the range so max_val - min_val + 1 >= vertex_count.","Lower vertex_count to fit the range.","Compute max_val from vertex_count at the call site instead of hard-coding both."],"exampleFix":"# before\ngraphs = generator(vertex_count=100, min_val=0, max_val=50)\n\n# after\nvertex_count = 100\ngraphs = generator(\n    vertex_count=vertex_count, min_val=0, max_val=10 * vertex_count\n)","handlingStrategy":"validation","validationCode":"if max_val - min_val + 1 < vertex_count:\n    raise ValueError(f\"need range >= {vertex_count}, got {max_val - min_val + 1}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Parameterize label ranges as a function of vertex_count.","Add a sanity assert in test fixtures: max_val - min_val + 1 >= vertex_count.","Remember random.sample requires the population to be at least as large as the sample."],"tags":["graph","adjacency-matrix","test-generator","random-sampling"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}