| Total Complexity | 5 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from enum import Enum |
||
| 2 | |||
| 3 | import networkx as nx |
||
| 4 | from typing_extensions import Self |
||
| 5 | |||
| 6 | |||
| 7 | class GraphType(Enum): |
||
| 8 | """Graph Types |
||
| 9 | |||
| 10 | The choice of graph class depends on the structure of the graph you want to represent. |
||
| 11 | |||
| 12 | | **Graph Type** | **Type** | **Self-loops allowed** | **Parallel edges allowed** | |
||
| 13 | |----------------|------------|:----------------------:|:--------------------------:| |
||
| 14 | | Graph | Undirected | Yes | No | |
||
| 15 | | DiGraph | Directed | Yes | No | |
||
| 16 | | MultiGraph | Undirected | Yes | Yes | |
||
| 17 | | MultiDiGraph | Directed | Yes | Yes | |
||
| 18 | |||
| 19 | See more here: [NetworkX Reference](https://networkx.org/documentation/stable/reference/classes) |
||
| 20 | """ |
||
| 21 | |||
| 22 | Graph = nx.Graph |
||
| 23 | DiGraph = nx.DiGraph |
||
| 24 | MultiDiGraph = nx.MultiDiGraph |
||
| 25 | MultiGraph = nx.MultiGraph |
||
| 26 | |||
| 27 | @classmethod |
||
| 28 | def of(cls, graph: nx.Graph) -> Self: |
||
| 29 | """Determine the graph type based on structure and properties. |
||
| 30 | |||
| 31 | Args: |
||
| 32 | graph (nx.Graph): A NetworkX graph object. |
||
| 33 | |||
| 34 | Returns: |
||
| 35 | GraphType: An instance of this Enum matching the input graph. |
||
| 36 | """ |
||
| 37 | if graph.is_directed() and graph.is_multigraph(): |
||
| 38 | return cls.MultiDiGraph |
||
| 39 | elif graph.is_directed(): |
||
| 40 | return cls.DiGraph |
||
| 41 | elif graph.is_multigraph(): |
||
| 42 | return cls.MultiGraph |
||
| 43 | else: |
||
| 44 | return cls.Graph |
||
| 45 |