|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from collections import defaultdict |
| 21 | + |
| 22 | + |
| 23 | +def _dfs_connected_components( |
| 24 | + temp: list[str], node_id: str, visited: dict[str, bool], adjacency_matrix: dict[str, list[str]] |
| 25 | +) -> list[str]: |
| 26 | + visited[node_id] = True |
| 27 | + |
| 28 | + temp.append(node_id) |
| 29 | + |
| 30 | + for adj_node_id in adjacency_matrix[node_id]: |
| 31 | + if not visited[adj_node_id]: |
| 32 | + temp = _dfs_connected_components(temp, adj_node_id, visited, adjacency_matrix) |
| 33 | + |
| 34 | + return temp |
| 35 | + |
| 36 | + |
| 37 | +def extract_connected_components(adjacency_matrix: dict[str, list[str]]) -> list[list[str]]: |
| 38 | + """Extract all connected components of a graph.""" |
| 39 | + visited: dict[str, bool] = {node_id: False for node_id in adjacency_matrix} |
| 40 | + |
| 41 | + connected_components: list[list[str]] = [] |
| 42 | + |
| 43 | + for node_id in adjacency_matrix: |
| 44 | + if visited[node_id] is False: |
| 45 | + temp: list[str] = [] |
| 46 | + connected_components.append(_dfs_connected_components(temp, node_id, visited, adjacency_matrix)) |
| 47 | + return connected_components |
| 48 | + |
| 49 | + |
| 50 | +def extract_single_connected_component( |
| 51 | + node_id: str, nodes: list[dict], edges: list[dict] |
| 52 | +) -> dict[str, list[dict]]: |
| 53 | + """Find the connected component that contains the node with the id ``node_id``.""" |
| 54 | + adjacency_matrix: dict[str, list[str]] = defaultdict(list) |
| 55 | + |
| 56 | + for edge in edges: |
| 57 | + adjacency_matrix[edge["source_id"]].append(edge["target_id"]) |
| 58 | + adjacency_matrix[edge["target_id"]].append(edge["source_id"]) |
| 59 | + |
| 60 | + connected_components = extract_connected_components(adjacency_matrix) |
| 61 | + |
| 62 | + filtered_connected_components = [cc for cc in connected_components if node_id in cc] |
| 63 | + |
| 64 | + if len(filtered_connected_components) != 1: |
| 65 | + raise ValueError( |
| 66 | + f"Unique connected component not found, got {filtered_connected_components} for connected components of node {node_id}, expected only 1 connected component." |
| 67 | + ) |
| 68 | + |
| 69 | + connected_component = filtered_connected_components[0] |
| 70 | + |
| 71 | + nodes = [node for node in nodes if node["id"] in connected_component] |
| 72 | + edges = [ |
| 73 | + edge |
| 74 | + for edge in edges |
| 75 | + if (edge["source_id"] in connected_component and edge["target_id"] in connected_component) |
| 76 | + ] |
| 77 | + |
| 78 | + return {"nodes": nodes, "edges": edges} |
0 commit comments