From 5fd1ed85ca456ff10748b5fa62ccf6a0bcea9023 Mon Sep 17 00:00:00 2001 From: eastagiletracker <310448263+eastagiletracker@users.noreply.github.com> Date: Thu, 13 Aug 2026 13:57:48 +0700 Subject: [PATCH] fix(algo): apply tg_wcc print_limit to the component maps print_limit bounded only the printed vertex set. @@comp_sizes_map and @@comp_group_by_size_map hold one entry per connected component, so on a graph with many components the response kept growing regardless of the configured limit and could exceed the result size limit. Truncate both maps to print_limit as well. print_limit = -1 still prints everything, so existing callers see identical output. Adds regression tests over the Empty graph, where every vertex is its own component. --- .../standard/tg_wcc.gsql | 16 ++++++++--- tests/test/test_community.py | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/algorithms/Community/connected_components/weakly_connected_components/standard/tg_wcc.gsql b/algorithms/Community/connected_components/weakly_connected_components/standard/tg_wcc.gsql index 94046cd7..1a425e7f 100644 --- a/algorithms/Community/connected_components/weakly_connected_components/standard/tg_wcc.gsql +++ b/algorithms/Community/connected_components/weakly_connected_components/standard/tg_wcc.gsql @@ -39,7 +39,7 @@ CREATE QUERY tg_wcc (SET v_type_set, SET e_type_set, INT print_l display_edges: output edges for visualization print_limit: - maximum number of vertices to output (-1 = all) + maximum number of vertices and of components to output (-1 = all) */ /* @@ -48,12 +48,14 @@ CREATE QUERY tg_wcc (SET v_type_set, SET e_type_set, INT print_l v_type_set: vertex types to traverse print_results: print JSON output e_type_set: edge types to traverse result_attribute: INT attribute to store results to file_path: file to write CSV output to display_edges: output edges for visualization - print_limit: max #vertices to output (-1 = all) + print_limit: max #vertices and #components to output (-1 = all) */ MinAccum @min_cc_id = 0; //each vertex's tentative component id MapAccum @@comp_sizes_map; +MapAccum @@print_comp_sizes_map; //@@comp_sizes_map truncated to print_limit MapAccum> @@comp_group_by_size_map; +SumAccum @@printed_comp_count; FILE f(file_path); Start = {v_type_set}; @@ -95,11 +97,17 @@ IF print_results THEN FROM Start:s LIMIT print_limit; END; + # print_limit also bounds the per-component maps: they grow with the number + # of components, so leaving them uncapped can exceed the response size limit FOREACH (compId,size) IN @@comp_sizes_map DO - @@comp_group_by_size_map += (size -> compId); + IF print_limit < 0 OR @@printed_comp_count < print_limit THEN + @@print_comp_sizes_map += (compId -> size); + @@comp_group_by_size_map += (size -> compId); + @@printed_comp_count += 1; + END; END; PRINT @@comp_group_by_size_map; - PRINT @@comp_sizes_map as sizes; + PRINT @@print_comp_sizes_map as sizes; PRINT Start[Start.@min_cc_id]; END; } diff --git a/tests/test/test_community.py b/tests/test/test_community.py index 642a4e8e..abe3b3b7 100644 --- a/tests/test/test_community.py +++ b/tests/test/test_community.py @@ -73,3 +73,31 @@ def test_lcc2(self, test_name): found = True if not found: pytest.fail() + + def run_wcc(self, print_limit): + params = { + "v_type_set": ["V20"], + "e_type_set": ["Empty"], + "print_limit": print_limit, + "print_results": True, + "result_attribute": "", + "file_path": "", + } + result = self.feat.runAlgorithm("tg_wcc", params=params) + sizes = [r for r in result if "sizes" in r][0]["sizes"] + vertices = [r for r in result if "Start" in r][0]["Start"] + return sizes, vertices + + @pytest.mark.parametrize("print_limit", [0, 1, 5]) + def test_wcc_print_limit(self, print_limit): + # Empty has no edges, so each of the 20 V20 vertices is its own + # component: print_limit has to bound the component maps too, or the + # response keeps growing with the component count + sizes, vertices = self.run_wcc(print_limit) + assert len(sizes) <= print_limit + assert len(vertices) <= print_limit + + def test_wcc_print_limit_all(self): + sizes, vertices = self.run_wcc(-1) + assert len(sizes) == 20 + assert len(vertices) == 20