def create_directed_graphs(self):
'''
:return:
'''
self.directed_graphs = np.empty(
(self.no_of_atoms, self.no_of_atoms - 1, 3), dtype=int)
# parse all the atoms one by one and get directed graph to that atom
# as the sink node
for idx in range(self.no_of_atoms):
# get shortest path from the root to all the other atoms and then reverse the edges.
path = nx.single_source_dijkstra_path(self.graph, idx)
G = nx.DiGraph()
for i in range(self.no_of_atoms):
temp = path[i]
temp.reverse()
G.add_path(temp)
# do a topological sort to get a order of atoms with all edges pointing to the root
topological_order = nx.topological_sort(G)
sorted_path = np.empty((self.no_of_atoms - 1, 3))
no_of_incoming_edges = {}
for i in range(self.no_of_atoms - 1):
node = topological_order[i]
edge = (nx.edges(G, node))[0]
if edge[1] in no_of_incoming_edges:
index = no_of_incoming_edges[edge[1]]
no_of_incoming_edges[edge[1]] += 1
else:
index = 0
no_of_incoming_edges[edge[1]] = 1
sorted_path[i, :] = [node, edge[1], index]
self.directed_graphs[idx, :, :] = sorted_path
评论列表
文章目录