From dd3dcde0b399e742aa311f7de143cc7837c5ee10 Mon Sep 17 00:00:00 2001 From: Philipp Rohde Date: Sun, 9 Jul 2023 14:42:19 +0200 Subject: [PATCH] add optional parameter for one connected component --- TravSHACL/core/GraphTraversal.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/TravSHACL/core/GraphTraversal.py b/TravSHACL/core/GraphTraversal.py index 9043811..04536fa 100644 --- a/TravSHACL/core/GraphTraversal.py +++ b/TravSHACL/core/GraphTraversal.py @@ -9,19 +9,25 @@ class GraphTraversal(Enum): BFS = 'Breadth-first search' DFS = 'Depth-first search' - def traverse_graph(self, dependencies, reversed_dependencies, starting_point): + def traverse_graph(self, dependencies, reversed_dependencies, starting_point, one_component=False): nodes = list(dependencies.keys()) visited = [] if self == GraphTraversal.DFS: while len(nodes) > 0: self._dfs(visited, dependencies, reversed_dependencies, starting_point) - [nodes.remove(v) for v in visited if v in nodes] - starting_point = nodes[0] if len(nodes) > 0 else None + if one_component: # only one connected component allowed, so drop the other nodes + nodes = [] + else: + [nodes.remove(v) for v in visited if v in nodes] + starting_point = nodes[0] if len(nodes) > 0 else None elif self == GraphTraversal.BFS: while len(nodes) > 0: self._bfs(visited, dependencies, reversed_dependencies, starting_point) - [nodes.remove(v) for v in visited if v in nodes] - starting_point = nodes[0] if len(nodes) > 0 else None + if one_component: # only one connected component allowed, so drop the other nodes + nodes = [] + else: + [nodes.remove(v) for v in visited if v in nodes] + starting_point = nodes[0] if len(nodes) > 0 else None return visited def _dfs(self, visited, dependencies, reversed_dependencies, node):