rustworkx


import rustworkx as rx

digraph = rx.PyDiGraph()
nodes = ["a", "b", "c", "d", "e"]
value_idx_dict = {node: digraph.add_node(node) for node in nodes}
print(value_idx_dict)
digraph.add_edge(value_idx_dict['a'], value_idx_dict['c'], None)
digraph.add_edge(value_idx_dict['b'], value_idx_dict['c'], None)
digraph.add_edge(value_idx_dict['c'], value_idx_dict['d'], None)
digraph.add_edge(value_idx_dict['d'], value_idx_dict['e'], None)

# 根据节点值获取索引
value_to_find = "c"
if value_to_find in value_idx_dict:
    idx = value_idx_dict[value_to_find]
    print(digraph.predecessors(idx))
    print(digraph.predecessor_indices(idx))

else:
    print(f"Node with value '{value_to_find}' not found.")

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容