diff --git a/rdflib/compare.py b/rdflib/compare.py index afc2c40b5..3e1b9e2ca 100644 --- a/rdflib/compare.py +++ b/rdflib/compare.py @@ -424,20 +424,11 @@ def _traces( stats["prunings"] = 0 depth[0] += 1 candidates = self._get_candidates(coloring) - best: List[List[Color]] = [] + best_coloring = None best_score = None - best_experimental_score = None - last_coloring = None - generator: Dict[Node, Set[Node]] = defaultdict(set) - visited: Set[Node] = set() + for candidate, color in candidates: - if candidate in generator: - v = generator[candidate] & visited - if len(v) > 0: - visited.add(candidate) - continue - visited.add(candidate) - coloring_copy: List[Color] = [] + coloring_copy: list[Color] = [] color_copy = None for c in coloring: c_copy = c.copy() @@ -447,42 +438,19 @@ def _traces( new_color = self._individuate(color_copy, candidate) coloring_copy.append(new_color) refined_coloring = self._refine(coloring_copy, [new_color]) - color_score = tuple([c.key() for c in refined_coloring]) - experimental = self._experimental_path(coloring_copy) - experimental_score = set([c.key() for c in experimental]) - if last_coloring: - generator = self._create_generator( # type: ignore[unreachable] - [last_coloring, experimental], generator - ) - last_coloring = experimental - if best_score is None or best_score < color_score: # type: ignore[unreachable] - best = [refined_coloring] + color_score = tuple(c.key() for c in refined_coloring) + + if best_score is None or best_score < color_score: + best_coloring = refined_coloring best_score = color_score - best_experimental_score = experimental_score - elif best_score > color_score: # type: ignore[unreachable] - # prune this branch. - if stats is not None: - stats["prunings"] += 1 - elif experimental_score != best_experimental_score: - best.append(refined_coloring) - else: - # prune this branch. - if stats is not None: - stats["prunings"] += 1 - discrete: List[List[Color]] = [x for x in best if self._discrete(x)] - if len(discrete) == 0: - best_score = None - best_depth = None - for coloring in best: - d = [depth[0]] - new_color = self._traces(coloring, stats=stats, depth=d) - color_score = tuple([c.key() for c in refined_coloring]) - if best_score is None or color_score > best_score: # type: ignore[unreachable] - discrete = [new_color] - best_score = color_score - best_depth = d[0] - depth[0] = best_depth # type: ignore[assignment] - return discrete[0] + + if best_coloring is None: + return coloring + + if not self._discrete(best_coloring): + return self._traces(best_coloring, stats=stats, depth=depth) + + return best_coloring def canonical_triples(self, stats: Optional[Stats] = None): if stats is not None: