diff --git a/cyaron/graph.py b/cyaron/graph.py index 66ae82d..48e8f0a 100644 --- a/cyaron/graph.py +++ b/cyaron/graph.py @@ -79,10 +79,9 @@ def to_str(self, **kwargs): **kwargs(Keyword args): bool shuffle = False -> whether shuffle the output or not str output(Edge) = str -> the convert function which converts object Edge to str. the default way is to use str() - list[int] node_shuffler(list[int]) - = lambda table: random.sample(table, k=len(table)) + list[int] node_shuffler(int) + = lambda n: random.sample(range(1, n + 1), k=n) -> the random function which shuffles the vertex sequence. - Note that this function will actually be passed in a `range`! list[Edge] edge_shuffler(list[Edge]) -> a random function. the default is to shuffle the edge sequence, also, if the graph is undirected, it will swap `u` and `v` randomly. @@ -96,10 +95,10 @@ def _edge_shuffler_default(table): shuffle = kwargs.get("shuffle", False) output = kwargs.get("output", str) - node_shuffler = kwargs.get("node_shuffler", lambda table: random.sample(table, k=len(table))) + node_shuffler = kwargs.get("node_shuffler", lambda n: random.sample(range(1, n + 1), k=n)) edge_shuffler = kwargs.get("edge_shuffler", _edge_shuffler_default) if shuffle: - new_node_id = [0] + node_shuffler(range(1, len(self.edges))) + new_node_id = [0] + node_shuffler(self.vertex_count()) edge_buf = [] for edge in self.iterate_edges(): edge_buf.append( diff --git a/cyaron/tests/graph_test.py b/cyaron/tests/graph_test.py index f42959e..9a2d6e6 100644 --- a/cyaron/tests/graph_test.py +++ b/cyaron/tests/graph_test.py @@ -221,9 +221,8 @@ def unit_test(n, m, shuffle_kwargs = {}, check_kwargs = {}): unit_test(8, 20) unit_test(8, 20, {"shuffle": True}) mapping = [0] + random.sample(range(1, 8), k = 7) - shuffer = lambda seq: list(map(lambda i: mapping[i], seq)) + shuffer = lambda n: list(map(lambda i: mapping[i], range(1, n + 1))) unit_test(7, 10, {"shuffle": True, "node_shuffler": shuffer}) unit_test(7, 14, {"shuffle": True, "node_shuffler": shuffer}, {"mapping": mapping}) shuffer_without_swap = lambda table: random.sample(table, k=len(table)) unit_test(7, 12, {"shuffle": True, "edge_shuffler": shuffer_without_swap}, {"directed": True}) -