{"record":{"id":"5d3f00460ed22ea0","repo":"TheAlgorithms/Python","slug":"will-result-in-duplicate-vertices-either-increase","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_list.py","lineNumber":255,"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[GraphAdjacencyList, GraphAdjacencyList, 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 range \"\n                \"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 = GraphAdjacencyList(\n            vertices=random_vertices, edges=random_edges, directed=False\n        )\n        directed_graph = GraphAdjacencyList(\n            vertices=random_vertices, edges=random_edges, directed=True","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_list.py#L237-L273","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Widen the range: ensure max_val - min_val + 1 >= vertex_count (e.g. set max_val = min_val + vertex_count - 1 or larger).","Reduce vertex_count to fit within the available range.","Derive the range from the count programmatically instead of hard-coding both."],"exampleFix":"# before\nvertices, edges = generator(vertex_count=50, min_val=0, max_val=20)\n\n# after\nvertex_count = 50\nvertices, edges = generator(\n    vertex_count=vertex_count, min_val=0, max_val=vertex_count - 1\n)","handlingStrategy":"validation","validationCode":"if max_val - min_val + 1 < vertex_count:\n    max_val = min_val + vertex_count - 1  # widen range to minimum feasible","typeGuard":null,"tryCatchPattern":"try:\n    graphs = generator(vertex_count, min_val, max_val)\nexcept ValueError:\n    graphs = generator(vertex_count, min_val, min_val + vertex_count - 1)","preventionTips":["Derive range bounds from vertex_count instead of hard-coding both.","In parameterized tests, compute max_val as a function of the vertex-count variable.","Remember vertices are sampled uniquely: the label space must be at least as large as the node count."],"tags":["graph","test-generator","random-sampling","parameter-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}