-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinterface.py
98 lines (82 loc) · 2.73 KB
/
interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from operator import attrgetter
import networkx as nx
from nx_parallel import algorithms
__all__ = ["BackendInterface", "ParallelGraph"]
ALGORITHMS = [
# Bipartite
"node_redundancy",
# Isolates
"number_of_isolates",
# Vitality
"closeness_vitality",
# Tournament
"is_reachable",
"tournament_is_strongly_connected",
# Centrality
"betweenness_centrality",
"edge_betweenness_centrality",
# Efficiency
"local_efficiency",
# Shortest Paths : generic
"all_pairs_all_shortest_paths",
# Shortest Paths : weighted graphs
"all_pairs_dijkstra",
"all_pairs_dijkstra_path_length",
"all_pairs_dijkstra_path",
"all_pairs_bellman_ford_path_length",
"all_pairs_bellman_ford_path",
"johnson",
# Clustering
"square_clustering",
# Shortest Paths : unweighted graphs
"all_pairs_shortest_path",
"all_pairs_shortest_path_length",
# Approximation
"approximate_all_pairs_node_connectivity",
# Connectivity
"connectivity.all_pairs_node_connectivity",
]
class ParallelGraph:
"""A wrapper class for networkx.Graph, networkx.DiGraph, networkx.MultiGraph,
and networkx.MultiDiGraph.
"""
__networkx_backend__ = "parallel"
def __init__(self, graph_object=None):
if graph_object is None:
self.graph_object = nx.Graph()
elif isinstance(graph_object, nx.Graph):
self.graph_object = graph_object
else:
self.graph_object = nx.Graph(graph_object)
def is_multigraph(self):
return self.graph_object.is_multigraph()
def is_directed(self):
return self.graph_object.is_directed()
def __str__(self):
return f"Parallel{self.graph_object}"
def assign_algorithms(cls):
"""Class decorator to assign algorithms to the class attributes."""
for attr in ALGORITHMS:
# get the function name by parsing the module hierarchy
func_name = attr.rsplit(".", 1)[-1]
setattr(cls, func_name, attrgetter(attr)(algorithms))
return cls
@assign_algorithms
class BackendInterface:
"""BackendInterface class for parallel algorithms."""
@staticmethod
def convert_from_nx(graph, *args, **kwargs):
"""Convert a networkx.Graph, networkx.DiGraph, networkx.MultiGraph,
or networkx.MultiDiGraph to a ParallelGraph.
"""
if isinstance(graph, ParallelGraph):
return graph
return ParallelGraph(graph)
@staticmethod
def convert_to_nx(result, *, name=None):
"""Convert a ParallelGraph to a networkx.Graph, networkx.DiGraph,
networkx.MultiGraph, or networkx.MultiDiGraph.
"""
if isinstance(result, ParallelGraph):
return result.graph_object
return result